summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/ttmserver/CrossLanguageTranslationSearchQuery.php
blob: ae49f7cc656e44d0000bf8d360cdf7d9d3cb210c (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
<?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 = array( '', '' );

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

	public function getDocuments() {
		$documents = array();
		$total = $start = 0;
		$queryString = $this->params['query'];
		$offset = $this->params['offset'];
		$limit = $this->params['limit'];
		$size = 1000;

		$options = $this->params;
		$options['limit'] = $size;
		$options['language'] = $this->params['sourcelanguage'];
		do {
			$options['offset'] = $start;
			$this->resultset = $this->server->search( $queryString, $options, $this->hl );

			list( $results, $offsets ) = $this->extractMessages(
				$this->resultset,
				$offset,
				$limit
			);
			$offset = $offsets['start'] + $offsets['left'] - $offsets['total'];
			$limit = $limit - $offsets['left'];
			$total = $total + $offsets['total'];

			$documents = array_merge( $documents, $results );
			$start = $start + $size;
		} while (
			$offsets['start'] + $offsets['left'] >= $offsets['total'] &&
			$this->resultset->getTotalHits() > $start
		);
		$this->total = $total;

		return $documents;
	}

	/*
	 * Extract messages from the resultset 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.
	 */
	protected function extractMessages( $resultset, $offset, $limit ) {
		$messages = $documents = $ret = array();

		$language = $this->params['language'];
		foreach ( $resultset->getResults() 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 );
		}

		$total = count( $collection );
		$offset = $collection->slice( $offset, $limit );
		$left = count( $collection );

		$offsets = array(
			'start' => $offset[2],
			'left' => $left,
			'total' => $total,
		);

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

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

		return array( $ret, $offsets );
	}

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

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

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