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( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
34 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
35 array( 'UnusedimagesPage', 'Unusedimages' ),
36 array( 'WantedCategoriesPage', 'Wantedcategories' ),
37 array( 'WantedPagesPage', 'Wantedpages' ),
38 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
39 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
41 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
43 global $wgDisableCounters;
44 if ( !$wgDisableCounters )
45 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
49 * This is a class for doing query pages; since they're almost all the same,
50 * we factor out some of the functionality into a superclass, and let
51 * 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 $sql = $this->getSQL();
286 $dbr =& wfGetDB( DB_SLAVE
);
287 $querycache = $dbr->tableName( 'querycache' );
289 $wgOut->setSyndicated( $this->isSyndicated() );
291 if ( $this->isCached() ) {
292 $type = $dbr->strencode( $sname );
294 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
295 FROM $querycache WHERE qc_type='$type'";
297 if( !$this->listoutput
) {
299 # Fetch the timestamp of this update
300 $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $type ), $fname );
301 $tRow = $dbr->fetchObject( $tRes );
304 $updated = $wgLang->timeAndDate( $tRow->qci_timestamp
, true, true );
305 $cacheNotice = wfMsg( 'perfcachedts', $updated );
306 $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp
);
307 $wgOut->addScript( '<script language="JavaScript">var dataCacheTime = \'' . $tRow->qci_timestamp
. '\';</script>' );
309 $cacheNotice = wfMsg( 'perfcached' );
312 $wgOut->addWikiText( $cacheNotice );
317 $sql .= $this->getOrder();
318 $sql = $dbr->limitResult($sql, $limit, $offset);
319 $res = $dbr->query( $sql );
320 $num = $dbr->numRows($res);
322 $this->preprocessResults( $dbr, $res );
324 $sk = $wgUser->getSkin( );
326 if($shownavigation) {
327 $wgOut->addHTML( $this->getPageHeader() );
328 $top = wfShowingResults( $offset, $num);
329 $wgOut->addHTML( "<p>{$top}\n" );
331 # often disable 'next' link when we reach the end
332 $atend = $num < $limit;
334 $sl = wfViewPrevNext( $offset, $limit ,
335 $wgContLang->specialPage( $sname ),
336 wfArrayToCGI( $this->linkParameters() ), $atend );
337 $wgOut->addHTML( "<br />{$sl}</p>\n" );
341 if ( ! $this->listoutput
)
342 $s[] = $this->openList( $offset );
344 # Only read at most $num rows, because $res may contain the whole 1000
345 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
346 $format = $this->formatResult( $sk, $obj );
348 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
349 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
350 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
354 if($this->tryLastResult()) {
355 // flush the very last result
357 $format = $this->formatResult( $sk, $obj );
359 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
360 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
361 $s[] = "<li{$attr}>{$format}</li>\n";
365 $dbr->freeResult( $res );
366 if ( ! $this->listoutput
)
367 $s[] = $this->closeList();
368 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
369 $wgOut->addHTML( $str );
371 if($shownavigation) {
372 $wgOut->addHTML( "<p>{$sl}</p>\n" );
377 function openList( $offset ) {
378 return "<ol start='" . ( $offset +
1 ) . "' class='special'>";
381 function closeList() {
386 * Do any necessary preprocessing of the result object.
387 * You should pass this by reference: &$db , &$res [although probably no longer necessary in PHP5]
389 function preprocessResults( &$db, &$res ) {}
392 * Similar to above, but packaging in a syndicated feed instead of a web page
394 function doFeed( $class = '', $limit = 50 ) {
395 global $wgFeedClasses;
397 if( isset($wgFeedClasses[$class]) ) {
398 $feed = new $wgFeedClasses[$class](
404 $dbr =& wfGetDB( DB_SLAVE
);
405 $sql = $this->getSQL() . $this->getOrder();
406 $sql = $dbr->limitResult( $sql, $limit, 0 );
407 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
408 while( $obj = $dbr->fetchObject( $res ) ) {
409 $item = $this->feedResult( $obj );
410 if( $item ) $feed->outItem( $item );
412 $dbr->freeResult( $res );
422 * Override for custom handling. If the titles/links are ok, just do
425 function feedResult( $row ) {
426 if( !isset( $row->title
) ) {
429 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
431 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
434 $talkpage = $title->getTalkPage();
435 $comments = $talkpage->getFullURL();
439 $title->getPrefixedText(),
440 $this->feedItemDesc( $row ),
441 $title->getFullURL(),
443 $this->feedItemAuthor( $row ),
450 function feedItemDesc( $row ) {
451 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
454 function feedItemAuthor( $row ) {
455 return isset( $row->user_text
) ?
$row->user_text
: '';
458 function feedTitle() {
459 global $wgContLanguageCode, $wgSitename;
460 $page = SpecialPage
::getPage( $this->getName() );
461 $desc = $page->getDescription();
462 return "$wgSitename - $desc [$wgContLanguageCode]";
465 function feedDesc() {
466 return wfMsg( 'tagline' );
470 $title = SpecialPage
::getTitleFor( $this->getName() );
471 return $title->getFullURL();
476 * This is a subclass for very simple queries that are just looking for page
477 * titles that match some criteria. It formats each result item as a link to
482 class PageQueryPage
extends QueryPage
{
484 function formatResult( $skin, $result ) {
486 $nt = Title
::makeTitle( $result->namespace, $result->title
);
487 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );