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