3 * Contain a class for special pages
10 require_once 'Feed.php';
13 * List of query page classes and their associated special pages, for periodic update purposes
15 global $wgQueryPages; // not redundant
16 $wgQueryPages = array(
17 // QueryPage subclass Special page name Limit (false for none, none for the default)
18 //----------------------------------------------------------------------------
19 array( 'AncientPagesPage', 'Ancientpages' ),
20 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
21 array( 'CategoriesPage', 'Categories' ),
22 array( 'DeadendPagesPage', 'Deadendpages' ),
23 array( 'DisambiguationsPage', 'Disambiguations' ),
24 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
25 array( 'ListUsersPage', 'Listusers' ),
26 array( 'LonelyPagesPage', 'Lonelypages' ),
27 array( 'LongPagesPage', 'Longpages' ),
28 array( 'MostcategoriesPage', 'Mostcategories' ),
29 array( 'MostimagesPage', 'Mostimages' ),
30 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
31 array( 'MostlinkedPage', 'Mostlinked' ),
32 array( 'MostrevisionsPage', 'Mostrevisions' ),
33 array( 'NewPagesPage', 'Newpages' ),
34 array( 'ShortPagesPage', 'Shortpages' ),
35 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
36 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
37 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
38 array( 'UnusedimagesPage', 'Unusedimages' ),
39 array( 'WantedCategoriesPage', 'Wantedcategories' ),
40 array( 'WantedPagesPage', 'Wantedpages' ),
41 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
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 * A mutator for $this->listoutput;
70 function setListoutput( $bool ) {
71 $this->listoutput
= $bool;
75 * Subclasses return their name here. Make sure the name is also
76 * specified in SpecialPage.php and in Language.php as a language message
84 * Subclasses return an SQL query here.
86 * Note that the query itself should return the following four columns:
87 * 'type' (your special page's name), 'namespace', 'title', and 'value'
88 * *in that order*. 'value' is used for sorting.
90 * These may be stored in the querycache table for expensive queries,
91 * and that cached data will be returned sometimes, so the presence of
92 * extra fields can't be relied upon. The cached 'value' column will be
93 * an integer; non-numeric values are useful only for sorting the initial
96 * Don't include an ORDER or LIMIT clause, this will be added.
99 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
103 * Override to sort by increasing values
105 function sortDescending() {
109 function getOrder() {
110 return ' ORDER BY value ' .
111 ($this->sortDescending() ?
'DESC' : '');
115 * Is this query expensive (for some definition of expensive)? Then we
116 * don't let it run in miser mode. $wgDisableQueryPages causes all query
117 * pages to be declared expensive. Some query pages are always expensive.
119 function isExpensive( ) {
120 global $wgDisableQueryPages;
121 return $wgDisableQueryPages;
125 * Whether or not the output of the page in question is retrived from
126 * the database cache.
130 function isCached() {
133 return $this->isExpensive() && $wgMiserMode;
137 * Sometime we dont want to build rss / atom feeds.
139 function isSyndicated() {
144 * Formats the results of the query for display. The skin is the current
145 * skin; you can use it for making links. The result is a single row of
146 * result data. You should be able to grab SQL results off of it.
147 * If the function return "false", the line output will be skipped.
149 function formatResult( $skin, $result ) {
154 * The content returned by this function will be output before any result
156 function getPageHeader( ) {
161 * If using extra form wheely-dealies, return a set of parameters here
162 * as an associative array. They will be encoded and added to the paging
163 * links (prev/next/lengths).
166 function linkParameters() {
171 * Some special pages (for example SpecialListusers) might not return the
172 * current object formatted, but return the previous one instead.
173 * Setting this to return true, will call one more time wfFormatResult to
174 * be sure that the very last result is formatted and shown.
176 function tryLastResult( ) {
181 * Clear the cache and save new results
183 function recache( $limit, $ignoreErrors = true ) {
184 $fname = get_class($this) . '::recache';
185 $dbw =& wfGetDB( DB_MASTER
);
186 $dbr =& wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
187 if ( !$dbw ||
!$dbr ) {
191 $querycache = $dbr->tableName( 'querycache' );
193 if ( $ignoreErrors ) {
194 $ignoreW = $dbw->ignoreErrors( true );
195 $ignoreR = $dbr->ignoreErrors( true );
198 # Clear out any old cached data
199 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
201 $sql = $this->getSQL() . $this->getOrder();
202 if ($limit !== false)
203 $sql = $dbr->limitResult($sql, $limit, 0);
204 $res = $dbr->query($sql, $fname);
207 $num = $dbr->numRows( $res );
209 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
211 while ( $res && $row = $dbr->fetchObject( $res ) ) {
217 if ( isset( $row->value
) ) {
218 $value = $row->value
;
224 $dbw->addQuotes( $row->type
) . ',' .
225 $dbw->addQuotes( $row->namespace ) . ',' .
226 $dbw->addQuotes( $row->title
) . ',' .
227 $dbw->addQuotes( $value ) . ')';
230 # Save results into the querycache table on the master
232 if ( !$dbw->query( $insertSql, $fname ) ) {
233 // Set result to false to indicate error
234 $dbr->freeResult( $res );
239 $dbr->freeResult( $res );
241 if ( $ignoreErrors ) {
242 $dbw->ignoreErrors( $ignoreW );
243 $dbr->ignoreErrors( $ignoreR );
250 * This is the actual workhorse. It does everything needed to make a
251 * real, honest-to-gosh query page.
253 * @param $offset database query offset
254 * @param $limit database query limit
255 * @param $shownavigation show navigation like "next 200"?
257 function doQuery( $offset, $limit, $shownavigation=true ) {
258 global $wgUser, $wgOut, $wgContLang;
260 $sname = $this->getName();
261 $fname = get_class($this) . '::doQuery';
262 $sql = $this->getSQL();
263 $dbr =& wfGetDB( DB_SLAVE
);
264 $querycache = $dbr->tableName( 'querycache' );
266 $wgOut->setSyndicated( $this->isSyndicated() );
268 if ( $this->isCached() ) {
269 $type = $dbr->strencode( $sname );
271 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
272 FROM $querycache WHERE qc_type='$type'";
273 if ( ! $this->listoutput
)
274 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
277 $sql .= $this->getOrder();
278 $sql = $dbr->limitResult($sql, $limit, $offset);
279 $res = $dbr->query( $sql );
280 $num = $dbr->numRows($res);
282 $this->preprocessResults( $dbr, $res );
284 $sk = $wgUser->getSkin( );
286 if($shownavigation) {
287 $wgOut->addHTML( $this->getPageHeader() );
288 $top = wfShowingResults( $offset, $num);
289 $wgOut->addHTML( "<p>{$top}\n" );
291 # often disable 'next' link when we reach the end
292 $atend = $num < $limit;
294 $sl = wfViewPrevNext( $offset, $limit ,
295 $wgContLang->specialPage( $sname ),
296 wfArrayToCGI( $this->linkParameters() ), $atend );
297 $wgOut->addHTML( "<br />{$sl}</p>\n" );
301 if ( ! $this->listoutput
)
302 $s[] = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
304 # Only read at most $num rows, because $res may contain the whole 1000
305 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
306 $format = $this->formatResult( $sk, $obj );
308 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
309 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
310 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
314 if($this->tryLastResult()) {
315 // flush the very last result
317 $format = $this->formatResult( $sk, $obj );
319 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
320 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
321 $s[] = "<li{$attr}>{$format}</li>\n";
325 $dbr->freeResult( $res );
326 if ( ! $this->listoutput
)
328 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
329 $wgOut->addHTML( $str );
331 if($shownavigation) {
332 $wgOut->addHTML( "<p>{$sl}</p>\n" );
338 * Do any necessary preprocessing of the result object.
339 * You should pass this by reference: &$db , &$res
341 function preprocessResults( $db, $res ) {}
344 * Similar to above, but packaging in a syndicated feed instead of a web page
346 function doFeed( $class = '' ) {
347 global $wgFeedClasses;
349 if( isset($wgFeedClasses[$class]) ) {
350 $feed = new $wgFeedClasses[$class](
356 $dbr =& wfGetDB( DB_SLAVE
);
357 $sql = $this->getSQL() . $this->getOrder();
358 $sql = $dbr->limitResult( $sql, 50, 0 );
359 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
360 while( $obj = $dbr->fetchObject( $res ) ) {
361 $item = $this->feedResult( $obj );
362 if( $item ) $feed->outItem( $item );
364 $dbr->freeResult( $res );
374 * Override for custom handling. If the titles/links are ok, just do
377 function feedResult( $row ) {
378 if( !isset( $row->title
) ) {
381 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
383 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
386 $talkpage = $title->getTalkPage();
387 $comments = $talkpage->getFullURL();
391 $title->getPrefixedText(),
392 $this->feedItemDesc( $row ),
393 $title->getFullURL(),
395 $this->feedItemAuthor( $row ),
402 function feedItemDesc( $row ) {
403 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
406 function feedItemAuthor( $row ) {
407 return isset( $row->user_text
) ?
$row->user_text
: '';
410 function feedTitle() {
411 global $wgLanguageCode, $wgSitename;
412 $page = SpecialPage
::getPage( $this->getName() );
413 $desc = $page->getDescription();
414 return "$wgSitename - $desc [$wgLanguageCode]";
417 function feedDesc() {
418 return wfMsg( 'tagline' );
422 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
423 return $title->getFullURL();
428 * This is a subclass for very simple queries that are just looking for page
429 * titles that match some criteria. It formats each result item as a link to
434 class PageQueryPage
extends QueryPage
{
436 function formatResult( $skin, $result ) {
438 $nt = Title
::makeTitle( $result->namespace, $result->title
);
439 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );