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