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