Merge "Removed old HTMLCacheUpdateJob b/c code"
[lhc/web/wiklou.git] / includes / specialpage / ChangesListSpecialPage.php
1 <?php
2 /**
3 * Special page which uses a ChangesList to show query results.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
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
27 *
28 * @ingroup SpecialPage
29 */
30 abstract class ChangesListSpecialPage extends SpecialPage {
31 /** @var string */
32 protected $rcSubpage;
33
34 /** @var FormOptions */
35 protected $rcOptions;
36
37 /** @var array */
38 protected $customFilters;
39
40 /**
41 * Main execution point
42 *
43 * @param string $subpage
44 */
45 public function execute( $subpage ) {
46 $this->rcSubpage = $subpage;
47
48 $this->setHeaders();
49 $this->outputHeader();
50 $this->addModules();
51
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 );
58 }
59
60 return;
61 }
62
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 );
72 }
73 }
74 }
75 $batch->execute();
76
77 $this->webOutput( $rows, $opts );
78
79 $rows->free();
80 }
81
82 /**
83 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
84 *
85 * @return bool|ResultWrapper Result or false
86 */
87 public function getRows() {
88 $opts = $this->getOptions();
89 $conds = $this->buildMainQueryConds( $opts );
90
91 return $this->doMainQuery( $conds, $opts );
92 }
93
94 /**
95 * Get the current FormOptions for this request
96 *
97 * @return FormOptions
98 */
99 public function getOptions() {
100 if ( $this->rcOptions === null ) {
101 $this->rcOptions = $this->setup( $this->rcSubpage );
102 }
103
104 return $this->rcOptions;
105 }
106
107 /**
108 * Create a FormOptions object with options as specified by the user
109 *
110 * @param array $parameters
111 *
112 * @return FormOptions
113 */
114 public function setup( $parameters ) {
115 $opts = $this->getDefaultOptions();
116 foreach ( $this->getCustomFilters() as $key => $params ) {
117 $opts->add( $key, $params['default'] );
118 }
119
120 $opts = $this->fetchOptionsFromRequest( $opts );
121
122 // Give precedence to subpage syntax
123 if ( $parameters !== null ) {
124 $this->parseParameters( $parameters, $opts );
125 }
126
127 $this->validateOptions( $opts );
128
129 return $opts;
130 }
131
132 /**
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.
135 *
136 * @return FormOptions
137 */
138 public function getDefaultOptions() {
139 $opts = new FormOptions();
140
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 );
147
148 $opts->add( 'namespace', '', FormOptions::INTNULL );
149 $opts->add( 'invert', false );
150 $opts->add( 'associated', false );
151
152 return $opts;
153 }
154
155 /**
156 * Get custom show/hide filters
157 *
158 * @return array Map of filter URL param names to properties (msg/default)
159 */
160 protected function getCustomFilters() {
161 if ( $this->customFilters === null ) {
162 $this->customFilters = array();
163 Hooks::run( 'ChangesListSpecialPageFilters', array( $this, &$this->customFilters ) );
164 }
165
166 return $this->customFilters;
167 }
168
169 /**
170 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
171 *
172 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
173 *
174 * @param FormOptions $opts
175 * @return FormOptions
176 */
177 protected function fetchOptionsFromRequest( $opts ) {
178 $opts->fetchValuesFromRequest( $this->getRequest() );
179
180 return $opts;
181 }
182
183 /**
184 * Process $par and put options found in $opts. Used when including the page.
185 *
186 * @param string $par
187 * @param FormOptions $opts
188 */
189 public function parseParameters( $par, FormOptions $opts ) {
190 // nothing by default
191 }
192
193 /**
194 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
195 *
196 * @param FormOptions $opts
197 */
198 public function validateOptions( FormOptions $opts ) {
199 // nothing by default
200 }
201
202 /**
203 * Return an array of conditions depending of options set in $opts
204 *
205 * @param FormOptions $opts
206 * @return array
207 */
208 public function buildMainQueryConds( FormOptions $opts ) {
209 $dbr = $this->getDB();
210 $user = $this->getUser();
211 $conds = array();
212
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;
222 }
223 }
224
225 // Toggles
226 if ( $opts['hideminor'] ) {
227 $conds['rc_minor'] = 0;
228 }
229 if ( $opts['hidebots'] ) {
230 $conds['rc_bot'] = 0;
231 }
232 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
233 $conds['rc_patrolled'] = 0;
234 }
235 if ( $botsonly ) {
236 $conds['rc_bot'] = 1;
237 } else {
238 if ( $opts['hideliu'] ) {
239 $conds[] = 'rc_user = 0';
240 }
241 if ( $hideanons ) {
242 $conds[] = 'rc_user != 0';
243 }
244 }
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() );
250 }
251 }
252
253 // Namespace filtering
254 if ( $opts['namespace'] !== '' ) {
255 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
256 $operator = $opts['invert'] ? '!=' : '=';
257 $boolean = $opts['invert'] ? 'AND' : 'OR';
258
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'] )
266 );
267 $condition = "(rc_namespace $operator $selectedNS "
268 . $boolean
269 . " rc_namespace $operator $associatedNS)";
270 }
271
272 $conds[] = $condition;
273 }
274
275 return $conds;
276 }
277
278 /**
279 * Process the query
280 *
281 * @param array $conds
282 * @param FormOptions $opts
283 * @return bool|ResultWrapper Result or false
284 */
285 public function doMainQuery( $conds, $opts ) {
286 $tables = array( 'recentchanges' );
287 $fields = RecentChange::selectFields();
288 $query_options = array();
289 $join_conds = array();
290
291 ChangeTags::modifyDisplayQuery(
292 $tables,
293 $fields,
294 $conds,
295 $join_conds,
296 $query_options,
297 ''
298 );
299
300 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
301 $opts )
302 ) {
303 return false;
304 }
305
306 $dbr = $this->getDB();
307
308 return $dbr->select(
309 $tables,
310 $fields,
311 $conds,
312 __METHOD__,
313 $query_options,
314 $join_conds
315 );
316 }
317
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 )
322 );
323 }
324
325 /**
326 * Return a IDatabase object for reading
327 *
328 * @return IDatabase
329 */
330 protected function getDB() {
331 return wfGetDB( DB_SLAVE );
332 }
333
334 /**
335 * Send output to the OutputPage object, only called if not used feeds
336 *
337 * @param ResultWrapper $rows Database rows
338 * @param FormOptions $opts
339 */
340 public function webOutput( $rows, $opts ) {
341 if ( !$this->including() ) {
342 $this->outputFeedLinks();
343 $this->doHeader( $opts, $rows->numRows() );
344 }
345
346 $this->outputChangesList( $rows, $opts );
347 }
348
349 /**
350 * Output feed links.
351 */
352 public function outputFeedLinks() {
353 // nothing by default
354 }
355
356 /**
357 * Build and output the actual changes list.
358 *
359 * @param array $rows Database rows
360 * @param FormOptions $opts
361 */
362 abstract public function outputChangesList( $rows, $opts );
363
364 /**
365 * Set the text to be displayed above the changes
366 *
367 * @param FormOptions $opts
368 * @param int $numRows Number of rows in the result to show after this header
369 */
370 public function doHeader( $opts, $numRows ) {
371 $this->setTopText( $opts );
372
373 // @todo Lots of stuff should be done here.
374
375 $this->setBottomText( $opts );
376 }
377
378 /**
379 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
380 * or similar methods to print the text.
381 *
382 * @param FormOptions $opts
383 */
384 function setTopText( FormOptions $opts ) {
385 // nothing by default
386 }
387
388 /**
389 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
390 * or similar methods to print the text.
391 *
392 * @param FormOptions $opts
393 */
394 function setBottomText( FormOptions $opts ) {
395 // nothing by default
396 }
397
398 /**
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?
402 *
403 * @param FormOptions $opts
404 * @return array
405 */
406 function getExtraOptions( $opts ) {
407 return array();
408 }
409
410 /**
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()
414 *
415 * @param IContextSource $context The object available as $this in non-static functions
416 * @return string
417 */
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'] );
426 }
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;
431
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";
438 }
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";
450
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>';
457
458 return $legend;
459 }
460
461 /**
462 * Add page-specific modules.
463 */
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' );
469 }
470
471 protected function getGroupName() {
472 return 'changes';
473 }
474 }