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