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( '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 );
150 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
151 $opts->add( 'hidecategorization', false );
154 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
155 $opts->add( 'invert', false );
156 $opts->add( 'associated', false );
162 * Get custom show/hide filters
164 * @return array Map of filter URL param names to properties (msg/default)
166 protected function getCustomFilters() {
167 if ( $this->customFilters
=== null ) {
168 $this->customFilters
= [];
169 Hooks
::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters
] );
172 return $this->customFilters
;
176 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
178 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
180 * @param FormOptions $opts
181 * @return FormOptions
183 protected function fetchOptionsFromRequest( $opts ) {
184 $opts->fetchValuesFromRequest( $this->getRequest() );
190 * Process $par and put options found in $opts. Used when including the page.
193 * @param FormOptions $opts
195 public function parseParameters( $par, FormOptions
$opts ) {
196 // nothing by default
200 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
202 * @param FormOptions $opts
204 public function validateOptions( FormOptions
$opts ) {
205 // nothing by default
209 * Return an array of conditions depending of options set in $opts
211 * @param FormOptions $opts
214 public function buildMainQueryConds( FormOptions
$opts ) {
215 $dbr = $this->getDB();
216 $user = $this->getUser();
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.
222 $hideanons = $opts['hideanons'];
223 if ( $opts['hideanons'] && $opts['hideliu'] ) {
224 if ( $opts['hidebots'] ) {
232 if ( $opts['hideminor'] ) {
233 $conds['rc_minor'] = 0;
235 if ( $opts['hidebots'] ) {
236 $conds['rc_bot'] = 0;
238 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
239 $conds['rc_patrolled'] = 0;
242 $conds['rc_bot'] = 1;
244 if ( $opts['hideliu'] ) {
245 $conds[] = 'rc_user = 0';
248 $conds[] = 'rc_user != 0';
252 if ( $opts['hidemyself'] ) {
253 if ( $user->getId() ) {
254 $conds[] = 'rc_user != ' . $dbr->addQuotes( $user->getId() );
256 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
259 if ( $opts['hidebyothers'] ) {
260 if ( $user->getId() ) {
261 $conds[] = 'rc_user = ' . $dbr->addQuotes( $user->getId() );
263 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
267 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' )
268 && $opts['hidecategorization'] === true
270 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE
);
273 // Namespace filtering
274 if ( $opts['namespace'] !== '' ) {
275 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
276 $operator = $opts['invert'] ?
'!=' : '=';
277 $boolean = $opts['invert'] ?
'AND' : 'OR';
279 // Namespace association (bug 2429)
280 if ( !$opts['associated'] ) {
281 $condition = "rc_namespace $operator $selectedNS";
283 // Also add the associated namespace
284 $associatedNS = $dbr->addQuotes(
285 MWNamespace
::getAssociated( $opts['namespace'] )
287 $condition = "(rc_namespace $operator $selectedNS "
289 . " rc_namespace $operator $associatedNS)";
292 $conds[] = $condition;
301 * @param array $conds
302 * @param FormOptions $opts
303 * @return bool|ResultWrapper Result or false
305 public function doMainQuery( $conds, $opts ) {
306 $tables = [ 'recentchanges' ];
307 $fields = RecentChange
::selectFields();
311 ChangeTags
::modifyDisplayQuery(
320 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
326 $dbr = $this->getDB();
338 protected function runMainQueryHook( &$tables, &$fields, &$conds,
339 &$query_options, &$join_conds, $opts
342 'ChangesListSpecialPageQuery',
343 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
348 * Return a IDatabase object for reading
352 protected function getDB() {
353 return wfGetDB( DB_REPLICA
);
357 * Send output to the OutputPage object, only called if not used feeds
359 * @param ResultWrapper $rows Database rows
360 * @param FormOptions $opts
362 public function webOutput( $rows, $opts ) {
363 if ( !$this->including() ) {
364 $this->outputFeedLinks();
365 $this->doHeader( $opts, $rows->numRows() );
368 $this->outputChangesList( $rows, $opts );
374 public function outputFeedLinks() {
375 // nothing by default
379 * Build and output the actual changes list.
381 * @param ResultWrapper $rows Database rows
382 * @param FormOptions $opts
384 abstract public function outputChangesList( $rows, $opts );
387 * Set the text to be displayed above the changes
389 * @param FormOptions $opts
390 * @param int $numRows Number of rows in the result to show after this header
392 public function doHeader( $opts, $numRows ) {
393 $this->setTopText( $opts );
395 // @todo Lots of stuff should be done here.
397 $this->setBottomText( $opts );
401 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
402 * or similar methods to print the text.
404 * @param FormOptions $opts
406 public function setTopText( FormOptions
$opts ) {
407 // nothing by default
411 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
412 * or similar methods to print the text.
414 * @param FormOptions $opts
416 public function setBottomText( FormOptions
$opts ) {
417 // nothing by default
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?
425 * @param FormOptions $opts
428 public function getExtraOptions( $opts ) {
433 * Return the legend displayed within the fieldset
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'] );
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;
452 $legend .= Html
::element( 'dt',
453 [ 'class' => $cssClass ], $context->msg( $letter )->text()
455 Html
::rawElement( 'dd',
456 [ 'class' => Sanitizer
::escapeClass( 'mw-changeslist-legend-' . $key ) ],
457 $context->msg( $label )->parse()
461 $legend .= Html
::rawElement( 'dt',
462 [ 'class' => 'mw-plusminus-pos' ],
463 $context->msg( 'recentchanges-legend-plusminus' )->parse()
465 $legend .= Html
::element(
467 [ 'class' => 'mw-changeslist-legend-plusminus' ],
468 $context->msg( 'recentchanges-label-plusminus' )->text()
470 $legend .= Html
::closeElement( 'dl' ) . "\n";
474 '<div class="mw-changeslist-legend">' .
475 $context->msg( 'recentchanges-legend-heading' )->parse() .
476 '<div class="mw-collapsible-content">' . $legend . '</div>' .
483 * Add page-specific modules.
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',
492 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
495 protected function getGroupName() {