Merge "Replace Linker::link() & Linker::linkKnown() with LinkRenderer"
[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( 'hidehumans', false );
145 $opts->add( 'hideanons', false );
146 $opts->add( 'hideliu', false );
147 $opts->add( 'hidepatrolled', false );
148 $opts->add( 'hideunpatrolled', false );
149 $opts->add( 'hidemyself', false );
150 $opts->add( 'hidebyothers', false );
151
152 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
153 $opts->add( 'hidecategorization', false );
154 }
155 $opts->add( 'hidepageedits', false );
156 $opts->add( 'hidenewpages', false );
157 $opts->add( 'hidelog', false );
158
159 $opts->add( 'namespace', '', FormOptions::INTNULL );
160 $opts->add( 'invert', false );
161 $opts->add( 'associated', false );
162
163 return $opts;
164 }
165
166 /**
167 * Get custom show/hide filters
168 *
169 * @return array Map of filter URL param names to properties (msg/default)
170 */
171 protected function getCustomFilters() {
172 if ( $this->customFilters === null ) {
173 $this->customFilters = [];
174 Hooks::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters ] );
175 }
176
177 return $this->customFilters;
178 }
179
180 /**
181 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
182 *
183 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
184 *
185 * @param FormOptions $opts
186 * @return FormOptions
187 */
188 protected function fetchOptionsFromRequest( $opts ) {
189 $opts->fetchValuesFromRequest( $this->getRequest() );
190
191 return $opts;
192 }
193
194 /**
195 * Process $par and put options found in $opts. Used when including the page.
196 *
197 * @param string $par
198 * @param FormOptions $opts
199 */
200 public function parseParameters( $par, FormOptions $opts ) {
201 // nothing by default
202 }
203
204 /**
205 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
206 *
207 * @param FormOptions $opts
208 */
209 public function validateOptions( FormOptions $opts ) {
210 // nothing by default
211 }
212
213 /**
214 * Return an array of conditions depending of options set in $opts
215 *
216 * @param FormOptions $opts
217 * @return array
218 */
219 public function buildMainQueryConds( FormOptions $opts ) {
220 $dbr = $this->getDB();
221 $user = $this->getUser();
222 $conds = [];
223
224 // It makes no sense to hide both anons and logged-in users. When this occurs, try a guess on
225 // what the user meant and either show only bots or force anons to be shown.
226 $botsonly = false;
227 $hideanons = $opts['hideanons'];
228 if ( $opts['hideanons'] && $opts['hideliu'] ) {
229 if ( $opts['hidebots'] ) {
230 $hideanons = false;
231 } else {
232 $botsonly = true;
233 }
234 }
235
236 // Toggles
237 if ( $opts['hideminor'] ) {
238 $conds['rc_minor'] = 0;
239 }
240 if ( $opts['hidebots'] ) {
241 $conds['rc_bot'] = 0;
242 }
243 if ( $opts['hidehumans'] ) {
244 $conds[] = 'rc_bot = 1';
245 }
246 if ( $user->useRCPatrol() ) {
247 if ( $opts['hidepatrolled'] ) {
248 $conds[] = 'rc_patrolled = 0';
249 }
250 if ( $opts['hideunpatrolled'] ) {
251 $conds[] = 'rc_patrolled = 1';
252 }
253 }
254 if ( $botsonly ) {
255 $conds['rc_bot'] = 1;
256 } else {
257 if ( $opts['hideliu'] ) {
258 $conds[] = 'rc_user = 0';
259 }
260 if ( $hideanons ) {
261 $conds[] = 'rc_user != 0';
262 }
263 }
264
265 if ( $opts['hidemyself'] ) {
266 if ( $user->getId() ) {
267 $conds[] = 'rc_user != ' . $dbr->addQuotes( $user->getId() );
268 } else {
269 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
270 }
271 }
272 if ( $opts['hidebyothers'] ) {
273 if ( $user->getId() ) {
274 $conds[] = 'rc_user = ' . $dbr->addQuotes( $user->getId() );
275 } else {
276 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
277 }
278 }
279
280 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' )
281 && $opts['hidecategorization'] === true
282 ) {
283 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
284 }
285 if ( $opts['hidepageedits'] ) {
286 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT );
287 }
288 if ( $opts['hidenewpages'] ) {
289 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW );
290 }
291 if ( $opts['hidelog'] ) {
292 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG );
293 }
294
295 // Namespace filtering
296 if ( $opts['namespace'] !== '' ) {
297 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
298 $operator = $opts['invert'] ? '!=' : '=';
299 $boolean = $opts['invert'] ? 'AND' : 'OR';
300
301 // Namespace association (bug 2429)
302 if ( !$opts['associated'] ) {
303 $condition = "rc_namespace $operator $selectedNS";
304 } else {
305 // Also add the associated namespace
306 $associatedNS = $dbr->addQuotes(
307 MWNamespace::getAssociated( $opts['namespace'] )
308 );
309 $condition = "(rc_namespace $operator $selectedNS "
310 . $boolean
311 . " rc_namespace $operator $associatedNS)";
312 }
313
314 $conds[] = $condition;
315 }
316
317 return $conds;
318 }
319
320 /**
321 * Process the query
322 *
323 * @param array $conds
324 * @param FormOptions $opts
325 * @return bool|ResultWrapper Result or false
326 */
327 public function doMainQuery( $conds, $opts ) {
328 $tables = [ 'recentchanges' ];
329 $fields = RecentChange::selectFields();
330 $query_options = [];
331 $join_conds = [];
332
333 ChangeTags::modifyDisplayQuery(
334 $tables,
335 $fields,
336 $conds,
337 $join_conds,
338 $query_options,
339 ''
340 );
341
342 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
343 $opts )
344 ) {
345 return false;
346 }
347
348 $dbr = $this->getDB();
349
350 return $dbr->select(
351 $tables,
352 $fields,
353 $conds,
354 __METHOD__,
355 $query_options,
356 $join_conds
357 );
358 }
359
360 protected function runMainQueryHook( &$tables, &$fields, &$conds,
361 &$query_options, &$join_conds, $opts
362 ) {
363 return Hooks::run(
364 'ChangesListSpecialPageQuery',
365 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
366 );
367 }
368
369 /**
370 * Return a IDatabase object for reading
371 *
372 * @return IDatabase
373 */
374 protected function getDB() {
375 return wfGetDB( DB_REPLICA );
376 }
377
378 /**
379 * Send output to the OutputPage object, only called if not used feeds
380 *
381 * @param ResultWrapper $rows Database rows
382 * @param FormOptions $opts
383 */
384 public function webOutput( $rows, $opts ) {
385 if ( !$this->including() ) {
386 $this->outputFeedLinks();
387 $this->doHeader( $opts, $rows->numRows() );
388 }
389
390 $this->outputChangesList( $rows, $opts );
391 }
392
393 /**
394 * Output feed links.
395 */
396 public function outputFeedLinks() {
397 // nothing by default
398 }
399
400 /**
401 * Build and output the actual changes list.
402 *
403 * @param ResultWrapper $rows Database rows
404 * @param FormOptions $opts
405 */
406 abstract public function outputChangesList( $rows, $opts );
407
408 /**
409 * Set the text to be displayed above the changes
410 *
411 * @param FormOptions $opts
412 * @param int $numRows Number of rows in the result to show after this header
413 */
414 public function doHeader( $opts, $numRows ) {
415 $this->setTopText( $opts );
416
417 // @todo Lots of stuff should be done here.
418
419 $this->setBottomText( $opts );
420 }
421
422 /**
423 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
424 * or similar methods to print the text.
425 *
426 * @param FormOptions $opts
427 */
428 public function setTopText( FormOptions $opts ) {
429 // nothing by default
430 }
431
432 /**
433 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
434 * or similar methods to print the text.
435 *
436 * @param FormOptions $opts
437 */
438 public function setBottomText( FormOptions $opts ) {
439 // nothing by default
440 }
441
442 /**
443 * Get options to be displayed in a form
444 * @todo This should handle options returned by getDefaultOptions().
445 * @todo Not called by anything, should be called by something… doHeader() maybe?
446 *
447 * @param FormOptions $opts
448 * @return array
449 */
450 public function getExtraOptions( $opts ) {
451 return [];
452 }
453
454 /**
455 * Return the legend displayed within the fieldset
456 *
457 * @return string
458 */
459 public function makeLegend() {
460 $context = $this->getContext();
461 $user = $context->getUser();
462 # The legend showing what the letters and stuff mean
463 $legend = Html::openElement( 'dl' ) . "\n";
464 # Iterates through them and gets the messages for both letter and tooltip
465 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
466 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
467 unset( $legendItems['unpatrolled'] );
468 }
469 foreach ( $legendItems as $key => $item ) { # generate items of the legend
470 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
471 $letter = $item['letter'];
472 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
473
474 $legend .= Html::element( 'dt',
475 [ 'class' => $cssClass ], $context->msg( $letter )->text()
476 ) . "\n" .
477 Html::rawElement( 'dd',
478 [ 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ],
479 $context->msg( $label )->parse()
480 ) . "\n";
481 }
482 # (+-123)
483 $legend .= Html::rawElement( 'dt',
484 [ 'class' => 'mw-plusminus-pos' ],
485 $context->msg( 'recentchanges-legend-plusminus' )->parse()
486 ) . "\n";
487 $legend .= Html::element(
488 'dd',
489 [ 'class' => 'mw-changeslist-legend-plusminus' ],
490 $context->msg( 'recentchanges-label-plusminus' )->text()
491 ) . "\n";
492 $legend .= Html::closeElement( 'dl' ) . "\n";
493
494 # Collapsibility
495 $legend =
496 '<div class="mw-changeslist-legend">' .
497 $context->msg( 'recentchanges-legend-heading' )->parse() .
498 '<div class="mw-collapsible-content">' . $legend . '</div>' .
499 '</div>';
500
501 return $legend;
502 }
503
504 /**
505 * Add page-specific modules.
506 */
507 protected function addModules() {
508 $out = $this->getOutput();
509 // Styles and behavior for the legend box (see makeLegend())
510 $out->addModuleStyles( [
511 'mediawiki.special.changeslist.legend',
512 'mediawiki.special.changeslist',
513 ] );
514 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
515 }
516
517 protected function getGroupName() {
518 return 'changes';
519 }
520
521 /**
522 * Get filters that can be rendered.
523 *
524 * Filters with 'msg' => false can be used to filter data but won't
525 * be presented as show/hide toggles in the UI. They are not returned
526 * by this function.
527 *
528 * @param array $allFilters Map of filter URL param names to properties (msg/default)
529 * @return array Map of filter URL param names to properties (msg/default)
530 */
531 protected function getRenderableCustomFilters( $allFilters ) {
532 return array_filter(
533 $allFilters,
534 function( $filter ) {
535 return isset( $filter['msg'] ) && ( $filter['msg'] !== false );
536 }
537 );
538 }
539 }