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( 'ListredirectsPage', 'Listredirects' ),
25 array( 'LonelyPagesPage', 'Lonelypages' ),
26 array( 'LongPagesPage', 'Longpages' ),
27 array( 'MostcategoriesPage', 'Mostcategories' ),
28 array( 'MostimagesPage', 'Mostimages' ),
29 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
30 array( 'SpecialMostlinkedtemplates', 'Mostlinkedtemplates' ),
31 array( 'MostlinkedPage', 'Mostlinked' ),
32 array( 'MostrevisionsPage', 'Mostrevisions' ),
33 array( 'FewestrevisionsPage', 'Fewestrevisions' ),
34 array( 'ShortPagesPage', 'Shortpages' ),
35 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
36 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
37 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
38 array( 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ),
39 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
40 array( 'UnusedimagesPage', 'Unusedimages' ),
41 array( 'WantedCategoriesPage', 'Wantedcategories' ),
42 array( 'WantedPagesPage', 'Wantedpages' ),
43 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
44 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
45 array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
47 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
49 global $wgDisableCounters;
50 if ( !$wgDisableCounters )
51 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
55 * This is a class for doing query pages; since they're almost all the same,
56 * we factor out some of the functionality into a superclass, and let
57 * subclasses derive from it.
58 * @ingroup SpecialPage
62 * Whether or not we want plain listoutput rather than an ordered list
66 var $listoutput = false;
69 * The offset and limit in use, as passed to the query() function
77 * A mutator for $this->listoutput;
81 function setListoutput( $bool ) {
82 $this->listoutput
= $bool;
86 * Subclasses return their name here. Make sure the name is also
87 * specified in SpecialPage.php and in Language.php as a language message
95 * Return title object representing this page
100 return SpecialPage
::getTitleFor( $this->getName() );
104 * Subclasses return an SQL query here.
106 * Note that the query itself should return the following four columns:
107 * 'type' (your special page's name), 'namespace', 'title', and 'value'
108 * *in that order*. 'value' is used for sorting.
110 * These may be stored in the querycache table for expensive queries,
111 * and that cached data will be returned sometimes, so the presence of
112 * extra fields can't be relied upon. The cached 'value' column will be
113 * an integer; non-numeric values are useful only for sorting the initial
116 * Don't include an ORDER or LIMIT clause, this will be added.
119 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
123 * Override to sort by increasing values
125 function sortDescending() {
129 function getOrder() {
130 return ' ORDER BY value ' .
131 ($this->sortDescending() ?
'DESC' : '');
135 * Is this query expensive (for some definition of expensive)? Then we
136 * don't let it run in miser mode. $wgDisableQueryPages causes all query
137 * pages to be declared expensive. Some query pages are always expensive.
139 function isExpensive( ) {
140 global $wgDisableQueryPages;
141 return $wgDisableQueryPages;
145 * Whether or not the output of the page in question is retrived from
146 * the database cache.
150 function isCached() {
153 return $this->isExpensive() && $wgMiserMode;
157 * Sometime we dont want to build rss / atom feeds.
159 function isSyndicated() {
164 * Formats the results of the query for display. The skin is the current
165 * skin; you can use it for making links. The result is a single row of
166 * result data. You should be able to grab SQL results off of it.
167 * If the function return "false", the line output will be skipped.
169 function formatResult( $skin, $result ) {
174 * The content returned by this function will be output before any result
176 function getPageHeader( ) {
181 * If using extra form wheely-dealies, return a set of parameters here
182 * as an associative array. They will be encoded and added to the paging
183 * links (prev/next/lengths).
186 function linkParameters() {
191 * Some special pages (for example SpecialListusers) might not return the
192 * current object formatted, but return the previous one instead.
193 * Setting this to return true, will call one more time wfFormatResult to
194 * be sure that the very last result is formatted and shown.
196 function tryLastResult( ) {
201 * Clear the cache and save new results
203 function recache( $limit, $ignoreErrors = true ) {
204 $fname = get_class($this) . '::recache';
205 $dbw = wfGetDB( DB_MASTER
);
206 $dbr = wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
207 if ( !$dbw ||
!$dbr ) {
211 $querycache = $dbr->tableName( 'querycache' );
213 if ( $ignoreErrors ) {
214 $ignoreW = $dbw->ignoreErrors( true );
215 $ignoreR = $dbr->ignoreErrors( true );
218 # Clear out any old cached data
219 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
221 $sql = $this->getSQL() . $this->getOrder();
222 if ($limit !== false)
223 $sql = $dbr->limitResult($sql, $limit, 0);
224 $res = $dbr->query($sql, $fname);
227 $num = $dbr->numRows( $res );
229 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
231 while ( $res && $row = $dbr->fetchObject( $res ) ) {
237 if ( isset( $row->value
) ) {
238 $value = $row->value
;
244 $dbw->addQuotes( $row->type
) . ',' .
245 $dbw->addQuotes( $row->namespace ) . ',' .
246 $dbw->addQuotes( $row->title
) . ',' .
247 $dbw->addQuotes( $value ) . ')';
250 # Save results into the querycache table on the master
252 if ( !$dbw->query( $insertSql, $fname ) ) {
253 // Set result to false to indicate error
254 $dbr->freeResult( $res );
259 $dbr->freeResult( $res );
261 if ( $ignoreErrors ) {
262 $dbw->ignoreErrors( $ignoreW );
263 $dbr->ignoreErrors( $ignoreR );
266 # Update the querycache_info record for the page
267 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
268 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
275 * This is the actual workhorse. It does everything needed to make a
276 * real, honest-to-gosh query page.
278 * @param $offset database query offset
279 * @param $limit database query limit
280 * @param $shownavigation show navigation like "next 200"?
282 function doQuery( $offset, $limit, $shownavigation=true ) {
283 global $wgUser, $wgOut, $wgLang, $wgContLang;
285 $this->offset
= $offset;
286 $this->limit
= $limit;
288 $sname = $this->getName();
289 $fname = get_class($this) . '::doQuery';
290 $dbr = wfGetDB( DB_SLAVE
);
292 $wgOut->setSyndicated( $this->isSyndicated() );
294 if ( !$this->isCached() ) {
295 $sql = $this->getSQL();
297 # Get the cached result
298 $querycache = $dbr->tableName( 'querycache' );
299 $type = $dbr->strencode( $sname );
301 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
302 FROM $querycache WHERE qc_type='$type'";
304 if( !$this->listoutput
) {
306 # Fetch the timestamp of this update
307 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
308 $tRow = $dbr->fetchObject( $tRes );
311 $updated = $wgLang->timeAndDate( $tRow->qci_timestamp
, true, true );
312 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
313 $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" );
314 $wgOut->addWikiMsg( 'perfcachedts', $updated );
316 $wgOut->addWikiMsg( 'perfcached' );
319 # If updates on this page have been disabled, let the user know
320 # that the data set won't be refreshed for now
321 global $wgDisableQueryPageUpdate;
322 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
323 $wgOut->addWikiMsg( 'querypage-no-updates' );
330 $sql .= $this->getOrder();
331 $sql = $dbr->limitResult($sql, $limit, $offset);
332 $res = $dbr->query( $sql );
333 $num = $dbr->numRows($res);
335 $this->preprocessResults( $dbr, $res );
337 $wgOut->addHtml( XML
::openElement( 'div', array('class' => 'mw-spcontent') ) );
339 # Top header and navigation
340 if( $shownavigation ) {
341 $wgOut->addHtml( $this->getPageHeader() );
343 $wgOut->addHtml( '<p>' . wfShowingResults( $offset, $num ) . '</p>' );
344 # Disable the "next" link when we reach the end
345 $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ),
346 wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) );
347 $wgOut->addHtml( '<p>' . $paging . '</p>' );
349 # No results to show, so don't bother with "showing X of Y" etc.
350 # -- just let the user know and give up now
351 $wgOut->addHtml( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
352 $wgOut->addHtml( XML
::closeElement( 'div' ) );
357 # The actual results; specialist subclasses will want to handle this
358 # with more than a straight list, so we hand them the info, plus
359 # an OutputPage, and let them get on with it
360 $this->outputResults( $wgOut,
362 $dbr, # Should use a ResultWrapper for this
364 $dbr->numRows( $res ),
367 # Repeat the paging links at the bottom
368 if( $shownavigation ) {
369 $wgOut->addHtml( '<p>' . $paging . '</p>' );
372 $wgOut->addHtml( XML
::closeElement( 'div' ) );
378 * Format and output report results using the given information plus
381 * @param OutputPage $out OutputPage to print to
382 * @param Skin $skin User skin to use
383 * @param Database $dbr Database (read) connection to use
384 * @param int $res Result pointer
385 * @param int $num Number of available result rows
386 * @param int $offset Paging offset
388 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
393 if( !$this->listoutput
)
394 $html[] = $this->openList( $offset );
396 # $res might contain the whole 1,000 rows, so we read up to
397 # $num [should update this to use a Pager]
398 for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++
) {
399 $line = $this->formatResult( $skin, $row );
401 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
402 ?
' class="not-patrolled"'
404 $html[] = $this->listoutput
406 : "<li{$attr}>{$line}</li>\n";
410 # Flush the final result
411 if( $this->tryLastResult() ) {
413 $line = $this->formatResult( $skin, $row );
415 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
416 ?
' class="not-patrolled"'
418 $html[] = $this->listoutput
420 : "<li{$attr}>{$line}</li>\n";
424 if( !$this->listoutput
)
425 $html[] = $this->closeList();
427 $html = $this->listoutput
428 ?
$wgContLang->listToText( $html )
429 : implode( '', $html );
431 $out->addHtml( $html );
435 function openList( $offset ) {
436 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
439 function closeList() {
444 * Do any necessary preprocessing of the result object.
446 function preprocessResults( $db, $res ) {}
449 * Similar to above, but packaging in a syndicated feed instead of a web page
451 function doFeed( $class = '', $limit = 50 ) {
452 global $wgFeed, $wgFeedClasses;
456 $wgOut->addWikiMsg( 'feed-unavailable' );
461 if( $limit > $wgFeedLimit ) {
462 $limit = $wgFeedLimit;
465 if( isset($wgFeedClasses[$class]) ) {
466 $feed = new $wgFeedClasses[$class](
472 $dbr = wfGetDB( DB_SLAVE
);
473 $sql = $this->getSQL() . $this->getOrder();
474 $sql = $dbr->limitResult( $sql, $limit, 0 );
475 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
476 while( $obj = $dbr->fetchObject( $res ) ) {
477 $item = $this->feedResult( $obj );
478 if( $item ) $feed->outItem( $item );
480 $dbr->freeResult( $res );
490 * Override for custom handling. If the titles/links are ok, just do
493 function feedResult( $row ) {
494 if( !isset( $row->title
) ) {
497 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
499 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
502 $talkpage = $title->getTalkPage();
503 $comments = $talkpage->getFullURL();
507 $title->getPrefixedText(),
508 $this->feedItemDesc( $row ),
509 $title->getFullURL(),
511 $this->feedItemAuthor( $row ),
518 function feedItemDesc( $row ) {
519 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
522 function feedItemAuthor( $row ) {
523 return isset( $row->user_text
) ?
$row->user_text
: '';
526 function feedTitle() {
527 global $wgContLanguageCode, $wgSitename;
528 $page = SpecialPage
::getPage( $this->getName() );
529 $desc = $page->getDescription();
530 return "$wgSitename - $desc [$wgContLanguageCode]";
533 function feedDesc() {
534 return wfMsg( 'tagline' );
538 $title = SpecialPage
::getTitleFor( $this->getName() );
539 return $title->getFullURL();