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( 'DeadendPagesPage', 'Deadendpages' ),
22 array( 'DisambiguationsPage', 'Disambiguations' ),
23 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
24 array( 'ListUsersPage', 'Listusers' ),
25 array( 'LonelyPagesPage', 'Lonelypages' ),
26 array( 'LongPagesPage', 'Longpages' ),
27 array( 'NewPagesPage', 'Newpages' ),
28 array( 'ShortPagesPage', 'Shortpages' ),
29 array( 'UncategorizedCategoriesPage','Uncategorizedcategories'),
30 array( 'UncategorizedPagesPage', 'Uncategorizedpages'),
31 array( 'UnusedimagesPage', 'Unusedimages' ),
32 array( 'WantedPagesPage', 'Wantedpages' ),
33 array( 'MostlinkedPage', 'Mostlinked' ),
35 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
37 global $wgDisableCounters;
38 if( !$wgDisableCounters ) {
39 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
43 * This is a class for doing query pages; since they're almost all the same,
44 * we factor out some of the functionality into a superclass, and let
45 * subclasses derive from it.
51 * Whether or not we want plain listoutput rather than an ordered list
55 var $listoutput = false;
58 * A mutator for $this->listoutput;
62 function setListoutput( $bool ) {
63 $this->listoutput
= $bool;
67 * Subclasses return their name here. Make sure the name is also
68 * specified in SpecialPage.php and in Language.php as a language message
76 * Subclasses return an SQL query here.
78 * Note that the query itself should return the following four columns:
79 * 'type' (your special page's name), 'namespace', 'title', and 'value'
80 * *in that order*. 'value' is used for sorting.
82 * These may be stored in the querycache table for expensive queries,
83 * and that cached data will be returned sometimes, so the presence of
84 * extra fields can't be relied upon. The cached 'value' column will be
85 * an integer; non-numeric values are useful only for sorting the initial
88 * Don't include an ORDER or LIMIT clause, this will be added.
91 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
95 * Override to sort by increasing values
97 function sortDescending() {
101 function getOrder() {
102 return ' ORDER BY value ' .
103 ($this->sortDescending() ?
'DESC' : '');
107 * Is this query expensive (for some definition of expensive)? Then we
108 * don't let it run in miser mode. $wgDisableQueryPages causes all query
109 * pages to be declared expensive. Some query pages are always expensive.
111 function isExpensive( ) {
112 global $wgDisableQueryPages;
113 return $wgDisableQueryPages;
117 * Whether or not the output of the page in question is retrived from
118 * the database cache.
122 function isCached() {
125 return $this->isExpensive() && $wgMiserMode;
129 * Sometime we dont want to build rss / atom feeds.
131 function isSyndicated() {
136 * Formats the results of the query for display. The skin is the current
137 * skin; you can use it for making links. The result is a single row of
138 * result data. You should be able to grab SQL results off of it.
139 * If the function return "false", the line output will be skipped.
141 function formatResult( $skin, $result ) {
146 * The content returned by this function will be output before any result
148 function getPageHeader( ) {
153 * If using extra form wheely-dealies, return a set of parameters here
154 * as an associative array. They will be encoded and added to the paging
155 * links (prev/next/lengths).
158 function linkParameters() {
163 * Some special pages (for example SpecialListusers) might not return the
164 * current object formatted, but return the previous one instead.
165 * Setting this to return true, will call one more time wfFormatResult to
166 * be sure that the very last result is formatted and shown.
168 function tryLastResult( ) {
173 * Clear the cache and save new results
175 function recache( $limit, $ignoreErrors = true ) {
176 $fname = get_class($this) . '::recache';
177 $dbw =& wfGetDB( DB_MASTER
);
178 $dbr =& wfGetDB( DB_SLAVE
, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
179 if ( !$dbw ||
!$dbr ) {
183 $querycache = $dbr->tableName( 'querycache' );
185 if ( $ignoreErrors ) {
186 $ignoreW = $dbw->ignoreErrors( true );
187 $ignoreR = $dbr->ignoreErrors( true );
190 # Clear out any old cached data
191 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
193 $sql = $this->getSQL() . $this->getOrder();
194 if ($limit !== false)
195 $sql = $dbr->limitResult($sql, $limit, 0);
196 $res = $dbr->query($sql, $fname);
199 $num = $dbr->numRows( $res );
201 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
203 while ( $res && $row = $dbr->fetchObject( $res ) ) {
209 if ( isset( $row->value
) ) {
210 $value = $row->value
;
216 $dbw->addQuotes( $row->type
) . ',' .
217 $dbw->addQuotes( $row->namespace ) . ',' .
218 $dbw->addQuotes( $row->title
) . ',' .
219 $dbw->addQuotes( $value ) . ')';
222 # Save results into the querycache table on the master
224 if ( !$dbw->query( $insertSql, $fname ) ) {
225 // Set result to false to indicate error
226 $dbr->freeResult( $res );
231 $dbr->freeResult( $res );
233 if ( $ignoreErrors ) {
234 $dbw->ignoreErrors( $ignoreW );
235 $dbr->ignoreErrors( $ignoreR );
242 * This is the actual workhorse. It does everything needed to make a
243 * real, honest-to-gosh query page.
245 * @param $offset database query offset
246 * @param $limit database query limit
247 * @param $shownavigation show navigation like "next 200"?
249 function doQuery( $offset, $limit, $shownavigation=true ) {
250 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
252 $sname = $this->getName();
253 $fname = get_class($this) . '::doQuery';
254 $sql = $this->getSQL();
255 $dbr =& wfGetDB( DB_SLAVE
);
256 $dbw =& wfGetDB( DB_MASTER
);
257 $querycache = $dbr->tableName( 'querycache' );
259 $wgOut->setSyndicated( $this->isSyndicated() );
261 if ( $this->isCached() ) {
262 $type = $dbr->strencode( $sname );
264 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
265 FROM $querycache WHERE qc_type='$type'";
266 if ( ! $this->listoutput
)
267 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
270 $sql .= $this->getOrder();
271 $sql = $dbr->limitResult($sql, $limit, $offset);
272 $res = $dbr->query( $sql );
273 $num = $dbr->numRows($res);
275 $this->preprocessResults( $dbr, $res );
277 $sk = $wgUser->getSkin( );
279 if($shownavigation) {
280 $wgOut->addHTML( $this->getPageHeader() );
281 $top = wfShowingResults( $offset, $num);
282 $wgOut->addHTML( "<p>{$top}\n" );
284 # often disable 'next' link when we reach the end
285 $atend = $num < $limit;
287 $sl = wfViewPrevNext( $offset, $limit ,
288 $wgContLang->specialPage( $sname ),
289 wfArrayToCGI( $this->linkParameters() ), $atend );
290 $wgOut->addHTML( "<br />{$sl}</p>\n" );
294 if ( ! $this->listoutput
)
295 $s[] = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
297 # Only read at most $num rows, because $res may contain the whole 1000
298 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
299 $format = $this->formatResult( $sk, $obj );
301 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
302 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
303 $s[] = $this->listoutput ?
$format : "<li{$attr}>{$format}</li>\n";
307 if($this->tryLastResult()) {
308 // flush the very last result
310 $format = $this->formatResult( $sk, $obj );
312 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
313 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
314 $s[] = "<li{$attr}>{$format}</li>\n";
318 $dbr->freeResult( $res );
319 if ( ! $this->listoutput
)
321 $str = $this->listoutput ?
$wgContLang->listToText( $s ) : implode( '', $s );
322 $wgOut->addHTML( $str );
324 if($shownavigation) {
325 $wgOut->addHTML( "<p>{$sl}</p>\n" );
331 * Do any necessary preprocessing of the result object
333 function preprocessResults( &$db, &$res ) {}
336 * Similar to above, but packaging in a syndicated feed instead of a web page
338 function doFeed( $class = '' ) {
339 global $wgFeedClasses;
341 if( isset($wgFeedClasses[$class]) ) {
342 $feed = new $wgFeedClasses[$class](
348 $dbr =& wfGetDB( DB_SLAVE
);
349 $sql = $this->getSQL() . $this->getOrder();
350 $sql = $dbr->limitResult( $sql, 50, 0 );
351 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
352 while( $obj = $dbr->fetchObject( $res ) ) {
353 $item = $this->feedResult( $obj );
354 if( $item ) $feed->outItem( $item );
356 $dbr->freeResult( $res );
366 * Override for custom handling. If the titles/links are ok, just do
369 function feedResult( $row ) {
370 if( !isset( $row->title
) ) {
373 $title = Title
::MakeTitle( intval( $row->namespace ), $row->title
);
375 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
378 $talkpage = $title->getTalkPage();
379 $comments = $talkpage->getFullURL();
383 $title->getPrefixedText(),
384 $this->feedItemDesc( $row ),
385 $title->getFullURL(),
387 $this->feedItemAuthor( $row ),
394 function feedItemDesc( $row ) {
395 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
398 function feedItemAuthor( $row ) {
399 return isset( $row->user_text
) ?
$row->user_text
: '';
402 function feedTitle() {
403 global $wgLanguageCode, $wgSitename;
404 $page = SpecialPage
::getPage( $this->getName() );
405 $desc = $page->getDescription();
406 return "$wgSitename - $desc [$wgLanguageCode]";
409 function feedDesc() {
410 return wfMsg( 'tagline' );
414 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
415 return $title->getFullURL();
420 * This is a subclass for very simple queries that are just looking for page
421 * titles that match some criteria. It formats each result item as a link to
426 class PageQueryPage
extends QueryPage
{
428 function formatResult( $skin, $result ) {
430 $nt = Title
::makeTitle( $result->namespace, $result->title
);
431 return $skin->makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );