Lessons learnt from installing MediaWiki with safe_mode, especially support for uploa...
[mediawiki.git] / includes / QueryPage.php
blob37b48161c847ed112264c1c084facedabb40dc47
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 */
7 /**
9 */
10 require_once ( 'Feed.php' );
12 /**
13 * This is a class for doing query pages; since they're almost all the same,
14 * we factor out some of the functionality into a superclass, and let
15 * subclasses derive from it.
17 * @package MediaWiki
19 class QueryPage {
21 /**
22 * Subclasses return their name here. Make sure the name is also
23 * specified in SpecialPage.php and in Language.php as a language message
24 * param.
26 function getName() {
27 return '';
30 /**
31 * Subclasses return an SQL query here.
33 * Note that the query itself should return the following four columns:
34 * 'type' (your special page's name), 'namespace', 'title', and 'value'
35 * *in that order*. 'value' is used for sorting.
37 * These may be stored in the querycache table for expensive queries,
38 * and that cached data will be returned sometimes, so the presence of
39 * extra fields can't be relied upon. The cached 'value' column will be
40 * an integer; non-numeric values are useful only for sorting the initial
41 * query.
43 * Don't include an ORDER or LIMIT clause, this will be added.
45 function getSQL() {
46 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
49 /**
50 * Override to sort by increasing values
52 function sortDescending() {
53 return true;
56 function getOrder() {
57 return ' ORDER BY value ' .
58 ($this->sortDescending() ? 'DESC' : '');
61 /**
62 * Is this query expensive (for some definition of expensive)? Then we
63 * don't let it run in miser mode. $wgDisableQueryPages causes all query
64 * pages to be declared expensive. Some query pages are always expensive.
66 function isExpensive( ) {
67 global $wgDisableQueryPages;
68 return $wgDisableQueryPages;
71 /**
72 * Sometime we dont want to build rss / atom feeds.
74 function isSyndicated() {
75 return true;
78 /**
79 * Formats the results of the query for display. The skin is the current
80 * skin; you can use it for making links. The result is a single row of
81 * result data. You should be able to grab SQL results off of it.
82 * If the function return "false", the line output will be skipped.
84 function formatResult( $skin, $result ) {
85 return '';
88 /**
89 * The content returned by this function will be output before any result
91 function getPageHeader( ) {
92 return '';
95 /**
96 * Some special pages (for example SpecialListusers) might not return the
97 * current object formatted, but return the previous one instead.
98 * Setting this to return true, will call one more time wfFormatResult to
99 * be sure that the very last result is formatted and shown.
101 function tryLastResult( ) {
102 return false;
106 * This is the actual workhorse. It does everything needed to make a
107 * real, honest-to-gosh query page.
109 * @param $offset database query offset
110 * @param $limit database query limit
112 function doQuery( $offset, $limit ) {
113 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
114 global $wgMiserMode;
116 $sname = $this->getName();
117 $fname = get_class($this) . '::doQuery';
118 $sql = $this->getSQL();
119 $dbr =& wfGetDB( DB_SLAVE );
120 $dbw =& wfGetDB( DB_MASTER );
121 $querycache = $dbr->tableName( 'querycache' );
123 $wgOut->setSyndicated( $this->isSyndicated() );
124 $res = false;
126 if ( $this->isExpensive() ) {
127 $recache = $wgRequest->getBool( 'recache' );
128 if( $recache ) {
129 # Clear out any old cached data
130 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
132 # Do query on the (possibly out of date) slave server
133 $maxstored = 1000;
134 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
136 # Fetch results
137 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
138 $first = true;
139 while ( $row = $dbr->fetchObject( $res ) ) {
140 if ( $first ) {
141 $first = false;
142 } else {
143 $insertSql .= ',';
145 $insertSql .= '(' .
146 $dbw->addQuotes( $row->type ) . ',' .
147 $dbw->addQuotes( $row->namespace ) . ',' .
148 $dbw->addQuotes( $row->title ) . ',' .
149 $dbw->addQuotes( $row->value ) . ')';
152 # Save results into the querycache table on the master
153 $dbw->query( $insertSql, $fname );
155 # Set result pointer to allow reading for display
156 $numRows = $dbr->numRows( $res );
157 if ( $numRows <= $offset ) {
158 $num = 0;
159 } else {
160 $dbr->dataSeek( $res, $offset );
161 $num = max( $limit, $numRows - $offset );
164 if( $wgMiserMode || $recache ) {
165 $type = $dbr->strencode( $sname );
166 $sql =
167 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
168 FROM $querycache WHERE qc_type='$type'";
170 if( $wgMiserMode ) {
171 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
174 if ( $res === false ) {
175 $res = $dbr->query( $sql . $this->getOrder() .
176 $dbr->limitResult( $limit,$offset ), $fname );
177 $num = $dbr->numRows($res);
181 $sk = $wgUser->getSkin( );
183 $wgOut->addHTML( $this->getPageHeader() );
185 $top = wfShowingResults( $offset, $num);
186 $wgOut->addHTML( "<p>{$top}\n" );
188 # often disable 'next' link when we reach the end
189 if($num < $limit) { $atend = true; } else { $atend = false; }
191 $sl = wfViewPrevNext( $offset, $limit , $wgContLang->specialPage( $sname ), "" ,$atend );
192 $wgOut->addHTML( "<br />{$sl}</p>\n" );
194 if ( $num > 0 ) {
195 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
197 # Only read at most $num rows, because $res may contain the whole 1000
198 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
199 $format = $this->formatResult( $sk, $obj );
200 if ( $format ) {
201 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
202 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
203 $s .= "<li{$attr}>{$format}</li>\n";
207 if($this->tryLastResult()) {
208 // flush the very last result
209 $obj = null;
210 $format = $this->formatResult( $sk, $obj );
211 if( $format ) {
212 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
213 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
214 $s .= "<li{$attr}>{$format}</li>\n";
218 $dbr->freeResult( $res );
219 $s .= '</ol>';
220 $wgOut->addHTML( $s );
222 $wgOut->addHTML( "<p>{$sl}</p>\n" );
226 * Similar to above, but packaging in a syndicated feed instead of a web page
228 function doFeed( $class = '' ) {
229 global $wgFeedClasses;
230 global $wgOut, $wgLanguageCode, $wgLang;
231 if( isset($wgFeedClasses[$class]) ) {
232 $feed = new $wgFeedClasses[$class](
233 $this->feedTitle(),
234 $this->feedDesc(),
235 $this->feedUrl() );
236 $feed->outHeader();
238 $dbr =& wfGetDB( DB_SLAVE );
239 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
240 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
241 while( $obj = $dbr->fetchObject( $res ) ) {
242 $item = $this->feedResult( $obj );
243 if( $item ) $feed->outItem( $item );
245 $dbr->freeResult( $res );
247 $feed->outFooter();
248 return true;
249 } else {
250 return false;
255 * Override for custom handling. If the titles/links are ok, just do
256 * feedItemDesc()
258 function feedResult( $row ) {
259 if( !isset( $row->title ) ) {
260 return NULL;
262 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
263 if( $title ) {
264 if( isset( $row->timestamp ) ) {
265 $date = $row->timestamp;
266 } else {
267 $date = '';
270 $comments = '';
271 if( $title ) {
272 $talkpage = $title->getTalkPage();
273 $comments = $talkpage->getFullURL();
276 return new FeedItem(
277 $title->getPrefixedText(),
278 $this->feedItemDesc( $row ),
279 $title->getFullURL(),
280 $date,
281 $this->feedItemAuthor( $row ),
282 $comments);
283 } else {
284 return NULL;
288 function feedItemDesc( $row ) {
289 return isset( $row->comment )
290 ? htmlspecialchars( $row->comment )
291 : '';
294 function feedItemAuthor( $row ) {
295 if( isset( $row->user_text ) ) {
296 return $row->user_text;
297 } else {
298 return '';
302 function feedTitle() {
303 global $wgLanguageCode, $wgSitename, $wgLang;
304 $page = SpecialPage::getPage( $this->getName() );
305 $desc = $page->getDescription();
306 return "$wgSitename - $desc [$wgLanguageCode]";
309 function feedDesc() {
310 return wfMsg( 'tagline' );
313 function feedUrl() {
314 global $wgLang;
315 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
316 return $title->getFullURL();
321 * This is a subclass for very simple queries that are just looking for page
322 * titles that match some criteria. It formats each result item as a link to
323 * that page.
325 * @package MediaWiki
327 class PageQueryPage extends QueryPage {
329 function formatResult( $skin, $result ) {
330 global $wgContLang;
331 $nt = Title::makeTitle( $result->namespace, $result->title );
332 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $result->title ) );