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