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