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