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( 'DeadendPagesPage', 'Deadendpages' ),
16 array( 'DisambiguationsPage', 'Disambiguations' ),
17 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
18 array( 'ListUsersPage', 'Listusers' ),
19 array( 'ListredirectsPage', 'Listredirects' ),
20 array( 'LonelyPagesPage', 'Lonelypages' ),
21 array( 'LongPagesPage', 'Longpages' ),
22 array( 'MostcategoriesPage', 'Mostcategories' ),
23 array( 'MostimagesPage', 'Mostimages' ),
24 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
25 array( 'MostlinkedPage', 'Mostlinked' ),
26 array( 'MostrevisionsPage', 'Mostrevisions' ),
27 array( 'NewPagesPage', 'Newpages' ),
28 array( 'ShortPagesPage', 'Shortpages' ),
29 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
30 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
31 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
32 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
33 array( 'UnusedimagesPage', 'Unusedimages' ),
34 array( 'WantedCategoriesPage', 'Wantedcategories' ),
35 array( 'WantedPagesPage', 'Wantedpages' ),
36 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
37 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
39 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
41 global $wgDisableCounters;
42 if ( !$wgDisableCounters )
43 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
47 * This is a class for doing query pages; since they're almost all the same,
48 * we factor out some of the functionality into a superclass, and let
49 * subclasses derive from it.
54 * Whether or not we want plain listoutput rather than an ordered list
58 var $listoutput = false;
61 * The offset and limit in use, as passed to the query() function
69 * A mutator for $this->listoutput;
73 function setListoutput( $bool ) {
74 $this->listoutput
= $bool;
78 * Subclasses return their name here. Make sure the name is also
79 * specified in SpecialPage.php and in Language.php as a language message
87 * Return title object representing this page
92 return SpecialPage
::getTitleFor( $this->getName() );
96 * Subclasses return an SQL query here.
98 * Note that the query itself should return the following four columns:
99 * 'type' (your special page's name), 'namespace', 'title', and 'value'
100 * *in that order*. 'value' is used for sorting.
102 * These may be stored in the querycache table for expensive queries,
103 * and that cached data will be returned sometimes, so the presence of
104 * extra fields can't be relied upon. The cached 'value' column will be
105 * an integer; non-numeric values are useful only for sorting the initial
108 * Don't include an ORDER or LIMIT clause, this will be added.
111 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
115 * Override to sort by increasing values
117 function sortDescending() {
121 function getOrder() {
122 return ' ORDER BY value ' .
123 ($this->sortDescending() ?
'DESC' : '');
127 * Is this query expensive (for some definition of expensive)? Then we
128 * don't let it run in miser mode. $wgDisableQueryPages causes all query
129 * pages to be declared expensive. Some query pages are always expensive.
131 function isExpensive( ) {
132 global $wgDisableQueryPages;
133 return $wgDisableQueryPages;
137 * Whether or not the output of the page in question is retrived from
138 * the database cache.
142 function isCached() {
145 return $this->isExpensive() && $wgMiserMode;
149 * Sometime we dont want to build rss / atom feeds.
151 function isSyndicated() {
156 * Formats the results of the query for display. The skin is the current
157 * skin; you can use it for making links. The result is a single row of
158 * result data. You should be able to grab SQL results off of it.
159 * If the function return "false", the line output will be skipped.
161 function formatResult( $skin, $result ) {
166 * The content returned by this function will be output before any result
168 function getPageHeader( ) {
173 * If using extra form wheely-dealies, return a set of parameters here
174 * as an associative array. They will be encoded and added to the paging
175 * links (prev/next/lengths).
178 function linkParameters() {
183 * Some special pages (for example SpecialListusers) might not return the
184 * current object formatted, but return the previous one instead.
185 * Setting this to return true, will call one more time wfFormatResult to
186 * be sure that the very last result is formatted and shown.
188 function tryLastResult( ) {
193 * Clear the cache and save new results
195 function recache( $limit, $ignoreErrors = true ) {
196 $fname = get_class($this) . '::recache';
197 $dbw = wfGetDB( DB_MASTER
);
198 $dbr = wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
199 if ( !$dbw ||
!$dbr ) {
203 $querycache = $dbr->tableName( 'querycache' );
205 if ( $ignoreErrors ) {
206 $ignoreW = $dbw->ignoreErrors( true );
207 $ignoreR = $dbr->ignoreErrors( true );
210 # Clear out any old cached data
211 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
213 $sql = $this->getSQL() . $this->getOrder();
214 if ($limit !== false)
215 $sql = $dbr->limitResult($sql, $limit, 0);
216 $res = $dbr->query($sql, $fname);
219 $num = $dbr->numRows( $res );
221 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
223 while ( $res && $row = $dbr->fetchObject( $res ) ) {
229 if ( isset( $row->value
) ) {
230 $value = $row->value
;
236 $dbw->addQuotes( $row->type
) . ',' .
237 $dbw->addQuotes( $row->namespace ) . ',' .
238 $dbw->addQuotes( $row->title
) . ',' .
239 $dbw->addQuotes( $value ) . ')';
242 # Save results into the querycache table on the master
244 if ( !$dbw->query( $insertSql, $fname ) ) {
245 // Set result to false to indicate error
246 $dbr->freeResult( $res );
251 $dbr->freeResult( $res );
253 if ( $ignoreErrors ) {
254 $dbw->ignoreErrors( $ignoreW );
255 $dbr->ignoreErrors( $ignoreR );
258 # Update the querycache_info record for the page
259 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
260 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
267 * This is the actual workhorse. It does everything needed to make a
268 * real, honest-to-gosh query page.
270 * @param $offset database query offset
271 * @param $limit database query limit
272 * @param $shownavigation show navigation like "next 200"?
274 function doQuery( $offset, $limit, $shownavigation=true ) {
275 global $wgUser, $wgOut, $wgLang, $wgContLang;
277 $this->offset
= $offset;
278 $this->limit
= $limit;
280 $sname = $this->getName();
281 $fname = get_class($this) . '::doQuery';
282 $dbr = wfGetDB( DB_SLAVE
);
284 $wgOut->setSyndicated( $this->isSyndicated() );
286 if ( !$this->isCached() ) {
287 $sql = $this->getSQL();
289 # Get the cached result
290 $querycache = $dbr->tableName( 'querycache' );
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 );
313 # If updates on this page have been disabled, let the user know
314 # that the data set won't be refreshed for now
315 global $wgDisableQueryPageUpdate;
316 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
317 $wgOut->addWikiText( wfMsg( 'querypage-no-updates' ) );
324 $sql .= $this->getOrder();
325 $sql = $dbr->limitResult($sql, $limit, $offset);
326 $res = $dbr->query( $sql );
327 $num = $dbr->numRows($res);
329 $this->preprocessResults( $dbr, $res );
331 $sk = $wgUser->getSkin( );
333 if($shownavigation) {
334 $wgOut->addHTML( $this->getPageHeader() );
336 # if list is empty, show it
338 $wgOut->addHTML( '<hr><p>' . wfMsgHTML('specialpage-empty') . '</p>' );
342 $top = wfShowingResults( $offset, $num);
343 $wgOut->addHTML( "<p>{$top}\n" );
345 # often disable 'next' link when we reach the end
346 $atend = $num < $limit;
348 $sl = wfViewPrevNext( $offset, $limit ,
349 $wgContLang->specialPage( $sname ),
350 wfArrayToCGI( $this->linkParameters() ), $atend );
351 $wgOut->addHTML( "<br />{$sl}</p>\n" );
355 if ( ! $this->listoutput
)
356 $s[] = $this->openList( $offset );
358 # Only read at most $num rows, because $res may contain the whole 1000
359 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
360 $format = $this->formatResult( $sk, $obj );
362 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
363 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
364 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
368 if($this->tryLastResult()) {
369 // flush the very last result
371 $format = $this->formatResult( $sk, $obj );
373 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
374 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
375 $s[] = "<li{$attr}>{$format}</li>\n";
379 $dbr->freeResult( $res );
380 if ( ! $this->listoutput
)
381 $s[] = $this->closeList();
382 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
383 $wgOut->addHTML( $str );
385 if($shownavigation) {
386 $wgOut->addHTML( "<p>{$sl}</p>\n" );
391 function openList( $offset ) {
392 return "<ol start='" . ( $offset +
1 ) . "' class='special'>";
395 function closeList() {
400 * Do any necessary preprocessing of the result object.
401 * You should pass this by reference: &$db , &$res [although probably no longer necessary in PHP5]
403 function preprocessResults( &$db, &$res ) {}
406 * Similar to above, but packaging in a syndicated feed instead of a web page
408 function doFeed( $class = '', $limit = 50 ) {
409 global $wgFeedClasses;
411 if( isset($wgFeedClasses[$class]) ) {
412 $feed = new $wgFeedClasses[$class](
418 $dbr = wfGetDB( DB_SLAVE
);
419 $sql = $this->getSQL() . $this->getOrder();
420 $sql = $dbr->limitResult( $sql, $limit, 0 );
421 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
422 while( $obj = $dbr->fetchObject( $res ) ) {
423 $item = $this->feedResult( $obj );
424 if( $item ) $feed->outItem( $item );
426 $dbr->freeResult( $res );
436 * Override for custom handling. If the titles/links are ok, just do
439 function feedResult( $row ) {
440 if( !isset( $row->title
) ) {
443 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
445 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
448 $talkpage = $title->getTalkPage();
449 $comments = $talkpage->getFullURL();
453 $title->getPrefixedText(),
454 $this->feedItemDesc( $row ),
455 $title->getFullURL(),
457 $this->feedItemAuthor( $row ),
464 function feedItemDesc( $row ) {
465 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
468 function feedItemAuthor( $row ) {
469 return isset( $row->user_text
) ?
$row->user_text
: '';
472 function feedTitle() {
473 global $wgContLanguageCode, $wgSitename;
474 $page = SpecialPage
::getPage( $this->getName() );
475 $desc = $page->getDescription();
476 return "$wgSitename - $desc [$wgContLanguageCode]";
479 function feedDesc() {
480 return wfMsg( 'tagline' );
484 $title = SpecialPage
::getTitleFor( $this->getName() );
485 return $title->getFullURL();
490 * This is a subclass for very simple queries that are just looking for page
491 * titles that match some criteria. It formats each result item as a link to
495 class PageQueryPage
extends QueryPage
{
497 function formatResult( $skin, $result ) {
499 $nt = Title
::makeTitle( $result->namespace, $result->title
);
500 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );