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( 'FewestrevisionsPage', 'Fewestrevisions' ),
31 array( 'NewPagesPage', 'Newpages' ),
32 array( 'ShortPagesPage', 'Shortpages' ),
33 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
34 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
35 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
36 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
37 array( 'UnusedimagesPage', 'Unusedimages' ),
38 array( 'WantedCategoriesPage', 'Wantedcategories' ),
39 array( 'WantedPagesPage', 'Wantedpages' ),
40 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
41 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
42 array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
44 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
46 global $wgDisableCounters;
47 if ( !$wgDisableCounters )
48 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
52 * This is a class for doing query pages; since they're almost all the same,
53 * we factor out some of the functionality into a superclass, and let
54 * subclasses derive from it.
55 * @addtogroup SpecialPage
59 * Whether or not we want plain listoutput rather than an ordered list
63 var $listoutput = false;
66 * The offset and limit in use, as passed to the query() function
74 * A mutator for $this->listoutput;
78 function setListoutput( $bool ) {
79 $this->listoutput
= $bool;
83 * Subclasses return their name here. Make sure the name is also
84 * specified in SpecialPage.php and in Language.php as a language message
92 * Return title object representing this page
97 return SpecialPage
::getTitleFor( $this->getName() );
101 * Subclasses return an SQL query here.
103 * Note that the query itself should return the following four columns:
104 * 'type' (your special page's name), 'namespace', 'title', and 'value'
105 * *in that order*. 'value' is used for sorting.
107 * These may be stored in the querycache table for expensive queries,
108 * and that cached data will be returned sometimes, so the presence of
109 * extra fields can't be relied upon. The cached 'value' column will be
110 * an integer; non-numeric values are useful only for sorting the initial
113 * Don't include an ORDER or LIMIT clause, this will be added.
116 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
120 * Override to sort by increasing values
122 function sortDescending() {
126 function getOrder() {
127 return ' ORDER BY value ' .
128 ($this->sortDescending() ?
'DESC' : '');
132 * Is this query expensive (for some definition of expensive)? Then we
133 * don't let it run in miser mode. $wgDisableQueryPages causes all query
134 * pages to be declared expensive. Some query pages are always expensive.
136 function isExpensive( ) {
137 global $wgDisableQueryPages;
138 return $wgDisableQueryPages;
142 * Whether or not the output of the page in question is retrived from
143 * the database cache.
147 function isCached() {
150 return $this->isExpensive() && $wgMiserMode;
154 * Sometime we dont want to build rss / atom feeds.
156 function isSyndicated() {
161 * Formats the results of the query for display. The skin is the current
162 * skin; you can use it for making links. The result is a single row of
163 * result data. You should be able to grab SQL results off of it.
164 * If the function return "false", the line output will be skipped.
166 function formatResult( $skin, $result ) {
171 * The content returned by this function will be output before any result
173 function getPageHeader( ) {
178 * If using extra form wheely-dealies, return a set of parameters here
179 * as an associative array. They will be encoded and added to the paging
180 * links (prev/next/lengths).
183 function linkParameters() {
188 * Some special pages (for example SpecialListusers) might not return the
189 * current object formatted, but return the previous one instead.
190 * Setting this to return true, will call one more time wfFormatResult to
191 * be sure that the very last result is formatted and shown.
193 function tryLastResult( ) {
198 * Clear the cache and save new results
200 function recache( $limit, $ignoreErrors = true ) {
201 $fname = get_class($this) . '::recache';
202 $dbw = wfGetDB( DB_MASTER
);
203 $dbr = wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
204 if ( !$dbw ||
!$dbr ) {
208 $querycache = $dbr->tableName( 'querycache' );
210 if ( $ignoreErrors ) {
211 $ignoreW = $dbw->ignoreErrors( true );
212 $ignoreR = $dbr->ignoreErrors( true );
215 # Clear out any old cached data
216 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
218 $sql = $this->getSQL() . $this->getOrder();
219 if ($limit !== false)
220 $sql = $dbr->limitResult($sql, $limit, 0);
221 $res = $dbr->query($sql, $fname);
224 $num = $dbr->numRows( $res );
226 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
228 while ( $res && $row = $dbr->fetchObject( $res ) ) {
234 if ( isset( $row->value
) ) {
235 $value = $row->value
;
241 $dbw->addQuotes( $row->type
) . ',' .
242 $dbw->addQuotes( $row->namespace ) . ',' .
243 $dbw->addQuotes( $row->title
) . ',' .
244 $dbw->addQuotes( $value ) . ')';
247 # Save results into the querycache table on the master
249 if ( !$dbw->query( $insertSql, $fname ) ) {
250 // Set result to false to indicate error
251 $dbr->freeResult( $res );
256 $dbr->freeResult( $res );
258 if ( $ignoreErrors ) {
259 $dbw->ignoreErrors( $ignoreW );
260 $dbr->ignoreErrors( $ignoreR );
263 # Update the querycache_info record for the page
264 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
265 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
272 * This is the actual workhorse. It does everything needed to make a
273 * real, honest-to-gosh query page.
275 * @param $offset database query offset
276 * @param $limit database query limit
277 * @param $shownavigation show navigation like "next 200"?
279 function doQuery( $offset, $limit, $shownavigation=true ) {
280 global $wgUser, $wgOut, $wgLang, $wgContLang;
282 $this->offset
= $offset;
283 $this->limit
= $limit;
285 $sname = $this->getName();
286 $fname = get_class($this) . '::doQuery';
287 $dbr = wfGetDB( DB_SLAVE
);
289 $wgOut->setSyndicated( $this->isSyndicated() );
291 if ( !$this->isCached() ) {
292 $sql = $this->getSQL();
294 # Get the cached result
295 $querycache = $dbr->tableName( 'querycache' );
296 $type = $dbr->strencode( $sname );
298 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
299 FROM $querycache WHERE qc_type='$type'";
301 if( !$this->listoutput
) {
303 # Fetch the timestamp of this update
304 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
305 $tRow = $dbr->fetchObject( $tRes );
308 $updated = $wgLang->timeAndDate( $tRow->qci_timestamp
, true, true );
309 $cacheNotice = wfMsg( 'perfcachedts', $updated );
310 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
311 $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" );
313 $cacheNotice = wfMsg( 'perfcached' );
316 $wgOut->addWikiText( $cacheNotice );
318 # If updates on this page have been disabled, let the user know
319 # that the data set won't be refreshed for now
320 global $wgDisableQueryPageUpdate;
321 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
322 $wgOut->addWikiText( wfMsg( 'querypage-no-updates' ) );
329 $sql .= $this->getOrder();
330 $sql = $dbr->limitResult($sql, $limit, $offset);
331 $res = $dbr->query( $sql );
332 $num = $dbr->numRows($res);
334 $this->preprocessResults( $dbr, $res );
336 # Top header and navigation
337 if( $shownavigation ) {
338 $wgOut->addHtml( $this->getPageHeader() );
340 $wgOut->addHtml( '<p>' . wfShowingResults( $offset, $num ) . '</p>' );
341 # Disable the "next" link when we reach the end
342 $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ),
343 wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) );
344 $wgOut->addHtml( '<p>' . $paging . '</p>' );
346 # No results to show, so don't bother with "showing X of Y" etc.
347 # -- just let the user know and give up now
348 $wgOut->addHtml( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
353 # The actual results; specialist subclasses will want to handle this
354 # with more than a straight list, so we hand them the info, plus
355 # an OutputPage, and let them get on with it
356 $this->outputResults( $wgOut,
358 $dbr, # Should use a ResultWrapper for this
360 $dbr->numRows( $res ),
363 # Repeat the paging links at the bottom
364 if( $shownavigation ) {
365 $wgOut->addHtml( '<p>' . $paging . '</p>' );
372 * Format and output report results using the given information plus
375 * @param OutputPage $out OutputPage to print to
376 * @param Skin $skin User skin to use
377 * @param Database $dbr Database (read) connection to use
378 * @param int $res Result pointer
379 * @param int $num Number of available result rows
380 * @param int $offset Paging offset
382 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
387 if( !$this->listoutput
)
388 $html[] = $this->openList( $offset );
390 # $res might contain the whole 1,000 rows, so we read up to
391 # $num [should update this to use a Pager]
392 for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++
) {
393 $line = $this->formatResult( $skin, $row );
395 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
396 ?
' class="not-patrolled"'
398 $html[] = $this->listoutput
400 : "<li{$attr}>{$line}</li>\n";
404 # Flush the final result
405 if( $this->tryLastResult() ) {
407 $line = $this->formatResult( $skin, $row );
409 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
410 ?
' class="not-patrolled"'
412 $html[] = $this->listoutput
414 : "<li{$attr}>{$line}</li>\n";
418 if( !$this->listoutput
)
419 $html[] = $this->closeList();
421 $html = $this->listoutput
422 ?
$wgContLang->listToText( $html )
423 : implode( '', $html );
425 $out->addHtml( $html );
429 function openList( $offset ) {
430 return "<ol start='" . ( $offset +
1 ) . "' class='special'>";
433 function closeList() {
438 * Do any necessary preprocessing of the result object.
439 * You should pass this by reference: &$db , &$res [although probably no longer necessary in PHP5]
441 function preprocessResults( &$db, &$res ) {}
444 * Similar to above, but packaging in a syndicated feed instead of a web page
446 function doFeed( $class = '', $limit = 50 ) {
447 global $wgFeedClasses;
449 if( isset($wgFeedClasses[$class]) ) {
450 $feed = new $wgFeedClasses[$class](
456 $dbr = wfGetDB( DB_SLAVE
);
457 $sql = $this->getSQL() . $this->getOrder();
458 $sql = $dbr->limitResult( $sql, $limit, 0 );
459 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
460 while( $obj = $dbr->fetchObject( $res ) ) {
461 $item = $this->feedResult( $obj );
462 if( $item ) $feed->outItem( $item );
464 $dbr->freeResult( $res );
474 * Override for custom handling. If the titles/links are ok, just do
477 function feedResult( $row ) {
478 if( !isset( $row->title
) ) {
481 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
483 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
486 $talkpage = $title->getTalkPage();
487 $comments = $talkpage->getFullURL();
491 $title->getPrefixedText(),
492 $this->feedItemDesc( $row ),
493 $title->getFullURL(),
495 $this->feedItemAuthor( $row ),
502 function feedItemDesc( $row ) {
503 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
506 function feedItemAuthor( $row ) {
507 return isset( $row->user_text
) ?
$row->user_text
: '';
510 function feedTitle() {
511 global $wgContLanguageCode, $wgSitename;
512 $page = SpecialPage
::getPage( $this->getName() );
513 $desc = $page->getDescription();
514 return "$wgSitename - $desc [$wgContLanguageCode]";
517 function feedDesc() {
518 return wfMsg( 'tagline' );
522 $title = SpecialPage
::getTitleFor( $this->getName() );
523 return $title->getFullURL();