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.
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.
17 # Subclasses return an SQL query here.
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.
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
29 # Don't include an ORDER or LIMIT clause, this will be added.
32 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
35 # Override to sort by increasing values
36 function sortDescending() {
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);
48 return " ORDER BY value " .
49 ($this->sortDescending() ?
"DESC" : "");
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;
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.
64 function formatResult( $skin, $result ) {
68 # This is the actual workhorse. It does everything needed to make a
69 # real, honest-to-gosh query page.
71 function doQuery( $offset, $limit ) {
72 global $wgUser, $wgOut, $wgLang, $wgRequest;
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' );
82 $wgOut->setSyndicated( true );
85 if ( $this->isExpensive() ) {
86 $recache = $wgRequest->getBool( "recache" );
88 # Clear out any old cached data
89 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
91 # Do query on the (possibly out of date) slave server
93 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
96 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
98 while ( $row = $dbr->fetchObject( $res ) ) {
105 $dbw->addQuotes( $row->type
) . "," .
106 $dbw->addQuotes( $row->namespace ) . "," .
107 $dbw->addQuotes( $row->title
) . "," .
108 $dbw->addQuotes( $row->value
) . ")";
111 # Save results into the querycache table on the master
112 $dbw->query( $insertSql, $fname );
114 # Set result pointer to allow reading for display
115 $numRows = $dbr->numRows( $res );
116 if ( $numRows <= $offset ) {
119 $dbr->dataSeek( $res, $offset );
120 $num = max( $limit, $numRows - $offset );
123 if( $wgMiserMode ||
$recache ) {
124 $type = $dbr->strencode( $sname );
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'";
130 $wgOut->addWikiText( wfMsg( "perfcached" ) );
133 if ( $res === false ) {
134 $res = $dbr->query( $sql . $this->getOrder() .
135 $dbr->limitResult( $limit,$offset ), $fname );
136 $num = $dbr->numRows($res);
140 $sk = $wgUser->getSkin( );
142 $top = wfShowingResults( $offset, $num);
143 $wgOut->addHTML( "<p>{$top}\n" );
145 # often disable 'next' link when we reach the end
146 if($num < $limit) { $atend = true; } else { $atend = false; }
148 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
149 $wgOut->addHTML( "<br />{$sl}</p>\n" );
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";
159 $dbr->freeResult( $res );
161 $wgOut->addHTML( $s );
162 $wgOut->addHTML( "<p>{$sl}</p>\n" );
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](
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 );
183 $dbr->freeResult( $res );
192 # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
193 function feedResult( $row ) {
194 if( !isset( $row->title
) ) {
197 $title = Title
::MakeTitle( IntVal( $row->namespace ), $row->title
);
199 if( isset( $row->timestamp
) ) {
200 $date = $row->timestamp
;
207 $talkpage = $title->getTalkPage();
208 $comments = $talkpage->getFullURL();
213 $this->feedItemDesc( $row ),
214 $title->getFullURL(),
216 $this->feedItemAuthor( $row ),
223 function feedItemDesc( $row ) {
225 if( isset( $row->comment
) ) {
226 $text = htmlspecialchars( $row->comment
);
231 if( isset( $row->text
) ) {
232 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
233 nl2br( htmlspecialchars( $row->text
) ) . "</div>";;
238 function feedItemAuthor( $row ) {
239 if( isset( $row->user_text
) ) {
240 return $row->user_text
;
246 function feedTitle() {
247 global $wgLanguageCode, $wgSitename, $wgLang;
248 $page = SpecialPage
::getPage( $this->getName() );
249 $desc = $page->getDescription();
250 return "$wgSitename - $desc [$wgLanguageCode]";
253 function feedDesc() {
254 return wfMsg( "fromwikipedia" );
259 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
260 return $title->getFullURL();
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
268 class PageQueryPage
extends QueryPage
{
270 function formatResult( $skin, $result ) {
271 $nt = Title
::makeTitle( $result->namespace, $result->title
);
272 return $skin->makeKnownLinkObj( $nt, "" );