typo :/
[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 $sql = $this->getSQL() . $this->getOrder();
164 $sql = $dbr->limitResult($sql, 1000,0);
165 $res = $dbr->query($sql, $fname);
166 $num = false;
167 if ( $res ) {
168 $num = $dbr->numRows( $res );
169 # Fetch results
170 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
171 $first = true;
172 while ( $res && $row = $dbr->fetchObject( $res ) ) {
173 if ( $first ) {
174 $first = false;
175 } else {
176 $insertSql .= ',';
177 }
178 if ( isset( $row->value ) ) {
179 $value = $row->value;
180 } else {
181 $value = '';
182 }
183
184 $insertSql .= '(' .
185 $dbw->addQuotes( $row->type ) . ',' .
186 $dbw->addQuotes( $row->namespace ) . ',' .
187 $dbw->addQuotes( $row->title ) . ',' .
188 $dbw->addQuotes( $value ) . ')';
189 }
190
191 # Save results into the querycache table on the master
192 if ( !$first ) {
193 if ( !$dbw->query( $insertSql, $fname ) ) {
194 // Set result to false to indicate error
195 $dbr->freeResult( $res );
196 $res = false;
197 }
198 }
199 if ( $res ) {
200 $dbr->freeResult( $res );
201 }
202 if ( $ignoreErrors ) {
203 $dbw->ignoreErrors( $ignoreW );
204 $dbr->ignoreErrors( $ignoreR );
205 }
206 }
207 return $num;
208 }
209
210 /**
211 * This is the actual workhorse. It does everything needed to make a
212 * real, honest-to-gosh query page.
213 *
214 * @param $offset database query offset
215 * @param $limit database query limit
216 * @param $shownavigation show navigation like "next 200"?
217 */
218 function doQuery( $offset, $limit, $shownavigation=true ) {
219 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
220 global $wgMiserMode;
221
222 $sname = $this->getName();
223 $fname = get_class($this) . '::doQuery';
224 $sql = $this->getSQL();
225 $dbr =& wfGetDB( DB_SLAVE );
226 $dbw =& wfGetDB( DB_MASTER );
227 $querycache = $dbr->tableName( 'querycache' );
228
229 $wgOut->setSyndicated( $this->isSyndicated() );
230
231 if ( $this->isExpensive() ) {
232 // Disabled recache parameter due to retry problems -- TS
233 if( $wgMiserMode ) {
234 $type = $dbr->strencode( $sname );
235 $sql =
236 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
237 FROM $querycache WHERE qc_type='$type'";
238 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
239 }
240 }
241
242 $sql .= $this->getOrder();
243 $sql = $dbr->limitResult($sql, $limit, $offset);
244 $res = $dbr->query( $sql );
245 $num = $dbr->numRows($res);
246
247 $sk = $wgUser->getSkin( );
248
249 if($shownavigation) {
250 $wgOut->addHTML( $this->getPageHeader() );
251 $top = wfShowingResults( $offset, $num);
252 $wgOut->addHTML( "<p>{$top}\n" );
253
254 # often disable 'next' link when we reach the end
255 if($num < $limit) { $atend = true; } else { $atend = false; }
256
257 $sl = wfViewPrevNext( $offset, $limit ,
258 $wgContLang->specialPage( $sname ),
259 wfArrayToCGI( $this->linkParameters() ), $atend );
260 $wgOut->addHTML( "<br />{$sl}</p>\n" );
261 }
262 if ( $num > 0 ) {
263 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
264
265 # Only read at most $num rows, because $res may contain the whole 1000
266 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
267 $format = $this->formatResult( $sk, $obj );
268 if ( $format ) {
269 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
270 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
271 $s .= "<li{$attr}>{$format}</li>\n";
272 }
273 }
274
275 if($this->tryLastResult()) {
276 // flush the very last result
277 $obj = null;
278 $format = $this->formatResult( $sk, $obj );
279 if( $format ) {
280 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
281 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
282 $s .= "<li{$attr}>{$format}</li>\n";
283 }
284 }
285
286 $dbr->freeResult( $res );
287 $s .= '</ol>';
288 $wgOut->addHTML( $s );
289 }
290 if($shownavigation) {
291 $wgOut->addHTML( "<p>{$sl}</p>\n" );
292 }
293 return $num;
294 }
295
296 /**
297 * Similar to above, but packaging in a syndicated feed instead of a web page
298 */
299 function doFeed( $class = '' ) {
300 global $wgFeedClasses;
301 global $wgOut, $wgLanguageCode, $wgLang;
302 if( isset($wgFeedClasses[$class]) ) {
303 $feed = new $wgFeedClasses[$class](
304 $this->feedTitle(),
305 $this->feedDesc(),
306 $this->feedUrl() );
307 $feed->outHeader();
308
309 $dbr =& wfGetDB( DB_SLAVE );
310 $sql = $this->getSQL() . $this->getOrder();
311 $sql = $dbr->limitResult( $sql, 50, 0 );
312 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
313 while( $obj = $dbr->fetchObject( $res ) ) {
314 $item = $this->feedResult( $obj );
315 if( $item ) $feed->outItem( $item );
316 }
317 $dbr->freeResult( $res );
318
319 $feed->outFooter();
320 return true;
321 } else {
322 return false;
323 }
324 }
325
326 /**
327 * Override for custom handling. If the titles/links are ok, just do
328 * feedItemDesc()
329 */
330 function feedResult( $row ) {
331 if( !isset( $row->title ) ) {
332 return NULL;
333 }
334 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
335 if( $title ) {
336 if( isset( $row->timestamp ) ) {
337 $date = $row->timestamp;
338 } else {
339 $date = '';
340 }
341
342 $comments = '';
343 if( $title ) {
344 $talkpage = $title->getTalkPage();
345 $comments = $talkpage->getFullURL();
346 }
347
348 return new FeedItem(
349 $title->getPrefixedText(),
350 $this->feedItemDesc( $row ),
351 $title->getFullURL(),
352 $date,
353 $this->feedItemAuthor( $row ),
354 $comments);
355 } else {
356 return NULL;
357 }
358 }
359
360 function feedItemDesc( $row ) {
361 return isset( $row->comment )
362 ? htmlspecialchars( $row->comment )
363 : '';
364 }
365
366 function feedItemAuthor( $row ) {
367 if( isset( $row->user_text ) ) {
368 return $row->user_text;
369 } else {
370 return '';
371 }
372 }
373
374 function feedTitle() {
375 global $wgLanguageCode, $wgSitename, $wgLang;
376 $page = SpecialPage::getPage( $this->getName() );
377 $desc = $page->getDescription();
378 return "$wgSitename - $desc [$wgLanguageCode]";
379 }
380
381 function feedDesc() {
382 return wfMsg( 'tagline' );
383 }
384
385 function feedUrl() {
386 global $wgLang;
387 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
388 return $title->getFullURL();
389 }
390 }
391
392 /**
393 * This is a subclass for very simple queries that are just looking for page
394 * titles that match some criteria. It formats each result item as a link to
395 * that page.
396 *
397 * @package MediaWiki
398 */
399 class PageQueryPage extends QueryPage {
400
401 function formatResult( $skin, $result ) {
402 global $wgContLang;
403 $nt = Title::makeTitle( $result->namespace, $result->title );
404 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $nt->getPrefixedText() ) );
405 }
406 }
407
408 ?>