3 * Contain a class for special pages
10 require_once ( 'Feed.php' );
13 * This is a class for doing query pages; since they're almost all the same,
14 * we factor out some of the functionality into a superclass, and let
15 * subclasses derive from it.
22 * Subclasses return their name here. Make sure the name is also
23 * specified in SpecialPage.php and in Language.php as a language message
31 * Subclasses return an SQL query here.
33 * Note that the query itself should return the following four columns:
34 * 'type' (your special page's name), 'namespace', 'title', and 'value'
35 * *in that order*. 'value' is used for sorting.
37 * These may be stored in the querycache table for expensive queries,
38 * and that cached data will be returned sometimes, so the presence of
39 * extra fields can't be relied upon. The cached 'value' column will be
40 * an integer; non-numeric values are useful only for sorting the initial
43 * Don't include an ORDER or LIMIT clause, this will be added.
46 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
50 * Override to sort by increasing values
52 function sortDescending() {
57 return ' ORDER BY value ' .
58 ($this->sortDescending() ?
'DESC' : '');
62 * Is this query expensive (for some definition of expensive)? Then we
63 * don't let it run in miser mode. $wgDisableQueryPages causes all query
64 * pages to be declared expensive. Some query pages are always expensive.
66 function isExpensive( ) {
67 global $wgDisableQueryPages;
68 return $wgDisableQueryPages;
72 * Sometime we dont want to build rss / atom feeds.
74 function isSyndicated() {
79 * Formats the results of the query for display. The skin is the current
80 * skin; you can use it for making links. The result is a single row of
81 * result data. You should be able to grab SQL results off of it.
82 * If the function return "false", the line output will be skipped.
84 function formatResult( $skin, $result ) {
89 * The content returned by this function will be output before any result
91 function getPageHeader( ) {
96 * Some special pages (for example SpecialListusers) might not return the
97 * current object formatted, but return the previous one instead.
98 * Setting this to return true, will call one more time wfFormatResult to
99 * be sure that the very last result is formatted and shown.
101 function tryLastResult( ) {
106 * This is the actual workhorse. It does everything needed to make a
107 * real, honest-to-gosh query page.
109 * @param $offset database query offset
110 * @param $limit database query limit
112 function doQuery( $offset, $limit ) {
113 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
116 $sname = $this->getName();
117 $fname = get_class($this) . '::doQuery';
118 $sql = $this->getSQL();
119 $dbr =& wfGetDB( DB_SLAVE
);
120 $dbw =& wfGetDB( DB_MASTER
);
121 $querycache = $dbr->tableName( 'querycache' );
123 $wgOut->setSyndicated( $this->isSyndicated() );
126 if ( $this->isExpensive() ) {
127 $recache = $wgRequest->getBool( 'recache' );
129 # Clear out any old cached data
130 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
132 # Do query on the (possibly out of date) slave server
134 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
137 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
139 while ( $row = $dbr->fetchObject( $res ) ) {
146 $dbw->addQuotes( $row->type
) . ',' .
147 $dbw->addQuotes( $row->namespace ) . ',' .
148 $dbw->addQuotes( $row->title
) . ',' .
149 $dbw->addQuotes( $row->value
) . ')';
152 # Save results into the querycache table on the master
153 $dbw->query( $insertSql, $fname );
155 # Set result pointer to allow reading for display
156 $numRows = $dbr->numRows( $res );
157 if ( $numRows <= $offset ) {
160 $dbr->dataSeek( $res, $offset );
161 $num = max( $limit, $numRows - $offset );
164 if( $wgMiserMode ||
$recache ) {
165 $type = $dbr->strencode( $sname );
167 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
168 FROM $querycache WHERE qc_type='$type'";
171 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
174 if ( $res === false ) {
175 $res = $dbr->query( $sql . $this->getOrder() .
176 $dbr->limitResult( $limit,$offset ), $fname );
177 $num = $dbr->numRows($res);
181 $sk = $wgUser->getSkin( );
183 $wgOut->addHTML( $this->getPageHeader() );
185 $top = wfShowingResults( $offset, $num);
186 $wgOut->addHTML( "<p>{$top}\n" );
188 # often disable 'next' link when we reach the end
189 if($num < $limit) { $atend = true; } else { $atend = false; }
191 $sl = wfViewPrevNext( $offset, $limit , $wgContLang->specialPage( $sname ), "" ,$atend );
192 $wgOut->addHTML( "<br />{$sl}</p>\n" );
195 $s = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
197 # Only read at most $num rows, because $res may contain the whole 1000
198 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
199 $format = $this->formatResult( $sk, $obj );
201 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
202 $obj->patrolled
== 0 ) ?
' class="not-patrolled"' : '';
203 $s .= "<li{$attr}>{$format}</li>\n";
207 if($this->tryLastResult()) {
208 // flush the very last result
210 $format = $this->formatResult( $sk, $obj );
212 $s .= "<li{$attr}>{$format}</li>\n";
216 $dbr->freeResult( $res );
218 $wgOut->addHTML( $s );
220 $wgOut->addHTML( "<p>{$sl}</p>\n" );
224 * Similar to above, but packaging in a syndicated feed instead of a web page
226 function doFeed( $class = '' ) {
227 global $wgFeedClasses;
228 global $wgOut, $wgLanguageCode, $wgLang;
229 if( isset($wgFeedClasses[$class]) ) {
230 $feed = new $wgFeedClasses[$class](
236 $dbr =& wfGetDB( DB_SLAVE
);
237 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
238 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
239 while( $obj = $dbr->fetchObject( $res ) ) {
240 $item = $this->feedResult( $obj );
241 if( $item ) $feed->outItem( $item );
243 $dbr->freeResult( $res );
253 * Override for custom handling. If the titles/links are ok, just do
256 function feedResult( $row ) {
257 if( !isset( $row->title
) ) {
260 $title = Title
::MakeTitle( IntVal( $row->namespace ), $row->title
);
262 if( isset( $row->timestamp
) ) {
263 $date = $row->timestamp
;
270 $talkpage = $title->getTalkPage();
271 $comments = $talkpage->getFullURL();
276 $this->feedItemDesc( $row ),
277 $title->getFullURL(),
279 $this->feedItemAuthor( $row ),
286 function feedItemDesc( $row ) {
288 if( isset( $row->comment
) ) {
289 $text = htmlspecialchars( $row->comment
);
294 if( isset( $row->text
) ) {
295 $text = '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' . $text . "</p>\n<hr />\n<div>" .
296 nl2br( htmlspecialchars( $row->text
) ) . "</div>";;
301 function feedItemAuthor( $row ) {
302 if( isset( $row->user_text
) ) {
303 return $row->user_text
;
309 function feedTitle() {
310 global $wgLanguageCode, $wgSitename, $wgLang;
311 $page = SpecialPage
::getPage( $this->getName() );
312 $desc = $page->getDescription();
313 return "$wgSitename - $desc [$wgLanguageCode]";
316 function feedDesc() {
317 return wfMsg( 'tagline' );
322 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
323 return $title->getFullURL();
328 * This is a subclass for very simple queries that are just looking for page
329 * titles that match some criteria. It formats each result item as a link to
334 class PageQueryPage
extends QueryPage
{
336 function formatResult( $skin, $result ) {
338 $nt = Title
::makeTitle( $result->namespace, $result->title
);
339 return $skin->makeKnownLinkObj( $nt, $wgContLang->convert( $result->title
) );