3 * Contain a class for special pages
5 * @ingroup SpecialPages
9 * List of query page classes and their associated special pages,
10 * for periodic updates.
12 * DO NOT CHANGE THIS LIST without testing that
13 * maintenance/updateSpecialPages.php still works.
15 global $wgQueryPages; // not redundant
16 $wgQueryPages = array(
17 // QueryPage subclass Special page name Limit (false for none, none for the default)
18 //----------------------------------------------------------------------------
19 array( 'AncientPagesPage', 'Ancientpages' ),
20 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
21 array( 'DeadendPagesPage', 'Deadendpages' ),
22 array( 'DisambiguationsPage', 'Disambiguations' ),
23 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
24 array( 'LinkSearchPage', 'LinkSearch' ),
25 array( 'ListredirectsPage', 'Listredirects' ),
26 array( 'LonelyPagesPage', 'Lonelypages' ),
27 array( 'LongPagesPage', 'Longpages' ),
28 array( 'MostcategoriesPage', 'Mostcategories' ),
29 array( 'MostimagesPage', 'Mostimages' ),
30 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
31 array( 'SpecialMostlinkedtemplates', 'Mostlinkedtemplates' ),
32 array( 'MostlinkedPage', 'Mostlinked' ),
33 array( 'MostrevisionsPage', 'Mostrevisions' ),
34 array( 'FewestrevisionsPage', 'Fewestrevisions' ),
35 array( 'ShortPagesPage', 'Shortpages' ),
36 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
37 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
38 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
39 array( 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ),
40 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
41 array( 'UnusedimagesPage', 'Unusedimages' ),
42 array( 'WantedCategoriesPage', 'Wantedcategories' ),
43 array( 'WantedFilesPage', 'Wantedfiles' ),
44 array( 'WantedPagesPage', 'Wantedpages' ),
45 array( 'WantedTemplatesPage', 'Wantedtemplates' ),
46 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
47 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
48 array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
50 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
52 global $wgDisableCounters;
53 if ( !$wgDisableCounters )
54 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
58 * This is a class for doing query pages; since they're almost all the same,
59 * we factor out some of the functionality into a superclass, and let
60 * subclasses derive from it.
61 * @ingroup SpecialPage
65 * Whether or not we want plain listoutput rather than an ordered list
69 var $listoutput = false;
72 * The offset and limit in use, as passed to the query() function
80 * A mutator for $this->listoutput;
82 * @param $bool Boolean
84 function setListoutput( $bool ) {
85 $this->listoutput
= $bool;
89 * Subclasses return their name here. Make sure the name is also
90 * specified in SpecialPage.php and in Language.php as a language message
100 * Return title object representing this page
104 function getTitle() {
105 return SpecialPage
::getTitleFor( $this->getName() );
109 * Subclasses return an SQL query here.
111 * Note that the query itself should return the following four columns:
112 * 'type' (your special page's name), 'namespace', 'title', and 'value'
113 * *in that order*. 'value' is used for sorting.
115 * These may be stored in the querycache table for expensive queries,
116 * and that cached data will be returned sometimes, so the presence of
117 * extra fields can't be relied upon. The cached 'value' column will be
118 * an integer; non-numeric values are useful only for sorting the initial
121 * Don't include an ORDER or LIMIT clause, this will be added.
124 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
128 * Override to sort by increasing values
132 function sortDescending() {
136 function getOrder() {
137 return ' ORDER BY value ' .
138 ($this->sortDescending() ?
'DESC' : '');
142 * Is this query expensive (for some definition of expensive)? Then we
143 * don't let it run in miser mode. $wgDisableQueryPages causes all query
144 * pages to be declared expensive. Some query pages are always expensive.
148 function isExpensive() {
149 global $wgDisableQueryPages;
150 return $wgDisableQueryPages;
154 * Whether or not the output of the page in question is retrived from
155 * the database cache.
159 function isCached() {
162 return $this->isExpensive() && $wgMiserMode;
166 * Sometime we dont want to build rss / atom feeds.
170 function isSyndicated() {
175 * Formats the results of the query for display. The skin is the current
176 * skin; you can use it for making links. The result is a single row of
177 * result data. You should be able to grab SQL results off of it.
178 * If the function return "false", the line output will be skipped.
180 * @param $skin Skin object
181 * @param $result Object: database row
183 function formatResult( $skin, $result ) {
188 * The content returned by this function will be output before any result
192 function getPageHeader() {
197 * If using extra form wheely-dealies, return a set of parameters here
198 * as an associative array. They will be encoded and added to the paging
199 * links (prev/next/lengths).
203 function linkParameters() {
208 * Some special pages (for example SpecialListusers) might not return the
209 * current object formatted, but return the previous one instead.
210 * Setting this to return true, will call one more time wfFormatResult to
211 * be sure that the very last result is formatted and shown.
213 function tryLastResult() {
218 * Clear the cache and save new results
220 * @param $limit Integer: limit for SQL statement
221 * @param $ignoreErrors Boolean: whether to ignore database errors
223 function recache( $limit, $ignoreErrors = true ) {
224 $fname = get_class( $this ) . '::recache';
225 $dbw = wfGetDB( DB_MASTER
);
226 $dbr = wfGetDB( DB_SLAVE
, array( $this->getName(), __METHOD__
, 'vslow' ) );
227 if ( !$dbw ||
!$dbr ) {
231 if ( $ignoreErrors ) {
232 $ignoreW = $dbw->ignoreErrors( true );
233 $ignoreR = $dbr->ignoreErrors( true );
236 # Clear out any old cached data
237 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
239 $sql = $this->getSQL() . $this->getOrder();
240 if ( $limit !== false )
241 $sql = $dbr->limitResult( $sql, $limit, 0 );
242 $res = $dbr->query( $sql, $fname );
245 $num = $dbr->numRows( $res );
248 while ( $res && $row = $dbr->fetchObject( $res ) ) {
249 if ( isset( $row->value
) ) {
250 $value = intval( $row->value
); // @bug 14414
255 $vals[] = array('qc_type' => $row->type
,
256 'qc_namespace' => $row->namespace,
257 'qc_title' => $row->title
,
258 'qc_value' => $value);
261 # Save results into the querycache table on the master
262 if ( count( $vals ) ) {
263 if ( !$dbw->insert( 'querycache', $vals, __METHOD__
) ) {
264 // Set result to false to indicate error
268 if ( $ignoreErrors ) {
269 $dbw->ignoreErrors( $ignoreW );
270 $dbr->ignoreErrors( $ignoreR );
273 # Update the querycache_info record for the page
274 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
275 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
282 * This is the actual workhorse. It does everything needed to make a
283 * real, honest-to-gosh query page.
285 * @param $offset database query offset
286 * @param $limit database query limit
287 * @param $shownavigation show navigation like "next 200"?
289 function doQuery( $offset, $limit, $shownavigation=true ) {
290 global $wgUser, $wgOut, $wgLang, $wgContLang;
292 $this->offset
= $offset;
293 $this->limit
= $limit;
295 $sname = $this->getName();
296 $fname = get_class($this) . '::doQuery';
297 $dbr = wfGetDB( DB_SLAVE
);
299 $wgOut->setSyndicated( $this->isSyndicated() );
301 if ( !$this->isCached() ) {
302 $sql = $this->getSQL();
304 # Get the cached result
305 $querycache = $dbr->tableName( 'querycache' );
306 $type = $dbr->strencode( $sname );
308 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
309 FROM $querycache WHERE qc_type='$type'";
311 if( !$this->listoutput
) {
313 # Fetch the timestamp of this update
314 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
315 $tRow = $dbr->fetchObject( $tRes );
318 $updated = $wgLang->timeanddate( $tRow->qci_timestamp
, true, true );
319 $updateddate = $wgLang->date( $tRow->qci_timestamp
, true, true );
320 $updatedtime = $wgLang->time( $tRow->qci_timestamp
, true, true );
321 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
322 $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" );
323 $wgOut->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime );
325 $wgOut->addWikiMsg( 'perfcached' );
328 # If updates on this page have been disabled, let the user know
329 # that the data set won't be refreshed for now
330 global $wgDisableQueryPageUpdate;
331 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
332 $wgOut->addWikiMsg( 'querypage-no-updates' );
339 $sql .= $this->getOrder();
340 $sql = $dbr->limitResult($sql, $limit, $offset);
341 $res = $dbr->query( $sql );
342 $num = $dbr->numRows($res);
344 $this->preprocessResults( $dbr, $res );
346 $wgOut->addHTML( Xml
::openElement( 'div', array('class' => 'mw-spcontent') ) );
348 # Top header and navigation
349 if( $shownavigation ) {
350 $wgOut->addHTML( $this->getPageHeader() );
352 $wgOut->addHTML( '<p>' . wfShowingResults( $offset, $num ) . '</p>' );
353 # Disable the "next" link when we reach the end
354 $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ),
355 wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) );
356 $wgOut->addHTML( '<p>' . $paging . '</p>' );
358 # No results to show, so don't bother with "showing X of Y" etc.
359 # -- just let the user know and give up now
360 $wgOut->addHTML( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
361 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
366 # The actual results; specialist subclasses will want to handle this
367 # with more than a straight list, so we hand them the info, plus
368 # an OutputPage, and let them get on with it
369 $this->outputResults( $wgOut,
371 $dbr, # Should use a ResultWrapper for this
373 $dbr->numRows( $res ),
376 # Repeat the paging links at the bottom
377 if( $shownavigation ) {
378 $wgOut->addHTML( '<p>' . $paging . '</p>' );
381 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
387 * Format and output report results using the given information plus
390 * @param $out OutputPage to print to
391 * @param $skin Skin: user skin to use
392 * @param $dbr Database (read) connection to use
393 * @param $res Integer: result pointer
394 * @param $num Integer: number of available result rows
395 * @param $offset Integer: paging offset
397 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
402 if( !$this->listoutput
)
403 $html[] = $this->openList( $offset );
405 # $res might contain the whole 1,000 rows, so we read up to
406 # $num [should update this to use a Pager]
407 for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++
) {
408 $line = $this->formatResult( $skin, $row );
410 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
411 ?
' class="not-patrolled"'
413 $html[] = $this->listoutput
415 : "<li{$attr}>{$line}</li>\n";
419 # Flush the final result
420 if( $this->tryLastResult() ) {
422 $line = $this->formatResult( $skin, $row );
424 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
425 ?
' class="not-patrolled"'
427 $html[] = $this->listoutput
429 : "<li{$attr}>{$line}</li>\n";
433 if( !$this->listoutput
)
434 $html[] = $this->closeList();
436 $html = $this->listoutput
437 ?
$wgContLang->listToText( $html )
438 : implode( '', $html );
440 $out->addHTML( $html );
444 function openList( $offset ) {
445 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
448 function closeList() {
453 * Do any necessary preprocessing of the result object.
455 function preprocessResults( $db, $res ) {}
458 * Similar to above, but packaging in a syndicated feed instead of a web page
460 function doFeed( $class = '', $limit = 50 ) {
461 global $wgFeed, $wgFeedClasses;
465 $wgOut->addWikiMsg( 'feed-unavailable' );
470 if( $limit > $wgFeedLimit ) {
471 $limit = $wgFeedLimit;
474 if( isset($wgFeedClasses[$class]) ) {
475 $feed = new $wgFeedClasses[$class](
481 $dbr = wfGetDB( DB_SLAVE
);
482 $sql = $this->getSQL() . $this->getOrder();
483 $sql = $dbr->limitResult( $sql, $limit, 0 );
484 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
485 foreach ( $res as $obj ) {
486 $item = $this->feedResult( $obj );
488 $feed->outItem( $item );
500 * Override for custom handling. If the titles/links are ok, just do
503 function feedResult( $row ) {
504 if( !isset( $row->title
) ) {
507 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
509 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
512 $talkpage = $title->getTalkPage();
513 $comments = $talkpage->getFullURL();
517 $title->getPrefixedText(),
518 $this->feedItemDesc( $row ),
519 $title->getFullURL(),
521 $this->feedItemAuthor( $row ),
528 function feedItemDesc( $row ) {
529 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
532 function feedItemAuthor( $row ) {
533 return isset( $row->user_text
) ?
$row->user_text
: '';
536 function feedTitle() {
537 global $wgLanguageCode, $wgSitename;
538 $page = SpecialPage
::getPage( $this->getName() );
539 $desc = $page->getDescription();
540 return "$wgSitename - $desc [$wgLanguageCode]";
543 function feedDesc() {
544 return wfMsgExt( 'tagline', 'parsemag' );
548 $title = SpecialPage
::getTitleFor( $this->getName() );
549 return $title->getFullURL();
554 * Class definition for a wanted query page like
555 * WantedPages, WantedTemplates, etc
557 abstract class WantedQueryPage
extends QueryPage
{
559 function isExpensive() {
563 function isSyndicated() {
568 * Cache page existence for performance
570 function preprocessResults( $db, $res ) {
571 $batch = new LinkBatch
;
572 foreach ( $res as $row ) {
573 $batch->add( $row->namespace, $row->title
);
577 // Back to start for display
578 if ( $db->numRows( $res ) > 0 )
579 // If there are no rows we get an error seeking.
580 $db->dataSeek( $res, 0 );
584 * Format an individual result
586 * @param $skin Skin to use for UI elements
587 * @param $result Result row
590 public function formatResult( $skin, $result ) {
591 $title = Title
::makeTitleSafe( $result->namespace, $result->title
);
592 if( $title instanceof Title
) {
593 if( $this->isCached() ) {
594 $pageLink = $title->exists()
595 ?
'<del>' . $skin->link( $title ) . '</del>'
604 $pageLink = $skin->link(
612 return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
614 $tsafe = htmlspecialchars( $result->title
);
615 return wfMsgHtml( 'wantedpages-badtitle', $tsafe );
620 * Make a "what links here" link for a given title
622 * @param $title Title to make the link for
623 * @param $skin Skin object to use
624 * @param $result Object: result row
627 private function makeWlhLink( $title, $skin, $result ) {
629 $wlh = SpecialPage
::getTitleFor( 'Whatlinkshere' );
630 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
631 $wgLang->formatNum( $result->value
) );
632 return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );