summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/utils/RcFilter.php
blob: 7c2334f2a377b9456f527a9d4973ec12ff008016 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
 * Contains class with filter to Special:RecentChanges to enable additional
 * filtering.
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2010, Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Adds a new filter to Special:RecentChanges which makes it possible to filter
 * translations away or show them only.
 */
class TranslateRcFilter {
	/**
	 * Hooks ChangesListSpecialPageQuery. See the hook documentation for
	 * documentation of the function parameters.
	 *
	 * Appends SQL filter conditions into $conds.
	 * @param string $pageName
	 * @param array &$tables
	 * @param array &$fields
	 * @param array &$conds
	 * @param array &$query_options
	 * @param array &$join_conds
	 * @param FormOptions $opts
	 * @return bool true
	 */
	public static function translationFilter( $pageName, &$tables, &$fields, &$conds,
		&$query_options, &$join_conds, FormOptions $opts
	) {
		global $wgTranslateRcFilterDefault;

		if ( $pageName !== 'Recentchanges' || self::isStructuredFilterUiEnabled() ) {
			return true;
		}

		$request = RequestContext::getMain()->getRequest();
		$translations = $request->getVal( 'translations', $wgTranslateRcFilterDefault );
		$opts->add( 'translations', $wgTranslateRcFilterDefault );
		$opts->setValue( 'translations', $translations );

		$dbr = wfGetDB( DB_REPLICA );

		$namespaces = self::getTranslateNamespaces();

		if ( $translations === 'only' ) {
			$conds[] = 'rc_namespace IN (' . $dbr->makeList( $namespaces ) . ')';
			$conds[] = 'rc_title like \'%%/%%\'';
		} elseif ( $translations === 'filter' ) {
			$conds[] = 'rc_namespace NOT IN (' . $dbr->makeList( $namespaces ) . ')';
		} elseif ( $translations === 'site' ) {
			$conds[] = 'rc_namespace IN (' . $dbr->makeList( $namespaces ) . ')';
			$conds[] = 'rc_title not like \'%%/%%\'';
		}

		return true;
	}

	private static function getTranslateNamespaces() {
		global $wgTranslateMessageNamespaces;
		$namespaces = [];

		foreach ( $wgTranslateMessageNamespaces as $index ) {
			$namespaces[] = $index;
			$namespaces[] = $index + 1; // Include Talk namespaces
		}

		return $namespaces;
	}

	/**
	 * Hooks SpecialRecentChangesPanel. See the hook documentation for
	 * documentation of the function parameters.
	 *
	 * Adds a HTMl selector into $items
	 * @param array &$items
	 * @param FormOptions $opts
	 * @return bool true
	 */
	public static function translationFilterForm( &$items, $opts ) {
		if ( self::isStructuredFilterUiEnabled() ) {
			return true;
		}

		$opts->consumeValue( 'translations' );
		$default = $opts->getValue( 'translations' );

		$label = Xml::label(
			wfMessage( 'translate-rc-translation-filter' )->text(),
			'mw-translation-filter'
		);
		$select = new XmlSelect( 'translations', 'mw-translation-filter', $default );
		$select->addOption(
			wfMessage( 'translate-rc-translation-filter-no' )->text(),
			'noaction'
		);
		$select->addOption( wfMessage( 'translate-rc-translation-filter-only' )->text(), 'only' );
		$select->addOption(
			wfMessage( 'translate-rc-translation-filter-filter' )->text(),
			'filter'
		);
		$select->addOption( wfMessage( 'translate-rc-translation-filter-site' )->text(), 'site' );

		$items['translations'] = [ $label, $select->getHTML() ];

		return true;
	}

	private static function isStructuredFilterUiEnabled() {
		$context = RequestContext::getMain();

		// This assumes usage only on RC page
		$page = new SpecialRecentChanges();
		$page->setContext( $context );

		// isStructuredFilterUiEnabled used to be a protected method in older versions :(
		return is_callable( [ $page, 'isStructuredFilterUiEnabled' ] ) &&
			$page->isStructuredFilterUiEnabled();
	}

	/**
	 * Hooks ChangesListSpecialPageStructuredFilters. See the hook documentation for
	 * documentation of the function parameters.
	 *
	 * Adds translations filters to structured UI
	 * @param ChangesListSpecialPage $special
	 * @return bool true
	 */
	public static function onChangesListSpecialPageStructuredFilters(
		ChangesListSpecialPage $special
	) {
		global $wgTranslateRcFilterDefault;
		$defaultFilter = $wgTranslateRcFilterDefault !== 'noaction' ?
			$wgTranslateRcFilterDefault :
			ChangesListStringOptionsFilterGroup::NONE;

		$translationsGroup = new ChangesListStringOptionsFilterGroup(
			[
				'name' => 'translations',
				'title' => 'translate-rcfilters-translations',
				'priority' => -7,
				'default' => $defaultFilter,
				'isFullCoverage' => true,
				'filters' => [
					[
						'name' => 'only',
						'label' => 'translate-rcfilters-translations-only-label',
						'description' => 'translate-rcfilters-translations-only-desc',
						'cssClassSuffix' => 'only',
						'isRowApplicableCallable' => function ( $ctx, $rc ) {
							$namespaces = self::getTranslateNamespaces();

							return in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces ) &&
								strpos( $rc->getAttribute( 'rc_title' ), '/' ) !== false;
						}
					],
					[
						'name' => 'site',
						'label' => 'translate-rcfilters-translations-site-label',
						'description' => 'translate-rcfilters-translations-site-desc',
						'cssClassSuffix' => 'site',
						'isRowApplicableCallable' => function ( $ctx, $rc ) {
							$namespaces = self::getTranslateNamespaces();

							return in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces ) &&
								strpos( $rc->getAttribute( 'rc_title' ), '/' ) === false;
						}
					],
					[
						'name' => 'filter',
						'label' => 'translate-rcfilters-translations-filter-label',
						'description' => 'translate-rcfilters-translations-filter-desc',
						'cssClassSuffix' => 'filter',
						'isRowApplicableCallable' => function ( $ctx, $rc ) {
							$namespaces = self::getTranslateNamespaces();

							return !in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces );
						}
					],
				],
				'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables,
					&$fields, &$conds, &$query_options, &$join_conds, $selectedValues
				) {
					$fields[] = 'rc_title';
					$fields[] = 'rc_namespace';

					$namespaces = self::getTranslateNamespaces();
					$inNamespaceCond = 'rc_namespace IN (' .
						$dbr->makeList( $namespaces ) . ')';
					$notInNamespaceCond = 'rc_namespace NOT IN (' .
						$dbr->makeList( $namespaces ) . ')';

					$onlyCond = $dbr->makeList( [
						$inNamespaceCond,
						'rc_title ' .
							$dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() )
					], LIST_AND );
					$siteCond = $dbr->makeList( [
						$inNamespaceCond,
						'rc_title NOT' .
							$dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() )
					], LIST_AND );

					if ( count( $selectedValues ) === 3 ) {
						// no filters
						return;
					}

					if ( $selectedValues === [ 'filter', 'only' ] ) {
						$conds[] = $dbr->makeList( [
							$notInNamespaceCond,
							$onlyCond
						], LIST_OR );
						return;
					}

					if ( $selectedValues === [ 'filter', 'site' ] ) {
						$conds[] = $dbr->makeList( [
							$notInNamespaceCond,
							$siteCond
						], LIST_OR );
						return;
					}

					if ( $selectedValues === [ 'only', 'site' ] ) {
						$conds[] = $inNamespaceCond;
						return;
					}

					if ( $selectedValues === [ 'filter' ] ) {
						$conds[] = $notInNamespaceCond;
						return;
					}

					if ( $selectedValues === [ 'only' ] ) {
						$conds[] = $onlyCond;
						return;
					}

					if ( $selectedValues === [ 'site' ] ) {
						$conds[] = $siteCond;
					}
				}
			]
		);

		$special->registerFilterGroup( $translationsGroup );
		return true;
	}
}