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