mediawiki.userSuggest: Use formatversion=2 for API request
[mediawiki.git] / includes / specialpage / ChangesListSpecialPage.php
blob13dffb46aea2092c78398371951537353d02c6f0
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
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
28 * @ingroup SpecialPage
30 abstract class ChangesListSpecialPage extends SpecialPage {
31 /** @var string */
32 protected $rcSubpage;
34 /** @var FormOptions */
35 protected $rcOptions;
37 /** @var array */
38 protected $customFilters;
40 /**
41 * Main execution point
43 * @param string $subpage
45 public function execute( $subpage ) {
46 $this->rcSubpage = $subpage;
48 $this->setHeaders();
49 $this->outputHeader();
50 $this->addModules();
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 );
60 return;
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 );
75 $batch->execute();
77 $this->webOutput( $rows, $opts );
79 $rows->free();
82 /**
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 );
94 /**
95 * Get the current FormOptions for this request
97 * @return FormOptions
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 );
129 return $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 );
149 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
150 $opts->add( 'hidecategorization', false );
153 $opts->add( 'namespace', '', FormOptions::INTNULL );
154 $opts->add( 'invert', false );
155 $opts->add( 'associated', false );
157 return $opts;
161 * Get custom show/hide filters
163 * @return array Map of filter URL param names to properties (msg/default)
165 protected function getCustomFilters() {
166 if ( $this->customFilters === null ) {
167 $this->customFilters = array();
168 Hooks::run( 'ChangesListSpecialPageFilters', array( $this, &$this->customFilters ) );
171 return $this->customFilters;
175 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
177 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
179 * @param FormOptions $opts
180 * @return FormOptions
182 protected function fetchOptionsFromRequest( $opts ) {
183 $opts->fetchValuesFromRequest( $this->getRequest() );
185 return $opts;
189 * Process $par and put options found in $opts. Used when including the page.
191 * @param string $par
192 * @param FormOptions $opts
194 public function parseParameters( $par, FormOptions $opts ) {
195 // nothing by default
199 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
201 * @param FormOptions $opts
203 public function validateOptions( FormOptions $opts ) {
204 // nothing by default
208 * Return an array of conditions depending of options set in $opts
210 * @param FormOptions $opts
211 * @return array
213 public function buildMainQueryConds( FormOptions $opts ) {
214 $dbr = $this->getDB();
215 $user = $this->getUser();
216 $conds = array();
218 // It makes no sense to hide both anons and logged-in users. When this occurs, try a guess on
219 // what the user meant and either show only bots or force anons to be shown.
220 $botsonly = false;
221 $hideanons = $opts['hideanons'];
222 if ( $opts['hideanons'] && $opts['hideliu'] ) {
223 if ( $opts['hidebots'] ) {
224 $hideanons = false;
225 } else {
226 $botsonly = true;
230 // Toggles
231 if ( $opts['hideminor'] ) {
232 $conds['rc_minor'] = 0;
234 if ( $opts['hidebots'] ) {
235 $conds['rc_bot'] = 0;
237 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
238 $conds['rc_patrolled'] = 0;
240 if ( $botsonly ) {
241 $conds['rc_bot'] = 1;
242 } else {
243 if ( $opts['hideliu'] ) {
244 $conds[] = 'rc_user = 0';
246 if ( $hideanons ) {
247 $conds[] = 'rc_user != 0';
250 if ( $opts['hidemyself'] ) {
251 if ( $user->getId() ) {
252 $conds[] = 'rc_user != ' . $dbr->addQuotes( $user->getId() );
253 } else {
254 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
257 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' )
258 && $opts['hidecategorization'] === true
260 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
263 // Namespace filtering
264 if ( $opts['namespace'] !== '' ) {
265 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
266 $operator = $opts['invert'] ? '!=' : '=';
267 $boolean = $opts['invert'] ? 'AND' : 'OR';
269 // Namespace association (bug 2429)
270 if ( !$opts['associated'] ) {
271 $condition = "rc_namespace $operator $selectedNS";
272 } else {
273 // Also add the associated namespace
274 $associatedNS = $dbr->addQuotes(
275 MWNamespace::getAssociated( $opts['namespace'] )
277 $condition = "(rc_namespace $operator $selectedNS "
278 . $boolean
279 . " rc_namespace $operator $associatedNS)";
282 $conds[] = $condition;
285 return $conds;
289 * Process the query
291 * @param array $conds
292 * @param FormOptions $opts
293 * @return bool|ResultWrapper Result or false
295 public function doMainQuery( $conds, $opts ) {
296 $tables = array( 'recentchanges' );
297 $fields = RecentChange::selectFields();
298 $query_options = array();
299 $join_conds = array();
301 ChangeTags::modifyDisplayQuery(
302 $tables,
303 $fields,
304 $conds,
305 $join_conds,
306 $query_options,
310 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
311 $opts )
313 return false;
316 $dbr = $this->getDB();
318 return $dbr->select(
319 $tables,
320 $fields,
321 $conds,
322 __METHOD__,
323 $query_options,
324 $join_conds
328 protected function runMainQueryHook( &$tables, &$fields, &$conds,
329 &$query_options, &$join_conds, $opts
331 return Hooks::run(
332 'ChangesListSpecialPageQuery',
333 array( $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts )
338 * Return a IDatabase object for reading
340 * @return IDatabase
342 protected function getDB() {
343 return wfGetDB( DB_SLAVE );
347 * Send output to the OutputPage object, only called if not used feeds
349 * @param ResultWrapper $rows Database rows
350 * @param FormOptions $opts
352 public function webOutput( $rows, $opts ) {
353 if ( !$this->including() ) {
354 $this->outputFeedLinks();
355 $this->doHeader( $opts, $rows->numRows() );
358 $this->outputChangesList( $rows, $opts );
362 * Output feed links.
364 public function outputFeedLinks() {
365 // nothing by default
369 * Build and output the actual changes list.
371 * @param array $rows Database rows
372 * @param FormOptions $opts
374 abstract public function outputChangesList( $rows, $opts );
377 * Set the text to be displayed above the changes
379 * @param FormOptions $opts
380 * @param int $numRows Number of rows in the result to show after this header
382 public function doHeader( $opts, $numRows ) {
383 $this->setTopText( $opts );
385 // @todo Lots of stuff should be done here.
387 $this->setBottomText( $opts );
391 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
392 * or similar methods to print the text.
394 * @param FormOptions $opts
396 public function setTopText( FormOptions $opts ) {
397 // nothing by default
401 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
402 * or similar methods to print the text.
404 * @param FormOptions $opts
406 public function setBottomText( FormOptions $opts ) {
407 // nothing by default
411 * Get options to be displayed in a form
412 * @todo This should handle options returned by getDefaultOptions().
413 * @todo Not called by anything, should be called by something… doHeader() maybe?
415 * @param FormOptions $opts
416 * @return array
418 public function getExtraOptions( $opts ) {
419 return array();
423 * Return the legend displayed within the fieldset
425 * @return string
427 public function makeLegend() {
428 $context = $this->getContext();
429 $user = $context->getUser();
430 # The legend showing what the letters and stuff mean
431 $legend = Html::openElement( 'dl' ) . "\n";
432 # Iterates through them and gets the messages for both letter and tooltip
433 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
434 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
435 unset( $legendItems['unpatrolled'] );
437 foreach ( $legendItems as $key => $item ) { # generate items of the legend
438 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
439 $letter = $item['letter'];
440 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
442 $legend .= Html::element( 'dt',
443 array( 'class' => $cssClass ), $context->msg( $letter )->text()
444 ) . "\n" .
445 Html::rawElement( 'dd',
446 array( 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ),
447 $context->msg( $label )->parse()
448 ) . "\n";
450 # (+-123)
451 $legend .= Html::rawElement( 'dt',
452 array( 'class' => 'mw-plusminus-pos' ),
453 $context->msg( 'recentchanges-legend-plusminus' )->parse()
454 ) . "\n";
455 $legend .= Html::element(
456 'dd',
457 array( 'class' => 'mw-changeslist-legend-plusminus' ),
458 $context->msg( 'recentchanges-label-plusminus' )->text()
459 ) . "\n";
460 $legend .= Html::closeElement( 'dl' ) . "\n";
462 # Collapsibility
463 $legend =
464 '<div class="mw-changeslist-legend">' .
465 $context->msg( 'recentchanges-legend-heading' )->parse() .
466 '<div class="mw-collapsible-content">' . $legend . '</div>' .
467 '</div>';
469 return $legend;
473 * Add page-specific modules.
475 protected function addModules() {
476 $out = $this->getOutput();
477 // Styles and behavior for the legend box (see makeLegend())
478 $out->addModuleStyles( 'mediawiki.special.changeslist.legend' );
479 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
482 protected function getGroupName() {
483 return 'changes';