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