change all occurrences of "fromwikipedia" to "tagline".
[lhc/web/wiklou.git] / includes / QueryPage.php
1 <?php
2 /**
3 * Contain a class for special pages
4 */
5
6 /**
7 *
8 */
9 require_once ( 'Feed.php' );
10
11 /**
12 * This is a class for doing query pages; since they're almost all the same,
13 * we factor out some of the functionality into a superclass, and let
14 * subclasses derive from it.
15 *
16 */
17 class QueryPage {
18
19 /**
20 * Subclasses return their name here. Make sure the name is also
21 * specified in SpecialPage.php and in Language.php as a language message
22 * param.
23 */
24 function getName() {
25 return '';
26 }
27
28 /**
29 * Subclasses return an SQL query here.
30 *
31 * Note that the query itself should return the following four columns:
32 * 'type' (your special page's name), 'namespace', 'title', and 'value'
33 * *in that order*. 'value' is used for sorting.
34 *
35 * These may be stored in the querycache table for expensive queries,
36 * and that cached data will be returned sometimes, so the presence of
37 * extra fields can't be relied upon. The cached 'value' column will be
38 * an integer; non-numeric values are useful only for sorting the initial
39 * query.
40 *
41 * Don't include an ORDER or LIMIT clause, this will be added.
42 */
43 function getSQL() {
44 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
45 }
46
47 /**
48 * Override to sort by increasing values
49 */
50 function sortDescending() {
51 return true;
52 }
53
54 function getOrder() {
55 return ' ORDER BY value ' .
56 ($this->sortDescending() ? 'DESC' : '');
57 }
58
59 /**
60 * Is this query expensive (for some definition of expensive)? Then we
61 * don't let it run in miser mode. $wgDisableQueryPages causes all query
62 * pages to be declared expensive. Some query pages are always expensive.
63 */
64 function isExpensive( ) {
65 global $wgDisableQueryPages;
66 return $wgDisableQueryPages;
67 }
68
69 /**
70 * Formats the results of the query for display. The skin is the current
71 * skin; you can use it for making links. The result is a single row of
72 * result data. You should be able to grab SQL results off of it.
73 */
74 function formatResult( $skin, $result ) {
75 return '';
76 }
77
78 /**
79 * The content returned by this function will be output before any result
80 */
81 function getPageHeader( ) {
82 return '';
83 }
84
85 /**
86 * This is the actual workhorse. It does everything needed to make a
87 * real, honest-to-gosh query page.
88 *
89 * @param $offset database query offset
90 * @param $limit database query limit
91 */
92 function doQuery( $offset, $limit ) {
93 global $wgUser, $wgOut, $wgLang, $wgRequest;
94 global $wgMiserMode;
95
96 $sname = $this->getName();
97 $fname = get_class($this) . '::doQuery';
98 $sql = $this->getSQL();
99 $dbr =& wfGetDB( DB_SLAVE );
100 $dbw =& wfGetDB( DB_MASTER );
101 $querycache = $dbr->tableName( 'querycache' );
102
103 $wgOut->setSyndicated( true );
104 $res = false;
105
106 if ( $this->isExpensive() ) {
107 $recache = $wgRequest->getBool( 'recache' );
108 if( $recache ) {
109 # Clear out any old cached data
110 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
111
112 # Do query on the (possibly out of date) slave server
113 $maxstored = 1000;
114 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
115
116 # Fetch results
117 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
118 $first = true;
119 while ( $row = $dbr->fetchObject( $res ) ) {
120 if ( $first ) {
121 $first = false;
122 } else {
123 $insertSql .= ',';
124 }
125 $insertSql .= '(' .
126 $dbw->addQuotes( $row->type ) . ',' .
127 $dbw->addQuotes( $row->namespace ) . ',' .
128 $dbw->addQuotes( $row->title ) . ',' .
129 $dbw->addQuotes( $row->value ) . ')';
130 }
131
132 # Save results into the querycache table on the master
133 $dbw->query( $insertSql, $fname );
134
135 # Set result pointer to allow reading for display
136 $numRows = $dbr->numRows( $res );
137 if ( $numRows <= $offset ) {
138 $num = 0;
139 } else {
140 $dbr->dataSeek( $res, $offset );
141 $num = max( $limit, $numRows - $offset );
142 }
143 }
144 if( $wgMiserMode || $recache ) {
145 $type = $dbr->strencode( $sname );
146 $sql =
147 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
148 FROM $querycache WHERE qc_type='$type'";
149 }
150 if( $wgMiserMode ) {
151 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
152 }
153 }
154 if ( $res === false ) {
155 $res = $dbr->query( $sql . $this->getOrder() .
156 $dbr->limitResult( $limit,$offset ), $fname );
157 $num = $dbr->numRows($res);
158 }
159
160
161 $sk = $wgUser->getSkin( );
162
163 $wgOut->addHTML( $this->getPageHeader() );
164
165 $top = wfShowingResults( $offset, $num);
166 $wgOut->addHTML( "<p>{$top}\n" );
167
168 # often disable 'next' link when we reach the end
169 if($num < $limit) { $atend = true; } else { $atend = false; }
170
171 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
172 $wgOut->addHTML( "<br />{$sl}</p>\n" );
173
174 $s = "<ol start='" . ( $offset + 1 ) . "' class='special'>";
175 # Only read at most $num rows, because $res may contain the whole 1000
176 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++ ) {
177 $format = $this->formatResult( $sk, $obj );
178 $attr = ( isset ( $obj->usepatrol ) && $obj->usepatrol &&
179 $obj->patrolled == 0 ) ? ' class="not_patrolled"' : '';
180 $s .= "<li{$attr}>{$format}</li>\n";
181 }
182 $dbr->freeResult( $res );
183 $s .= '</ol>';
184 $wgOut->addHTML( $s );
185 $wgOut->addHTML( "<p>{$sl}</p>\n" );
186 }
187
188 /**
189 * Similar to above, but packaging in a syndicated feed instead of a web page
190 */
191 function doFeed( $class = '' ) {
192 global $wgFeedClasses;
193 global $wgOut, $wgLanguageCode, $wgLang;
194 if( isset($wgFeedClasses[$class]) ) {
195 $feed = new $wgFeedClasses[$class](
196 $this->feedTitle(),
197 $this->feedDesc(),
198 $this->feedUrl() );
199 $feed->outHeader();
200
201 $dbr =& wfGetDB( DB_SLAVE );
202 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
203 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
204 while( $obj = $dbr->fetchObject( $res ) ) {
205 $item = $this->feedResult( $obj );
206 if( $item ) $feed->outItem( $item );
207 }
208 $dbr->freeResult( $res );
209
210 $feed->outFooter();
211 return true;
212 } else {
213 return false;
214 }
215 }
216
217 /**
218 * Override for custom handling. If the titles/links are ok, just do
219 * feedItemDesc()
220 */
221 function feedResult( $row ) {
222 if( !isset( $row->title ) ) {
223 return NULL;
224 }
225 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
226 if( $title ) {
227 if( isset( $row->timestamp ) ) {
228 $date = $row->timestamp;
229 } else {
230 $date = '';
231 }
232
233 $comments = '';
234 if( $title ) {
235 $talkpage = $title->getTalkPage();
236 $comments = $talkpage->getFullURL();
237 }
238
239 return new FeedItem(
240 $title->getText(),
241 $this->feedItemDesc( $row ),
242 $title->getFullURL(),
243 $date,
244 $this->feedItemAuthor( $row ),
245 $comments);
246 } else {
247 return NULL;
248 }
249 }
250
251 function feedItemDesc( $row ) {
252 $text = '';
253 if( isset( $row->comment ) ) {
254 $text = htmlspecialchars( $row->comment );
255 } else {
256 $text = '';
257 }
258
259 if( isset( $row->text ) ) {
260 $text = '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' . $text . "</p>\n<hr />\n<div>" .
261 nl2br( htmlspecialchars( $row->text ) ) . "</div>";;
262 }
263 return $text;
264 }
265
266 function feedItemAuthor( $row ) {
267 if( isset( $row->user_text ) ) {
268 return $row->user_text;
269 } else {
270 return '';
271 }
272 }
273
274 function feedTitle() {
275 global $wgLanguageCode, $wgSitename, $wgLang;
276 $page = SpecialPage::getPage( $this->getName() );
277 $desc = $page->getDescription();
278 return "$wgSitename - $desc [$wgLanguageCode]";
279 }
280
281 function feedDesc() {
282 return wfMsg( 'tagline' );
283 }
284
285 function feedUrl() {
286 global $wgLang;
287 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
288 return $title->getFullURL();
289 }
290 }
291
292 /**
293 * This is a subclass for very simple queries that are just looking for page
294 * titles that match some criteria. It formats each result item as a link to
295 * that page.
296 */
297 class PageQueryPage extends QueryPage {
298
299 function formatResult( $skin, $result ) {
300 $nt = Title::makeTitle( $result->namespace, $result->title );
301 return $skin->makeKnownLinkObj( $nt, '' );
302 }
303 }
304
305 ?>