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