accidentially removed the test for
[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 . " LIMIT {$offset}, {$limit}";
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( $offset, $limit );
69
70 $wgOut->setSyndicated( true );
71
72 if ( $this->isExpensive() ) {
73 $type = wfStrencode( $sname );
74 $recache = $wgRequest->getBool( "recache" );
75 if( $recache ) {
76 # Clear out any old cached data
77 $res = wfQuery( "DELETE FROM querycache WHERE qc_type='$type'", DB_WRITE, $fname );
78
79 # Save results into the querycache table
80 $maxstored = 1000;
81 $res = wfQuery(
82 "INSERT INTO querycache(qc_type,qc_namespace,qc_title,qc_value) " .
83 $this->getSQL() .
84 $this->getOrderLimit( 0, $maxstored ),
85 DB_WRITE, $fname );
86 }
87 if( $wgMiserMode || $recache ) {
88 $sql =
89 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
90 FROM querycache WHERE qc_type='$type'";
91 }
92 if( $wgMiserMode ) {
93 $wgOut->addWikiText( wfMsg( "perfcached" ) );
94 }
95 }
96
97 $res = wfQuery( $sql . $this->getOrderLimit( $offset, $limit ), DB_READ, $fname );
98
99 $num = wfNumRows($res);
100
101 $sk = $wgUser->getSkin( );
102
103 $top = wfShowingResults( $offset, $num);
104 $wgOut->addHTML( "<p>{$top}\n" );
105
106 # often disable 'next' link when we reach the end
107 if($num < $limit) { $atend = true; } else { $atend = false; }
108
109 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
110 $wgOut->addHTML( "<br />{$sl}</p>\n" );
111
112 $s = "<ol start='" . ( $offset + 1 ) . "'>";
113 while ( $obj = wfFetchObject( $res ) ) {
114 $format = $this->formatResult( $sk, $obj );
115 $s .= "<li>{$format}</li>\n";
116 }
117 wfFreeResult( $res );
118 $s .= "</ol>";
119 $wgOut->addHTML( $s );
120 $wgOut->addHTML( "<p>{$sl}</p>\n" );
121 }
122
123 # Similar to above, but packaging in a syndicated feed instead of a web page
124 function doFeed( $class = "" ) {
125 global $wgFeedClasses;
126 global $wgOut, $wgLanguageCode, $wgLang;
127 if( isset($wgFeedClasses[$class]) ) {
128 $feed = new $wgFeedClasses[$class](
129 $this->feedTitle(),
130 $this->feedDesc(),
131 $this->feedUrl() );
132 $feed->outHeader();
133
134 $sql = $this->getSQL( 0, 50 );
135 $res = wfQuery( $sql, DB_READ, "QueryPage::doFeed" );
136 while( $obj = wfFetchObject( $res ) ) {
137 $item = $this->feedResult( $obj );
138 if( $item ) $feed->outItem( $item );
139 }
140 wfFreeResult( $res );
141
142 $feed->outFooter();
143 return true;
144 } else {
145 return false;
146 }
147 }
148
149 # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
150 function feedResult( $row ) {
151 if( !isset( $row->title ) ) {
152 return NULL;
153 }
154 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title );
155 if( $title ) {
156 if( isset( $row->timestamp ) ) {
157 $date = $row->timestamp;
158 } else {
159 $date = "";
160 }
161
162 $comments = "";
163 if( $title ) {
164 $talkpage = $title->getTalkPage();
165 $comments = $talkpage->getFullURL();
166 }
167
168 return new FeedItem(
169 $title->getText(),
170 $this->feedItemDesc( $row ),
171 $title->getFullURL(),
172 $date,
173 $this->feedItemAuthor( $row ),
174 $comments);
175 } else {
176 return NULL;
177 }
178 }
179
180 function feedItemDesc( $row ) {
181 $text = "";
182 if( isset( $row->comment ) ) {
183 $text = htmlspecialchars( $row->comment );
184 } else {
185 $text = "";
186 }
187
188 if( isset( $row->text ) ) {
189 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
190 nl2br( htmlspecialchars( $row->text ) ) . "</div>";;
191 }
192 return $text;
193 }
194
195 function feedItemAuthor( $row ) {
196 if( isset( $row->user_text ) ) {
197 return $row->user_text;
198 } else {
199 return "";
200 }
201 }
202
203 function feedTitle() {
204 global $wgLanguageCode, $wgSitename, $wgLang;
205 $page = SpecialPage::getPage( $this->getName() );
206 $desc = $page->getDescription();
207 return "$wgSitename - $desc [$wgLanguageCode]";
208 }
209
210 function feedDesc() {
211 return wfMsg( "fromwikipedia" );
212 }
213
214 function feedUrl() {
215 global $wgLang;
216 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
217 return $title->getFullURL();
218 }
219 }
220
221 # This is a subclass for very simple queries that are just looking for page
222 # titles that match some criteria. It formats each result item as a link to
223 # that page.
224
225 class PageQueryPage extends QueryPage {
226
227 function formatResult( $skin, $result ) {
228 $nt = Title::makeTitle( $result->namespace, $result->title );
229 return $skin->makeKnownLinkObj( $nt, "" );
230 }
231 }
232
233 ?>