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