fix notice for unset variable when only one item displayed; not sure this code is...
[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 * If the function return "false", the line output will be skipped.
83 */
84 function formatResult( $skin, $result ) {
85 return '';
86 }
87
88 /**
89 * The content returned by this function will be output before any result
90 */
91 function getPageHeader( ) {
92 return '';
93 }
94
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.
100 */
101 function tryLastResult( ) {
102 return false;
103 }
104
105 /**
106 * This is the actual workhorse. It does everything needed to make a
107 * real, honest-to-gosh query page.
108 *
109 * @param $offset database query offset
110 * @param $limit database query limit
111 */
112 function doQuery( $offset, $limit ) {
113 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
114 global $wgMiserMode;
115
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' );
122
123 $wgOut->setSyndicated( $this->isSyndicated() );
124 $res = false;
125
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 );
131
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 );
135
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 .= ',';
144 }
145 $insertSql .= '(' .
146 $dbw->addQuotes( $row->type ) . ',' .
147 $dbw->addQuotes( $row->namespace ) . ',' .
148 $dbw->addQuotes( $row->title ) . ',' .
149 $dbw->addQuotes( $row->value ) . ')';
150 }
151
152 # Save results into the querycache table on the master
153 $dbw->query( $insertSql, $fname );
154
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 );
162 }
163 }
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'";
169 }
170 if( $wgMiserMode ) {
171 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
172 }
173 }
174 if ( $res === false ) {
175 $res = $dbr->query( $sql . $this->getOrder() .
176 $dbr->limitResult( $limit,$offset ), $fname );
177 $num = $dbr->numRows($res);
178 }
179
180
181 $sk = $wgUser->getSkin( );
182
183 $wgOut->addHTML( $this->getPageHeader() );
184
185 $top = wfShowingResults( $offset, $num);
186 $wgOut->addHTML( "<p>{$top}\n" );
187
188 # often disable 'next' link when we reach the end
189 if($num < $limit) { $atend = true; } else { $atend = false; }
190
191 $sl = wfViewPrevNext( $offset, $limit , $wgContLang->specialPage( $sname ), "" ,$atend );
192 $wgOut->addHTML( "<br />{$sl}</p>\n" );
193
194 if ( $num > 0 ) {
195 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
196
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";
204 }
205 }
206
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";
215 }
216 }
217
218 $dbr->freeResult( $res );
219 $s .= '</ol>';
220 $wgOut->addHTML( $s );
221 }
222 $wgOut->addHTML( "<p>{$sl}</p>\n" );
223 }
224
225 /**
226 * Similar to above, but packaging in a syndicated feed instead of a web page
227 */
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();
237
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 );
244 }
245 $dbr->freeResult( $res );
246
247 $feed->outFooter();
248 return true;
249 } else {
250 return false;
251 }
252 }
253
254 /**
255 * Override for custom handling. If the titles/links are ok, just do
256 * feedItemDesc()
257 */
258 function feedResult( $row ) {
259 if( !isset( $row->title ) ) {
260 return NULL;
261 }
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 = '';
268 }
269
270 $comments = '';
271 if( $title ) {
272 $talkpage = $title->getTalkPage();
273 $comments = $talkpage->getFullURL();
274 }
275
276 return new FeedItem(
277 $title->getText(),
278 $this->feedItemDesc( $row ),
279 $title->getFullURL(),
280 $date,
281 $this->feedItemAuthor( $row ),
282 $comments);
283 } else {
284 return NULL;
285 }
286 }
287
288 function feedItemDesc( $row ) {
289 $text = '';
290 if( isset( $row->comment ) ) {
291 $text = htmlspecialchars( $row->comment );
292 } else {
293 $text = '';
294 }
295
296 if( isset( $row->text ) ) {
297 $text = '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' . $text . "</p>\n<hr />\n<div>" .
298 nl2br( htmlspecialchars( $row->text ) ) . "</div>";;
299 }
300 return $text;
301 }
302
303 function feedItemAuthor( $row ) {
304 if( isset( $row->user_text ) ) {
305 return $row->user_text;
306 } else {
307 return '';
308 }
309 }
310
311 function feedTitle() {
312 global $wgLanguageCode, $wgSitename, $wgLang;
313 $page = SpecialPage::getPage( $this->getName() );
314 $desc = $page->getDescription();
315 return "$wgSitename - $desc [$wgLanguageCode]";
316 }
317
318 function feedDesc() {
319 return wfMsg( 'tagline' );
320 }
321
322 function feedUrl() {
323 global $wgLang;
324 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
325 return $title->getFullURL();
326 }
327 }
328
329 /**
330 * This is a subclass for very simple queries that are just looking for page
331 * titles that match some criteria. It formats each result item as a link to
332 * that page.
333 *
334 * @package MediaWiki
335 */
336 class PageQueryPage extends QueryPage {
337
338 function formatResult( $skin, $result ) {
339 global $wgContLang;
340 $nt = Title::makeTitle( $result->namespace, $result->title );
341 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $result->title ) );
342 }
343 }
344
345 ?>