Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

QueryPage.php

Go to the documentation of this file.
00001 <?php 00002 00003 require_once ( "Feed.php" ); 00004 00005 # This is a class for doing query pages; since they're almost all the same, 00006 # we factor out some of the functionality into a superclass, and let 00007 # subclasses derive from it. 00008 00009 class QueryPage { 00010 # Subclasses return their name here. Make sure the name is also 00011 # specified in SpecialPage.php and in Language.php as a language message param. 00012 00013 function getName() { 00014 return ""; 00015 } 00016 00017 # Subclasses return an SQL query here. 00018 # 00019 # Note that the query itself should return the following three columns: 00020 # 'type' (your special page's name), 'namespace, 'title', and 'value' 00021 # (numeric) *in that order*. These may be stored in the querycache table 00022 # for expensive queries, and that cached data will be returned sometimes, 00023 # so the presence of extra fields can't be relied on. 00024 # 00025 # Don't include an ORDER or LIMIT clause, this will be added. 00026 00027 function getSQL() { 00028 return "SELECT 'sample' as type, 0 as namespace, 'Sample result' as title, 42 as value"; 00029 } 00030 00031 # Override to sort by increasing values 00032 function sortDescending() { 00033 return true; 00034 } 00035 00036 # Don't override this unless you're darn sure. 00037 function getOrderLimit( $offset, $limit ) { 00038 return " ORDER BY value " . 00039 ($this->sortDescending() ? "DESC" : "") 00040 . wfLimitResult($limit,$offset); 00041 } 00042 00043 # Is this query expensive (for some definition of expensive)? Then we 00044 # don't let it run in miser mode. $wgDisableQueryPages causes all query 00045 # pages to be declared expensive. Some query pages are always expensive. 00046 function isExpensive( ) { 00047 global $wgDisableQueryPages; 00048 return $wgDisableQueryPages; 00049 } 00050 00051 # Formats the results of the query for display. The skin is the current 00052 # skin; you can use it for making links. The result is a single row of 00053 # result data. You should be able to grab SQL results off of it. 00054 00055 function formatResult( $skin, $result ) { 00056 return ""; 00057 } 00058 00059 # This is the actual workhorse. It does everything needed to make a 00060 # real, honest-to-gosh query page. 00061 00062 function doQuery( $offset, $limit ) { 00063 global $wgUser, $wgOut, $wgLang, $wgRequest; 00064 global $wgMiserMode; 00065 00066 $sname = $this->getName(); 00067 $fname = get_class($this) . "::doQuery"; 00068 $sql = $this->getSQL( $offset, $limit ); 00069 00070 $wgOut->setSyndicated( true ); 00071 00072 if ( $this->isExpensive() ) { 00073 $type = wfStrencode( $sname ); 00074 $recache = $wgRequest->getBool( "recache" ); 00075 if( $recache ) { 00076 # Clear out any old cached data 00077 $res = wfQuery( "DELETE FROM querycache WHERE qc_type='$type'", DB_WRITE, $fname ); 00078 00079 # Save results into the querycache table 00080 $maxstored = 1000; 00081 $res = wfQuery( 00082 "INSERT INTO querycache(qc_type,qc_namespace,qc_title,qc_value) " . 00083 $this->getSQL() . 00084 $this->getOrderLimit( 0, $maxstored ), 00085 DB_WRITE, $fname ); 00086 } 00087 if( $wgMiserMode || $recache ) { 00088 $sql = 00089 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value 00090 FROM querycache WHERE qc_type='$type'"; 00091 } 00092 if( $wgMiserMode ) { 00093 $wgOut->addWikiText( wfMsg( "perfcached" ) ); 00094 } 00095 } 00096 00097 $res = wfQuery( $sql . $this->getOrderLimit( $offset, $limit ), DB_READ, $fname ); 00098 00099 $num = wfNumRows($res); 00100 00101 $sk = $wgUser->getSkin( ); 00102 00103 $top = wfShowingResults( $offset, $num); 00104 $wgOut->addHTML( "<p>{$top}\n" ); 00105 00106 # often disable 'next' link when we reach the end 00107 if($num < $limit) { $atend = true; } else { $atend = false; } 00108 00109 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend ); 00110 $wgOut->addHTML( "<br />{$sl}</p>\n" ); 00111 00112 $s = "<ol start='" . ( $offset + 1 ) . "'>"; 00113 while ( $obj = wfFetchObject( $res ) ) { 00114 $format = $this->formatResult( $sk, $obj ); 00115 $s .= "<li>{$format}</li>\n"; 00116 } 00117 wfFreeResult( $res ); 00118 $s .= "</ol>"; 00119 $wgOut->addHTML( $s ); 00120 $wgOut->addHTML( "<p>{$sl}</p>\n" ); 00121 } 00122 00123 # Similar to above, but packaging in a syndicated feed instead of a web page 00124 function doFeed( $class = "" ) { 00125 global $wgFeedClasses; 00126 global $wgOut, $wgLanguageCode, $wgLang; 00127 if( isset($wgFeedClasses[$class]) ) { 00128 $feed = new $wgFeedClasses[$class]( 00129 $this->feedTitle(), 00130 $this->feedDesc(), 00131 $this->feedUrl() ); 00132 $feed->outHeader(); 00133 00134 $sql = $this->getSQL( 0, 50 ); 00135 $res = wfQuery( $sql, DB_READ, "QueryPage::doFeed" ); 00136 while( $obj = wfFetchObject( $res ) ) { 00137 $item = $this->feedResult( $obj ); 00138 if( $item ) $feed->outItem( $item ); 00139 } 00140 wfFreeResult( $res ); 00141 00142 $feed->outFooter(); 00143 return true; 00144 } else { 00145 return false; 00146 } 00147 } 00148 00149 # Override for custom handling. If the titles/links are ok, just do feedItemDesc() 00150 function feedResult( $row ) { 00151 if( !isset( $row->title ) ) { 00152 return NULL; 00153 } 00154 $title = Title::MakeTitle( IntVal( $row->namespace ), $row->title ); 00155 if( $title ) { 00156 if( isset( $row->timestamp ) ) { 00157 $date = $row->timestamp; 00158 } else { 00159 $date = ""; 00160 } 00161 00162 $comments = ""; 00163 if( $title ) { 00164 $talkpage = $title->getTalkPage(); 00165 $comments = $talkpage->getFullURL(); 00166 } 00167 00168 return new FeedItem( 00169 $title->getText(), 00170 $this->feedItemDesc( $row ), 00171 $title->getFullURL(), 00172 $date, 00173 $this->feedItemAuthor( $row ), 00174 $comments); 00175 } else { 00176 return NULL; 00177 } 00178 } 00179 00180 function feedItemDesc( $row ) { 00181 $text = ""; 00182 if( isset( $row->comment ) ) { 00183 $text = htmlspecialchars( $row->comment ); 00184 } else { 00185 $text = ""; 00186 } 00187 00188 if( isset( $row->text ) ) { 00189 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" . 00190 nl2br( htmlspecialchars( $row->text ) ) . "</div>";; 00191 } 00192 return $text; 00193 } 00194 00195 function feedItemAuthor( $row ) { 00196 if( isset( $row->user_text ) ) { 00197 return $row->user_text; 00198 } else { 00199 return ""; 00200 } 00201 } 00202 00203 function feedTitle() { 00204 global $wgLanguageCode, $wgSitename, $wgLang; 00205 $page = SpecialPage::getPage( $this->getName() ); 00206 $desc = $page->getDescription(); 00207 return "$wgSitename - $desc [$wgLanguageCode]"; 00208 } 00209 00210 function feedDesc() { 00211 return wfMsg( "fromwikipedia" ); 00212 } 00213 00214 function feedUrl() { 00215 global $wgLang; 00216 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() ); 00217 return $title->getFullURL(); 00218 } 00219 } 00220 00221 # This is a subclass for very simple queries that are just looking for page 00222 # titles that match some criteria. It formats each result item as a link to 00223 # that page. 00224 00225 class PageQueryPage extends QueryPage { 00226 00227 function formatResult( $skin, $result ) { 00228 $nt = Title::makeTitle( $result->namespace, $result->title ); 00229 return $skin->makeKnownLinkObj( $nt, "" ); 00230 } 00231 } 00232 00233 ?>

Generated on Tue Jun 29 23:40:06 2004 for Mediawiki by doxygen 1.3.7