3 * Contain a class for special pages
9 require_once ( 'Feed.php' );
12 * This is a class for doing query pages; since they're almost all the same,
13 * we factor out some of the functionality into a superclass, and let
14 * subclasses derive from it.
20 * Subclasses return their name here. Make sure the name is also
21 * specified in SpecialPage.php and in Language.php as a language message
29 * Subclasses return an SQL query here.
31 * Note that the query itself should return the following four columns:
32 * 'type' (your special page's name), 'namespace', 'title', and 'value'
33 * *in that order*. 'value' is used for sorting.
35 * These may be stored in the querycache table for expensive queries,
36 * and that cached data will be returned sometimes, so the presence of
37 * extra fields can't be relied upon. The cached 'value' column will be
38 * an integer; non-numeric values are useful only for sorting the initial
41 * Don't include an ORDER or LIMIT clause, this will be added.
44 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value";
48 * Override to sort by increasing values
50 function sortDescending() {
55 return ' ORDER BY value ' .
56 ($this->sortDescending() ?
'DESC' : '');
60 * Is this query expensive (for some definition of expensive)? Then we
61 * don't let it run in miser mode. $wgDisableQueryPages causes all query
62 * pages to be declared expensive. Some query pages are always expensive.
64 function isExpensive( ) {
65 global $wgDisableQueryPages;
66 return $wgDisableQueryPages;
70 * Formats the results of the query for display. The skin is the current
71 * skin; you can use it for making links. The result is a single row of
72 * result data. You should be able to grab SQL results off of it.
74 function formatResult( $skin, $result ) {
79 * The content returned by this function will be output before any result
81 function getPageHeader( ) {
86 * This is the actual workhorse. It does everything needed to make a
87 * real, honest-to-gosh query page.
89 * @param $offset database query offset
90 * @param $limit database query limit
92 function doQuery( $offset, $limit ) {
93 global $wgUser, $wgOut, $wgLang, $wgRequest;
96 $sname = $this->getName();
97 $fname = get_class($this) . '::doQuery';
98 $sql = $this->getSQL();
99 $dbr =& wfGetDB( DB_SLAVE
);
100 $dbw =& wfGetDB( DB_MASTER
);
101 $querycache = $dbr->tableName( 'querycache' );
103 $wgOut->setSyndicated( true );
106 if ( $this->isExpensive() ) {
107 $recache = $wgRequest->getBool( 'recache' );
109 # Clear out any old cached data
110 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
112 # Do query on the (possibly out of date) slave server
114 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
117 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
119 while ( $row = $dbr->fetchObject( $res ) ) {
126 $dbw->addQuotes( $row->type
) . ',' .
127 $dbw->addQuotes( $row->namespace ) . ',' .
128 $dbw->addQuotes( $row->title
) . ',' .
129 $dbw->addQuotes( $row->value
) . ')';
132 # Save results into the querycache table on the master
133 $dbw->query( $insertSql, $fname );
135 # Set result pointer to allow reading for display
136 $numRows = $dbr->numRows( $res );
137 if ( $numRows <= $offset ) {
140 $dbr->dataSeek( $res, $offset );
141 $num = max( $limit, $numRows - $offset );
144 if( $wgMiserMode ||
$recache ) {
145 $type = $dbr->strencode( $sname );
147 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
148 FROM $querycache WHERE qc_type='$type'";
151 $wgOut->addWikiText( wfMsg( 'perfcached' ) );
154 if ( $res === false ) {
155 $res = $dbr->query( $sql . $this->getOrder() .
156 $dbr->limitResult( $limit,$offset ), $fname );
157 $num = $dbr->numRows($res);
161 $sk = $wgUser->getSkin( );
163 $wgOut->addHTML( $this->getPageHeader() );
165 $top = wfShowingResults( $offset, $num);
166 $wgOut->addHTML( "<p>{$top}\n" );
168 # often disable 'next' link when we reach the end
169 if($num < $limit) { $atend = true; } else { $atend = false; }
171 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
172 $wgOut->addHTML( "<br />{$sl}</p>\n" );
174 $s = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
175 # Only read at most $num rows, because $res may contain the whole 1000
176 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
177 $format = $this->formatResult( $sk, $obj );
178 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
179 $obj->patrolled
== 0 ) ?
' class="not_patrolled"' : '';
180 $s .= "<li{$attr}>{$format}</li>\n";
182 $dbr->freeResult( $res );
184 $wgOut->addHTML( $s );
185 $wgOut->addHTML( "<p>{$sl}</p>\n" );
189 * Similar to above, but packaging in a syndicated feed instead of a web page
191 function doFeed( $class = '' ) {
192 global $wgFeedClasses;
193 global $wgOut, $wgLanguageCode, $wgLang;
194 if( isset($wgFeedClasses[$class]) ) {
195 $feed = new $wgFeedClasses[$class](
201 $dbr =& wfGetDB( DB_SLAVE
);
202 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
203 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
204 while( $obj = $dbr->fetchObject( $res ) ) {
205 $item = $this->feedResult( $obj );
206 if( $item ) $feed->outItem( $item );
208 $dbr->freeResult( $res );
218 * Override for custom handling. If the titles/links are ok, just do
221 function feedResult( $row ) {
222 if( !isset( $row->title
) ) {
225 $title = Title
::MakeTitle( IntVal( $row->namespace ), $row->title
);
227 if( isset( $row->timestamp
) ) {
228 $date = $row->timestamp
;
235 $talkpage = $title->getTalkPage();
236 $comments = $talkpage->getFullURL();
241 $this->feedItemDesc( $row ),
242 $title->getFullURL(),
244 $this->feedItemAuthor( $row ),
251 function feedItemDesc( $row ) {
253 if( isset( $row->comment
) ) {
254 $text = htmlspecialchars( $row->comment
);
259 if( isset( $row->text
) ) {
260 $text = '<p>' . htmlspecialchars( wfMsg( 'summary' ) ) . ': ' . $text . "</p>\n<hr />\n<div>" .
261 nl2br( htmlspecialchars( $row->text
) ) . "</div>";;
266 function feedItemAuthor( $row ) {
267 if( isset( $row->user_text
) ) {
268 return $row->user_text
;
274 function feedTitle() {
275 global $wgLanguageCode, $wgSitename, $wgLang;
276 $page = SpecialPage
::getPage( $this->getName() );
277 $desc = $page->getDescription();
278 return "$wgSitename - $desc [$wgLanguageCode]";
281 function feedDesc() {
282 return wfMsg( 'fromwikipedia' );
287 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
288 return $title->getFullURL();
293 * This is a subclass for very simple queries that are just looking for page
294 * titles that match some criteria. It formats each result item as a link to
297 class PageQueryPage
extends QueryPage
{
299 function formatResult( $skin, $result ) {
300 $nt = Title
::makeTitle( $result->namespace, $result->title
);
301 return $skin->makeKnownLinkObj( $nt, '' );