3 * Special page which uses a ChangesList to show query results.
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.
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.
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
21 * @ingroup SpecialPage
25 * Special page which uses a ChangesList to show query results.
26 * @todo Way too many public functions, most of them should be protected
28 * @ingroup SpecialPage
30 abstract class ChangesListSpecialPage
extends SpecialPage
{
34 /** @var FormOptions */
38 protected $customFilters;
41 * Main execution point
43 * @param string $subpage
45 public function execute( $subpage ) {
46 $this->rcSubpage
= $subpage;
49 $this->outputHeader();
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 );
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 );
77 $this->webOutput( $rows, $opts );
83 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
85 * @return bool|ResultWrapper Result or false
87 public function getRows() {
88 $opts = $this->getOptions();
89 $conds = $this->buildMainQueryConds( $opts );
91 return $this->doMainQuery( $conds, $opts );
95 * Get the current FormOptions for this request
99 public function getOptions() {
100 if ( $this->rcOptions
=== null ) {
101 $this->rcOptions
= $this->setup( $this->rcSubpage
);
104 return $this->rcOptions
;
108 * Create a FormOptions object with options as specified by the user
110 * @param array $parameters
112 * @return FormOptions
114 public function setup( $parameters ) {
115 $opts = $this->getDefaultOptions();
116 foreach ( $this->getCustomFilters() as $key => $params ) {
117 $opts->add( $key, $params['default'] );
120 $opts = $this->fetchOptionsFromRequest( $opts );
122 // Give precedence to subpage syntax
123 if ( $parameters !== null ) {
124 $this->parseParameters( $parameters, $opts );
127 $this->validateOptions( $opts );
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.
136 * @return FormOptions
138 public function getDefaultOptions() {
139 $config = $this->getConfig();
140 $opts = new FormOptions();
142 $opts->add( 'hideminor', false );
143 $opts->add( 'hidemajor', false );
144 $opts->add( 'hidebots', false );
145 $opts->add( 'hidehumans', false );
146 $opts->add( 'hideanons', false );
147 $opts->add( 'hideliu', false );
148 $opts->add( 'hidepatrolled', false );
149 $opts->add( 'hideunpatrolled', false );
150 $opts->add( 'hidemyself', false );
151 $opts->add( 'hidebyothers', false );
153 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
154 $opts->add( 'hidecategorization', false );
156 $opts->add( 'hidepageedits', false );
157 $opts->add( 'hidenewpages', false );
158 $opts->add( 'hidelog', false );
160 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
161 $opts->add( 'invert', false );
162 $opts->add( 'associated', false );
168 * Get custom show/hide filters
170 * @return array Map of filter URL param names to properties (msg/default)
172 protected function getCustomFilters() {
173 if ( $this->customFilters
=== null ) {
174 $this->customFilters
= [];
175 Hooks
::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters
] );
178 return $this->customFilters
;
182 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
184 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
186 * @param FormOptions $opts
187 * @return FormOptions
189 protected function fetchOptionsFromRequest( $opts ) {
190 $opts->fetchValuesFromRequest( $this->getRequest() );
196 * Process $par and put options found in $opts. Used when including the page.
199 * @param FormOptions $opts
201 public function parseParameters( $par, FormOptions
$opts ) {
202 // nothing by default
206 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
208 * @param FormOptions $opts
210 public function validateOptions( FormOptions
$opts ) {
211 // nothing by default
215 * Return an array of conditions depending of options set in $opts
217 * @param FormOptions $opts
220 public function buildMainQueryConds( FormOptions
$opts ) {
221 $dbr = $this->getDB();
222 $user = $this->getUser();
225 // It makes no sense to hide both anons and logged-in users. When this occurs, try a guess on
226 // what the user meant and either show only bots or force anons to be shown.
228 $hideanons = $opts['hideanons'];
229 if ( $opts['hideanons'] && $opts['hideliu'] ) {
230 if ( $opts['hidebots'] ) {
238 if ( $opts['hideminor'] ) {
239 $conds[] = 'rc_minor = 0';
241 if ( $opts['hidemajor'] ) {
242 $conds[] = 'rc_minor = 1';
244 if ( $opts['hidebots'] ) {
245 $conds['rc_bot'] = 0;
247 if ( $opts['hidehumans'] ) {
248 $conds[] = 'rc_bot = 1';
250 if ( $user->useRCPatrol() ) {
251 if ( $opts['hidepatrolled'] ) {
252 $conds[] = 'rc_patrolled = 0';
254 if ( $opts['hideunpatrolled'] ) {
255 $conds[] = 'rc_patrolled = 1';
259 $conds['rc_bot'] = 1;
261 if ( $opts['hideliu'] ) {
262 $conds[] = 'rc_user = 0';
265 $conds[] = 'rc_user != 0';
269 if ( $opts['hidemyself'] ) {
270 if ( $user->getId() ) {
271 $conds[] = 'rc_user != ' . $dbr->addQuotes( $user->getId() );
273 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
276 if ( $opts['hidebyothers'] ) {
277 if ( $user->getId() ) {
278 $conds[] = 'rc_user = ' . $dbr->addQuotes( $user->getId() );
280 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
284 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' )
285 && $opts['hidecategorization'] === true
287 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE
);
289 if ( $opts['hidepageedits'] ) {
290 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT
);
292 if ( $opts['hidenewpages'] ) {
293 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW
);
295 if ( $opts['hidelog'] ) {
296 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG
);
299 // Namespace filtering
300 if ( $opts['namespace'] !== '' ) {
301 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
302 $operator = $opts['invert'] ?
'!=' : '=';
303 $boolean = $opts['invert'] ?
'AND' : 'OR';
305 // Namespace association (bug 2429)
306 if ( !$opts['associated'] ) {
307 $condition = "rc_namespace $operator $selectedNS";
309 // Also add the associated namespace
310 $associatedNS = $dbr->addQuotes(
311 MWNamespace
::getAssociated( $opts['namespace'] )
313 $condition = "(rc_namespace $operator $selectedNS "
315 . " rc_namespace $operator $associatedNS)";
318 $conds[] = $condition;
327 * @param array $conds
328 * @param FormOptions $opts
329 * @return bool|ResultWrapper Result or false
331 public function doMainQuery( $conds, $opts ) {
332 $tables = [ 'recentchanges' ];
333 $fields = RecentChange
::selectFields();
337 ChangeTags
::modifyDisplayQuery(
346 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
352 $dbr = $this->getDB();
364 protected function runMainQueryHook( &$tables, &$fields, &$conds,
365 &$query_options, &$join_conds, $opts
368 'ChangesListSpecialPageQuery',
369 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
374 * Return a IDatabase object for reading
378 protected function getDB() {
379 return wfGetDB( DB_REPLICA
);
383 * Send output to the OutputPage object, only called if not used feeds
385 * @param ResultWrapper $rows Database rows
386 * @param FormOptions $opts
388 public function webOutput( $rows, $opts ) {
389 if ( !$this->including() ) {
390 $this->outputFeedLinks();
391 $this->doHeader( $opts, $rows->numRows() );
394 $this->outputChangesList( $rows, $opts );
400 public function outputFeedLinks() {
401 // nothing by default
405 * Build and output the actual changes list.
407 * @param ResultWrapper $rows Database rows
408 * @param FormOptions $opts
410 abstract public function outputChangesList( $rows, $opts );
413 * Set the text to be displayed above the changes
415 * @param FormOptions $opts
416 * @param int $numRows Number of rows in the result to show after this header
418 public function doHeader( $opts, $numRows ) {
419 $this->setTopText( $opts );
421 // @todo Lots of stuff should be done here.
423 $this->setBottomText( $opts );
427 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
428 * or similar methods to print the text.
430 * @param FormOptions $opts
432 public function setTopText( FormOptions
$opts ) {
433 // nothing by default
437 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
438 * or similar methods to print the text.
440 * @param FormOptions $opts
442 public function setBottomText( FormOptions
$opts ) {
443 // nothing by default
447 * Get options to be displayed in a form
448 * @todo This should handle options returned by getDefaultOptions().
449 * @todo Not called by anything, should be called by something… doHeader() maybe?
451 * @param FormOptions $opts
454 public function getExtraOptions( $opts ) {
459 * Return the legend displayed within the fieldset
463 public function makeLegend() {
464 $context = $this->getContext();
465 $user = $context->getUser();
466 # The legend showing what the letters and stuff mean
467 $legend = Html
::openElement( 'dl' ) . "\n";
468 # Iterates through them and gets the messages for both letter and tooltip
469 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
470 if ( !( $user->useRCPatrol() ||
$user->useNPPatrol() ) ) {
471 unset( $legendItems['unpatrolled'] );
473 foreach ( $legendItems as $key => $item ) { # generate items of the legend
474 $label = isset( $item['legend'] ) ?
$item['legend'] : $item['title'];
475 $letter = $item['letter'];
476 $cssClass = isset( $item['class'] ) ?
$item['class'] : $key;
478 $legend .= Html
::element( 'dt',
479 [ 'class' => $cssClass ], $context->msg( $letter )->text()
481 Html
::rawElement( 'dd',
482 [ 'class' => Sanitizer
::escapeClass( 'mw-changeslist-legend-' . $key ) ],
483 $context->msg( $label )->parse()
487 $legend .= Html
::rawElement( 'dt',
488 [ 'class' => 'mw-plusminus-pos' ],
489 $context->msg( 'recentchanges-legend-plusminus' )->parse()
491 $legend .= Html
::element(
493 [ 'class' => 'mw-changeslist-legend-plusminus' ],
494 $context->msg( 'recentchanges-label-plusminus' )->text()
496 $legend .= Html
::closeElement( 'dl' ) . "\n";
500 '<div class="mw-changeslist-legend">' .
501 $context->msg( 'recentchanges-legend-heading' )->parse() .
502 '<div class="mw-collapsible-content">' . $legend . '</div>' .
509 * Add page-specific modules.
511 protected function addModules() {
512 $out = $this->getOutput();
513 // Styles and behavior for the legend box (see makeLegend())
514 $out->addModuleStyles( [
515 'mediawiki.special.changeslist.legend',
516 'mediawiki.special.changeslist',
518 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
521 protected function getGroupName() {
526 * Get filters that can be rendered.
528 * Filters with 'msg' => false can be used to filter data but won't
529 * be presented as show/hide toggles in the UI. They are not returned
532 * @param array $allFilters Map of filter URL param names to properties (msg/default)
533 * @return array Map of filter URL param names to properties (msg/default)
535 protected function getRenderableCustomFilters( $allFilters ) {
538 function( $filter ) {
539 return isset( $filter['msg'] ) && ( $filter['msg'] !== false );