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