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( 'DeadendPagesPage', 'Deadendpages' ),
18 array( 'DisambiguationsPage', 'Disambiguations' ),
19 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
20 array( 'ListUsersPage', 'Listusers' ),
21 array( 'ListredirectsPage', 'Listredirects' ),
22 array( 'LonelyPagesPage', 'Lonelypages' ),
23 array( 'LongPagesPage', 'Longpages' ),
24 array( 'MostcategoriesPage', 'Mostcategories' ),
25 array( 'MostimagesPage', 'Mostimages' ),
26 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
27 array( 'MostlinkedPage', 'Mostlinked' ),
28 array( 'MostrevisionsPage', 'Mostrevisions' ),
29 array( 'NewPagesPage', 'Newpages' ),
30 array( 'ShortPagesPage', 'Shortpages' ),
31 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
32 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
33 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
34 array( 'UnusedimagesPage', 'Unusedimages' ),
35 array( 'WantedCategoriesPage', 'Wantedcategories' ),
36 array( 'WantedPagesPage', 'Wantedpages' ),
37 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
38 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
40 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
42 global $wgDisableCounters;
43 if ( !$wgDisableCounters )
44 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
48 * This is a class for doing query pages; since they're almost all the same,
49 * we factor out some of the functionality into a superclass, and let
50 * subclasses derive from it.
56 * Whether or not we want plain listoutput rather than an ordered list
60 var $listoutput = false;
63 * The offset and limit in use, as passed to the query() function
71 * A mutator for $this->listoutput;
75 function setListoutput( $bool ) {
76 $this->listoutput
= $bool;
80 * Subclasses return their name here. Make sure the name is also
81 * specified in SpecialPage.php and in Language.php as a language message
89 * Return title object representing this page
94 return Title
::makeTitle( NS_SPECIAL
, $this->getName() );
98 * Subclasses return an SQL query here.
100 * Note that the query itself should return the following four columns:
101 * 'type' (your special page's name), 'namespace', 'title', and 'value'
102 * *in that order*. 'value' is used for sorting.
104 * These may be stored in the querycache table for expensive queries,
105 * and that cached data will be returned sometimes, so the presence of
106 * extra fields can't be relied upon. The cached 'value' column will be
107 * an integer; non-numeric values are useful only for sorting the initial
110 * Don't include an ORDER or LIMIT clause, this will be added.
113 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
117 * Override to sort by increasing values
119 function sortDescending() {
123 function getOrder() {
124 return ' ORDER BY value ' .
125 ($this->sortDescending() ?
'DESC' : '');
129 * Is this query expensive (for some definition of expensive)? Then we
130 * don't let it run in miser mode. $wgDisableQueryPages causes all query
131 * pages to be declared expensive. Some query pages are always expensive.
133 function isExpensive( ) {
134 global $wgDisableQueryPages;
135 return $wgDisableQueryPages;
139 * Whether or not the output of the page in question is retrived from
140 * the database cache.
144 function isCached() {
147 return $this->isExpensive() && $wgMiserMode;
151 * Sometime we dont want to build rss / atom feeds.
153 function isSyndicated() {
158 * Formats the results of the query for display. The skin is the current
159 * skin; you can use it for making links. The result is a single row of
160 * result data. You should be able to grab SQL results off of it.
161 * If the function return "false", the line output will be skipped.
163 function formatResult( $skin, $result ) {
168 * The content returned by this function will be output before any result
170 function getPageHeader( ) {
175 * If using extra form wheely-dealies, return a set of parameters here
176 * as an associative array. They will be encoded and added to the paging
177 * links (prev/next/lengths).
180 function linkParameters() {
185 * Some special pages (for example SpecialListusers) might not return the
186 * current object formatted, but return the previous one instead.
187 * Setting this to return true, will call one more time wfFormatResult to
188 * be sure that the very last result is formatted and shown.
190 function tryLastResult( ) {
195 * Clear the cache and save new results
197 function recache( $limit, $ignoreErrors = true ) {
198 $fname = get_class($this) . '::recache';
199 $dbw =& wfGetDB( DB_MASTER
);
200 $dbr =& wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
201 if ( !$dbw ||
!$dbr ) {
205 $querycache = $dbr->tableName( 'querycache' );
207 if ( $ignoreErrors ) {
208 $ignoreW = $dbw->ignoreErrors( true );
209 $ignoreR = $dbr->ignoreErrors( true );
212 # Clear out any old cached data
213 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
215 $sql = $this->getSQL() . $this->getOrder();
216 if ($limit !== false)
217 $sql = $dbr->limitResult($sql, $limit, 0);
218 $res = $dbr->query($sql, $fname);
221 $num = $dbr->numRows( $res );
223 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
225 while ( $res && $row = $dbr->fetchObject( $res ) ) {
231 if ( isset( $row->value
) ) {
232 $value = $row->value
;
238 $dbw->addQuotes( $row->type
) . ',' .
239 $dbw->addQuotes( $row->namespace ) . ',' .
240 $dbw->addQuotes( $row->title
) . ',' .
241 $dbw->addQuotes( $value ) . ')';
244 # Save results into the querycache table on the master
246 if ( !$dbw->query( $insertSql, $fname ) ) {
247 // Set result to false to indicate error
248 $dbr->freeResult( $res );
253 $dbr->freeResult( $res );
255 if ( $ignoreErrors ) {
256 $dbw->ignoreErrors( $ignoreW );
257 $dbr->ignoreErrors( $ignoreR );
260 # Update the querycache_info record for the page
261 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
262 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
269 * This is the actual workhorse. It does everything needed to make a
270 * real, honest-to-gosh query page.
272 * @param $offset database query offset
273 * @param $limit database query limit
274 * @param $shownavigation show navigation like "next 200"?
276 function doQuery( $offset, $limit, $shownavigation=true ) {
277 global $wgUser, $wgOut, $wgLang, $wgContLang;
279 $this->offset
= $offset;
280 $this->limit
= $limit;
282 $sname = $this->getName();
283 $fname = get_class($this) . '::doQuery';
284 $sql = $this->getSQL();
285 $dbr =& wfGetDB( DB_SLAVE
);
286 $querycache = $dbr->tableName( 'querycache' );
288 $wgOut->setSyndicated( $this->isSyndicated() );
290 if ( $this->isCached() ) {
291 $type = $dbr->strencode( $sname );
293 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
294 FROM $querycache WHERE qc_type='$type'";
296 if( !$this->listoutput
) {
298 # Fetch the timestamp of this update
299 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
300 $tRow = $dbr->fetchObject( $tRes );
303 $updated = $wgLang->timeAndDate( $tRow->qci_timestamp
, true, true );
304 $cacheNotice = wfMsg( 'perfcachedts', $updated );
305 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
306 $wgOut->addScript( '<script language="JavaScript">var dataCacheTime = \'' . $tRow->qci_timestamp
. '\';</script>' );
308 $cacheNotice = wfMsg( 'perfcached' );
311 $wgOut->addWikiText( $cacheNotice );
316 $sql .= $this->getOrder();
317 $sql = $dbr->limitResult($sql, $limit, $offset);
318 $res = $dbr->query( $sql );
319 $num = $dbr->numRows($res);
321 $this->preprocessResults( $dbr, $res );
323 $sk = $wgUser->getSkin( );
325 if($shownavigation) {
326 $wgOut->addHTML( $this->getPageHeader() );
327 $top = wfShowingResults( $offset, $num);
328 $wgOut->addHTML( "<p>{$top}\n" );
330 # often disable 'next' link when we reach the end
331 $atend = $num < $limit;
333 $sl = wfViewPrevNext( $offset, $limit ,
334 $wgContLang->specialPage( $sname ),
335 wfArrayToCGI( $this->linkParameters() ), $atend );
336 $wgOut->addHTML( "<br />{$sl}</p>\n" );
340 if ( ! $this->listoutput
)
341 $s[] = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
343 # Only read at most $num rows, because $res may contain the whole 1000
344 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
345 $format = $this->formatResult( $sk, $obj );
347 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
348 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
349 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
353 if($this->tryLastResult()) {
354 // flush the very last result
356 $format = $this->formatResult( $sk, $obj );
358 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
359 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
360 $s[] = "<li{$attr}>{$format}</li>\n";
364 $dbr->freeResult( $res );
365 if ( ! $this->listoutput
)
367 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
368 $wgOut->addHTML( $str );
370 if($shownavigation) {
371 $wgOut->addHTML( "<p>{$sl}</p>\n" );
377 * Do any necessary preprocessing of the result object.
378 * You should pass this by reference: &$db , &$res
380 function preprocessResults( $db, $res ) {}
383 * Similar to above, but packaging in a syndicated feed instead of a web page
385 function doFeed( $class = '', $limit = 50 ) {
386 global $wgFeedClasses;
388 if( isset($wgFeedClasses[$class]) ) {
389 $feed = new $wgFeedClasses[$class](
395 $dbr =& wfGetDB( DB_SLAVE
);
396 $sql = $this->getSQL() . $this->getOrder();
397 $sql = $dbr->limitResult( $sql, $limit, 0 );
398 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
399 while( $obj = $dbr->fetchObject( $res ) ) {
400 $item = $this->feedResult( $obj );
401 if( $item ) $feed->outItem( $item );
403 $dbr->freeResult( $res );
413 * Override for custom handling. If the titles/links are ok, just do
416 function feedResult( $row ) {
417 if( !isset( $row->title
) ) {
420 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
422 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
425 $talkpage = $title->getTalkPage();
426 $comments = $talkpage->getFullURL();
430 $title->getPrefixedText(),
431 $this->feedItemDesc( $row ),
432 $title->getFullURL(),
434 $this->feedItemAuthor( $row ),
441 function feedItemDesc( $row ) {
442 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
445 function feedItemAuthor( $row ) {
446 return isset( $row->user_text
) ?
$row->user_text
: '';
449 function feedTitle() {
450 global $wgLanguageCode, $wgSitename;
451 $page = SpecialPage
::getPage( $this->getName() );
452 $desc = $page->getDescription();
453 return "$wgSitename - $desc [$wgLanguageCode]";
456 function feedDesc() {
457 return wfMsg( 'tagline' );
461 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
462 return $title->getFullURL();
467 * This is a subclass for very simple queries that are just looking for page
468 * titles that match some criteria. It formats each result item as a link to
473 class PageQueryPage
extends QueryPage
{
475 function formatResult( $skin, $result ) {
477 $nt = Title
::makeTitle( $result->namespace, $result->title
);
478 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );