summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/ttmserver/CrossLanguageTranslationSearchQuery.php
blob: 4b04791863bc054132ae6d295546020a03da08a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
 * Cross Language Translation Search.
 * @since 2015.08
 */
class CrossLanguageTranslationSearchQuery {
	/** @var TTMServer */
	protected $server;

	/** @var array */
	protected $params;

	/** @var ResultSet */
	protected $resultset;

	/** @var int */
	protected $total = 0;

	protected $hl = [ '', '' ];

	public function __construct( array $params, SearchableTTMServer $server ) {
		$this->params = $params;
		$this->server = $server;
	}

	public function getDocuments() {
		$documents = [];
		$offset = $this->params['offset'];
		$limit = $this->params['limit'];

		$options = $this->params;
		$options['language'] = $this->params['sourcelanguage'];
		// Use a bigger limit that what was requested, since we are likely to throw away many
		// results in the local filtering step at extractMessages
		$options['limit'] = $limit * 10;
		// TODO: the real offset should be communicated to the frontend. It currently assumes
		// next offset is current offset + limit and previous one is current offset - limit.
		// It might be difficult to fix scrolling results backwards. For now we handle offset
		// locally.
		$options['offset'] = 0;

		$search = $this->server->createSearch( $this->params['query'], $options, $this->hl );
		$scroll = $search->scroll( '5s' );

		// Used for aggregations. Only the first scroll response has them.
		$this->resultset = null;

		foreach ( $scroll as $resultSet ) {
			if ( !$this->resultset ) {
				$this->resultset = $resultSet;
				$this->total = $resultSet->getTotalHits();
			}

			$results = $this->extractMessages( $resultSet->getDocuments() );
			$documents = array_merge( $documents, $results );

			$count = count( $documents );

			if ( $count >= $offset + $limit ) {
				break;
			}
		}

		// clear was introduced in Elastica 5.3.1, but Elastica extension uses 5.3.0
		if ( is_callable( [ $scroll, 'clear' ] ) ) {
			$scroll->clear();
		}
		$documents = array_slice( $documents, $offset, $limit );

		return $documents;
	}

	/**
	 * Extract messages from the documents and build message definitions.
	 * Create a message collection from the definitions in the target language.
	 * Filter the message collection to get filtered messages.
	 * Slice messages according to limit and offset given.
	 * @param \Elastica\Document[] $documents
	 * @return array[]
	 */
	protected function extractMessages( $documents ) {
		$messages = $ret = [];

		$language = $this->params['language'];
		foreach ( $documents as $document ) {
			$data = $document->getData();

			if ( !$this->server->isLocalSuggestion( $data ) ) {
				continue;
			}

			$title = Title::newFromText( $data['localid'] );
			if ( !$title ) {
				continue;
			}

			$handle = new MessageHandle( $title );
			if ( !$handle->isValid() ) {
				continue;
			}

			$key = $title->getNamespace() . ':' . $title->getDBkey();
			$messages[$key] = $data['content'];
		}

		$definitions = new MessageDefinitions( $messages );
		$collection = MessageCollection::newFromDefinitions( $definitions, $language );

		$filter = $this->params['filter'];
		if ( $filter === 'untranslated' ) {
			$collection->filter( 'hastranslation', true );
		} elseif ( in_array( $filter, $this->getAvailableFilters() ) ) {
			$collection->filter( $filter, false );
		}

		if ( $filter === 'translated' || $filter === 'fuzzy' ) {
			$collection->loadTranslations();
		}

		foreach ( $collection->keys() as $mkey => $title ) {
			$result = [];
			$result['content'] = $messages[$mkey];
			if ( $filter === 'translated' || $filter === 'fuzzy' ) {
				$result['content'] = $collection[$mkey]->translation();
			}
			$handle = new MessageHandle( $title );
			$result['localid'] = $handle->getTitleForBase()->getPrefixedText();
			$result['language'] = $language;

			$ret[] = $result;
		}

		return $ret;
	}

	/**
	 * @return array
	 */
	public function getAvailableFilters() {
		return [
			'translated',
			'fuzzy',
			'untranslated'
		];
	}

	public function getTotalHits() {
		return $this->total;
	}

	public function getResultSet() {
		return $this->resultset;
	}
}