include_once -> require_once
[lhc/web/wiklou.git] / includes / QueryPage.php
1 <?php
2
3 require_once ( "LogPage.php" ) ;
4 require_once ( "Feed.php" );
5
6 # This is a class for doing query pages; since they're almost all the same,
7 # we factor out some of the functionality into a superclass, and let
8 # subclasses derive from it.
9
10 class QueryPage {
11 # Subclasses return their name here. Make sure the name is also
12 # specified in Language.php, both in the $wgValidSpecialPagesEn
13 # variable, and as a language message param.
14
15 function getName() {
16 return "";
17 }
18
19 # Subclasses return a SQL query here.
20
21 function getSQL( $offset, $limit ) {
22 return "";
23 }
24
25 # Is this query expensive (for some definition of expensive)? Then we
26 # don't let it run in miser mode. $wgDisableQueryPages causes all query
27 # pages to be declared expensive. Some query pages are always expensive.
28 function isExpensive( ) {
29 global $wgDisableQueryPages;
30 return $wgDisableQueryPages;
31 }
32
33 # Formats the results of the query for display. The skin is the current
34 # skin; you can use it for making links. The result is a single row of
35 # result data. You should be able to grab SQL results off of it.
36
37 function formatResult( $skin, $result ) {
38 return "";
39 }
40
41 # This is the actual workhorse. It does everything needed to make a
42 # real, honest-to-gosh query page.
43
44 function doQuery( $offset, $limit ) {
45 global $wgUser, $wgOut, $wgLang, $wgMiserMode;
46
47 $sname = $this->getName();
48 $fname = get_class($this) . "::doQuery";
49
50 $wgOut->setSyndicated( true );
51
52 if ( $this->isExpensive( ) ) {
53 $vsp = $wgLang->getValidSpecialPages();
54 $logpage = new LogPage( "!" . $vsp[$sname] );
55 $logpage->mUpdateRecentChanges = false;
56
57 if ( $wgMiserMode ) {
58 $logpage->showAsDisabledPage();
59 return;
60 }
61 }
62
63 $sql = $this->getSQL( $offset, $limit );
64
65 $res = wfQuery( $sql, DB_READ, $fname );
66
67 $num = wfNumRows($res);
68
69 $sk = $wgUser->getSkin( );
70
71 $top = wfShowingResults( $offset, $num);
72 $wgOut->addHTML( "<p>{$top}\n" );
73
74 # often disable 'next' link when we reach the end
75 if($num < $limit) { $atend = true; } else { $atend = false; }
76
77 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ), "" ,$atend );
78 $wgOut->addHTML( "<br />{$sl}</p>\n" );
79
80 $s = "<ol start='" . ( $offset + 1 ) . "'>";
81 while ( $obj = wfFetchObject( $res ) ) {
82 $format = $this->formatResult( $sk, $obj );
83 $s .= "<li>{$format}</li>\n";
84 }
85 wfFreeResult( $res );
86 $s .= "</ol>";
87 $wgOut->addHTML( $s );
88 $wgOut->addHTML( "<p>{$sl}</p>\n" );
89
90 # Saving cache
91
92 if ( $this->isExpensive() && $offset == 0 && $limit >= 50 ) {
93 $logpage->replaceContent( $s );
94 }
95 }
96
97 # Similar to above, but packaging in a syndicated feed instead of a web page
98 function doFeed( $class = "" ) {
99 global $wgFeedClasses;
100 global $wgOut, $wgLanguageCode, $wgLang;
101 if( isset($wgFeedClasses[$class]) ) {
102 $feed = new $wgFeedClasses[$class](
103 $this->feedTitle(),
104 $this->feedDesc(),
105 $this->feedUrl() );
106 $feed->outHeader();
107
108 $sql = $this->getSQL( 0, 50 );
109 $res = wfQuery( $sql, DB_READ, "QueryPage::doFeed" );
110 while( $obj = wfFetchObject( $res ) ) {
111 $item = $this->feedResult( $obj );
112 if( $item ) $feed->outItem( $item );
113 }
114 wfFreeResult( $res );
115
116 $feed->outFooter();
117 return true;
118 } else {
119 return false;
120 }
121 }
122
123 # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
124 function feedResult( $row ) {
125 if( isset( $row->cur_title ) ) {
126 $title = Title::MakeTitle( $row->cur_namespace, $row->cur_title );
127 } elseif( isset( $row->old_title ) ) {
128 $title = Title::MakeTitle( $row->old_namespace, $row->old_title );
129 } elseif( isset( $row->rc_title ) ) {
130 $title = Title::MakeTitle( $row->rc_namespace, $row->rc_title );
131 } else {
132 return NULL;
133 }
134 if( $title ) {
135 $date = "";
136 if( isset( $row->cur_timestamp ) ) {
137 $date = $row->cur_timestamp;
138 } elseif( isset( $row->old_timestamp ) ) {
139 $date = $row->old_timestamp;
140 } elseif( isset( $row->rc_cur_timestamp ) ) {
141 $date = $row->rc_cur_timestamp;
142 }
143
144 $comments = "";
145 if( $title ) {
146 $talkpage = $title->getTalkPage();
147 $comments = $talkpage->getFullURL();
148 }
149
150 return new FeedItem(
151 $title->getText(),
152 $this->feedItemDesc( $row ),
153 $title->getFullURL(),
154 $date,
155 $this->feedItemAuthor( $row ),
156 $comments);
157 } else {
158 return NULL;
159 }
160 }
161
162 function feedItemDesc( $row ) {
163 $text = "";
164 if( isset( $row->cur_comment ) ) {
165 $text = $row->cur_comment;
166 } elseif( isset( $row->old_comment ) ) {
167 $text = $row->old_comment;
168 } elseif( isset( $row->rc_comment ) ) {
169 $text = $row->rc_comment;
170 }
171 $text = htmlspecialchars( $text );
172
173 if( isset( $row->cur_text ) ) {
174 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
175 nl2br( $row->cur_text ) . "</div>";;
176 }
177 return $text;
178 }
179
180 function feedItemAuthor( $row ) {
181 /* old code
182 $fields = array( "cur_user_text", "old_user_text", "rc_user_text" );
183 foreach( $fields as $field ) {
184 if( isset( $row->$field ) ) return $row->field;
185 }
186
187 new code follow, that's an ugly hack to fix things: */
188 if( isset( $row->cur_user_text ) ) return $row->cur_user_text;
189 if( isset( $row->old_user_text ) ) return $row->old_user_text;
190 if( isset( $row->rc_user_text ) ) return $row->rc_user_text;
191 return "";
192 }
193
194 function feedTitle() {
195 global $wgLanguageCode, $wgSitename, $wgLang;
196 $pages = $wgLang->getValidSpecialPages();
197 $title = $pages[$this->getName()];
198 return "$wgSitename - $title [$wgLanguageCode]";
199 }
200
201 function feedDesc() {
202 return wfMsg( "fromwikipedia" );
203 }
204
205 function feedUrl() {
206 global $wgLang;
207 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
208 return $title->getFullURL();
209 }
210 }
211
212 # This is a subclass for very simple queries that are just looking for page
213 # titles that match some criteria. It formats each result item as a link to
214 # that page.
215
216 class PageQueryPage extends QueryPage {
217
218 function formatResult( $skin, $result ) {
219 return $skin->makeKnownLink( $result->cur_title, "" );
220 }
221 }
222
223 ?>