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( 'UnwatchedPagesPage', 'Unwatchedpages' ),
46 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
47 array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
49 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
51 global $wgDisableCounters;
52 if ( !$wgDisableCounters )
53 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
57 * This is a class for doing query pages; since they're almost all the same,
58 * we factor out some of the functionality into a superclass, and let
59 * subclasses derive from it.
60 * @ingroup SpecialPage
64 * Whether or not we want plain listoutput rather than an ordered list
68 var $listoutput = false;
71 * The offset and limit in use, as passed to the query() function
79 * A mutator for $this->listoutput;
83 function setListoutput( $bool ) {
84 $this->listoutput
= $bool;
88 * Subclasses return their name here. Make sure the name is also
89 * specified in SpecialPage.php and in Language.php as a language message
97 * Return title object representing this page
101 function getTitle() {
102 return SpecialPage
::getTitleFor( $this->getName() );
106 * Subclasses return an SQL query here.
108 * Note that the query itself should return the following four columns:
109 * 'type' (your special page's name), 'namespace', 'title', and 'value'
110 * *in that order*. 'value' is used for sorting.
112 * These may be stored in the querycache table for expensive queries,
113 * and that cached data will be returned sometimes, so the presence of
114 * extra fields can't be relied upon. The cached 'value' column will be
115 * an integer; non-numeric values are useful only for sorting the initial
118 * Don't include an ORDER or LIMIT clause, this will be added.
121 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
125 * Override to sort by increasing values
127 function sortDescending() {
131 function getOrder() {
132 return ' ORDER BY value ' .
133 ($this->sortDescending() ?
'DESC' : '');
137 * Is this query expensive (for some definition of expensive)? Then we
138 * don't let it run in miser mode. $wgDisableQueryPages causes all query
139 * pages to be declared expensive. Some query pages are always expensive.
141 function isExpensive( ) {
142 global $wgDisableQueryPages;
143 return $wgDisableQueryPages;
147 * Whether or not the output of the page in question is retrived from
148 * the database cache.
152 function isCached() {
155 return $this->isExpensive() && $wgMiserMode;
159 * Sometime we dont want to build rss / atom feeds.
161 function isSyndicated() {
166 * Formats the results of the query for display. The skin is the current
167 * skin; you can use it for making links. The result is a single row of
168 * result data. You should be able to grab SQL results off of it.
169 * If the function return "false", the line output will be skipped.
171 function formatResult( $skin, $result ) {
176 * The content returned by this function will be output before any result
178 function getPageHeader( ) {
183 * If using extra form wheely-dealies, return a set of parameters here
184 * as an associative array. They will be encoded and added to the paging
185 * links (prev/next/lengths).
188 function linkParameters() {
193 * Some special pages (for example SpecialListusers) might not return the
194 * current object formatted, but return the previous one instead.
195 * Setting this to return true, will call one more time wfFormatResult to
196 * be sure that the very last result is formatted and shown.
198 function tryLastResult( ) {
203 * Clear the cache and save new results
205 function recache( $limit, $ignoreErrors = true ) {
206 $fname = get_class($this) . '::recache';
207 $dbw = wfGetDB( DB_MASTER
);
208 $dbr = wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
209 if ( !$dbw ||
!$dbr ) {
213 $querycache = $dbr->tableName( 'querycache' );
215 if ( $ignoreErrors ) {
216 $ignoreW = $dbw->ignoreErrors( true );
217 $ignoreR = $dbr->ignoreErrors( true );
220 # Clear out any old cached data
221 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
223 $sql = $this->getSQL() . $this->getOrder();
224 if ($limit !== false)
225 $sql = $dbr->limitResult($sql, $limit, 0);
226 $res = $dbr->query($sql, $fname);
229 $num = $dbr->numRows( $res );
231 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
233 while ( $res && $row = $dbr->fetchObject( $res ) ) {
239 if ( isset( $row->value
) ) {
240 $value = $row->value
;
246 $dbw->addQuotes( $row->type
) . ',' .
247 $dbw->addQuotes( $row->namespace ) . ',' .
248 $dbw->addQuotes( $row->title
) . ',' .
249 $dbw->addQuotes( $value ) . ')';
252 # Save results into the querycache table on the master
254 if ( !$dbw->query( $insertSql, $fname ) ) {
255 // Set result to false to indicate error
256 $dbr->freeResult( $res );
261 $dbr->freeResult( $res );
263 if ( $ignoreErrors ) {
264 $dbw->ignoreErrors( $ignoreW );
265 $dbr->ignoreErrors( $ignoreR );
268 # Update the querycache_info record for the page
269 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
270 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
277 * This is the actual workhorse. It does everything needed to make a
278 * real, honest-to-gosh query page.
280 * @param $offset database query offset
281 * @param $limit database query limit
282 * @param $shownavigation show navigation like "next 200"?
284 function doQuery( $offset, $limit, $shownavigation=true ) {
285 global $wgUser, $wgOut, $wgLang, $wgContLang;
287 $this->offset
= $offset;
288 $this->limit
= $limit;
290 $sname = $this->getName();
291 $fname = get_class($this) . '::doQuery';
292 $dbr = wfGetDB( DB_SLAVE
);
294 $wgOut->setSyndicated( $this->isSyndicated() );
296 if ( !$this->isCached() ) {
297 $sql = $this->getSQL();
299 # Get the cached result
300 $querycache = $dbr->tableName( 'querycache' );
301 $type = $dbr->strencode( $sname );
303 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
304 FROM $querycache WHERE qc_type='$type'";
306 if( !$this->listoutput
) {
308 # Fetch the timestamp of this update
309 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
310 $tRow = $dbr->fetchObject( $tRes );
313 $updated = $wgLang->timeAndDate( $tRow->qci_timestamp
, true, true );
314 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
315 $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" );
316 $wgOut->addWikiMsg( 'perfcachedts', $updated );
318 $wgOut->addWikiMsg( 'perfcached' );
321 # If updates on this page have been disabled, let the user know
322 # that the data set won't be refreshed for now
323 global $wgDisableQueryPageUpdate;
324 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
325 $wgOut->addWikiMsg( 'querypage-no-updates' );
332 $sql .= $this->getOrder();
333 $sql = $dbr->limitResult($sql, $limit, $offset);
334 $res = $dbr->query( $sql );
335 $num = $dbr->numRows($res);
337 $this->preprocessResults( $dbr, $res );
339 $wgOut->addHtml( XML
::openElement( 'div', array('class' => 'mw-spcontent') ) );
341 # Top header and navigation
342 if( $shownavigation ) {
343 $wgOut->addHtml( $this->getPageHeader() );
345 $wgOut->addHtml( '<p>' . wfShowingResults( $offset, $num ) . '</p>' );
346 # Disable the "next" link when we reach the end
347 $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ),
348 wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) );
349 $wgOut->addHtml( '<p>' . $paging . '</p>' );
351 # No results to show, so don't bother with "showing X of Y" etc.
352 # -- just let the user know and give up now
353 $wgOut->addHtml( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
354 $wgOut->addHtml( XML
::closeElement( 'div' ) );
359 # The actual results; specialist subclasses will want to handle this
360 # with more than a straight list, so we hand them the info, plus
361 # an OutputPage, and let them get on with it
362 $this->outputResults( $wgOut,
364 $dbr, # Should use a ResultWrapper for this
366 $dbr->numRows( $res ),
369 # Repeat the paging links at the bottom
370 if( $shownavigation ) {
371 $wgOut->addHtml( '<p>' . $paging . '</p>' );
374 $wgOut->addHtml( XML
::closeElement( 'div' ) );
380 * Format and output report results using the given information plus
383 * @param OutputPage $out OutputPage to print to
384 * @param Skin $skin User skin to use
385 * @param Database $dbr Database (read) connection to use
386 * @param int $res Result pointer
387 * @param int $num Number of available result rows
388 * @param int $offset Paging offset
390 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
395 if( !$this->listoutput
)
396 $html[] = $this->openList( $offset );
398 # $res might contain the whole 1,000 rows, so we read up to
399 # $num [should update this to use a Pager]
400 for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++
) {
401 $line = $this->formatResult( $skin, $row );
403 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
404 ?
' class="not-patrolled"'
406 $html[] = $this->listoutput
408 : "<li{$attr}>{$line}</li>\n";
412 # Flush the final result
413 if( $this->tryLastResult() ) {
415 $line = $this->formatResult( $skin, $row );
417 $attr = ( isset( $row->usepatrol
) && $row->usepatrol
&& $row->patrolled
== 0 )
418 ?
' class="not-patrolled"'
420 $html[] = $this->listoutput
422 : "<li{$attr}>{$line}</li>\n";
426 if( !$this->listoutput
)
427 $html[] = $this->closeList();
429 $html = $this->listoutput
430 ?
$wgContLang->listToText( $html )
431 : implode( '', $html );
433 $out->addHtml( $html );
437 function openList( $offset ) {
438 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
441 function closeList() {
446 * Do any necessary preprocessing of the result object.
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 $wgFeed, $wgFeedClasses;
458 $wgOut->addWikiMsg( 'feed-unavailable' );
463 if( $limit > $wgFeedLimit ) {
464 $limit = $wgFeedLimit;
467 if( isset($wgFeedClasses[$class]) ) {
468 $feed = new $wgFeedClasses[$class](
474 $dbr = wfGetDB( DB_SLAVE
);
475 $sql = $this->getSQL() . $this->getOrder();
476 $sql = $dbr->limitResult( $sql, $limit, 0 );
477 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
478 while( $obj = $dbr->fetchObject( $res ) ) {
479 $item = $this->feedResult( $obj );
480 if( $item ) $feed->outItem( $item );
482 $dbr->freeResult( $res );
492 * Override for custom handling. If the titles/links are ok, just do
495 function feedResult( $row ) {
496 if( !isset( $row->title
) ) {
499 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
501 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
504 $talkpage = $title->getTalkPage();
505 $comments = $talkpage->getFullURL();
509 $title->getPrefixedText(),
510 $this->feedItemDesc( $row ),
511 $title->getFullURL(),
513 $this->feedItemAuthor( $row ),
520 function feedItemDesc( $row ) {
521 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
524 function feedItemAuthor( $row ) {
525 return isset( $row->user_text
) ?
$row->user_text
: '';
528 function feedTitle() {
529 global $wgContLanguageCode, $wgSitename;
530 $page = SpecialPage
::getPage( $this->getName() );
531 $desc = $page->getDescription();
532 return "$wgSitename - $desc [$wgContLanguageCode]";
535 function feedDesc() {
536 return wfMsg( 'tagline' );
540 $title = SpecialPage
::getTitleFor( $this->getName() );
541 return $title->getFullURL();