Migrated as a QueryPage extension
[mediawiki.git] / includes / QueryPage.php
blob43804cee26e80395c431ea65aa0b44407094ef8b
1 <?php
3 require_once ( "Feed.php" );
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.
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.
13 function getName() {
14 return "";
17 # Subclasses return an SQL query here.
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.
25 # Don't include an ORDER or LIMIT clause, this will be added.
27 function getSQL() {
28 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
31 # Override to sort by increasing values
32 function sortDescending() {
33 return true;
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);
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;
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.
55 function formatResult( $skin, $result ) {
56 return "";
59 # This is the actual workhorse. It does everything needed to make a
60 # real, honest-to-gosh query page.
62 function doQuery( $offset, $limit ) {
63 global $wgUser, $wgOut, $wgLang, $wgRequest;
64 global $wgMiserMode;
66 $sname = $this->getName();
67 $fname = get_class($this) . "::doQuery";
68 $sql = $this->getSQL( $offset, $limit );
70 $wgOut->setSyndicated( true );
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 );
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 );
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'";
92 if( $wgMiserMode ) {
93 $wgOut->addWikiText( wfMsg( "perfcached" ) );
97 $res = wfQuery( $sql . $this->getOrderLimit( $offset, $limit ), DB_READ, $fname );
99 $num = wfNumRows($res);
101 $sk = $wgUser->getSkin( );
103 $top = wfShowingResults( $offset, $num);
104 $wgOut->addHTML( "<p>{$top}\n" );
106 # often disable 'next' link when we reach the end
107 if($num < $limit) { $atend = true; } else { $atend = false; }
109 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
110 $wgOut->addHTML( "<br />{$sl}</p>\n" );
112 $s = "<ol start='" . ( $offset + 1 ) . "'>";
113 while ( $obj = wfFetchObject( $res ) ) {
114 $format = $this->formatResult( $sk, $obj );
115 $s .= "<li>{$format}</li>\n";
117 wfFreeResult( $res );
118 $s .= "</ol>";
119 $wgOut->addHTML( $s );
120 $wgOut->addHTML( "<p>{$sl}</p>\n" );
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();
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 );
140 wfFreeResult( $res );
142 $feed->outFooter();
143 return true;
144 } else {
145 return false;
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;
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 = "";
162 $comments = "";
163 if( $title ) {
164 $talkpage = $title->getTalkPage();
165 $comments = $talkpage->getFullURL();
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;
180 function feedItemDesc( $row ) {
181 $text = "";
182 if( isset( $row->comment ) ) {
183 $text = htmlspecialchars( $row->comment );
184 } else {
185 $text = "";
188 if( isset( $row->text ) ) {
189 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
190 nl2br( htmlspecialchars( $row->text ) ) . "</div>";;
192 return $text;
195 function feedItemAuthor( $row ) {
196 if( isset( $row->user_text ) ) {
197 return $row->user_text;
198 } else {
199 return "";
203 function feedTitle() {
204 global $wgLanguageCode, $wgSitename, $wgLang;
205 $page = SpecialPage::getPage( $this->getName() );
206 $desc = $page->getDescription();
207 return "$wgSitename - $desc [$wgLanguageCode]";
210 function feedDesc() {
211 return wfMsg( "fromwikipedia" );
214 function feedUrl() {
215 global $wgLang;
216 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
217 return $title->getFullURL();
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.
225 class PageQueryPage extends QueryPage {
227 function formatResult( $skin, $result ) {
228 $nt = Title::makeTitle( $result->namespace, $result->title );
229 return $skin->makeKnownLinkObj( $nt, "" );