Merge "Don't check namespace in SpecialWantedtemplates"
[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,
319 &$query_options, &$join_conds, $opts
320 ) {
321 return Hooks::run(
322 'ChangesListSpecialPageQuery',
323 array( $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts )
324 );
325 }
326
327 /**
328 * Return a IDatabase object for reading
329 *
330 * @return IDatabase
331 */
332 protected function getDB() {
333 return wfGetDB( DB_SLAVE );
334 }
335
336 /**
337 * Send output to the OutputPage object, only called if not used feeds
338 *
339 * @param ResultWrapper $rows Database rows
340 * @param FormOptions $opts
341 */
342 public function webOutput( $rows, $opts ) {
343 if ( !$this->including() ) {
344 $this->outputFeedLinks();
345 $this->doHeader( $opts, $rows->numRows() );
346 }
347
348 $this->outputChangesList( $rows, $opts );
349 }
350
351 /**
352 * Output feed links.
353 */
354 public function outputFeedLinks() {
355 // nothing by default
356 }
357
358 /**
359 * Build and output the actual changes list.
360 *
361 * @param array $rows Database rows
362 * @param FormOptions $opts
363 */
364 abstract public function outputChangesList( $rows, $opts );
365
366 /**
367 * Set the text to be displayed above the changes
368 *
369 * @param FormOptions $opts
370 * @param int $numRows Number of rows in the result to show after this header
371 */
372 public function doHeader( $opts, $numRows ) {
373 $this->setTopText( $opts );
374
375 // @todo Lots of stuff should be done here.
376
377 $this->setBottomText( $opts );
378 }
379
380 /**
381 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
382 * or similar methods to print the text.
383 *
384 * @param FormOptions $opts
385 */
386 function setTopText( FormOptions $opts ) {
387 // nothing by default
388 }
389
390 /**
391 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
392 * or similar methods to print the text.
393 *
394 * @param FormOptions $opts
395 */
396 function setBottomText( FormOptions $opts ) {
397 // nothing by default
398 }
399
400 /**
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?
404 *
405 * @param FormOptions $opts
406 * @return array
407 */
408 function getExtraOptions( $opts ) {
409 return array();
410 }
411
412 /**
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()
416 *
417 * @param IContextSource $context The object available as $this in non-static functions
418 * @return string
419 */
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'] );
428 }
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;
433
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";
441 }
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";
453
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>';
460
461 return $legend;
462 }
463
464 /**
465 * Add page-specific modules.
466 */
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' );
472 }
473
474 protected function getGroupName() {
475 return 'changes';
476 }
477 }