3 * Contain a class for special pages
7 * List of query page classes and their associated special pages, for periodic update purposes
9 global $wgQueryPages; // not redundant
10 $wgQueryPages = array(
11 // QueryPage subclass Special page name Limit (false for none, none for the default)
12 //----------------------------------------------------------------------------
13 array( 'AncientPagesPage', 'Ancientpages' ),
14 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
15 array( 'CategoriesPage', 'Categories' ),
16 array( 'DeadendPagesPage', 'Deadendpages' ),
17 array( 'DisambiguationsPage', 'Disambiguations' ),
18 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
19 array( 'ListUsersPage', 'Listusers' ),
20 array( 'ListredirectsPage', 'Listredirects' ),
21 array( 'LonelyPagesPage', 'Lonelypages' ),
22 array( 'LongPagesPage', 'Longpages' ),
23 array( 'MostcategoriesPage', 'Mostcategories' ),
24 array( 'MostimagesPage', 'Mostimages' ),
25 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
26 array( 'MostlinkedPage', 'Mostlinked' ),
27 array( 'MostrevisionsPage', 'Mostrevisions' ),
28 array( 'NewPagesPage', 'Newpages' ),
29 array( 'ShortPagesPage', 'Shortpages' ),
30 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
31 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
32 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
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.
55 * Whether or not we want plain listoutput rather than an ordered list
59 var $listoutput = false;
62 * The offset and limit in use, as passed to the query() function
70 * A mutator for $this->listoutput;
74 function setListoutput( $bool ) {
75 $this->listoutput
= $bool;
79 * Subclasses return their name here. Make sure the name is also
80 * specified in SpecialPage.php and in Language.php as a language message
88 * Return title object representing this page
93 return SpecialPage
::getTitleFor( $this->getName() );
97 * Subclasses return an SQL query here.
99 * Note that the query itself should return the following four columns:
100 * 'type' (your special page's name), 'namespace', 'title', and 'value'
101 * *in that order*. 'value' is used for sorting.
103 * These may be stored in the querycache table for expensive queries,
104 * and that cached data will be returned sometimes, so the presence of
105 * extra fields can't be relied upon. The cached 'value' column will be
106 * an integer; non-numeric values are useful only for sorting the initial
109 * Don't include an ORDER or LIMIT clause, this will be added.
112 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
116 * Override to sort by increasing values
118 function sortDescending() {
122 function getOrder() {
123 return ' ORDER BY value ' .
124 ($this->sortDescending() ?
'DESC' : '');
128 * Is this query expensive (for some definition of expensive)? Then we
129 * don't let it run in miser mode. $wgDisableQueryPages causes all query
130 * pages to be declared expensive. Some query pages are always expensive.
132 function isExpensive( ) {
133 global $wgDisableQueryPages;
134 return $wgDisableQueryPages;
138 * Whether or not the output of the page in question is retrived from
139 * the database cache.
143 function isCached() {
146 return $this->isExpensive() && $wgMiserMode;
150 * Sometime we dont want to build rss / atom feeds.
152 function isSyndicated() {
157 * Formats the results of the query for display. The skin is the current
158 * skin; you can use it for making links. The result is a single row of
159 * result data. You should be able to grab SQL results off of it.
160 * If the function return "false", the line output will be skipped.
162 function formatResult( $skin, $result ) {
167 * The content returned by this function will be output before any result
169 function getPageHeader( ) {
174 * If using extra form wheely-dealies, return a set of parameters here
175 * as an associative array. They will be encoded and added to the paging
176 * links (prev/next/lengths).
179 function linkParameters() {
184 * Some special pages (for example SpecialListusers) might not return the
185 * current object formatted, but return the previous one instead.
186 * Setting this to return true, will call one more time wfFormatResult to
187 * be sure that the very last result is formatted and shown.
189 function tryLastResult( ) {
194 * Clear the cache and save new results
196 function recache( $limit, $ignoreErrors = true ) {
197 $fname = get_class($this) . '::recache';
198 $dbw =& wfGetDB( DB_MASTER
);
199 $dbr =& wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
200 if ( !$dbw ||
!$dbr ) {
204 $querycache = $dbr->tableName( 'querycache' );
206 if ( $ignoreErrors ) {
207 $ignoreW = $dbw->ignoreErrors( true );
208 $ignoreR = $dbr->ignoreErrors( true );
211 # Clear out any old cached data
212 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
214 $sql = $this->getSQL() . $this->getOrder();
215 if ($limit !== false)
216 $sql = $dbr->limitResult($sql, $limit, 0);
217 $res = $dbr->query($sql, $fname);
220 $num = $dbr->numRows( $res );
222 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
224 while ( $res && $row = $dbr->fetchObject( $res ) ) {
230 if ( isset( $row->value
) ) {
231 $value = $row->value
;
237 $dbw->addQuotes( $row->type
) . ',' .
238 $dbw->addQuotes( $row->namespace ) . ',' .
239 $dbw->addQuotes( $row->title
) . ',' .
240 $dbw->addQuotes( $value ) . ')';
243 # Save results into the querycache table on the master
245 if ( !$dbw->query( $insertSql, $fname ) ) {
246 // Set result to false to indicate error
247 $dbr->freeResult( $res );
252 $dbr->freeResult( $res );
254 if ( $ignoreErrors ) {
255 $dbw->ignoreErrors( $ignoreW );
256 $dbr->ignoreErrors( $ignoreR );
259 # Update the querycache_info record for the page
260 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
261 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
268 * This is the actual workhorse. It does everything needed to make a
269 * real, honest-to-gosh query page.
271 * @param $offset database query offset
272 * @param $limit database query limit
273 * @param $shownavigation show navigation like "next 200"?
275 function doQuery( $offset, $limit, $shownavigation=true ) {
276 global $wgUser, $wgOut, $wgLang, $wgContLang;
278 $this->offset
= $offset;
279 $this->limit
= $limit;
281 $sname = $this->getName();
282 $fname = get_class($this) . '::doQuery';
283 $dbr =& wfGetDB( DB_SLAVE
);
285 $wgOut->setSyndicated( $this->isSyndicated() );
287 if ( !$this->isCached() ) {
288 $sql = $this->getSQL();
290 # Get the cached result
291 $querycache = $dbr->tableName( 'querycache' );
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 );
314 # If updates on this page have been disabled, let the user know
315 # that the data set won't be refreshed for now
316 global $wgDisableQueryPageUpdate;
317 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
318 $wgOut->addWikiText( wfMsg( 'querypage-no-updates' ) );
325 $sql .= $this->getOrder();
326 $sql = $dbr->limitResult($sql, $limit, $offset);
327 $res = $dbr->query( $sql );
328 $num = $dbr->numRows($res);
330 $this->preprocessResults( $dbr, $res );
332 $sk = $wgUser->getSkin( );
334 if($shownavigation) {
335 $wgOut->addHTML( $this->getPageHeader() );
336 $top = wfShowingResults( $offset, $num);
337 $wgOut->addHTML( "<p>{$top}\n" );
339 # often disable 'next' link when we reach the end
340 $atend = $num < $limit;
342 $sl = wfViewPrevNext( $offset, $limit ,
343 $wgContLang->specialPage( $sname ),
344 wfArrayToCGI( $this->linkParameters() ), $atend );
345 $wgOut->addHTML( "<br />{$sl}</p>\n" );
349 if ( ! $this->listoutput
)
350 $s[] = $this->openList( $offset );
352 # Only read at most $num rows, because $res may contain the whole 1000
353 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
354 $format = $this->formatResult( $sk, $obj );
356 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
357 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
358 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
362 if($this->tryLastResult()) {
363 // flush the very last result
365 $format = $this->formatResult( $sk, $obj );
367 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
368 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
369 $s[] = "<li{$attr}>{$format}</li>\n";
373 $dbr->freeResult( $res );
374 if ( ! $this->listoutput
)
375 $s[] = $this->closeList();
376 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
377 $wgOut->addHTML( $str );
379 if($shownavigation) {
380 $wgOut->addHTML( "<p>{$sl}</p>\n" );
385 function openList( $offset ) {
386 return "<ol start='" . ( $offset +
1 ) . "' class='special'>";
389 function closeList() {
394 * Do any necessary preprocessing of the result object.
395 * You should pass this by reference: &$db , &$res [although probably no longer necessary in PHP5]
397 function preprocessResults( &$db, &$res ) {}
400 * Similar to above, but packaging in a syndicated feed instead of a web page
402 function doFeed( $class = '', $limit = 50 ) {
403 global $wgFeedClasses;
405 if( isset($wgFeedClasses[$class]) ) {
406 $feed = new $wgFeedClasses[$class](
412 $dbr =& wfGetDB( DB_SLAVE
);
413 $sql = $this->getSQL() . $this->getOrder();
414 $sql = $dbr->limitResult( $sql, $limit, 0 );
415 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
416 while( $obj = $dbr->fetchObject( $res ) ) {
417 $item = $this->feedResult( $obj );
418 if( $item ) $feed->outItem( $item );
420 $dbr->freeResult( $res );
430 * Override for custom handling. If the titles/links are ok, just do
433 function feedResult( $row ) {
434 if( !isset( $row->title
) ) {
437 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
439 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
442 $talkpage = $title->getTalkPage();
443 $comments = $talkpage->getFullURL();
447 $title->getPrefixedText(),
448 $this->feedItemDesc( $row ),
449 $title->getFullURL(),
451 $this->feedItemAuthor( $row ),
458 function feedItemDesc( $row ) {
459 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
462 function feedItemAuthor( $row ) {
463 return isset( $row->user_text
) ?
$row->user_text
: '';
466 function feedTitle() {
467 global $wgContLanguageCode, $wgSitename;
468 $page = SpecialPage
::getPage( $this->getName() );
469 $desc = $page->getDescription();
470 return "$wgSitename - $desc [$wgContLanguageCode]";
473 function feedDesc() {
474 return wfMsg( 'tagline' );
478 $title = SpecialPage
::getTitleFor( $this->getName() );
479 return $title->getFullURL();
484 * This is a subclass for very simple queries that are just looking for page
485 * titles that match some criteria. It formats each result item as a link to
489 class PageQueryPage
extends QueryPage
{
491 function formatResult( $skin, $result ) {
493 $nt = Title
::makeTitle( $result->namespace, $result->title
);
494 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );