#3364: pt_br name typo
[mediawiki.git] / includes / QueryPage.php
blobb7ff53985323856279f02551af4f3996b6d82219
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 */
7 /**
9 */
10 require_once ( 'Feed.php' );
12 /**
13 * List of query page classes and their associated special pages, for periodic update purposes
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' ),
35 global $wgDisableCounters;
36 if( !$wgDisableCounters ) {
37 $wgQueryPages[] = array( 'PopularPagesPage', 'Popularpages' );
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.
45 * @package MediaWiki
47 class QueryPage {
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.
54 function getName() {
55 return '';
58 /**
59 * Subclasses return an SQL query here.
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.
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.
71 * Don't include an ORDER or LIMIT clause, this will be added.
73 function getSQL() {
74 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
77 /**
78 * Override to sort by increasing values
80 function sortDescending() {
81 return true;
84 function getOrder() {
85 return ' ORDER BY value ' .
86 ($this->sortDescending() ? 'DESC' : '');
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.
94 function isExpensive( ) {
95 global $wgDisableQueryPages;
96 return $wgDisableQueryPages;
99 /**
100 * Sometime we dont want to build rss / atom feeds.
102 function isSyndicated() {
103 return true;
107 * Formats the results of the query for display. The skin is the current
108 * skin; you can use it for making links. The result is a single row of
109 * result data. You should be able to grab SQL results off of it.
110 * If the function return "false", the line output will be skipped.
112 function formatResult( $skin, $result ) {
113 return '';
117 * The content returned by this function will be output before any result
119 function getPageHeader( ) {
120 return '';
124 * If using extra form wheely-dealies, return a set of parameters here
125 * as an associative array. They will be encoded and added to the paging
126 * links (prev/next/lengths).
127 * @return array
129 function linkParameters() {
130 return array();
134 * Some special pages (for example SpecialListusers) might not return the
135 * current object formatted, but return the previous one instead.
136 * Setting this to return true, will call one more time wfFormatResult to
137 * be sure that the very last result is formatted and shown.
139 function tryLastResult( ) {
140 return false;
144 * Clear the cache and save new results
146 function recache( $ignoreErrors = true ) {
147 $fname = get_class($this) . '::recache';
148 $dbw =& wfGetDB( DB_MASTER );
149 $dbr =& wfGetDB( DB_SLAVE, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
150 if ( !$dbw || !$dbr ) {
151 return false;
154 $querycache = $dbr->tableName( 'querycache' );
156 if ( $ignoreErrors ) {
157 $ignoreW = $dbw->ignoreErrors( true );
158 $ignoreR = $dbr->ignoreErrors( true );
161 # Clear out any old cached data
162 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
163 # Do query
164 $sql = $this->getSQL() . $this->getOrder();
165 $sql = $dbr->limitResult($sql, 1000,0);
166 $res = $dbr->query($sql, $fname);
167 $num = false;
168 if ( $res ) {
169 $num = $dbr->numRows( $res );
170 # Fetch results
171 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
172 $first = true;
173 while ( $res && $row = $dbr->fetchObject( $res ) ) {
174 if ( $first ) {
175 $first = false;
176 } else {
177 $insertSql .= ',';
179 if ( isset( $row->value ) ) {
180 $value = $row->value;
181 } else {
182 $value = '';
185 $insertSql .= '(' .
186 $dbw->addQuotes( $row->type ) . ',' .
187 $dbw->addQuotes( $row->namespace ) . ',' .
188 $dbw->addQuotes( $row->title ) . ',' .
189 $dbw->addQuotes( $value ) . ')';
192 # Save results into the querycache table on the master
193 if ( !$first ) {
194 if ( !$dbw->query( $insertSql, $fname ) ) {
195 // Set result to false to indicate error
196 $dbr->freeResult( $res );
197 $res = false;
200 if ( $res ) {
201 $dbr->freeResult( $res );
203 if ( $ignoreErrors ) {
204 $dbw->ignoreErrors( $ignoreW );
205 $dbr->ignoreErrors( $ignoreR );
208 return $num;
212 * This is the actual workhorse. It does everything needed to make a
213 * real, honest-to-gosh query page.
215 * @param $offset database query offset
216 * @param $limit database query limit
217 * @param $shownavigation show navigation like "next 200"?
219 function doQuery( $offset, $limit, $shownavigation=true ) {
220 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
221 global $wgMiserMode;
223 $sname = $this->getName();
224 $fname = get_class($this) . '::doQuery';
225 $sql = $this->getSQL();
226 $dbr =& wfGetDB( DB_SLAVE );
227 $dbw =& wfGetDB( DB_MASTER );
228 $querycache = $dbr->tableName( 'querycache' );
230 $wgOut->setSyndicated( $this->isSyndicated() );
232 if ( $this->isExpensive() ) {
233 // Disabled recache parameter due to retry problems -- TS
234 if( $wgMiserMode ) {
235 $type = $dbr->strencode( $sname );
236 $sql =
237 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
238 FROM $querycache WHERE qc_type='$type'";
239 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
243 $sql .= $this->getOrder();
244 $sql = $dbr->limitResult($sql, $limit, $offset);
245 $res = $dbr->query( $sql );
246 $num = $dbr->numRows($res);
248 $sk = $wgUser->getSkin( );
250 if($shownavigation) {
251 $wgOut->addHTML( $this->getPageHeader() );
252 $top = wfShowingResults( $offset, $num);
253 $wgOut->addHTML( "<p>{$top}\n" );
255 # often disable 'next' link when we reach the end
256 if($num < $limit) { $atend = true; } else { $atend = false; }
258 $sl = wfViewPrevNext( $offset, $limit ,
259 $wgContLang->specialPage( $sname ),
260 wfArrayToCGI( $this->linkParameters() ), $atend );
261 $wgOut->addHTML( "<br />{$sl}</p>\n" );
263 if ( $num > 0 ) {
264 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
266 # Only read at most $num rows, because $res may contain the whole 1000
267 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
268 $format = $this->formatResult( $sk, $obj );
269 if ( $format ) {
270 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
271 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
272 $s .= "<li{$attr}>{$format}</li>\n";
276 if($this->tryLastResult()) {
277 // flush the very last result
278 $obj = null;
279 $format = $this->formatResult( $sk, $obj );
280 if( $format ) {
281 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
282 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
283 $s .= "<li{$attr}>{$format}</li>\n";
287 $dbr->freeResult( $res );
288 $s .= '</ol>';
289 $wgOut->addHTML( $s );
291 if($shownavigation) {
292 $wgOut->addHTML( "<p>{$sl}</p>\n" );
294 return $num;
298 * Similar to above, but packaging in a syndicated feed instead of a web page
300 function doFeed( $class = '' ) {
301 global $wgFeedClasses;
302 global $wgOut, $wgLanguageCode, $wgLang;
303 if( isset($wgFeedClasses[$class]) ) {
304 $feed = new $wgFeedClasses[$class](
305 $this->feedTitle(),
306 $this->feedDesc(),
307 $this->feedUrl() );
308 $feed->outHeader();
310 $dbr =& wfGetDB( DB_SLAVE );
311 $sql = $this->getSQL() . $this->getOrder();
312 $sql = $dbr->limitResult( $sql, 50, 0 );
313 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
314 while( $obj = $dbr->fetchObject( $res ) ) {
315 $item = $this->feedResult( $obj );
316 if( $item ) $feed->outItem( $item );
318 $dbr->freeResult( $res );
320 $feed->outFooter();
321 return true;
322 } else {
323 return false;
328 * Override for custom handling. If the titles/links are ok, just do
329 * feedItemDesc()
331 function feedResult( $row ) {
332 if( !isset( $row->title ) ) {
333 return NULL;
335 $title = Title::MakeTitle( intval( $row->namespace ), $row->title );
336 if( $title ) {
337 if( isset( $row->timestamp ) ) {
338 $date = $row->timestamp;
339 } else {
340 $date = '';
343 $comments = '';
344 if( $title ) {
345 $talkpage = $title->getTalkPage();
346 $comments = $talkpage->getFullURL();
349 return new FeedItem(
350 $title->getPrefixedText(),
351 $this->feedItemDesc( $row ),
352 $title->getFullURL(),
353 $date,
354 $this->feedItemAuthor( $row ),
355 $comments);
356 } else {
357 return NULL;
361 function feedItemDesc( $row ) {
362 return isset( $row->comment )
363 ? htmlspecialchars( $row->comment )
364 : '';
367 function feedItemAuthor( $row ) {
368 if( isset( $row->user_text ) ) {
369 return $row->user_text;
370 } else {
371 return '';
375 function feedTitle() {
376 global $wgLanguageCode, $wgSitename, $wgLang;
377 $page = SpecialPage::getPage( $this->getName() );
378 $desc = $page->getDescription();
379 return "$wgSitename - $desc [$wgLanguageCode]";
382 function feedDesc() {
383 return wfMsg( 'tagline' );
386 function feedUrl() {
387 global $wgLang;
388 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
389 return $title->getFullURL();
394 * This is a subclass for very simple queries that are just looking for page
395 * titles that match some criteria. It formats each result item as a link to
396 * that page.
398 * @package MediaWiki
400 class PageQueryPage extends QueryPage {
402 function formatResult( $skin, $result ) {
403 global $wgContLang;
404 $nt = Title::makeTitle( $result->namespace, $result->title );
405 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $nt->getPrefixedText() ) );