Merge "API: Properly handle limit and continuation for generator=prefixsearch"
[mediawiki.git] / includes / specialpage / ChangesListSpecialPage.php
blobb1ed63c9fceb859daf811c0d48626bd1e45928ed
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 $opts = new FormOptions();
141 $opts->add( 'hideminor', false );
142 $opts->add( 'hidebots', false );
143 $opts->add( 'hideanons', false );
144 $opts->add( 'hideliu', false );
145 $opts->add( 'hidepatrolled', false );
146 $opts->add( 'hidemyself', false );
148 $opts->add( 'namespace', '', FormOptions::INTNULL );
149 $opts->add( 'invert', false );
150 $opts->add( 'associated', false );
152 return $opts;
156 * Get custom show/hide filters
158 * @return array Map of filter URL param names to properties (msg/default)
160 protected function getCustomFilters() {
161 if ( $this->customFilters === null ) {
162 $this->customFilters = array();
163 Hooks::run( 'ChangesListSpecialPageFilters', array( $this, &$this->customFilters ) );
166 return $this->customFilters;
170 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
172 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
174 * @param FormOptions $opts
175 * @return FormOptions
177 protected function fetchOptionsFromRequest( $opts ) {
178 $opts->fetchValuesFromRequest( $this->getRequest() );
180 return $opts;
184 * Process $par and put options found in $opts. Used when including the page.
186 * @param string $par
187 * @param FormOptions $opts
189 public function parseParameters( $par, FormOptions $opts ) {
190 // nothing by default
194 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
196 * @param FormOptions $opts
198 public function validateOptions( FormOptions $opts ) {
199 // nothing by default
203 * Return an array of conditions depending of options set in $opts
205 * @param FormOptions $opts
206 * @return array
208 public function buildMainQueryConds( FormOptions $opts ) {
209 $dbr = $this->getDB();
210 $user = $this->getUser();
211 $conds = array();
213 // It makes no sense to hide both anons and logged-in users. When this occurs, try a guess on
214 // what the user meant and either show only bots or force anons to be shown.
215 $botsonly = false;
216 $hideanons = $opts['hideanons'];
217 if ( $opts['hideanons'] && $opts['hideliu'] ) {
218 if ( $opts['hidebots'] ) {
219 $hideanons = false;
220 } else {
221 $botsonly = true;
225 // Toggles
226 if ( $opts['hideminor'] ) {
227 $conds['rc_minor'] = 0;
229 if ( $opts['hidebots'] ) {
230 $conds['rc_bot'] = 0;
232 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
233 $conds['rc_patrolled'] = 0;
235 if ( $botsonly ) {
236 $conds['rc_bot'] = 1;
237 } else {
238 if ( $opts['hideliu'] ) {
239 $conds[] = 'rc_user = 0';
241 if ( $hideanons ) {
242 $conds[] = 'rc_user != 0';
245 if ( $opts['hidemyself'] ) {
246 if ( $user->getId() ) {
247 $conds[] = 'rc_user != ' . $dbr->addQuotes( $user->getId() );
248 } else {
249 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
253 // Namespace filtering
254 if ( $opts['namespace'] !== '' ) {
255 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
256 $operator = $opts['invert'] ? '!=' : '=';
257 $boolean = $opts['invert'] ? 'AND' : 'OR';
259 // Namespace association (bug 2429)
260 if ( !$opts['associated'] ) {
261 $condition = "rc_namespace $operator $selectedNS";
262 } else {
263 // Also add the associated namespace
264 $associatedNS = $dbr->addQuotes(
265 MWNamespace::getAssociated( $opts['namespace'] )
267 $condition = "(rc_namespace $operator $selectedNS "
268 . $boolean
269 . " rc_namespace $operator $associatedNS)";
272 $conds[] = $condition;
275 return $conds;
279 * Process the query
281 * @param array $conds
282 * @param FormOptions $opts
283 * @return bool|ResultWrapper Result or false
285 public function doMainQuery( $conds, $opts ) {
286 $tables = array( 'recentchanges' );
287 $fields = RecentChange::selectFields();
288 $query_options = array();
289 $join_conds = array();
291 ChangeTags::modifyDisplayQuery(
292 $tables,
293 $fields,
294 $conds,
295 $join_conds,
296 $query_options,
300 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
301 $opts )
303 return false;
306 $dbr = $this->getDB();
308 return $dbr->select(
309 $tables,
310 $fields,
311 $conds,
312 __METHOD__,
313 $query_options,
314 $join_conds
318 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ) {
319 return Hooks::run(
320 'ChangesListSpecialPageQuery',
321 array( $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts )
326 * Return a IDatabase object for reading
328 * @return IDatabase
330 protected function getDB() {
331 return wfGetDB( DB_SLAVE );
335 * Send output to the OutputPage object, only called if not used feeds
337 * @param ResultWrapper $rows Database rows
338 * @param FormOptions $opts
340 public function webOutput( $rows, $opts ) {
341 if ( !$this->including() ) {
342 $this->outputFeedLinks();
343 $this->doHeader( $opts, $rows->numRows() );
346 $this->outputChangesList( $rows, $opts );
350 * Output feed links.
352 public function outputFeedLinks() {
353 // nothing by default
357 * Build and output the actual changes list.
359 * @param array $rows Database rows
360 * @param FormOptions $opts
362 abstract public function outputChangesList( $rows, $opts );
365 * Set the text to be displayed above the changes
367 * @param FormOptions $opts
368 * @param int $numRows Number of rows in the result to show after this header
370 public function doHeader( $opts, $numRows ) {
371 $this->setTopText( $opts );
373 // @todo Lots of stuff should be done here.
375 $this->setBottomText( $opts );
379 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
380 * or similar methods to print the text.
382 * @param FormOptions $opts
384 function setTopText( FormOptions $opts ) {
385 // nothing by default
389 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
390 * or similar methods to print the text.
392 * @param FormOptions $opts
394 function setBottomText( FormOptions $opts ) {
395 // nothing by default
399 * Get options to be displayed in a form
400 * @todo This should handle options returned by getDefaultOptions().
401 * @todo Not called by anything, should be called by something… doHeader() maybe?
403 * @param FormOptions $opts
404 * @return array
406 function getExtraOptions( $opts ) {
407 return array();
411 * Return the legend displayed within the fieldset
412 * @todo This should not be static, then we can drop the parameter
413 * @todo Not called by anything, should be called by doHeader()
415 * @param IContextSource $context The object available as $this in non-static functions
416 * @return string
418 public static function makeLegend( IContextSource $context ) {
419 $user = $context->getUser();
420 # The legend showing what the letters and stuff mean
421 $legend = Html::openElement( 'dl' ) . "\n";
422 # Iterates through them and gets the messages for both letter and tooltip
423 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
424 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
425 unset( $legendItems['unpatrolled'] );
427 foreach ( $legendItems as $key => $item ) { # generate items of the legend
428 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
429 $letter = $item['letter'];
430 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
432 $legend .= Html::element( 'dt',
433 array( 'class' => $cssClass ), $context->msg( $letter )->text()
434 ) . "\n" .
435 Html::rawElement( 'dd', array(),
436 $context->msg( $label )->parse()
437 ) . "\n";
439 # (+-123)
440 $legend .= Html::rawElement( 'dt',
441 array( 'class' => 'mw-plusminus-pos' ),
442 $context->msg( 'recentchanges-legend-plusminus' )->parse()
443 ) . "\n";
444 $legend .= Html::element(
445 'dd',
446 array( 'class' => 'mw-changeslist-legend-plusminus' ),
447 $context->msg( 'recentchanges-label-plusminus' )->text()
448 ) . "\n";
449 $legend .= Html::closeElement( 'dl' ) . "\n";
451 # Collapsibility
452 $legend =
453 '<div class="mw-changeslist-legend">' .
454 $context->msg( 'recentchanges-legend-heading' )->parse() .
455 '<div class="mw-collapsible-content">' . $legend . '</div>' .
456 '</div>';
458 return $legend;
462 * Add page-specific modules.
464 protected function addModules() {
465 $out = $this->getOutput();
466 // Styles and behavior for the legend box (see makeLegend())
467 $out->addModuleStyles( 'mediawiki.special.changeslist.legend' );
468 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
471 protected function getGroupName() {
472 return 'changes';