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