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() {
41 return ' ORDER BY value ' .
42 ($this->sortDescending() ?
'DESC' : '');
45 # Is this query expensive (for some definition of expensive)? Then we
46 # don't let it run in miser mode. $wgDisableQueryPages causes all query
47 # pages to be declared expensive. Some query pages are always expensive.
48 function isExpensive( ) {
49 global $wgDisableQueryPages;
50 return $wgDisableQueryPages;
53 # Formats the results of the query for display. The skin is the current
54 # skin; you can use it for making links. The result is a single row of
55 # result data. You should be able to grab SQL results off of it.
57 function formatResult( $skin, $result ) {
61 # The content returned by this function will be output before any result
63 function getPageHeader( ) {
67 # This is the actual workhorse. It does everything needed to make a
68 # real, honest-to-gosh query page.
70 function doQuery( $offset, $limit ) {
71 global $wgUser, $wgOut, $wgLang, $wgRequest;
74 $sname = $this->getName();
75 $fname = get_class($this) . '::doQuery';
76 $sql = $this->getSQL();
77 $dbr =& wfGetDB( DB_SLAVE
);
78 $dbw =& wfGetDB( DB_MASTER
);
79 $querycache = $dbr->tableName( 'querycache' );
81 $wgOut->setSyndicated( true );
84 if ( $this->isExpensive() ) {
85 $recache = $wgRequest->getBool( 'recache' );
87 # Clear out any old cached data
88 $dbw->delete( 'querycache', array( 'qc_type' => $sname ), $fname );
90 # Do query on the (possibly out of date) slave server
92 $res = $dbr->query( $sql . $this->getOrder() . $dbr->limitResult( $maxstored,0 ), $fname );
95 $insertSql = "INSERT INTO $querycache (qc_type,qc_namespace,qc_title,qc_value) VALUES ";
97 while ( $row = $dbr->fetchObject( $res ) ) {
104 $dbw->addQuotes( $row->type
) . ',' .
105 $dbw->addQuotes( $row->namespace ) . ',' .
106 $dbw->addQuotes( $row->title
) . ',' .
107 $dbw->addQuotes( $row->value
) . ')';
110 # Save results into the querycache table on the master
111 $dbw->query( $insertSql, $fname );
113 # Set result pointer to allow reading for display
114 $numRows = $dbr->numRows( $res );
115 if ( $numRows <= $offset ) {
118 $dbr->dataSeek( $res, $offset );
119 $num = max( $limit, $numRows - $offset );
122 if( $wgMiserMode ||
$recache ) {
123 $type = $dbr->strencode( $sname );
125 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
126 FROM $querycache WHERE qc_type='$type'";
129 $wgOut->addWikiText( wfMsg( "perfcached" ) );
132 if ( $res === false ) {
133 $res = $dbr->query( $sql . $this->getOrder() .
134 $dbr->limitResult( $limit,$offset ), $fname );
135 $num = $dbr->numRows($res);
139 $sk = $wgUser->getSkin( );
141 $wgOut->addHTML( $this->getPageHeader() );
143 $top = wfShowingResults( $offset, $num);
144 $wgOut->addHTML( "<p>{$top}\n" );
146 # often disable 'next' link when we reach the end
147 if($num < $limit) { $atend = true; } else { $atend = false; }
149 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
150 $wgOut->addHTML( "<br />{$sl}</p>\n" );
152 $s = "<ol start='" . ( $offset +
1 ) . "' class='special'>";
153 # Only read at most $num rows, because $res may contain the whole 1000
154 for ( $i = 0; $i < $num && $obj = $dbr->fetchObject( $res ); $i++
) {
155 $format = $this->formatResult( $sk, $obj );
156 $attr = ( isset ( $obj->usepatrol
) && $obj->usepatrol
&&
157 $obj->patrolled
== 0 ) ?
' class="not_patrolled"' : '';
158 $s .= "<li{$attr}>{$format}</li>\n";
160 $dbr->freeResult( $res );
162 $wgOut->addHTML( $s );
163 $wgOut->addHTML( "<p>{$sl}</p>\n" );
166 # Similar to above, but packaging in a syndicated feed instead of a web page
167 function doFeed( $class = '' ) {
168 global $wgFeedClasses;
169 global $wgOut, $wgLanguageCode, $wgLang;
170 if( isset($wgFeedClasses[$class]) ) {
171 $feed = new $wgFeedClasses[$class](
177 $dbr =& wfGetDB( DB_SLAVE
);
178 $sql = $this->getSQL() . $this->getOrder().$dbr->limitResult( 50, 0 );
179 $res = $dbr->query( $sql, 'QueryPage::doFeed' );
180 while( $obj = $dbr->fetchObject( $res ) ) {
181 $item = $this->feedResult( $obj );
182 if( $item ) $feed->outItem( $item );
184 $dbr->freeResult( $res );
193 # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
194 function feedResult( $row ) {
195 if( !isset( $row->title
) ) {
198 $title = Title
::MakeTitle( IntVal( $row->namespace ), $row->title
);
200 if( isset( $row->timestamp
) ) {
201 $date = $row->timestamp
;
208 $talkpage = $title->getTalkPage();
209 $comments = $talkpage->getFullURL();
214 $this->feedItemDesc( $row ),
215 $title->getFullURL(),
217 $this->feedItemAuthor( $row ),
224 function feedItemDesc( $row ) {
226 if( isset( $row->comment
) ) {
227 $text = htmlspecialchars( $row->comment
);
232 if( isset( $row->text
) ) {
233 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
234 nl2br( htmlspecialchars( $row->text
) ) . "</div>";;
239 function feedItemAuthor( $row ) {
240 if( isset( $row->user_text
) ) {
241 return $row->user_text
;
247 function feedTitle() {
248 global $wgLanguageCode, $wgSitename, $wgLang;
249 $page = SpecialPage
::getPage( $this->getName() );
250 $desc = $page->getDescription();
251 return "$wgSitename - $desc [$wgLanguageCode]";
254 function feedDesc() {
255 return wfMsg( "fromwikipedia" );
260 $title = Title
::MakeTitle( NS_SPECIAL
, $this->getName() );
261 return $title->getFullURL();
265 # This is a subclass for very simple queries that are just looking for page
266 # titles that match some criteria. It formats each result item as a link to
269 class PageQueryPage
extends QueryPage
{
271 function formatResult( $skin, $result ) {
272 $nt = Title
::makeTitle( $result->namespace, $result->title
);
273 return $skin->makeKnownLinkObj( $nt, "" );