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