781a482720e8e7ee67033059b1c95acbf8856baa
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( 'MostlinkedPage', 'Mostlinked' ),
29 array( 'MostrevisionsPage', 'Mostrevisions' ),
30 array( 'NewPagesPage', 'Newpages' ),
31 array( 'ShortPagesPage', 'Shortpages' ),
32 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
33 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
34 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
35 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
36 array( 'UnusedimagesPage', 'Unusedimages' ),
37 array( 'WantedCategoriesPage', 'Wantedcategories' ),
38 array( 'WantedPagesPage', 'Wantedpages' ),
39 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
40 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
42 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
44 global $wgDisableCounters;
45 if ( !$wgDisableCounters )
46 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
50 * This is a class for doing query pages; since they're almost all the same,
51 * we factor out some of the functionality into a superclass, and let
52 * subclasses derive from it.
57 * Whether or not we want plain listoutput rather than an ordered list
61 var $listoutput = false;
64 * The offset and limit in use, as passed to the query() function
72 * A mutator for $this->listoutput;
76 function setListoutput( $bool ) {
77 $this->listoutput
= $bool;
81 * Subclasses return their name here. Make sure the name is also
82 * specified in SpecialPage.php and in Language.php as a language message
90 * Return title object representing this page
95 return SpecialPage
::getTitleFor( $this->getName() );
99 * Subclasses return an SQL query here.
101 * Note that the query itself should return the following four columns:
102 * 'type' (your special page's name), 'namespace', 'title', and 'value'
103 * *in that order*. 'value' is used for sorting.
105 * These may be stored in the querycache table for expensive queries,
106 * and that cached data will be returned sometimes, so the presence of
107 * extra fields can't be relied upon. The cached 'value' column will be
108 * an integer; non-numeric values are useful only for sorting the initial
111 * Don't include an ORDER or LIMIT clause, this will be added.
114 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
118 * Override to sort by increasing values
120 function sortDescending() {
124 function getOrder() {
125 return ' ORDER BY value ' .
126 ($this->sortDescending() ?
'DESC' : '');
130 * Is this query expensive (for some definition of expensive)? Then we
131 * don't let it run in miser mode. $wgDisableQueryPages causes all query
132 * pages to be declared expensive. Some query pages are always expensive.
134 function isExpensive( ) {
135 global $wgDisableQueryPages;
136 return $wgDisableQueryPages;
140 * Whether or not the output of the page in question is retrived from
141 * the database cache.
145 function isCached() {
148 return $this->isExpensive() && $wgMiserMode;
152 * Sometime we dont want to build rss / atom feeds.
154 function isSyndicated() {
159 * Formats the results of the query for display. The skin is the current
160 * skin; you can use it for making links. The result is a single row of
161 * result data. You should be able to grab SQL results off of it.
162 * If the function return "false", the line output will be skipped.
164 function formatResult( $skin, $result ) {
169 * The content returned by this function will be output before any result
171 function getPageHeader( ) {
176 * If using extra form wheely-dealies, return a set of parameters here
177 * as an associative array. They will be encoded and added to the paging
178 * links (prev/next/lengths).
181 function linkParameters() {
186 * Some special pages (for example SpecialListusers) might not return the
187 * current object formatted, but return the previous one instead.
188 * Setting this to return true, will call one more time wfFormatResult to
189 * be sure that the very last result is formatted and shown.
191 function tryLastResult( ) {
196 * Clear the cache and save new results
198 function recache( $limit, $ignoreErrors = true ) {
199 $fname = get_class($this) . '::recache';
200 $dbw = wfGetDB( DB_MASTER
);
201 $dbr = wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
202 if ( !$dbw ||
!$dbr ) {
206 $querycache = $dbr->tableName( 'querycache' );
208 if ( $ignoreErrors ) {
209 $ignoreW = $dbw->ignoreErrors( true );
210 $ignoreR = $dbr->ignoreErrors( true );
213 # Clear out any old cached data
214 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
216 $sql = $this->getSQL() . $this->getOrder();
217 if ($limit !== false)
218 $sql = $dbr->limitResult($sql, $limit, 0);
219 $res = $dbr->query($sql, $fname);
222 $num = $dbr->numRows( $res );
224 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
226 while ( $res && $row = $dbr->fetchObject( $res ) ) {
232 if ( isset( $row->value
) ) {
233 $value = $row->value
;
239 $dbw->addQuotes( $row->type
) . ',' .
240 $dbw->addQuotes( $row->namespace ) . ',' .
241 $dbw->addQuotes( $row->title
) . ',' .
242 $dbw->addQuotes( $value ) . ')';
245 # Save results into the querycache table on the master
247 if ( !$dbw->query( $insertSql, $fname ) ) {
248 // Set result to false to indicate error
249 $dbr->freeResult( $res );
254 $dbr->freeResult( $res );
256 if ( $ignoreErrors ) {
257 $dbw->ignoreErrors( $ignoreW );
258 $dbr->ignoreErrors( $ignoreR );
261 # Update the querycache_info record for the page
262 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
263 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
270 * This is the actual workhorse. It does everything needed to make a
271 * real, honest-to-gosh query page.
273 * @param $offset database query offset
274 * @param $limit database query limit
275 * @param $shownavigation show navigation like "next 200"?
277 function doQuery( $offset, $limit, $shownavigation=true ) {
278 global $wgUser, $wgOut, $wgLang, $wgContLang;
280 $this->offset
= $offset;
281 $this->limit
= $limit;
283 $sname = $this->getName();
284 $fname = get_class($this) . '::doQuery';
285 $dbr = wfGetDB( DB_SLAVE
);
287 $wgOut->setSyndicated( $this->isSyndicated() );
289 if ( !$this->isCached() ) {
290 $sql = $this->getSQL();
292 # Get the cached result
293 $querycache = $dbr->tableName( 'querycache' );
294 $type = $dbr->strencode( $sname );
296 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
297 FROM $querycache WHERE qc_type='$type'";
299 if( !$this->listoutput
) {
301 # Fetch the timestamp of this update
302 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
303 $tRow = $dbr->fetchObject( $tRes );
306 $updated = $wgLang->timeAndDate( $tRow->qci_timestamp
, true, true );
307 $cacheNotice = wfMsg( 'perfcachedts', $updated );
308 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
309 $wgOut->addScript( '<script language="JavaScript">var dataCacheTime = \'' . $tRow->qci_timestamp
. '\';</script>' );
311 $cacheNotice = wfMsg( 'perfcached' );
314 $wgOut->addWikiText( $cacheNotice );
316 # If updates on this page have been disabled, let the user know
317 # that the data set won't be refreshed for now
318 global $wgDisableQueryPageUpdate;
319 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
320 $wgOut->addWikiText( wfMsg( 'querypage-no-updates' ) );
327 $sql .= $this->getOrder();
328 $sql = $dbr->limitResult($sql, $limit, $offset);
329 $res = $dbr->query( $sql );
330 $num = $dbr->numRows($res);
332 $this->preprocessResults( $dbr, $res );
333 $sk = $wgUser->getSkin();
335 # Top header and navigation
336 if( $shownavigation ) {
337 $wgOut->addHtml( $this->getPageHeader() );
339 $wgOut->addHtml( '<p>' . wfShowingResults( $offset, $num ) . '</p>' );
340 # Disable the "next" link when we reach the end
341 $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ),
342 wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) );
343 $wgOut->addHtml( '<p>' . $paging . '</p>' );
345 # No results to show, so don't bother with "showing X of Y" etc.
346 # -- just let the user know and give up now
347 $wgOut->addHtml( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
352 # The actual results; specialist subclasses will want to handle this
353 # with more than a straight list, so we hand them the info, plus
354 # an OutputPage, and let them get on with it
355 $this->outputResults( $wgOut,
357 $dbr, # Should use a ResultWrapper for this
359 $dbr->numRows( $res ),
362 # Repeat the paging links at the bottom
363 if( $shownavigation ) {
364 $wgOut->addHtml( '<p>' . $paging . '</p>' );
371 * Format and output report results using the given information plus
374 * @param OutputPage $out OutputPage to print to
375 * @param Skin $skin User skin to use
376 * @param Database $dbr Database (read) connection to use
377 * @param int $res Result pointer
378 * @param int $num Number of available result rows
379 * @param int $offset Paging offset
381 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
386 if( !$this->listoutput
)
387 $html[] = $this->openList( $offset );
389 # $res might contain the whole 1,000 rows, so we read up to
390 # $num [should update this to use a Pager]
391 for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++
) {
392 $line = $this->formatResult( $skin, $row );
394 $attr = ( isset( $obj->usepatrol
) && $obj->usepatrol
&& $obj->patrolled
== 0 )
395 ?
' class="not-patrolled"'
397 $html[] = $this->listoutput
399 : "<li{$attr}>{$line}</li>\n";
403 # Flush the final result
404 if( $this->tryLastResult() ) {
406 $line = $this->formatResult( $skin, $row );
408 $attr = ( isset( $obj->usepatrol
) && $obj->usepatrol
&& $obj->patrolled
== 0 )
409 ?
' class="not-patrolled"'
411 $html[] = $this->listoutput
413 : "<li{$attr}>{$line}</li>\n";
417 if( !$this->listoutput
)
418 $html[] = $this->closeList();
420 $html = $this->listoutput
421 ?
$wgContLang->listToText( $html )
422 : implode( '', $html );
424 $out->addHtml( $html );
428 function openList( $offset ) {
429 return "<ol start='" . ( $offset +
1 ) . "' class='special'>";
432 function closeList() {
437 * Do any necessary preprocessing of the result object.
438 * You should pass this by reference: &$db , &$res [although probably no longer necessary in PHP5]
440 function preprocessResults( &$db, &$res ) {}
443 * Similar to above, but packaging in a syndicated feed instead of a web page
445 function doFeed( $class = '', $limit = 50 ) {
446 global $wgFeedClasses;
448 if( isset($wgFeedClasses[$class]) ) {
449 $feed = new $wgFeedClasses[$class](
455 $dbr = wfGetDB( DB_SLAVE
);
456 $sql = $this->getSQL() . $this->getOrder();
457 $sql = $dbr->limitResult( $sql, $limit, 0 );
458 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
459 while( $obj = $dbr->fetchObject( $res ) ) {
460 $item = $this->feedResult( $obj );
461 if( $item ) $feed->outItem( $item );
463 $dbr->freeResult( $res );
473 * Override for custom handling. If the titles/links are ok, just do
476 function feedResult( $row ) {
477 if( !isset( $row->title
) ) {
480 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
482 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
485 $talkpage = $title->getTalkPage();
486 $comments = $talkpage->getFullURL();
490 $title->getPrefixedText(),
491 $this->feedItemDesc( $row ),
492 $title->getFullURL(),
494 $this->feedItemAuthor( $row ),
501 function feedItemDesc( $row ) {
502 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
505 function feedItemAuthor( $row ) {
506 return isset( $row->user_text
) ?
$row->user_text
: '';
509 function feedTitle() {
510 global $wgContLanguageCode, $wgSitename;
511 $page = SpecialPage
::getPage( $this->getName() );
512 $desc = $page->getDescription();
513 return "$wgSitename - $desc [$wgContLanguageCode]";
516 function feedDesc() {
517 return wfMsg( 'tagline' );
521 $title = SpecialPage
::getTitleFor( $this->getName() );
522 return $title->getFullURL();
527 * This is a subclass for very simple queries that are just looking for page
528 * titles that match some criteria. It formats each result item as a link to
532 class PageQueryPage
extends QueryPage
{
534 function formatResult( $skin, $result ) {
536 $nt = Title
::makeTitle( $result->namespace, $result->title
);
537 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );