449ec23e418239f497ff5280af4c97daa720fa43
3 * Contain a class for special pages
7 * List of query page classes and their associated special pages,
8 * for periodic updates.
10 * DO NOT CHANGE THIS LIST without testing that
11 * maintenance/updateSpecialPages.php still works.
13 global $wgQueryPages; // not redundant
14 $wgQueryPages = array(
15 // QueryPage subclass Special page name Limit (false for none, none for the default)
16 //----------------------------------------------------------------------------
17 array( 'AncientPagesPage', 'Ancientpages' ),
18 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
19 array( 'DeadendPagesPage', 'Deadendpages' ),
20 array( 'DisambiguationsPage', 'Disambiguations' ),
21 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
22 array( 'ListredirectsPage', 'Listredirects' ),
23 array( 'LonelyPagesPage', 'Lonelypages' ),
24 array( 'LongPagesPage', 'Longpages' ),
25 array( 'MostcategoriesPage', 'Mostcategories' ),
26 array( 'MostimagesPage', 'Mostimages' ),
27 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
28 array( 'SpecialMostlinkedtemplates', 'Mostlinkedtemplates' ),
29 array( 'MostlinkedPage', 'Mostlinked' ),
30 array( 'MostrevisionsPage', 'Mostrevisions' ),
31 array( 'FewestrevisionsPage', 'Fewestrevisions' ),
32 array( 'NewPagesPage', 'Newpages' ),
33 array( 'ShortPagesPage', 'Shortpages' ),
34 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
35 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
36 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
37 array( 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ),
38 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
39 array( 'UnusedimagesPage', 'Unusedimages' ),
40 array( 'WantedCategoriesPage', 'Wantedcategories' ),
41 array( 'WantedPagesPage', 'Wantedpages' ),
42 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
43 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
44 array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
46 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
48 global $wgDisableCounters;
49 if ( !$wgDisableCounters )
50 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
54 * This is a class for doing query pages; since they're almost all the same,
55 * we factor out some of the functionality into a superclass, and let
56 * subclasses derive from it.
57 * @addtogroup SpecialPage
61 * Whether or not we want plain listoutput rather than an ordered list
65 var $listoutput = false;
68 * The offset and limit in use, as passed to the query() function
76 * A mutator for $this->listoutput;
80 function setListoutput( $bool ) {
81 $this->listoutput
= $bool;
85 * Subclasses return their name here. Make sure the name is also
86 * specified in SpecialPage.php and in Language.php as a language message
94 * Return title object representing this page
99 return SpecialPage
::getTitleFor( $this->getName() );
103 * Subclasses return an SQL query here.
105 * Note that the query itself should return the following four columns:
106 * 'type' (your special page's name), 'namespace', 'title', and 'value'
107 * *in that order*. 'value' is used for sorting.
109 * These may be stored in the querycache table for expensive queries,
110 * and that cached data will be returned sometimes, so the presence of
111 * extra fields can't be relied upon. The cached 'value' column will be
112 * an integer; non-numeric values are useful only for sorting the initial
115 * Don't include an ORDER or LIMIT clause, this will be added.
118 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
122 * Override to sort by increasing values
124 function sortDescending() {
128 function getOrder() {
129 return ' ORDER BY value ' .
130 ($this->sortDescending() ?
'DESC' : '');
134 * Is this query expensive (for some definition of expensive)? Then we
135 * don't let it run in miser mode. $wgDisableQueryPages causes all query
136 * pages to be declared expensive. Some query pages are always expensive.
138 function isExpensive( ) {
139 global $wgDisableQueryPages;
140 return $wgDisableQueryPages;
144 * Whether or not the output of the page in question is retrived from
145 * the database cache.
149 function isCached() {
152 return $this->isExpensive() && $wgMiserMode;
156 * Sometime we dont want to build rss / atom feeds.
158 function isSyndicated() {
163 * Formats the results of the query for display. The skin is the current
164 * skin; you can use it for making links. The result is a single row of
165 * result data. You should be able to grab SQL results off of it.
166 * If the function return "false", the line output will be skipped.
168 function formatResult( $skin, $result ) {
173 * The content returned by this function will be output before any result
175 function getPageHeader( ) {
180 * If using extra form wheely-dealies, return a set of parameters here
181 * as an associative array. They will be encoded and added to the paging
182 * links (prev/next/lengths).
185 function linkParameters() {
190 * Some special pages (for example SpecialListusers) might not return the
191 * current object formatted, but return the previous one instead.
192 * Setting this to return true, will call one more time wfFormatResult to
193 * be sure that the very last result is formatted and shown.
195 function tryLastResult( ) {
200 * Clear the cache and save new results
202 function recache( $limit, $ignoreErrors = true ) {
203 $fname = get_class($this) . '::recache';
204 $dbw = wfGetDB( DB_MASTER
);
205 $dbr = wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
206 if ( !$dbw ||
!$dbr ) {
210 $querycache = $dbr->tableName( 'querycache' );
212 if ( $ignoreErrors ) {
213 $ignoreW = $dbw->ignoreErrors( true );
214 $ignoreR = $dbr->ignoreErrors( true );
217 # Clear out any old cached data
218 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
220 $sql = $this->getSQL() . $this->getOrder();
221 if ($limit !== false)
222 $sql = $dbr->limitResult($sql, $limit, 0);
223 $res = $dbr->query($sql, $fname);
226 $num = $dbr->numRows( $res );
228 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
230 while ( $res && $row = $dbr->fetchObject( $res ) ) {
236 if ( isset( $row->value
) ) {
237 $value = $row->value
;
243 $dbw->addQuotes( $row->type
) . ',' .
244 $dbw->addQuotes( $row->namespace ) . ',' .
245 $dbw->addQuotes( $row->title
) . ',' .
246 $dbw->addQuotes( $value ) . ')';
249 # Save results into the querycache table on the master
251 if ( !$dbw->query( $insertSql, $fname ) ) {
252 // Set result to false to indicate error
253 $dbr->freeResult( $res );
258 $dbr->freeResult( $res );
260 if ( $ignoreErrors ) {
261 $dbw->ignoreErrors( $ignoreW );
262 $dbr->ignoreErrors( $ignoreR );
265 # Update the querycache_info record for the page
266 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
267 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
274 * This is the actual workhorse. It does everything needed to make a
275 * real, honest-to-gosh query page.
277 * @param $offset database query offset
278 * @param $limit database query limit
279 * @param $shownavigation show navigation like "next 200"?
281 function doQuery( $offset, $limit, $shownavigation=true ) {
282 global $wgUser, $wgOut, $wgLang, $wgContLang;
284 $this->offset
= $offset;
285 $this->limit
= $limit;
287 $sname = $this->getName();
288 $fname = get_class($this) . '::doQuery';
289 $dbr = wfGetDB( DB_SLAVE
);
291 $wgOut->setSyndicated( $this->isSyndicated() );
293 if ( !$this->isCached() ) {
294 $sql = $this->getSQL();
296 # Get the cached result
297 $querycache = $dbr->tableName( 'querycache' );
298 $type = $dbr->strencode( $sname );
300 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
301 FROM $querycache WHERE qc_type='$type'";
303 if( !$this->listoutput
) {
305 # Fetch the timestamp of this update
306 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
307 $tRow = $dbr->fetchObject( $tRes );
310 $updated = $wgLang->timeAndDate( $tRow->qci_timestamp
, true, true );
311 $cacheNotice = wfMsg( 'perfcachedts', $updated );
312 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
313 $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" );
315 $cacheNotice = wfMsg( 'perfcached' );
318 $wgOut->addWikiText( $cacheNotice );
320 # If updates on this page have been disabled, let the user know
321 # that the data set won't be refreshed for now
322 global $wgDisableQueryPageUpdate;
323 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
324 $wgOut->addWikiText( wfMsg( 'querypage-no-updates' ) );
331 $sql .= $this->getOrder();
332 $sql = $dbr->limitResult($sql, $limit, $offset);
333 $res = $dbr->query( $sql );
334 $num = $dbr->numRows($res);
336 $this->preprocessResults( $dbr, $res );
338 $wgOut->addHtml( XML
::openElement( 'div', array('class' => 'mw-spcontent') ) );
340 # Top header and navigation
341 if( $shownavigation ) {
342 $wgOut->addHtml( $this->getPageHeader() );
344 $wgOut->addHtml( '<p>' . wfShowingResults( $offset, $num ) . '</p>' );
345 # Disable the "next" link when we reach the end
346 $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ),
347 wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) );
348 $wgOut->addHtml( '<p>' . $paging . '</p>' );
350 # No results to show, so don't bother with "showing X of Y" etc.
351 # -- just let the user know and give up now
352 $wgOut->addHtml( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
353 $wgOut->addHtml( XML
::closeElement( 'div' ) );
358 # The actual results; specialist subclasses will want to handle this
359 # with more than a straight list, so we hand them the info, plus
360 # an OutputPage, and let them get on with it
361 $this->outputResults( $wgOut,
363 $dbr, # Should use a ResultWrapper for this
365 $dbr->numRows( $res ),
368 # Repeat the paging links at the bottom
369 if( $shownavigation ) {
370 $wgOut->addHtml( '<p>' . $paging . '</p>' );
373 $wgOut->addHtml( XML
::closeElement( 'div' ) );
379 * Format and output report results using the given information plus
382 * @param OutputPage $out OutputPage to print to
383 * @param Skin $skin User skin to use
384 * @param Database $dbr Database (read) connection to use
385 * @param int $res Result pointer
386 * @param int $num Number of available result rows
387 * @param int $offset Paging offset
389 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
394 if( !$this->listoutput
)
395 $html[] = $this->openList( $offset );
397 # $res might contain the whole 1,000 rows, so we read up to
398 # $num [should update this to use a Pager]
399 for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++
) {
400 $line = $this->formatResult( $skin, $row );
402 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
403 ?
' class="not-patrolled"'
405 $html[] = $this->listoutput
407 : "<li{$attr}>{$line}</li>\n";
411 # Flush the final result
412 if( $this->tryLastResult() ) {
414 $line = $this->formatResult( $skin, $row );
416 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
417 ?
' class="not-patrolled"'
419 $html[] = $this->listoutput
421 : "<li{$attr}>{$line}</li>\n";
425 if( !$this->listoutput
)
426 $html[] = $this->closeList();
428 $html = $this->listoutput
429 ?
$wgContLang->listToText( $html )
430 : implode( '', $html );
432 $out->addHtml( $html );
436 function openList( $offset ) {
437 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
440 function closeList() {
445 * Do any necessary preprocessing of the result object.
446 * You should pass this by reference: &$db , &$res [although probably no longer necessary in PHP5]
448 function preprocessResults( &$db, &$res ) {}
451 * Similar to above, but packaging in a syndicated feed instead of a web page
453 function doFeed( $class = '', $limit = 50 ) {
454 global $wgFeedClasses;
456 if( isset($wgFeedClasses[$class]) ) {
457 $feed = new $wgFeedClasses[$class](
463 $dbr = wfGetDB( DB_SLAVE
);
464 $sql = $this->getSQL() . $this->getOrder();
465 $sql = $dbr->limitResult( $sql, $limit, 0 );
466 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
467 while( $obj = $dbr->fetchObject( $res ) ) {
468 $item = $this->feedResult( $obj );
469 if( $item ) $feed->outItem( $item );
471 $dbr->freeResult( $res );
481 * Override for custom handling. If the titles/links are ok, just do
484 function feedResult( $row ) {
485 if( !isset( $row->title
) ) {
488 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
490 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
493 $talkpage = $title->getTalkPage();
494 $comments = $talkpage->getFullURL();
498 $title->getPrefixedText(),
499 $this->feedItemDesc( $row ),
500 $title->getFullURL(),
502 $this->feedItemAuthor( $row ),
509 function feedItemDesc( $row ) {
510 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
513 function feedItemAuthor( $row ) {
514 return isset( $row->user_text
) ?
$row->user_text
: '';
517 function feedTitle() {
518 global $wgContLanguageCode, $wgSitename;
519 $page = SpecialPage
::getPage( $this->getName() );
520 $desc = $page->getDescription();
521 return "$wgSitename - $desc [$wgContLanguageCode]";
524 function feedDesc() {
525 return wfMsg( 'tagline' );
529 $title = SpecialPage
::getTitleFor( $this->getName() );
530 return $title->getFullURL();