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 $querycache = $dbr->tableName( 'querycache' );
233 if ( $ignoreErrors ) {
234 $ignoreW = $dbw->ignoreErrors( true );
235 $ignoreR = $dbr->ignoreErrors( true );
238 # Clear out any old cached data
239 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
241 $sql = $this->getSQL() . $this->getOrder();
242 if ( $limit !== false )
243 $sql = $dbr->limitResult( $sql, $limit, 0 );
244 $res = $dbr->query( $sql, $fname );
247 $num = $dbr->numRows( $res );
250 while ( $res && $row = $dbr->fetchObject( $res ) ) {
251 if ( isset( $row->value
) ) {
252 $value = intval( $row->value
); // @bug 14414
257 $vals[] = array('qc_type' => $row->type
,
258 'qc_namespace' => $row->namespace,
259 'qc_title' => $row->title
,
260 'qc_value' => $value);
263 # Save results into the querycache table on the master
264 if ( count( $vals ) ) {
265 if ( !$dbw->insert( 'querycache', $vals, __METHOD__
) ) {
266 // Set result to false to indicate error
270 if ( $ignoreErrors ) {
271 $dbw->ignoreErrors( $ignoreW );
272 $dbr->ignoreErrors( $ignoreR );
275 # Update the querycache_info record for the page
276 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
277 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
284 * This is the actual workhorse. It does everything needed to make a
285 * real, honest-to-gosh query page.
287 * @param $offset database query offset
288 * @param $limit database query limit
289 * @param $shownavigation show navigation like "next 200"?
291 function doQuery( $offset, $limit, $shownavigation=true ) {
292 global $wgUser, $wgOut, $wgLang, $wgContLang;
294 $this->offset
= $offset;
295 $this->limit
= $limit;
297 $sname = $this->getName();
298 $fname = get_class($this) . '::doQuery';
299 $dbr = wfGetDB( DB_SLAVE
);
301 $wgOut->setSyndicated( $this->isSyndicated() );
303 if ( !$this->isCached() ) {
304 $sql = $this->getSQL();
306 # Get the cached result
307 $querycache = $dbr->tableName( 'querycache' );
308 $type = $dbr->strencode( $sname );
310 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
311 FROM $querycache WHERE qc_type='$type'";
313 if( !$this->listoutput
) {
315 # Fetch the timestamp of this update
316 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
317 $tRow = $dbr->fetchObject( $tRes );
320 $updated = $wgLang->timeanddate( $tRow->qci_timestamp
, true, true );
321 $updateddate = $wgLang->date( $tRow->qci_timestamp
, true, true );
322 $updatedtime = $wgLang->time( $tRow->qci_timestamp
, true, true );
323 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
324 $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" );
325 $wgOut->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime );
327 $wgOut->addWikiMsg( 'perfcached' );
330 # If updates on this page have been disabled, let the user know
331 # that the data set won't be refreshed for now
332 global $wgDisableQueryPageUpdate;
333 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
334 $wgOut->addWikiMsg( 'querypage-no-updates' );
341 $sql .= $this->getOrder();
342 $sql = $dbr->limitResult($sql, $limit, $offset);
343 $res = $dbr->query( $sql );
344 $num = $dbr->numRows($res);
346 $this->preprocessResults( $dbr, $res );
348 $wgOut->addHTML( Xml
::openElement( 'div', array('class' => 'mw-spcontent') ) );
350 # Top header and navigation
351 if( $shownavigation ) {
352 $wgOut->addHTML( $this->getPageHeader() );
354 $wgOut->addHTML( '<p>' . wfShowingResults( $offset, $num ) . '</p>' );
355 # Disable the "next" link when we reach the end
356 $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ),
357 wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) );
358 $wgOut->addHTML( '<p>' . $paging . '</p>' );
360 # No results to show, so don't bother with "showing X of Y" etc.
361 # -- just let the user know and give up now
362 $wgOut->addHTML( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
363 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
368 # The actual results; specialist subclasses will want to handle this
369 # with more than a straight list, so we hand them the info, plus
370 # an OutputPage, and let them get on with it
371 $this->outputResults( $wgOut,
373 $dbr, # Should use a ResultWrapper for this
375 $dbr->numRows( $res ),
378 # Repeat the paging links at the bottom
379 if( $shownavigation ) {
380 $wgOut->addHTML( '<p>' . $paging . '</p>' );
383 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
389 * Format and output report results using the given information plus
392 * @param $out OutputPage to print to
393 * @param $skin Skin: user skin to use
394 * @param $dbr Database (read) connection to use
395 * @param $res Integer: result pointer
396 * @param $num Integer: number of available result rows
397 * @param $offset Integer: paging offset
399 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
404 if( !$this->listoutput
)
405 $html[] = $this->openList( $offset );
407 # $res might contain the whole 1,000 rows, so we read up to
408 # $num [should update this to use a Pager]
409 for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++
) {
410 $line = $this->formatResult( $skin, $row );
412 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
413 ?
' class="not-patrolled"'
415 $html[] = $this->listoutput
417 : "<li{$attr}>{$line}</li>\n";
421 # Flush the final result
422 if( $this->tryLastResult() ) {
424 $line = $this->formatResult( $skin, $row );
426 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
427 ?
' class="not-patrolled"'
429 $html[] = $this->listoutput
431 : "<li{$attr}>{$line}</li>\n";
435 if( !$this->listoutput
)
436 $html[] = $this->closeList();
438 $html = $this->listoutput
439 ?
$wgContLang->listToText( $html )
440 : implode( '', $html );
442 $out->addHTML( $html );
446 function openList( $offset ) {
447 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
450 function closeList() {
455 * Do any necessary preprocessing of the result object.
457 function preprocessResults( $db, $res ) {}
460 * Similar to above, but packaging in a syndicated feed instead of a web page
462 function doFeed( $class = '', $limit = 50 ) {
463 global $wgFeed, $wgFeedClasses;
467 $wgOut->addWikiMsg( 'feed-unavailable' );
472 if( $limit > $wgFeedLimit ) {
473 $limit = $wgFeedLimit;
476 if( isset($wgFeedClasses[$class]) ) {
477 $feed = new $wgFeedClasses[$class](
483 $dbr = wfGetDB( DB_SLAVE
);
484 $sql = $this->getSQL() . $this->getOrder();
485 $sql = $dbr->limitResult( $sql, $limit, 0 );
486 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
487 while( $obj = $dbr->fetchObject( $res ) ) {
488 $item = $this->feedResult( $obj );
489 if( $item ) $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 $wgContLanguageCode, $wgSitename;
538 $page = SpecialPage
::getPage( $this->getName() );
539 $desc = $page->getDescription();
540 return "$wgSitename - $desc [$wgContLanguageCode]";
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 while ( $row = $db->fetchObject( $res ) )
573 $batch->add( $row->namespace, $row->title
);
576 // Back to start for display
577 if ( $db->numRows( $res ) > 0 )
578 // If there are no rows we get an error seeking.
579 $db->dataSeek( $res, 0 );
583 * Format an individual result
585 * @param $skin Skin to use for UI elements
586 * @param $result Result row
589 public function formatResult( $skin, $result ) {
590 $title = Title
::makeTitleSafe( $result->namespace, $result->title
);
591 if( $title instanceof Title
) {
592 if( $this->isCached() ) {
593 $pageLink = $title->exists()
594 ?
'<del>' . $skin->link( $title ) . '</del>'
603 $pageLink = $skin->link(
611 return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
613 $tsafe = htmlspecialchars( $result->title
);
614 return wfMsgHtml( 'wantedpages-badtitle', $tsafe );
619 * Make a "what links here" link for a given title
621 * @param $title Title to make the link for
622 * @param $skin Skin object to use
623 * @param $result Object: result row
626 private function makeWlhLink( $title, $skin, $result ) {
628 $wlh = SpecialPage
::getTitleFor( 'Whatlinkshere' );
629 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
630 $wgLang->formatNum( $result->value
) );
631 return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );