Fix undeclared variable usage introduced in last update
[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 * 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.
16 *
17 * @package MediaWiki
18 */
19 class QueryPage {
20
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.
25 */
26 function getName() {
27 return '';
28 }
29
30 /**
31 * Subclasses return an SQL query here.
32 *
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.
36 *
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.
42 *
43 * Don't include an ORDER or LIMIT clause, this will be added.
44 */
45 function getSQL() {
46 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
47 }
48
49 /**
50 * Override to sort by increasing values
51 */
52 function sortDescending() {
53 return true;
54 }
55
56 function getOrder() {
57 return ' ORDER BY value ' .
58 ($this->sortDescending() ? 'DESC' : '');
59 }
60
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.
65 */
66 function isExpensive( ) {
67 global $wgDisableQueryPages;
68 return $wgDisableQueryPages;
69 }
70
71 /**
72 * Sometime we dont want to build rss / atom feeds.
73 */
74 function isSyndicated() {
75 return true;
76 }
77
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 */
83 function formatResult( $skin, $result ) {
84 return '';
85 }
86
87 /**
88 * The content returned by this function will be output before any result
89 */
90 function getPageHeader( ) {
91 return '';
92 }
93
94 /**
95 * This is the actual workhorse. It does everything needed to make a
96 * real, honest-to-gosh query page.
97 *
98 * @param $offset database query offset
99 * @param $limit database query limit
100 */
101 function doQuery( $offset, $limit ) {
102 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
103 global $wgMiserMode;
104
105 $sname = $this->getName();
106 $fname = get_class($this) . '::doQuery';
107 $sql = $this->getSQL();
108 $dbr =& wfGetDB( DB_SLAVE );
109 $dbw =& wfGetDB( DB_MASTER );
110 $querycache = $dbr->tableName( 'querycache' );
111
112 $wgOut->setSyndicated( $this->isSyndicated() );
113 $res = false;
114
115 if ( $this->isExpensive() ) {
116 $recache = $wgRequest->getBool( 'recache' );
117 if( $recache ) {
118 # Clear out any old cached data
119 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
120
121 # Do query on the (possibly out of date) slave server
122 $maxstored = 1000;
123 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
124
125 # Fetch results
126 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
127 $first = true;
128 while ( $row = $dbr->fetchObject( $res ) ) {
129 if ( $first ) {
130 $first = false;
131 } else {
132 $insertSql .= ',';
133 }
134 $insertSql .= '(' .
135 $dbw->addQuotes( $row->type ) . ',' .
136 $dbw->addQuotes( $row->namespace ) . ',' .
137 $dbw->addQuotes( $row->title ) . ',' .
138 $dbw->addQuotes( $row->value ) . ')';
139 }
140
141 # Save results into the querycache table on the master
142 $dbw->query( $insertSql, $fname );
143
144 # Set result pointer to allow reading for display
145 $numRows = $dbr->numRows( $res );
146 if ( $numRows <= $offset ) {
147 $num = 0;
148 } else {
149 $dbr->dataSeek( $res, $offset );
150 $num = max( $limit, $numRows - $offset );
151 }
152 }
153 if( $wgMiserMode || $recache ) {
154 $type = $dbr->strencode( $sname );
155 $sql =
156 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
157 FROM $querycache WHERE qc_type='$type'";
158 }
159 if( $wgMiserMode ) {
160 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
161 }
162 }
163 if ( $res === false ) {
164 $res = $dbr->query( $sql . $this->getOrder() .
165 $dbr->limitResult( $limit,$offset ), $fname );
166 $num = $dbr->numRows($res);
167 }
168
169
170 $sk = $wgUser->getSkin( );
171
172 $wgOut->addHTML( $this->getPageHeader() );
173
174 $top = wfShowingResults( $offset, $num);
175 $wgOut->addHTML( "<p>{$top}\n" );
176
177 # often disable 'next' link when we reach the end
178 if($num < $limit) { $atend = true; } else { $atend = false; }
179
180 $sl = wfViewPrevNext( $offset, $limit , $wgContLang->specialPage( $sname ), "" ,$atend );
181 $wgOut->addHTML( "<br />{$sl}</p>\n" );
182
183 if ( $num > 0 ) {
184 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
185 # Only read at most $num rows, because $res may contain the whole 1000
186 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
187 $format = $this->formatResult( $sk, $obj );
188 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
189 $obj->patrolled == 0 ) ? ' class="not_patrolled"' : '';
190 $s .= "<li{$attr}>{$format}</li>\n";
191 }
192 $dbr->freeResult( $res );
193 $s .= '</ol>';
194 $wgOut->addHTML( $s );
195 }
196 $wgOut->addHTML( "<p>{$sl}</p>\n" );
197 }
198
199 /**
200 * Similar to above, but packaging in a syndicated feed instead of a web page
201 */
202 function doFeed( $class = '' ) {
203 global $wgFeedClasses;
204 global $wgOut, $wgLanguageCode, $wgLang;
205 if( isset($wgFeedClasses[$class]) ) {
206 $feed = new $wgFeedClasses[$class](
207 $this->feedTitle(),
208 $this->feedDesc(),
209 $this->feedUrl() );
210 $feed->outHeader();
211
212 $dbr =& wfGetDB( DB_SLAVE );
213 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
214 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
215 while( $obj = $dbr->fetchObject( $res ) ) {
216 $item = $this->feedResult( $obj );
217 if( $item ) $feed->outItem( $item );
218 }
219 $dbr->freeResult( $res );
220
221 $feed->outFooter();
222 return true;
223 } else {
224 return false;
225 }
226 }
227
228 /**
229 * Override for custom handling. If the titles/links are ok, just do
230 * feedItemDesc()
231 */
232 function feedResult( $row ) {
233 if( !isset( $row->title ) ) {
234 return NULL;
235 }
236 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
237 if( $title ) {
238 if( isset( $row->timestamp ) ) {
239 $date = $row->timestamp;
240 } else {
241 $date = '';
242 }
243
244 $comments = '';
245 if( $title ) {
246 $talkpage = $title->getTalkPage();
247 $comments = $talkpage->getFullURL();
248 }
249
250 return new FeedItem(
251 $title->getText(),
252 $this->feedItemDesc( $row ),
253 $title->getFullURL(),
254 $date,
255 $this->feedItemAuthor( $row ),
256 $comments);
257 } else {
258 return NULL;
259 }
260 }
261
262 function feedItemDesc( $row ) {
263 $text = '';
264 if( isset( $row->comment ) ) {
265 $text = htmlspecialchars( $row->comment );
266 } else {
267 $text = '';
268 }
269
270 if( isset( $row->text ) ) {
271 $text = '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' . $text . "</p>\n<hr />\n<div>" .
272 nl2br( htmlspecialchars( $row->text ) ) . "</div>";;
273 }
274 return $text;
275 }
276
277 function feedItemAuthor( $row ) {
278 if( isset( $row->user_text ) ) {
279 return $row->user_text;
280 } else {
281 return '';
282 }
283 }
284
285 function feedTitle() {
286 global $wgLanguageCode, $wgSitename, $wgLang;
287 $page = SpecialPage::getPage( $this->getName() );
288 $desc = $page->getDescription();
289 return "$wgSitename - $desc [$wgLanguageCode]";
290 }
291
292 function feedDesc() {
293 return wfMsg( 'tagline' );
294 }
295
296 function feedUrl() {
297 global $wgLang;
298 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
299 return $title->getFullURL();
300 }
301 }
302
303 /**
304 * This is a subclass for very simple queries that are just looking for page
305 * titles that match some criteria. It formats each result item as a link to
306 * that page.
307 *
308 * @package MediaWiki
309 */
310 class PageQueryPage extends QueryPage {
311
312 function formatResult( $skin, $result ) {
313 global $wgContLang;
314 $nt = Title::makeTitle( $result->namespace, $result->title );
315 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $result->title ) );
316 }
317 }
318
319 ?>