3 * Contain a class for special pages
8 * List of query page classes and their associated special pages, for periodic update purposes
10 global $wgQueryPages; // not redundant
11 $wgQueryPages = array(
12 // QueryPage subclass Special page name Limit (false for none, none for the default)
13 //----------------------------------------------------------------------------
14 array( 'AncientPagesPage', 'Ancientpages' ),
15 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
16 array( 'CategoriesPage', 'Categories' ),
17 array( 'ContributionsPage', 'Contributions' ),
18 array( 'DeadendPagesPage', 'Deadendpages' ),
19 array( 'DisambiguationsPage', 'Disambiguations' ),
20 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
21 array( 'ListUsersPage', 'Listusers' ),
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( 'NewbieContributionsPage', 'NewbieContributions' ),
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' ),
43 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
45 global $wgDisableCounters;
46 if ( !$wgDisableCounters )
47 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
51 * This is a class for doing query pages; since they're almost all the same,
52 * we factor out some of the functionality into a superclass, and let
53 * subclasses derive from it.
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 $sql = $this->getSQL();
288 $dbr =& wfGetDB( DB_SLAVE
);
289 $querycache = $dbr->tableName( 'querycache' );
291 $wgOut->setSyndicated( $this->isSyndicated() );
293 if ( $this->isCached() ) {
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 );
319 $sql .= $this->getOrder();
320 $sql = $dbr->limitResult($sql, $limit, $offset);
321 $res = $dbr->query( $sql );
322 $num = $dbr->numRows($res);
324 $this->preprocessResults( $dbr, $res );
326 $sk = $wgUser->getSkin( );
328 if($shownavigation) {
329 $wgOut->addHTML( $this->getPageHeader() );
330 $top = wfShowingResults( $offset, $num);
331 $wgOut->addHTML( "<p>{$top}\n" );
333 # often disable 'next' link when we reach the end
334 $atend = $num < $limit;
336 $sl = wfViewPrevNext( $offset, $limit ,
337 $wgContLang->specialPage( $sname ),
338 wfArrayToCGI( $this->linkParameters() ), $atend );
339 $wgOut->addHTML( "<br />{$sl}</p>\n" );
343 if ( ! $this->listoutput
)
344 $s[] = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
346 # Only read at most $num rows, because $res may contain the whole 1000
347 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
348 $format = $this->formatResult( $sk, $obj );
350 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
351 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
352 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
356 if($this->tryLastResult()) {
357 // flush the very last result
359 $format = $this->formatResult( $sk, $obj );
361 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
362 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
363 $s[] = "<li{$attr}>{$format}</li>\n";
367 $dbr->freeResult( $res );
368 if ( ! $this->listoutput
)
370 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
371 $wgOut->addHTML( $str );
373 if($shownavigation) {
374 $wgOut->addHTML( "<p>{$sl}</p>\n" );
380 * Do any necessary preprocessing of the result object.
381 * You should pass this by reference: &$db , &$res [although probably no longer necessary in PHP5]
383 function preprocessResults( &$db, &$res ) {}
386 * Similar to above, but packaging in a syndicated feed instead of a web page
388 function doFeed( $class = '', $limit = 50 ) {
389 global $wgFeedClasses;
391 if( isset($wgFeedClasses[$class]) ) {
392 $feed = new $wgFeedClasses[$class](
398 $dbr =& wfGetDB( DB_SLAVE
);
399 $sql = $this->getSQL() . $this->getOrder();
400 $sql = $dbr->limitResult( $sql, $limit, 0 );
401 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
402 while( $obj = $dbr->fetchObject( $res ) ) {
403 $item = $this->feedResult( $obj );
404 if( $item ) $feed->outItem( $item );
406 $dbr->freeResult( $res );
416 * Override for custom handling. If the titles/links are ok, just do
419 function feedResult( $row ) {
420 if( !isset( $row->title
) ) {
423 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
425 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
428 $talkpage = $title->getTalkPage();
429 $comments = $talkpage->getFullURL();
433 $title->getPrefixedText(),
434 $this->feedItemDesc( $row ),
435 $title->getFullURL(),
437 $this->feedItemAuthor( $row ),
444 function feedItemDesc( $row ) {
445 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
448 function feedItemAuthor( $row ) {
449 return isset( $row->user_text
) ?
$row->user_text
: '';
452 function feedTitle() {
453 global $wgContLanguageCode, $wgSitename;
454 $page = SpecialPage
::getPage( $this->getName() );
455 $desc = $page->getDescription();
456 return "$wgSitename - $desc [$wgContLanguageCode]";
459 function feedDesc() {
460 return wfMsg( 'tagline' );
464 $title = SpecialPage
::getTitleFor( $this->getName() );
465 return $title->getFullURL();
470 * This is a subclass for very simple queries that are just looking for page
471 * titles that match some criteria. It formats each result item as a link to
476 class PageQueryPage
extends QueryPage
{
478 function formatResult( $skin, $result ) {
480 $nt = Title
::makeTitle( $result->namespace, $result->title
);
481 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );