21 private array $params;
22 private ?ResultSet $resultSet =
null;
23 private int $total = 0;
24 private array $hl = [
'',
'' ];
27 $this->params = $params;
28 $this->server = $server;
31 public function getDocuments(): array {
33 $offset = $this->params[
'offset'];
34 $limit = $this->params[
'limit'];
36 $options = $this->params;
37 $options[
'language'] = $this->params[
'sourcelanguage'];
40 $options[
'limit'] = $limit * 10;
45 $options[
'offset'] = 0;
48 $search = $this->server->createSearch( $this->params[
'query'], $options, $this->hl );
49 $scroll = $search->scroll(
'5s' );
52 $this->resultSet =
null;
54 foreach ( $scroll as $resultSet ) {
55 if ( !$this->resultSet ) {
56 $this->resultSet = $resultSet;
57 $this->total = $resultSet->getTotalHits();
60 $results = $this->extractMessages( $resultSet->getDocuments() );
61 $documents = array_merge( $documents, $results );
63 $count = count( $documents );
65 if ( $count >= $offset + $limit ) {
70 if ( !$this->resultSet ) {
72 $this->resultSet = $scroll->current();
73 $this->total = $scroll->current()->getTotalHits();
77 if ( is_callable( [ $scroll,
'clear' ] ) ) {
80 return array_slice( $documents, $offset, $limit );
90 private function extractMessages( array $documents ): array {
91 $messages = $ret = [];
93 $language = $this->params[
'language'];
94 foreach ( $documents as $document ) {
95 $data = $document->getData();
98 if ( !$this->server->isLocalSuggestion( $data ) ) {
102 $title = Title::newFromText( $data[
'localid'] );
108 if ( !$handle->isValid() ) {
112 $key = $title->getNamespace() .
':' . $title->getDBkey();
113 $messages[$key] = $data[
'content'];
117 $collection = MessageCollection::newFromDefinitions( $definitions, $language );
119 $filter = $this->params[
'filter'];
120 if ( $filter ===
'untranslated' ) {
121 $collection->filter( MessageCollection::FILTER_HAS_TRANSLATION, MessageCollection::EXCLUDE_MATCHING );
122 } elseif ( in_array( $filter, $this->getAvailableFilters() ) ) {
123 $collection->filter( $filter, MessageCollection::INCLUDE_MATCHING );
126 if ( $filter ===
'translated' || $filter ===
'fuzzy' ) {
127 $collection->loadTranslations();
130 foreach ( $collection->keys() as $messageKey => $titleValue ) {
131 $title = Title::newFromLinkTarget( $titleValue );
134 $result[
'content'] = $messages[$messageKey];
135 if ( $filter ===
'translated' || $filter ===
'fuzzy' ) {
136 $result[
'content'] = $collection[$messageKey]->translation();
139 $result[
'localid'] = $handle->getTitleForBase()->getPrefixedText();
140 $result[
'language'] = $language;
148 public function getAvailableFilters(): array {
156 public function getTotalHits():
int {
160 public function getResultSet(): ResultSet {
161 return $this->resultSet;