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