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' ),
42 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
44 global $wgDisableCounters;
45 if ( !$wgDisableCounters )
46 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
47 global $wgEnableUnwatchedpages;
48 if ( $wgEnableUnwatchedpages )
49 $wgQueryPages[] = array( 'UnwatchedPagesPage', 'Unwatchedpages' );
53 * This is a class for doing query pages; since they're almost all the same,
54 * we factor out some of the functionality into a superclass, and let
55 * subclasses derive from it.
61 * Whether or not we want plain listoutput rather than an ordered list
65 var $listoutput = false;
68 * A mutator for $this->listoutput;
72 function setListoutput( $bool ) {
73 $this->listoutput
= $bool;
77 * Subclasses return their name here. Make sure the name is also
78 * specified in SpecialPage.php and in Language.php as a language message
86 * Subclasses return an SQL query here.
88 * Note that the query itself should return the following four columns:
89 * 'type' (your special page's name), 'namespace', 'title', and 'value'
90 * *in that order*. 'value' is used for sorting.
92 * These may be stored in the querycache table for expensive queries,
93 * and that cached data will be returned sometimes, so the presence of
94 * extra fields can't be relied upon. The cached 'value' column will be
95 * an integer; non-numeric values are useful only for sorting the initial
98 * Don't include an ORDER or LIMIT clause, this will be added.
101 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
105 * Override to sort by increasing values
107 function sortDescending() {
111 function getOrder() {
112 return ' ORDER BY value ' .
113 ($this->sortDescending() ?
'DESC' : '');
117 * Is this query expensive (for some definition of expensive)? Then we
118 * don't let it run in miser mode. $wgDisableQueryPages causes all query
119 * pages to be declared expensive. Some query pages are always expensive.
121 function isExpensive( ) {
122 global $wgDisableQueryPages;
123 return $wgDisableQueryPages;
127 * Whether or not the output of the page in question is retrived from
128 * the database cache.
132 function isCached() {
135 return $this->isExpensive() && $wgMiserMode;
139 * Sometime we dont want to build rss / atom feeds.
141 function isSyndicated() {
146 * Formats the results of the query for display. The skin is the current
147 * skin; you can use it for making links. The result is a single row of
148 * result data. You should be able to grab SQL results off of it.
149 * If the function return "false", the line output will be skipped.
151 function formatResult( $skin, $result ) {
156 * The content returned by this function will be output before any result
158 function getPageHeader( ) {
163 * If using extra form wheely-dealies, return a set of parameters here
164 * as an associative array. They will be encoded and added to the paging
165 * links (prev/next/lengths).
168 function linkParameters() {
173 * Some special pages (for example SpecialListusers) might not return the
174 * current object formatted, but return the previous one instead.
175 * Setting this to return true, will call one more time wfFormatResult to
176 * be sure that the very last result is formatted and shown.
178 function tryLastResult( ) {
183 * Clear the cache and save new results
185 function recache( $limit, $ignoreErrors = true ) {
186 $fname = get_class($this) . '::recache';
187 $dbw =& wfGetDB( DB_MASTER
);
188 $dbr =& wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
189 if ( !$dbw ||
!$dbr ) {
193 $querycache = $dbr->tableName( 'querycache' );
195 if ( $ignoreErrors ) {
196 $ignoreW = $dbw->ignoreErrors( true );
197 $ignoreR = $dbr->ignoreErrors( true );
200 # Clear out any old cached data
201 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
203 $sql = $this->getSQL() . $this->getOrder();
204 if ($limit !== false)
205 $sql = $dbr->limitResult($sql, $limit, 0);
206 $res = $dbr->query($sql, $fname);
209 $num = $dbr->numRows( $res );
211 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
213 while ( $res && $row = $dbr->fetchObject( $res ) ) {
219 if ( isset( $row->value
) ) {
220 $value = $row->value
;
226 $dbw->addQuotes( $row->type
) . ',' .
227 $dbw->addQuotes( $row->namespace ) . ',' .
228 $dbw->addQuotes( $row->title
) . ',' .
229 $dbw->addQuotes( $value ) . ')';
232 # Save results into the querycache table on the master
234 if ( !$dbw->query( $insertSql, $fname ) ) {
235 // Set result to false to indicate error
236 $dbr->freeResult( $res );
241 $dbr->freeResult( $res );
243 if ( $ignoreErrors ) {
244 $dbw->ignoreErrors( $ignoreW );
245 $dbr->ignoreErrors( $ignoreR );
252 * This is the actual workhorse. It does everything needed to make a
253 * real, honest-to-gosh query page.
255 * @param $offset database query offset
256 * @param $limit database query limit
257 * @param $shownavigation show navigation like "next 200"?
259 function doQuery( $offset, $limit, $shownavigation=true ) {
260 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
262 $sname = $this->getName();
263 $fname = get_class($this) . '::doQuery';
264 $sql = $this->getSQL();
265 $dbr =& wfGetDB( DB_SLAVE
);
266 $dbw =& wfGetDB( DB_MASTER
);
267 $querycache = $dbr->tableName( 'querycache' );
269 $wgOut->setSyndicated( $this->isSyndicated() );
271 if ( $this->isCached() ) {
272 $type = $dbr->strencode( $sname );
274 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
275 FROM $querycache WHERE qc_type='$type'";
276 if ( ! $this->listoutput
)
277 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
280 $sql .= $this->getOrder();
281 $sql = $dbr->limitResult($sql, $limit, $offset);
282 $res = $dbr->query( $sql );
283 $num = $dbr->numRows($res);
285 $this->preprocessResults( $dbr, $res );
287 $sk = $wgUser->getSkin( );
289 if($shownavigation) {
290 $wgOut->addHTML( $this->getPageHeader() );
291 $top = wfShowingResults( $offset, $num);
292 $wgOut->addHTML( "<p>{$top}\n" );
294 # often disable 'next' link when we reach the end
295 $atend = $num < $limit;
297 $sl = wfViewPrevNext( $offset, $limit ,
298 $wgContLang->specialPage( $sname ),
299 wfArrayToCGI( $this->linkParameters() ), $atend );
300 $wgOut->addHTML( "<br />{$sl}</p>\n" );
304 if ( ! $this->listoutput
)
305 $s[] = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
307 # Only read at most $num rows, because $res may contain the whole 1000
308 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
309 $format = $this->formatResult( $sk, $obj );
311 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
312 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
313 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
317 if($this->tryLastResult()) {
318 // flush the very last result
320 $format = $this->formatResult( $sk, $obj );
322 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
323 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
324 $s[] = "<li{$attr}>{$format}</li>\n";
328 $dbr->freeResult( $res );
329 if ( ! $this->listoutput
)
331 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
332 $wgOut->addHTML( $str );
334 if($shownavigation) {
335 $wgOut->addHTML( "<p>{$sl}</p>\n" );
341 * Do any necessary preprocessing of the result object
343 function preprocessResults( &$db, &$res ) {}
346 * Similar to above, but packaging in a syndicated feed instead of a web page
348 function doFeed( $class = '' ) {
349 global $wgFeedClasses;
351 if( isset($wgFeedClasses[$class]) ) {
352 $feed = new $wgFeedClasses[$class](
358 $dbr =& wfGetDB( DB_SLAVE
);
359 $sql = $this->getSQL() . $this->getOrder();
360 $sql = $dbr->limitResult( $sql, 50, 0 );
361 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
362 while( $obj = $dbr->fetchObject( $res ) ) {
363 $item = $this->feedResult( $obj );
364 if( $item ) $feed->outItem( $item );
366 $dbr->freeResult( $res );
376 * Override for custom handling. If the titles/links are ok, just do
379 function feedResult( $row ) {
380 if( !isset( $row->title
) ) {
383 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
385 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
388 $talkpage = $title->getTalkPage();
389 $comments = $talkpage->getFullURL();
393 $title->getPrefixedText(),
394 $this->feedItemDesc( $row ),
395 $title->getFullURL(),
397 $this->feedItemAuthor( $row ),
404 function feedItemDesc( $row ) {
405 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
408 function feedItemAuthor( $row ) {
409 return isset( $row->user_text
) ?
$row->user_text
: '';
412 function feedTitle() {
413 global $wgLanguageCode, $wgSitename;
414 $page = SpecialPage
::getPage( $this->getName() );
415 $desc = $page->getDescription();
416 return "$wgSitename - $desc [$wgLanguageCode]";
419 function feedDesc() {
420 return wfMsg( 'tagline' );
424 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
425 return $title->getFullURL();
430 * This is a subclass for very simple queries that are just looking for page
431 * titles that match some criteria. It formats each result item as a link to
436 class PageQueryPage
extends QueryPage
{
438 function formatResult( $skin, $result ) {
440 $nt = Title
::makeTitle( $result->namespace, $result->title
);
441 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );