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. These may be stored in
22 # the querycache table for expensive queries, and that cached data will
23 # be returned sometimes, so the presence of extra fields can't be
26 # Don't include an ORDER or LIMIT clause, this will be added.
29 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
32 # Override to sort by increasing values
33 function sortDescending() {
37 # Don't override this unless you're darn sure.
38 function getOrderLimit( $offset, $limit ) {
39 return " ORDER BY value " .
40 ($this->sortDescending() ?
"DESC" : "")
41 . wfLimitResult($limit,$offset);
45 return " ORDER BY value " .
46 ($this->sortDescending() ?
"DESC" : "");
49 # Is this query expensive (for some definition of expensive)? Then we
50 # don't let it run in miser mode. $wgDisableQueryPages causes all query
51 # pages to be declared expensive. Some query pages are always expensive.
52 function isExpensive( ) {
53 global $wgDisableQueryPages;
54 return $wgDisableQueryPages;
57 # Formats the results of the query for display. The skin is the current
58 # skin; you can use it for making links. The result is a single row of
59 # result data. You should be able to grab SQL results off of it.
61 function formatResult( $skin, $result ) {
65 # This is the actual workhorse. It does everything needed to make a
66 # real, honest-to-gosh query page.
68 function doQuery( $offset, $limit ) {
69 global $wgUser, $wgOut, $wgLang, $wgRequest;
72 $sname = $this->getName();
73 $fname = get_class($this) . "::doQuery";
74 $sql = $this->getSQL();
75 $dbr =& wfGetDB( DB_SLAVE
);
76 $dbw =& wfGetDB( DB_MASTER
);
77 $querycache = $dbr->tableName( 'querycache' );
79 $wgOut->setSyndicated( true );
82 if ( $this->isExpensive() ) {
83 $recache = $wgRequest->getBool( "recache" );
85 # Clear out any old cached data
86 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
88 # Do query on the (possibly out of date) slave server
90 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
93 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
95 while ( $row = $dbr->fetchObject( $res ) ) {
102 $dbw->addQuotes( $row->type
) . "," .
103 $dbw->addQuotes( $row->namespace ) . "," .
104 $dbw->addQuotes( $row->title
) . "," .
105 $dbw->addQuotes( $row->value
) . ")";
108 # Save results into the querycache table on the master
109 $dbw->query( $insertSql, $fname );
111 # Set result pointer to allow reading for display
112 $numRows = $dbr->numRows( $res );
113 if ( $numRows <= $offset ) {
116 $dbr->dataSeek( $res, $offset );
117 $num = max( $limit, $numRows - $offset );
120 if( $wgMiserMode ||
$recache ) {
121 $type = $dbr->strencode( $sname );
123 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
124 FROM $querycache WHERE qc_type='$type'";
127 $wgOut->addWikiText( wfMsg( "perfcached" ) );
130 if ( $res === false ) {
131 $res = $dbr->query( $sql . $this->getOrder() .
132 $dbr->limitResult( $limit,$offset ), $fname );
133 $num = $dbr->numRows($res);
137 $sk = $wgUser->getSkin( );
139 $top = wfShowingResults( $offset, $num);
140 $wgOut->addHTML( "<p>{$top}\n" );
142 # often disable 'next' link when we reach the end
143 if($num < $limit) { $atend = true; } else { $atend = false; }
145 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
146 $wgOut->addHTML( "<br />{$sl}</p>\n" );
148 $s = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
149 # Only read at most $num rows, because $res may contain the whole 1000
150 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
151 $format = $this->formatResult( $sk, $obj );
152 $attr = ( !is_null ( $obj->usepatrol
) && $obj->usepatrol
&&
153 $obj->patrolled
== 0 ) ?
' class="not_patrolled"' : '';
154 $s .= "<li{$attr}>{$format}</li>\n";
156 $dbr->freeResult( $res );
158 $wgOut->addHTML( $s );
159 $wgOut->addHTML( "<p>{$sl}</p>\n" );
162 # Similar to above, but packaging in a syndicated feed instead of a web page
163 function doFeed( $class = "" ) {
164 global $wgFeedClasses;
165 global $wgOut, $wgLanguageCode, $wgLang;
166 if( isset($wgFeedClasses[$class]) ) {
167 $feed = new $wgFeedClasses[$class](
173 $dbr =& wfGetDB( DB_SLAVE
);
174 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
175 $res = $dbr->query( $sql, "QueryPage::doFeed" );
176 while( $obj = $dbr->fetchObject( $res ) ) {
177 $item = $this->feedResult( $obj );
178 if( $item ) $feed->outItem( $item );
180 $dbr->freeResult( $res );
189 # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
190 function feedResult( $row ) {
191 if( !isset( $row->title
) ) {
194 $title = Title
::MakeTitle( IntVal( $row->namespace ), $row->title
);
196 if( isset( $row->timestamp
) ) {
197 $date = $row->timestamp
;
204 $talkpage = $title->getTalkPage();
205 $comments = $talkpage->getFullURL();
210 $this->feedItemDesc( $row ),
211 $title->getFullURL(),
213 $this->feedItemAuthor( $row ),
220 function feedItemDesc( $row ) {
222 if( isset( $row->comment
) ) {
223 $text = htmlspecialchars( $row->comment
);
228 if( isset( $row->text
) ) {
229 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
230 nl2br( htmlspecialchars( $row->text
) ) . "</div>";;
235 function feedItemAuthor( $row ) {
236 if( isset( $row->user_text
) ) {
237 return $row->user_text
;
243 function feedTitle() {
244 global $wgLanguageCode, $wgSitename, $wgLang;
245 $page = SpecialPage
::getPage( $this->getName() );
246 $desc = $page->getDescription();
247 return "$wgSitename - $desc [$wgLanguageCode]";
250 function feedDesc() {
251 return wfMsg( "fromwikipedia" );
256 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
257 return $title->getFullURL();
261 # This is a subclass for very simple queries that are just looking for page
262 # titles that match some criteria. It formats each result item as a link to
265 class PageQueryPage
extends QueryPage
{
267 function formatResult( $skin, $result ) {
268 $nt = Title
::makeTitle( $result->namespace, $result->title
);
269 return $skin->makeKnownLinkObj( $nt, "" );