* (bug 7405) Make Linker methods static. Patch by Dan Li.
[lhc/web/wiklou.git] / includes / QueryPage.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 */
6
7 /**
8 * List of query page classes and their associated special pages, for periodic update purposes
9 */
10 global $wgQueryPages; // not redundant
11 $wgQueryPages = array(
12 // QueryPage subclass Special page name Limit (false for none, none for the default)
13 //----------------------------------------------------------------------------
14 array( 'AncientPagesPage', 'Ancientpages' ),
15 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
16 array( 'CategoriesPage', 'Categories' ),
17 array( 'DeadendPagesPage', 'Deadendpages' ),
18 array( 'DisambiguationsPage', 'Disambiguations' ),
19 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
20 array( 'ListUsersPage', 'Listusers' ),
21 array( 'ListredirectsPage', 'Listredirects' ),
22 array( 'LonelyPagesPage', 'Lonelypages' ),
23 array( 'LongPagesPage', 'Longpages' ),
24 array( 'MostcategoriesPage', 'Mostcategories' ),
25 array( 'MostimagesPage', 'Mostimages' ),
26 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
27 array( 'MostlinkedPage', 'Mostlinked' ),
28 array( 'MostrevisionsPage', 'Mostrevisions' ),
29 array( 'NewPagesPage', 'Newpages' ),
30 array( 'ShortPagesPage', 'Shortpages' ),
31 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
32 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
33 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
34 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
35 array( 'UnusedimagesPage', 'Unusedimages' ),
36 array( 'WantedCategoriesPage', 'Wantedcategories' ),
37 array( 'WantedPagesPage', 'Wantedpages' ),
38 array( 'UnwatchedPagesPage', 'Unwatchedpages' ),
39 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
40 );
41 wfRunHooks( 'wgQueryPages', array( &$wgQueryPages ) );
42
43 global $wgDisableCounters;
44 if ( !$wgDisableCounters )
45 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
46
47
48 /**
49 * This is a class for doing query pages; since they're almost all the same,
50 * we factor out some of the functionality into a superclass, and let
51 * subclasses derive from it.
52 *
53 * @package MediaWiki
54 */
55 class QueryPage {
56 /**
57 * Whether or not we want plain listoutput rather than an ordered list
58 *
59 * @var bool
60 */
61 var $listoutput = false;
62
63 /**
64 * The offset and limit in use, as passed to the query() function
65 *
66 * @var integer
67 */
68 var $offset = 0;
69 var $limit = 0;
70
71 /**
72 * A mutator for $this->listoutput;
73 *
74 * @param bool $bool
75 */
76 function setListoutput( $bool ) {
77 $this->listoutput = $bool;
78 }
79
80 /**
81 * Subclasses return their name here. Make sure the name is also
82 * specified in SpecialPage.php and in Language.php as a language message
83 * param.
84 */
85 function getName() {
86 return '';
87 }
88
89 /**
90 * Return title object representing this page
91 *
92 * @return Title
93 */
94 function getTitle() {
95 return SpecialPage::getTitleFor( $this->getName() );
96 }
97
98 /**
99 * Subclasses return an SQL query here.
100 *
101 * Note that the query itself should return the following four columns:
102 * 'type' (your special page's name), 'namespace', 'title', and 'value'
103 * *in that order*. 'value' is used for sorting.
104 *
105 * These may be stored in the querycache table for expensive queries,
106 * and that cached data will be returned sometimes, so the presence of
107 * extra fields can't be relied upon. The cached 'value' column will be
108 * an integer; non-numeric values are useful only for sorting the initial
109 * query.
110 *
111 * Don't include an ORDER or LIMIT clause, this will be added.
112 */
113 function getSQL() {
114 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
115 }
116
117 /**
118 * Override to sort by increasing values
119 */
120 function sortDescending() {
121 return true;
122 }
123
124 function getOrder() {
125 return ' ORDER BY value ' .
126 ($this->sortDescending() ? 'DESC' : '');
127 }
128
129 /**
130 * Is this query expensive (for some definition of expensive)? Then we
131 * don't let it run in miser mode. $wgDisableQueryPages causes all query
132 * pages to be declared expensive. Some query pages are always expensive.
133 */
134 function isExpensive( ) {
135 global $wgDisableQueryPages;
136 return $wgDisableQueryPages;
137 }
138
139 /**
140 * Whether or not the output of the page in question is retrived from
141 * the database cache.
142 *
143 * @return bool
144 */
145 function isCached() {
146 global $wgMiserMode;
147
148 return $this->isExpensive() && $wgMiserMode;
149 }
150
151 /**
152 * Sometime we dont want to build rss / atom feeds.
153 */
154 function isSyndicated() {
155 return true;
156 }
157
158 /**
159 * Formats the results of the query for display. The result is a single
160 * row of result data. You should be able to grab SQL results off of it.
161 * If the function return "false", the line output will be skipped.
162 */
163 function formatResult( $result ) {
164 return '';
165 }
166
167 /**
168 * The content returned by this function will be output before any result
169 */
170 function getPageHeader( ) {
171 return '';
172 }
173
174 /**
175 * If using extra form wheely-dealies, return a set of parameters here
176 * as an associative array. They will be encoded and added to the paging
177 * links (prev/next/lengths).
178 * @return array
179 */
180 function linkParameters() {
181 return array();
182 }
183
184 /**
185 * Some special pages (for example SpecialListusers) might not return the
186 * current object formatted, but return the previous one instead.
187 * Setting this to return true, will call one more time wfFormatResult to
188 * be sure that the very last result is formatted and shown.
189 */
190 function tryLastResult( ) {
191 return false;
192 }
193
194 /**
195 * Clear the cache and save new results
196 */
197 function recache( $limit, $ignoreErrors = true ) {
198 $fname = get_class($this) . '::recache';
199 $dbw =& wfGetDB( DB_MASTER );
200 $dbr =& wfGetDB( DB_SLAVE, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
201 if ( !$dbw || !$dbr ) {
202 return false;
203 }
204
205 $querycache = $dbr->tableName( 'querycache' );
206
207 if ( $ignoreErrors ) {
208 $ignoreW = $dbw->ignoreErrors( true );
209 $ignoreR = $dbr->ignoreErrors( true );
210 }
211
212 # Clear out any old cached data
213 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
214 # Do query
215 $sql = $this->getSQL() . $this->getOrder();
216 if ($limit !== false)
217 $sql = $dbr->limitResult($sql, $limit, 0);
218 $res = $dbr->query($sql, $fname);
219 $num = false;
220 if ( $res ) {
221 $num = $dbr->numRows( $res );
222 # Fetch results
223 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
224 $first = true;
225 while ( $res && $row = $dbr->fetchObject( $res ) ) {
226 if ( $first ) {
227 $first = false;
228 } else {
229 $insertSql .= ',';
230 }
231 if ( isset( $row->value ) ) {
232 $value = $row->value;
233 } else {
234 $value = '';
235 }
236
237 $insertSql .= '(' .
238 $dbw->addQuotes( $row->type ) . ',' .
239 $dbw->addQuotes( $row->namespace ) . ',' .
240 $dbw->addQuotes( $row->title ) . ',' .
241 $dbw->addQuotes( $value ) . ')';
242 }
243
244 # Save results into the querycache table on the master
245 if ( !$first ) {
246 if ( !$dbw->query( $insertSql, $fname ) ) {
247 // Set result to false to indicate error
248 $dbr->freeResult( $res );
249 $res = false;
250 }
251 }
252 if ( $res ) {
253 $dbr->freeResult( $res );
254 }
255 if ( $ignoreErrors ) {
256 $dbw->ignoreErrors( $ignoreW );
257 $dbr->ignoreErrors( $ignoreR );
258 }
259
260 # Update the querycache_info record for the page
261 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
262 $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
263
264 }
265 return $num;
266 }
267
268 /**
269 * This is the actual workhorse. It does everything needed to make a
270 * real, honest-to-gosh query page.
271 *
272 * @param $offset database query offset
273 * @param $limit database query limit
274 * @param $shownavigation show navigation like "next 200"?
275 */
276 function doQuery( $offset, $limit, $shownavigation=true ) {
277 global $wgOut, $wgLang, $wgContLang;
278
279 $this->offset = $offset;
280 $this->limit = $limit;
281
282 $sname = $this->getName();
283 $fname = get_class($this) . '::doQuery';
284 $sql = $this->getSQL();
285 $dbr =& wfGetDB( DB_SLAVE );
286 $querycache = $dbr->tableName( 'querycache' );
287
288 $wgOut->setSyndicated( $this->isSyndicated() );
289
290 if ( $this->isCached() ) {
291 $type = $dbr->strencode( $sname );
292 $sql =
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'";
295
296 if( !$this->listoutput ) {
297
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 );
301
302 if( $tRow ) {
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>' );
307 } else {
308 $cacheNotice = wfMsg( 'perfcached' );
309 }
310
311 $wgOut->addWikiText( $cacheNotice );
312 }
313
314 }
315
316 $sql .= $this->getOrder();
317 $sql = $dbr->limitResult($sql, $limit, $offset);
318 $res = $dbr->query( $sql );
319 $num = $dbr->numRows($res);
320
321 $this->preprocessResults( $dbr, $res );
322
323 if($shownavigation) {
324 $wgOut->addHTML( $this->getPageHeader() );
325 $top = wfShowingResults( $offset, $num);
326 $wgOut->addHTML( "<p>{$top}\n" );
327
328 # often disable 'next' link when we reach the end
329 $atend = $num < $limit;
330
331 $sl = wfViewPrevNext( $offset, $limit ,
332 $wgContLang->specialPage( $sname ),
333 wfArrayToCGI( $this->linkParameters() ), $atend );
334 $wgOut->addHTML( "<br />{$sl}</p>\n" );
335 }
336 if ( $num > 0 ) {
337 $s = array();
338 if ( ! $this->listoutput )
339 $s[] = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
340
341 # Only read at most $num rows, because $res may contain the whole 1000
342 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
343 $format = $this->formatResult( $obj );
344 if ( $format ) {
345 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
346 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
347 $s[] = $this->listoutput ? $format : "<li{$attr}>{$format}</li>\n";
348 }
349 }
350
351 if($this->tryLastResult()) {
352 // flush the very last result
353 $obj = null;
354 $format = $this->formatResult( $obj );
355 if( $format ) {
356 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
357 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
358 $s[] = "<li{$attr}>{$format}</li>\n";
359 }
360 }
361
362 $dbr->freeResult( $res );
363 if ( ! $this->listoutput )
364 $s[] = '</ol>';
365 $str = $this->listoutput ? $wgContLang->listToText( $s ) : implode( '', $s );
366 $wgOut->addHTML( $str );
367 }
368 if($shownavigation) {
369 $wgOut->addHTML( "<p>{$sl}</p>\n" );
370 }
371 return $num;
372 }
373
374 /**
375 * Do any necessary preprocessing of the result object.
376 * You should pass this by reference: &$db , &$res
377 */
378 function preprocessResults( $db, $res ) {}
379
380 /**
381 * Similar to above, but packaging in a syndicated feed instead of a web page
382 */
383 function doFeed( $class = '', $limit = 50 ) {
384 global $wgFeedClasses;
385
386 if( isset($wgFeedClasses[$class]) ) {
387 $feed = new $wgFeedClasses[$class](
388 $this->feedTitle(),
389 $this->feedDesc(),
390 $this->feedUrl() );
391 $feed->outHeader();
392
393 $dbr =& wfGetDB( DB_SLAVE );
394 $sql = $this->getSQL() . $this->getOrder();
395 $sql = $dbr->limitResult( $sql, $limit, 0 );
396 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
397 while( $obj = $dbr->fetchObject( $res ) ) {
398 $item = $this->feedResult( $obj );
399 if( $item ) $feed->outItem( $item );
400 }
401 $dbr->freeResult( $res );
402
403 $feed->outFooter();
404 return true;
405 } else {
406 return false;
407 }
408 }
409
410 /**
411 * Override for custom handling. If the titles/links are ok, just do
412 * feedItemDesc()
413 */
414 function feedResult( $row ) {
415 if( !isset( $row->title ) ) {
416 return NULL;
417 }
418 $title = Title::MakeTitle( intval( $row->namespace ), $row->title );
419 if( $title ) {
420 $date = isset( $row->timestamp ) ? $row->timestamp : '';
421 $comments = '';
422 if( $title ) {
423 $talkpage = $title->getTalkPage();
424 $comments = $talkpage->getFullURL();
425 }
426
427 return new FeedItem(
428 $title->getPrefixedText(),
429 $this->feedItemDesc( $row ),
430 $title->getFullURL(),
431 $date,
432 $this->feedItemAuthor( $row ),
433 $comments);
434 } else {
435 return NULL;
436 }
437 }
438
439 function feedItemDesc( $row ) {
440 return isset( $row->comment ) ? htmlspecialchars( $row->comment ) : '';
441 }
442
443 function feedItemAuthor( $row ) {
444 return isset( $row->user_text ) ? $row->user_text : '';
445 }
446
447 function feedTitle() {
448 global $wgContLanguageCode, $wgSitename;
449 $page = SpecialPage::getPage( $this->getName() );
450 $desc = $page->getDescription();
451 return "$wgSitename - $desc [$wgContLanguageCode]";
452 }
453
454 function feedDesc() {
455 return wfMsg( 'tagline' );
456 }
457
458 function feedUrl() {
459 $title = SpecialPage::getTitleFor( $this->getName() );
460 return $title->getFullURL();
461 }
462 }
463
464 /**
465 * This is a subclass for very simple queries that are just looking for page
466 * titles that match some criteria. It formats each result item as a link to
467 * that page.
468 *
469 * @package MediaWiki
470 */
471 class PageQueryPage extends QueryPage {
472
473 function formatResult( $result ) {
474 global $wgContLang;
475 $nt = Title::makeTitle( $result->namespace, $result->title );
476 return Linker::makeKnownLinkObj( $nt, htmlspecialchars( $wgContLang->convert( $nt->getPrefixedText() ) ) );
477 }
478 }
479
480 ?>