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( '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 );
152 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
153 $opts->add( 'hidecategorization', false );
155 $opts->add( 'hidepageedits', false );
156 $opts->add( 'hidenewpages', false );
157 $opts->add( 'hidelog', false );
159 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
160 $opts->add( 'invert', false );
161 $opts->add( 'associated', false );
167 * Get custom show/hide filters
169 * @return array Map of filter URL param names to properties (msg/default)
171 protected function getCustomFilters() {
172 if ( $this->customFilters
=== null ) {
173 $this->customFilters
= [];
174 Hooks
::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters
] );
177 return $this->customFilters
;
181 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
183 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
185 * @param FormOptions $opts
186 * @return FormOptions
188 protected function fetchOptionsFromRequest( $opts ) {
189 $opts->fetchValuesFromRequest( $this->getRequest() );
195 * Process $par and put options found in $opts. Used when including the page.
198 * @param FormOptions $opts
200 public function parseParameters( $par, FormOptions
$opts ) {
201 // nothing by default
205 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
207 * @param FormOptions $opts
209 public function validateOptions( FormOptions
$opts ) {
210 // nothing by default
214 * Return an array of conditions depending of options set in $opts
216 * @param FormOptions $opts
219 public function buildMainQueryConds( FormOptions
$opts ) {
220 $dbr = $this->getDB();
221 $user = $this->getUser();
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.
227 $hideanons = $opts['hideanons'];
228 if ( $opts['hideanons'] && $opts['hideliu'] ) {
229 if ( $opts['hidebots'] ) {
237 if ( $opts['hideminor'] ) {
238 $conds['rc_minor'] = 0;
240 if ( $opts['hidebots'] ) {
241 $conds['rc_bot'] = 0;
243 if ( $opts['hidehumans'] ) {
244 $conds[] = 'rc_bot = 1';
246 if ( $user->useRCPatrol() ) {
247 if ( $opts['hidepatrolled'] ) {
248 $conds[] = 'rc_patrolled = 0';
250 if ( $opts['hideunpatrolled'] ) {
251 $conds[] = 'rc_patrolled = 1';
255 $conds['rc_bot'] = 1;
257 if ( $opts['hideliu'] ) {
258 $conds[] = 'rc_user = 0';
261 $conds[] = 'rc_user != 0';
265 if ( $opts['hidemyself'] ) {
266 if ( $user->getId() ) {
267 $conds[] = 'rc_user != ' . $dbr->addQuotes( $user->getId() );
269 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
272 if ( $opts['hidebyothers'] ) {
273 if ( $user->getId() ) {
274 $conds[] = 'rc_user = ' . $dbr->addQuotes( $user->getId() );
276 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
280 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' )
281 && $opts['hidecategorization'] === true
283 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE
);
285 if ( $opts['hidepageedits'] ) {
286 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT
);
288 if ( $opts['hidenewpages'] ) {
289 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW
);
291 if ( $opts['hidelog'] ) {
292 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG
);
295 // Namespace filtering
296 if ( $opts['namespace'] !== '' ) {
297 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
298 $operator = $opts['invert'] ?
'!=' : '=';
299 $boolean = $opts['invert'] ?
'AND' : 'OR';
301 // Namespace association (bug 2429)
302 if ( !$opts['associated'] ) {
303 $condition = "rc_namespace $operator $selectedNS";
305 // Also add the associated namespace
306 $associatedNS = $dbr->addQuotes(
307 MWNamespace
::getAssociated( $opts['namespace'] )
309 $condition = "(rc_namespace $operator $selectedNS "
311 . " rc_namespace $operator $associatedNS)";
314 $conds[] = $condition;
323 * @param array $conds
324 * @param FormOptions $opts
325 * @return bool|ResultWrapper Result or false
327 public function doMainQuery( $conds, $opts ) {
328 $tables = [ 'recentchanges' ];
329 $fields = RecentChange
::selectFields();
333 ChangeTags
::modifyDisplayQuery(
342 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
348 $dbr = $this->getDB();
360 protected function runMainQueryHook( &$tables, &$fields, &$conds,
361 &$query_options, &$join_conds, $opts
364 'ChangesListSpecialPageQuery',
365 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
370 * Return a IDatabase object for reading
374 protected function getDB() {
375 return wfGetDB( DB_REPLICA
);
379 * Send output to the OutputPage object, only called if not used feeds
381 * @param ResultWrapper $rows Database rows
382 * @param FormOptions $opts
384 public function webOutput( $rows, $opts ) {
385 if ( !$this->including() ) {
386 $this->outputFeedLinks();
387 $this->doHeader( $opts, $rows->numRows() );
390 $this->outputChangesList( $rows, $opts );
396 public function outputFeedLinks() {
397 // nothing by default
401 * Build and output the actual changes list.
403 * @param ResultWrapper $rows Database rows
404 * @param FormOptions $opts
406 abstract public function outputChangesList( $rows, $opts );
409 * Set the text to be displayed above the changes
411 * @param FormOptions $opts
412 * @param int $numRows Number of rows in the result to show after this header
414 public function doHeader( $opts, $numRows ) {
415 $this->setTopText( $opts );
417 // @todo Lots of stuff should be done here.
419 $this->setBottomText( $opts );
423 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
424 * or similar methods to print the text.
426 * @param FormOptions $opts
428 public function setTopText( FormOptions
$opts ) {
429 // nothing by default
433 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
434 * or similar methods to print the text.
436 * @param FormOptions $opts
438 public function setBottomText( FormOptions
$opts ) {
439 // nothing by default
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?
447 * @param FormOptions $opts
450 public function getExtraOptions( $opts ) {
455 * Return the legend displayed within the fieldset
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'] );
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;
474 $legend .= Html
::element( 'dt',
475 [ 'class' => $cssClass ], $context->msg( $letter )->text()
477 Html
::rawElement( 'dd',
478 [ 'class' => Sanitizer
::escapeClass( 'mw-changeslist-legend-' . $key ) ],
479 $context->msg( $label )->parse()
483 $legend .= Html
::rawElement( 'dt',
484 [ 'class' => 'mw-plusminus-pos' ],
485 $context->msg( 'recentchanges-legend-plusminus' )->parse()
487 $legend .= Html
::element(
489 [ 'class' => 'mw-changeslist-legend-plusminus' ],
490 $context->msg( 'recentchanges-label-plusminus' )->text()
492 $legend .= Html
::closeElement( 'dl' ) . "\n";
496 '<div class="mw-changeslist-legend">' .
497 $context->msg( 'recentchanges-legend-heading' )->parse() .
498 '<div class="mw-collapsible-content">' . $legend . '</div>' .
505 * Add page-specific modules.
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',
514 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
517 protected function getGroupName() {
522 * Get filters that can be rendered.
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
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)
531 protected function getRenderableCustomFilters( $allFilters ) {
534 function( $filter ) {
535 return isset( $filter['msg'] ) && ( $filter['msg'] !== false );