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