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