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