Do not output lines for which nothing is formatted (formatResult returning false)
[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 if ( $format ) {
189 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
190 $obj->patrolled == 0 ) ? ' class="not-patrolled"' : '';
191 $s .= "<li{$attr}>{$format}</li>\n";
192 }
193 }
194 $dbr->freeResult( $res );
195 $s .= '</ol>';
196 $wgOut->addHTML( $s );
197 }
198 $wgOut->addHTML( "<p>{$sl}</p>\n" );
199 }
200
201 /**
202 * Similar to above, but packaging in a syndicated feed instead of a web page
203 */
204 function doFeed( $class = '' ) {
205 global $wgFeedClasses;
206 global $wgOut, $wgLanguageCode, $wgLang;
207 if( isset($wgFeedClasses[$class]) ) {
208 $feed = new $wgFeedClasses[$class](
209 $this->feedTitle(),
210 $this->feedDesc(),
211 $this->feedUrl() );
212 $feed->outHeader();
213
214 $dbr =& wfGetDB( DB_SLAVE );
215 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
216 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
217 while( $obj = $dbr->fetchObject( $res ) ) {
218 $item = $this->feedResult( $obj );
219 if( $item ) $feed->outItem( $item );
220 }
221 $dbr->freeResult( $res );
222
223 $feed->outFooter();
224 return true;
225 } else {
226 return false;
227 }
228 }
229
230 /**
231 * Override for custom handling. If the titles/links are ok, just do
232 * feedItemDesc()
233 */
234 function feedResult( $row ) {
235 if( !isset( $row->title ) ) {
236 return NULL;
237 }
238 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
239 if( $title ) {
240 if( isset( $row->timestamp ) ) {
241 $date = $row->timestamp;
242 } else {
243 $date = '';
244 }
245
246 $comments = '';
247 if( $title ) {
248 $talkpage = $title->getTalkPage();
249 $comments = $talkpage->getFullURL();
250 }
251
252 return new FeedItem(
253 $title->getText(),
254 $this->feedItemDesc( $row ),
255 $title->getFullURL(),
256 $date,
257 $this->feedItemAuthor( $row ),
258 $comments);
259 } else {
260 return NULL;
261 }
262 }
263
264 function feedItemDesc( $row ) {
265 $text = '';
266 if( isset( $row->comment ) ) {
267 $text = htmlspecialchars( $row->comment );
268 } else {
269 $text = '';
270 }
271
272 if( isset( $row->text ) ) {
273 $text = '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' . $text . "</p>\n<hr />\n<div>" .
274 nl2br( htmlspecialchars( $row->text ) ) . "</div>";;
275 }
276 return $text;
277 }
278
279 function feedItemAuthor( $row ) {
280 if( isset( $row->user_text ) ) {
281 return $row->user_text;
282 } else {
283 return '';
284 }
285 }
286
287 function feedTitle() {
288 global $wgLanguageCode, $wgSitename, $wgLang;
289 $page = SpecialPage::getPage( $this->getName() );
290 $desc = $page->getDescription();
291 return "$wgSitename - $desc [$wgLanguageCode]";
292 }
293
294 function feedDesc() {
295 return wfMsg( 'tagline' );
296 }
297
298 function feedUrl() {
299 global $wgLang;
300 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
301 return $title->getFullURL();
302 }
303 }
304
305 /**
306 * This is a subclass for very simple queries that are just looking for page
307 * titles that match some criteria. It formats each result item as a link to
308 * that page.
309 *
310 * @package MediaWiki
311 */
312 class PageQueryPage extends QueryPage {
313
314 function formatResult( $skin, $result ) {
315 global $wgContLang;
316 $nt = Title::makeTitle( $result->namespace, $result->title );
317 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $result->title ) );
318 }
319 }
320
321 ?>