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