Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslateRcFilter.php
Go to the documentation of this file.
1<?php
31 public static function translationFilter( $pageName, &$tables, &$fields, &$conds,
32 &$query_options, &$join_conds, FormOptions $opts
33 ) {
34 global $wgTranslateRcFilterDefault;
35
36 if ( $pageName !== 'Recentchanges' || self::isStructuredFilterUiEnabled() ) {
37 return true;
38 }
39
40 $request = RequestContext::getMain()->getRequest();
41 $translations = $request->getVal( 'translations', $wgTranslateRcFilterDefault );
42 $opts->add( 'translations', $wgTranslateRcFilterDefault );
43 $opts->setValue( 'translations', $translations );
44
45 $dbr = wfGetDB( DB_REPLICA );
46
47 $namespaces = self::getTranslateNamespaces();
48
49 if ( $translations === 'only' ) {
50 $conds[] = 'rc_namespace IN (' . $dbr->makeList( $namespaces ) . ')';
51 $conds[] = 'rc_title like \'%%/%%\'';
52 } elseif ( $translations === 'filter' ) {
53 $conds[] = 'rc_namespace NOT IN (' . $dbr->makeList( $namespaces ) . ')';
54 } elseif ( $translations === 'site' ) {
55 $conds[] = 'rc_namespace IN (' . $dbr->makeList( $namespaces ) . ')';
56 $conds[] = 'rc_title not like \'%%/%%\'';
57 }
58
59 return true;
60 }
61
62 private static function getTranslateNamespaces() {
63 global $wgTranslateMessageNamespaces;
64 $namespaces = [];
65
66 foreach ( $wgTranslateMessageNamespaces as $index ) {
67 $namespaces[] = $index;
68 $namespaces[] = $index + 1; // Include Talk namespaces
69 }
70
71 return $namespaces;
72 }
73
83 public static function translationFilterForm( &$items, $opts ) {
84 if ( self::isStructuredFilterUiEnabled() ) {
85 return true;
86 }
87
88 $opts->consumeValue( 'translations' );
89 $default = $opts->getValue( 'translations' );
90
91 $label = Xml::label(
92 wfMessage( 'translate-rc-translation-filter' )->text(),
93 'mw-translation-filter'
94 );
95 $select = new XmlSelect( 'translations', 'mw-translation-filter', $default );
96 $select->addOption(
97 wfMessage( 'translate-rc-translation-filter-no' )->text(),
98 'noaction'
99 );
100 $select->addOption( wfMessage( 'translate-rc-translation-filter-only' )->text(), 'only' );
101 $select->addOption(
102 wfMessage( 'translate-rc-translation-filter-filter' )->text(),
103 'filter'
104 );
105 $select->addOption( wfMessage( 'translate-rc-translation-filter-site' )->text(), 'site' );
106
107 $items['translations'] = [ $label, $select->getHTML() ];
108
109 return true;
110 }
111
112 private static function isStructuredFilterUiEnabled() {
113 $context = RequestContext::getMain();
114
115 // This assumes usage only on RC page
116 $page = new SpecialRecentChanges();
117 $page->setContext( $context );
118
119 // isStructuredFilterUiEnabled used to be a protected method in older versions :(
120 return is_callable( [ $page, 'isStructuredFilterUiEnabled' ] ) &&
121 $page->isStructuredFilterUiEnabled();
122 }
123
133 ChangesListSpecialPage $special
134 ) {
135 global $wgTranslateRcFilterDefault;
136 $defaultFilter = $wgTranslateRcFilterDefault !== 'noaction' ?
137 $wgTranslateRcFilterDefault :
138 ChangesListStringOptionsFilterGroup::NONE;
139
140 $translationsGroup = new ChangesListStringOptionsFilterGroup(
141 [
142 'name' => 'translations',
143 'title' => 'translate-rcfilters-translations',
144 'priority' => -7,
145 'default' => $defaultFilter,
146 'isFullCoverage' => true,
147 'filters' => [
148 [
149 'name' => 'only',
150 'label' => 'translate-rcfilters-translations-only-label',
151 'description' => 'translate-rcfilters-translations-only-desc',
152 'cssClassSuffix' => 'only',
153 'isRowApplicableCallable' => function ( $ctx, $rc ) {
154 $namespaces = self::getTranslateNamespaces();
155
156 return in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces ) &&
157 strpos( $rc->getAttribute( 'rc_title' ), '/' ) !== false;
158 }
159 ],
160 [
161 'name' => 'site',
162 'label' => 'translate-rcfilters-translations-site-label',
163 'description' => 'translate-rcfilters-translations-site-desc',
164 'cssClassSuffix' => 'site',
165 'isRowApplicableCallable' => function ( $ctx, $rc ) {
166 $namespaces = self::getTranslateNamespaces();
167
168 return in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces ) &&
169 strpos( $rc->getAttribute( 'rc_title' ), '/' ) === false;
170 }
171 ],
172 [
173 'name' => 'filter',
174 'label' => 'translate-rcfilters-translations-filter-label',
175 'description' => 'translate-rcfilters-translations-filter-desc',
176 'cssClassSuffix' => 'filter',
177 'isRowApplicableCallable' => function ( $ctx, $rc ) {
178 $namespaces = self::getTranslateNamespaces();
179
180 return !in_array( $rc->getAttribute( 'rc_namespace' ), $namespaces );
181 }
182 ],
183 ],
184 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables,
185 &$fields, &$conds, &$query_options, &$join_conds, $selectedValues
186 ) {
187 $fields = array_merge( $fields, [ 'rc_title', 'rc_namespace' ] );
188
189 $namespaces = self::getTranslateNamespaces();
190 $inNamespaceCond = 'rc_namespace IN (' .
191 $dbr->makeList( $namespaces ) . ')';
192 $notInNamespaceCond = 'rc_namespace NOT IN (' .
193 $dbr->makeList( $namespaces ) . ')';
194
195 $onlyCond = $dbr->makeList( [
196 $inNamespaceCond,
197 'rc_title ' .
198 $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() )
199 ], LIST_AND );
200 $siteCond = $dbr->makeList( [
201 $inNamespaceCond,
202 'rc_title NOT' .
203 $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() )
204 ], LIST_AND );
205
206 if ( count( $selectedValues ) === 3 ) {
207 // no filters
208 return;
209 }
210
211 if ( $selectedValues === [ 'filter', 'only' ] ) {
212 $conds[] = $dbr->makeList( [
213 $notInNamespaceCond,
214 $onlyCond
215 ], LIST_OR );
216 return;
217 }
218
219 if ( $selectedValues === [ 'filter', 'site' ] ) {
220 $conds[] = $dbr->makeList( [
221 $notInNamespaceCond,
222 $siteCond
223 ], LIST_OR );
224 return;
225 }
226
227 if ( $selectedValues === [ 'only', 'site' ] ) {
228 $conds[] = $inNamespaceCond;
229 return;
230 }
231
232 if ( $selectedValues === [ 'filter' ] ) {
233 $conds[] = $notInNamespaceCond;
234 return;
235 }
236
237 if ( $selectedValues === [ 'only' ] ) {
238 $conds[] = $onlyCond;
239 return;
240 }
241
242 if ( $selectedValues === [ 'site' ] ) {
243 $conds[] = $siteCond;
244 }
245 }
246 ]
247 );
248
249 $special->registerFilterGroup( $translationsGroup );
250 return true;
251 }
252}
Adds a new filter to Special:RecentChanges which makes it possible to filter translations away or sho...
static translationFilter( $pageName, &$tables, &$fields, &$conds, &$query_options, &$join_conds, FormOptions $opts)
Hooks ChangesListSpecialPageQuery.
static onChangesListSpecialPageStructuredFilters(ChangesListSpecialPage $special)
Hooks ChangesListSpecialPageStructuredFilters.
static translationFilterForm(&$items, $opts)
Hooks SpecialRecentChangesPanel.