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