oops
[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 $slowDB =& wfGetDB( DB_SLAVE, array( $this->getName(), 'QueryPage-recache', 'vslow' ) );
134 $maxstored = 1000;
135 $res = $slowDB->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
136
137 # Fetch results
138 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
139 $first = true;
140 while ( $row = $dbr->fetchObject( $res ) ) {
141 if ( $first ) {
142 $first = false;
143 } else {
144 $insertSql .= ',';
145 }
146 $insertSql .= '(' .
147 $dbw->addQuotes( $row->type ) . ',' .
148 $dbw->addQuotes( $row->namespace ) . ',' .
149 $dbw->addQuotes( $row->title ) . ',' .
150 $dbw->addQuotes( $row->value ) . ')';
151 }
152
153 # Save results into the querycache table on the master
154 $dbw->query( $insertSql, $fname );
155
156 # Set result pointer to allow reading for display
157 $numRows = $dbr->numRows( $res );
158 if ( $numRows <= $offset ) {
159 $num = 0;
160 } else {
161 $dbr->dataSeek( $res, $offset );
162 $num = max( $limit, $numRows - $offset );
163 }
164 }
165 if( $wgMiserMode || $recache ) {
166 $type = $dbr->strencode( $sname );
167 $sql =
168 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
169 FROM $querycache WHERE qc_type='$type'";
170 }
171 if( $wgMiserMode ) {
172 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
173 }
174 }
175 if ( $res === false ) {
176 $res = $dbr->query( $sql . $this->getOrder() .
177 $dbr->limitResult( $limit,$offset ), $fname );
178 $num = $dbr->numRows($res);
179 }
180
181
182 $sk = $wgUser->getSkin( );
183
184 $wgOut->addHTML( $this->getPageHeader() );
185
186 $top = wfShowingResults( $offset, $num);
187 $wgOut->addHTML( "<p>{$top}\n" );
188
189 # often disable 'next' link when we reach the end
190 if($num < $limit) { $atend = true; } else { $atend = false; }
191
192 $sl = wfViewPrevNext( $offset, $limit , $wgContLang->specialPage( $sname ), "" ,$atend );
193 $wgOut->addHTML( "<br />{$sl}</p>\n" );
194
195 if ( $num > 0 ) {
196 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
197
198 # Only read at most $num rows, because $res may contain the whole 1000
199 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
200 $format = $this->formatResult( $sk, $obj );
201 if ( $format ) {
202 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
203 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
204 $s .= "<li{$attr}>{$format}</li>\n";
205 }
206 }
207
208 if($this->tryLastResult()) {
209 // flush the very last result
210 $obj = null;
211 $format = $this->formatResult( $sk, $obj );
212 if( $format ) {
213 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
214 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
215 $s .= "<li{$attr}>{$format}</li>\n";
216 }
217 }
218
219 $dbr->freeResult( $res );
220 $s .= '</ol>';
221 $wgOut->addHTML( $s );
222 }
223 $wgOut->addHTML( "<p>{$sl}</p>\n" );
224 }
225
226 /**
227 * Similar to above, but packaging in a syndicated feed instead of a web page
228 */
229 function doFeed( $class = '' ) {
230 global $wgFeedClasses;
231 global $wgOut, $wgLanguageCode, $wgLang;
232 if( isset($wgFeedClasses[$class]) ) {
233 $feed = new $wgFeedClasses[$class](
234 $this->feedTitle(),
235 $this->feedDesc(),
236 $this->feedUrl() );
237 $feed->outHeader();
238
239 $dbr =& wfGetDB( DB_SLAVE );
240 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
241 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
242 while( $obj = $dbr->fetchObject( $res ) ) {
243 $item = $this->feedResult( $obj );
244 if( $item ) $feed->outItem( $item );
245 }
246 $dbr->freeResult( $res );
247
248 $feed->outFooter();
249 return true;
250 } else {
251 return false;
252 }
253 }
254
255 /**
256 * Override for custom handling. If the titles/links are ok, just do
257 * feedItemDesc()
258 */
259 function feedResult( $row ) {
260 if( !isset( $row->title ) ) {
261 return NULL;
262 }
263 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
264 if( $title ) {
265 if( isset( $row->timestamp ) ) {
266 $date = $row->timestamp;
267 } else {
268 $date = '';
269 }
270
271 $comments = '';
272 if( $title ) {
273 $talkpage = $title->getTalkPage();
274 $comments = $talkpage->getFullURL();
275 }
276
277 return new FeedItem(
278 $title->getPrefixedText(),
279 $this->feedItemDesc( $row ),
280 $title->getFullURL(),
281 $date,
282 $this->feedItemAuthor( $row ),
283 $comments);
284 } else {
285 return NULL;
286 }
287 }
288
289 function feedItemDesc( $row ) {
290 return isset( $row->comment )
291 ? htmlspecialchars( $row->comment )
292 : '';
293 }
294
295 function feedItemAuthor( $row ) {
296 if( isset( $row->user_text ) ) {
297 return $row->user_text;
298 } else {
299 return '';
300 }
301 }
302
303 function feedTitle() {
304 global $wgLanguageCode, $wgSitename, $wgLang;
305 $page = SpecialPage::getPage( $this->getName() );
306 $desc = $page->getDescription();
307 return "$wgSitename - $desc [$wgLanguageCode]";
308 }
309
310 function feedDesc() {
311 return wfMsg( 'tagline' );
312 }
313
314 function feedUrl() {
315 global $wgLang;
316 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
317 return $title->getFullURL();
318 }
319 }
320
321 /**
322 * This is a subclass for very simple queries that are just looking for page
323 * titles that match some criteria. It formats each result item as a link to
324 * that page.
325 *
326 * @package MediaWiki
327 */
328 class PageQueryPage extends QueryPage {
329
330 function formatResult( $skin, $result ) {
331 global $wgContLang;
332 $nt = Title::makeTitle( $result->namespace, $result->title );
333 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $result->title ) );
334 }
335 }
336
337 ?>