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 # The content returned by this function will be output before any result
70 function getPageHeader( ) {
74 # This is the actual workhorse. It does everything needed to make a
75 # real, honest-to-gosh query page.
77 function doQuery( $offset, $limit ) {
78 global $wgUser, $wgOut, $wgLang, $wgRequest;
81 $sname = $this->getName();
82 $fname = get_class($this) . '::doQuery';
83 $sql = $this->getSQL();
84 $dbr =& wfGetDB( DB_SLAVE
);
85 $dbw =& wfGetDB( DB_MASTER
);
86 $querycache = $dbr->tableName( 'querycache' );
88 $wgOut->setSyndicated( true );
91 if ( $this->isExpensive() ) {
92 $recache = $wgRequest->getBool( 'recache' );
94 # Clear out any old cached data
95 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
97 # Do query on the (possibly out of date) slave server
99 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
102 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
104 while ( $row = $dbr->fetchObject( $res ) ) {
111 $dbw->addQuotes( $row->type
) . ',' .
112 $dbw->addQuotes( $row->namespace ) . ',' .
113 $dbw->addQuotes( $row->title
) . ',' .
114 $dbw->addQuotes( $row->value
) . ')';
117 # Save results into the querycache table on the master
118 $dbw->query( $insertSql, $fname );
120 # Set result pointer to allow reading for display
121 $numRows = $dbr->numRows( $res );
122 if ( $numRows <= $offset ) {
125 $dbr->dataSeek( $res, $offset );
126 $num = max( $limit, $numRows - $offset );
129 if( $wgMiserMode ||
$recache ) {
130 $type = $dbr->strencode( $sname );
132 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
133 FROM $querycache WHERE qc_type='$type'";
136 $wgOut->addWikiText( wfMsg( "perfcached" ) );
139 if ( $res === false ) {
140 $res = $dbr->query( $sql . $this->getOrder() .
141 $dbr->limitResult( $limit,$offset ), $fname );
142 $num = $dbr->numRows($res);
146 $sk = $wgUser->getSkin( );
148 $wgOut->addHTML( $this->getPageHeader() );
150 $top = wfShowingResults( $offset, $num);
151 $wgOut->addHTML( "<p>{$top}\n" );
153 # often disable 'next' link when we reach the end
154 if($num < $limit) { $atend = true; } else { $atend = false; }
156 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
157 $wgOut->addHTML( "<br />{$sl}</p>\n" );
159 $s = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
160 # Only read at most $num rows, because $res may contain the whole 1000
161 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
162 $format = $this->formatResult( $sk, $obj );
163 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
164 $obj->patrolled
== 0 ) ?
' class="not_patrolled"' : '';
165 $s .= "<li{$attr}>{$format}</li>\n";
167 $dbr->freeResult( $res );
169 $wgOut->addHTML( $s );
170 $wgOut->addHTML( "<p>{$sl}</p>\n" );
173 # Similar to above, but packaging in a syndicated feed instead of a web page
174 function doFeed( $class = '' ) {
175 global $wgFeedClasses;
176 global $wgOut, $wgLanguageCode, $wgLang;
177 if( isset($wgFeedClasses[$class]) ) {
178 $feed = new $wgFeedClasses[$class](
184 $dbr =& wfGetDB( DB_SLAVE
);
185 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
186 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
187 while( $obj = $dbr->fetchObject( $res ) ) {
188 $item = $this->feedResult( $obj );
189 if( $item ) $feed->outItem( $item );
191 $dbr->freeResult( $res );
200 # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
201 function feedResult( $row ) {
202 if( !isset( $row->title
) ) {
205 $title = Title
::MakeTitle( IntVal( $row->namespace ), $row->title
);
207 if( isset( $row->timestamp
) ) {
208 $date = $row->timestamp
;
215 $talkpage = $title->getTalkPage();
216 $comments = $talkpage->getFullURL();
221 $this->feedItemDesc( $row ),
222 $title->getFullURL(),
224 $this->feedItemAuthor( $row ),
231 function feedItemDesc( $row ) {
233 if( isset( $row->comment
) ) {
234 $text = htmlspecialchars( $row->comment
);
239 if( isset( $row->text
) ) {
240 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
241 nl2br( htmlspecialchars( $row->text
) ) . "</div>";;
246 function feedItemAuthor( $row ) {
247 if( isset( $row->user_text
) ) {
248 return $row->user_text
;
254 function feedTitle() {
255 global $wgLanguageCode, $wgSitename, $wgLang;
256 $page = SpecialPage
::getPage( $this->getName() );
257 $desc = $page->getDescription();
258 return "$wgSitename - $desc [$wgLanguageCode]";
261 function feedDesc() {
262 return wfMsg( "fromwikipedia" );
267 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
268 return $title->getFullURL();
272 # This is a subclass for very simple queries that are just looking for page
273 # titles that match some criteria. It formats each result item as a link to
276 class PageQueryPage
extends QueryPage
{
278 function formatResult( $skin, $result ) {
279 $nt = Title
::makeTitle( $result->namespace, $result->title
);
280 return $skin->makeKnownLinkObj( $nt, "" );