Made wfIsBadImage() use APC
[mediawiki.git] / includes / specialpage / ChangesListSpecialPage.php
blob23bd394ccb3e226c2fa69ef121d13bdb073cc713
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,
319 &$query_options, &$join_conds, $opts
321 return Hooks::run(
322 'ChangesListSpecialPageQuery',
323 array( $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts )
328 * Return a IDatabase object for reading
330 * @return IDatabase
332 protected function getDB() {
333 return wfGetDB( DB_SLAVE );
337 * Send output to the OutputPage object, only called if not used feeds
339 * @param ResultWrapper $rows Database rows
340 * @param FormOptions $opts
342 public function webOutput( $rows, $opts ) {
343 if ( !$this->including() ) {
344 $this->outputFeedLinks();
345 $this->doHeader( $opts, $rows->numRows() );
348 $this->outputChangesList( $rows, $opts );
352 * Output feed links.
354 public function outputFeedLinks() {
355 // nothing by default
359 * Build and output the actual changes list.
361 * @param array $rows Database rows
362 * @param FormOptions $opts
364 abstract public function outputChangesList( $rows, $opts );
367 * Set the text to be displayed above the changes
369 * @param FormOptions $opts
370 * @param int $numRows Number of rows in the result to show after this header
372 public function doHeader( $opts, $numRows ) {
373 $this->setTopText( $opts );
375 // @todo Lots of stuff should be done here.
377 $this->setBottomText( $opts );
381 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
382 * or similar methods to print the text.
384 * @param FormOptions $opts
386 function setTopText( FormOptions $opts ) {
387 // nothing by default
391 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
392 * or similar methods to print the text.
394 * @param FormOptions $opts
396 function setBottomText( FormOptions $opts ) {
397 // nothing by default
401 * Get options to be displayed in a form
402 * @todo This should handle options returned by getDefaultOptions().
403 * @todo Not called by anything, should be called by something… doHeader() maybe?
405 * @param FormOptions $opts
406 * @return array
408 function getExtraOptions( $opts ) {
409 return array();
413 * Return the legend displayed within the fieldset
414 * @todo This should not be static, then we can drop the parameter
415 * @todo Not called by anything, should be called by doHeader()
417 * @param IContextSource $context The object available as $this in non-static functions
418 * @return string
420 public static function makeLegend( IContextSource $context ) {
421 $user = $context->getUser();
422 # The legend showing what the letters and stuff mean
423 $legend = Html::openElement( 'dl' ) . "\n";
424 # Iterates through them and gets the messages for both letter and tooltip
425 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
426 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
427 unset( $legendItems['unpatrolled'] );
429 foreach ( $legendItems as $key => $item ) { # generate items of the legend
430 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
431 $letter = $item['letter'];
432 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
434 $legend .= Html::element( 'dt',
435 array( 'class' => $cssClass ), $context->msg( $letter )->text()
436 ) . "\n" .
437 Html::rawElement( 'dd',
438 array( 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ),
439 $context->msg( $label )->parse()
440 ) . "\n";
442 # (+-123)
443 $legend .= Html::rawElement( 'dt',
444 array( 'class' => 'mw-plusminus-pos' ),
445 $context->msg( 'recentchanges-legend-plusminus' )->parse()
446 ) . "\n";
447 $legend .= Html::element(
448 'dd',
449 array( 'class' => 'mw-changeslist-legend-plusminus' ),
450 $context->msg( 'recentchanges-label-plusminus' )->text()
451 ) . "\n";
452 $legend .= Html::closeElement( 'dl' ) . "\n";
454 # Collapsibility
455 $legend =
456 '<div class="mw-changeslist-legend">' .
457 $context->msg( 'recentchanges-legend-heading' )->parse() .
458 '<div class="mw-collapsible-content">' . $legend . '</div>' .
459 '</div>';
461 return $legend;
465 * Add page-specific modules.
467 protected function addModules() {
468 $out = $this->getOutput();
469 // Styles and behavior for the legend box (see makeLegend())
470 $out->addModuleStyles( 'mediawiki.special.changeslist.legend' );
471 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
474 protected function getGroupName() {
475 return 'changes';