Merge "Don't fallback from uk to ru"
[lhc/web/wiklou.git] / includes / specialpage / ChangesListSpecialPage.php
1 <?php
2 /**
3 * Special page which uses a ChangesList to show query results.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special page which uses a ChangesList to show query results.
26 * @todo Way too many public functions, most of them should be protected
27 *
28 * @ingroup SpecialPage
29 */
30 abstract class ChangesListSpecialPage extends SpecialPage {
31 /** @var string */
32 protected $rcSubpage;
33
34 /** @var FormOptions */
35 protected $rcOptions;
36
37 /** @var array */
38 protected $customFilters;
39
40 /**
41 * Main execution point
42 *
43 * @param string $subpage
44 */
45 public function execute( $subpage ) {
46 $this->rcSubpage = $subpage;
47
48 $this->setHeaders();
49 $this->outputHeader();
50 $this->addModules();
51
52 $rows = $this->getRows();
53 $opts = $this->getOptions();
54 if ( $rows === false ) {
55 if ( !$this->including() ) {
56 $this->doHeader( $opts, 0 );
57 $this->getOutput()->setStatusCode( 404 );
58 }
59
60 return;
61 }
62
63 $batch = new LinkBatch;
64 foreach ( $rows as $row ) {
65 $batch->add( NS_USER, $row->rc_user_text );
66 $batch->add( NS_USER_TALK, $row->rc_user_text );
67 $batch->add( $row->rc_namespace, $row->rc_title );
68 if ( $row->rc_source === RecentChange::SRC_LOG ) {
69 $formatter = LogFormatter::newFromRow( $row );
70 foreach ( $formatter->getPreloadTitles() as $title ) {
71 $batch->addObj( $title );
72 }
73 }
74 }
75 $batch->execute();
76
77 $this->webOutput( $rows, $opts );
78
79 $rows->free();
80 }
81
82 /**
83 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
84 *
85 * @return bool|ResultWrapper Result or false
86 */
87 public function getRows() {
88 $opts = $this->getOptions();
89 $conds = $this->buildMainQueryConds( $opts );
90
91 return $this->doMainQuery( $conds, $opts );
92 }
93
94 /**
95 * Get the current FormOptions for this request
96 *
97 * @return FormOptions
98 */
99 public function getOptions() {
100 if ( $this->rcOptions === null ) {
101 $this->rcOptions = $this->setup( $this->rcSubpage );
102 }
103
104 return $this->rcOptions;
105 }
106
107 /**
108 * Create a FormOptions object with options as specified by the user
109 *
110 * @param array $parameters
111 *
112 * @return FormOptions
113 */
114 public function setup( $parameters ) {
115 $opts = $this->getDefaultOptions();
116 foreach ( $this->getCustomFilters() as $key => $params ) {
117 $opts->add( $key, $params['default'] );
118 }
119
120 $opts = $this->fetchOptionsFromRequest( $opts );
121
122 // Give precedence to subpage syntax
123 if ( $parameters !== null ) {
124 $this->parseParameters( $parameters, $opts );
125 }
126
127 $this->validateOptions( $opts );
128
129 return $opts;
130 }
131
132 /**
133 * Get a FormOptions object containing the default options. By default returns some basic options,
134 * you might want to not call parent method and discard them, or to override default values.
135 *
136 * @return FormOptions
137 */
138 public function getDefaultOptions() {
139 $config = $this->getConfig();
140 $opts = new FormOptions();
141
142 $opts->add( 'hideminor', false );
143 $opts->add( 'hidebots', false );
144 $opts->add( 'hideanons', false );
145 $opts->add( 'hideliu', false );
146 $opts->add( 'hidepatrolled', false );
147 $opts->add( 'hidemyself', false );
148 $opts->add( 'hidebyothers', false );
149
150 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
151 $opts->add( 'hidecategorization', false );
152 }
153
154 $opts->add( 'namespace', '', FormOptions::INTNULL );
155 $opts->add( 'invert', false );
156 $opts->add( 'associated', false );
157
158 return $opts;
159 }
160
161 /**
162 * Get custom show/hide filters
163 *
164 * @return array Map of filter URL param names to properties (msg/default)
165 */
166 protected function getCustomFilters() {
167 if ( $this->customFilters === null ) {
168 $this->customFilters = [];
169 Hooks::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters ] );
170 }
171
172 return $this->customFilters;
173 }
174
175 /**
176 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
177 *
178 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
179 *
180 * @param FormOptions $opts
181 * @return FormOptions
182 */
183 protected function fetchOptionsFromRequest( $opts ) {
184 $opts->fetchValuesFromRequest( $this->getRequest() );
185
186 return $opts;
187 }
188
189 /**
190 * Process $par and put options found in $opts. Used when including the page.
191 *
192 * @param string $par
193 * @param FormOptions $opts
194 */
195 public function parseParameters( $par, FormOptions $opts ) {
196 // nothing by default
197 }
198
199 /**
200 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
201 *
202 * @param FormOptions $opts
203 */
204 public function validateOptions( FormOptions $opts ) {
205 // nothing by default
206 }
207
208 /**
209 * Return an array of conditions depending of options set in $opts
210 *
211 * @param FormOptions $opts
212 * @return array
213 */
214 public function buildMainQueryConds( FormOptions $opts ) {
215 $dbr = $this->getDB();
216 $user = $this->getUser();
217 $conds = [];
218
219 // It makes no sense to hide both anons and logged-in users. When this occurs, try a guess on
220 // what the user meant and either show only bots or force anons to be shown.
221 $botsonly = false;
222 $hideanons = $opts['hideanons'];
223 if ( $opts['hideanons'] && $opts['hideliu'] ) {
224 if ( $opts['hidebots'] ) {
225 $hideanons = false;
226 } else {
227 $botsonly = true;
228 }
229 }
230
231 // Toggles
232 if ( $opts['hideminor'] ) {
233 $conds['rc_minor'] = 0;
234 }
235 if ( $opts['hidebots'] ) {
236 $conds['rc_bot'] = 0;
237 }
238 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
239 $conds['rc_patrolled'] = 0;
240 }
241 if ( $botsonly ) {
242 $conds['rc_bot'] = 1;
243 } else {
244 if ( $opts['hideliu'] ) {
245 $conds[] = 'rc_user = 0';
246 }
247 if ( $hideanons ) {
248 $conds[] = 'rc_user != 0';
249 }
250 }
251
252 if ( $opts['hidemyself'] ) {
253 if ( $user->getId() ) {
254 $conds[] = 'rc_user != ' . $dbr->addQuotes( $user->getId() );
255 } else {
256 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
257 }
258 }
259 if ( $opts['hidebyothers'] ) {
260 if ( $user->getId() ) {
261 $conds[] = 'rc_user = ' . $dbr->addQuotes( $user->getId() );
262 } else {
263 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
264 }
265 }
266
267 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' )
268 && $opts['hidecategorization'] === true
269 ) {
270 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
271 }
272
273 // Namespace filtering
274 if ( $opts['namespace'] !== '' ) {
275 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
276 $operator = $opts['invert'] ? '!=' : '=';
277 $boolean = $opts['invert'] ? 'AND' : 'OR';
278
279 // Namespace association (bug 2429)
280 if ( !$opts['associated'] ) {
281 $condition = "rc_namespace $operator $selectedNS";
282 } else {
283 // Also add the associated namespace
284 $associatedNS = $dbr->addQuotes(
285 MWNamespace::getAssociated( $opts['namespace'] )
286 );
287 $condition = "(rc_namespace $operator $selectedNS "
288 . $boolean
289 . " rc_namespace $operator $associatedNS)";
290 }
291
292 $conds[] = $condition;
293 }
294
295 return $conds;
296 }
297
298 /**
299 * Process the query
300 *
301 * @param array $conds
302 * @param FormOptions $opts
303 * @return bool|ResultWrapper Result or false
304 */
305 public function doMainQuery( $conds, $opts ) {
306 $tables = [ 'recentchanges' ];
307 $fields = RecentChange::selectFields();
308 $query_options = [];
309 $join_conds = [];
310
311 ChangeTags::modifyDisplayQuery(
312 $tables,
313 $fields,
314 $conds,
315 $join_conds,
316 $query_options,
317 ''
318 );
319
320 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
321 $opts )
322 ) {
323 return false;
324 }
325
326 $dbr = $this->getDB();
327
328 return $dbr->select(
329 $tables,
330 $fields,
331 $conds,
332 __METHOD__,
333 $query_options,
334 $join_conds
335 );
336 }
337
338 protected function runMainQueryHook( &$tables, &$fields, &$conds,
339 &$query_options, &$join_conds, $opts
340 ) {
341 return Hooks::run(
342 'ChangesListSpecialPageQuery',
343 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
344 );
345 }
346
347 /**
348 * Return a IDatabase object for reading
349 *
350 * @return IDatabase
351 */
352 protected function getDB() {
353 return wfGetDB( DB_REPLICA );
354 }
355
356 /**
357 * Send output to the OutputPage object, only called if not used feeds
358 *
359 * @param ResultWrapper $rows Database rows
360 * @param FormOptions $opts
361 */
362 public function webOutput( $rows, $opts ) {
363 if ( !$this->including() ) {
364 $this->outputFeedLinks();
365 $this->doHeader( $opts, $rows->numRows() );
366 }
367
368 $this->outputChangesList( $rows, $opts );
369 }
370
371 /**
372 * Output feed links.
373 */
374 public function outputFeedLinks() {
375 // nothing by default
376 }
377
378 /**
379 * Build and output the actual changes list.
380 *
381 * @param ResultWrapper $rows Database rows
382 * @param FormOptions $opts
383 */
384 abstract public function outputChangesList( $rows, $opts );
385
386 /**
387 * Set the text to be displayed above the changes
388 *
389 * @param FormOptions $opts
390 * @param int $numRows Number of rows in the result to show after this header
391 */
392 public function doHeader( $opts, $numRows ) {
393 $this->setTopText( $opts );
394
395 // @todo Lots of stuff should be done here.
396
397 $this->setBottomText( $opts );
398 }
399
400 /**
401 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
402 * or similar methods to print the text.
403 *
404 * @param FormOptions $opts
405 */
406 public function setTopText( FormOptions $opts ) {
407 // nothing by default
408 }
409
410 /**
411 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
412 * or similar methods to print the text.
413 *
414 * @param FormOptions $opts
415 */
416 public function setBottomText( FormOptions $opts ) {
417 // nothing by default
418 }
419
420 /**
421 * Get options to be displayed in a form
422 * @todo This should handle options returned by getDefaultOptions().
423 * @todo Not called by anything, should be called by something… doHeader() maybe?
424 *
425 * @param FormOptions $opts
426 * @return array
427 */
428 public function getExtraOptions( $opts ) {
429 return [];
430 }
431
432 /**
433 * Return the legend displayed within the fieldset
434 *
435 * @return string
436 */
437 public function makeLegend() {
438 $context = $this->getContext();
439 $user = $context->getUser();
440 # The legend showing what the letters and stuff mean
441 $legend = Html::openElement( 'dl' ) . "\n";
442 # Iterates through them and gets the messages for both letter and tooltip
443 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
444 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
445 unset( $legendItems['unpatrolled'] );
446 }
447 foreach ( $legendItems as $key => $item ) { # generate items of the legend
448 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
449 $letter = $item['letter'];
450 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
451
452 $legend .= Html::element( 'dt',
453 [ 'class' => $cssClass ], $context->msg( $letter )->text()
454 ) . "\n" .
455 Html::rawElement( 'dd',
456 [ 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ],
457 $context->msg( $label )->parse()
458 ) . "\n";
459 }
460 # (+-123)
461 $legend .= Html::rawElement( 'dt',
462 [ 'class' => 'mw-plusminus-pos' ],
463 $context->msg( 'recentchanges-legend-plusminus' )->parse()
464 ) . "\n";
465 $legend .= Html::element(
466 'dd',
467 [ 'class' => 'mw-changeslist-legend-plusminus' ],
468 $context->msg( 'recentchanges-label-plusminus' )->text()
469 ) . "\n";
470 $legend .= Html::closeElement( 'dl' ) . "\n";
471
472 # Collapsibility
473 $legend =
474 '<div class="mw-changeslist-legend">' .
475 $context->msg( 'recentchanges-legend-heading' )->parse() .
476 '<div class="mw-collapsible-content">' . $legend . '</div>' .
477 '</div>';
478
479 return $legend;
480 }
481
482 /**
483 * Add page-specific modules.
484 */
485 protected function addModules() {
486 $out = $this->getOutput();
487 // Styles and behavior for the legend box (see makeLegend())
488 $out->addModuleStyles( [
489 'mediawiki.special.changeslist.legend',
490 'mediawiki.special.changeslist',
491 ] );
492 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
493 }
494
495 protected function getGroupName() {
496 return 'changes';
497 }
498 }