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