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