Fix sourceforge bug 919061
[lhc/web/wiklou.git] / includes / QueryPage.php
1 <?php
2
3 include_once ( "LogPage.php" ) ;
4 include_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 $sl = wfViewPrevNext( $offset, $limit , $wgLang->specialPage( $sname ) );
75 $wgOut->addHTML( "<br />{$sl}</p>\n" );
76
77 $s = "<ol start='" . ( $offset + 1 ) . "'>";
78 while ( $obj = wfFetchObject( $res ) ) {
79 $format = $this->formatResult( $sk, $obj );
80 $s .= "<li>{$format}</li>\n";
81 }
82 wfFreeResult( $res );
83 $s .= "</ol>";
84 $wgOut->addHTML( $s );
85 $wgOut->addHTML( "<p>{$sl}</p>\n" );
86
87 # Saving cache
88
89 if ( $this->isExpensive() && $offset == 0 && $limit >= 50 ) {
90 $logpage->replaceContent( $s );
91 }
92 }
93
94 # Similar to above, but packaging in a syndicated feed instead of a web page
95 function doFeed( $class = "" ) {
96 global $wgFeedClasses;
97 global $wgOut, $wgLanguageCode, $wgLang;
98 if( $class == "rss" ) {
99 $feed = new RSSFeed(
100 $this->feedTitle(),
101 $this->feedDesc(),
102 $this->feedUrl() );
103 $feed->outHeader();
104
105 $sql = $this->getSQL( 0, 50 );
106 $res = wfQuery( $sql, DB_READ, "QueryPage::doFeed" );
107 while( $obj = wfFetchObject( $res ) ) {
108 $item = $this->feedResult( $obj );
109 if( $item ) $feed->outItem( $item );
110 }
111 wfFreeResult( $res );
112
113 $feed->outFooter();
114 return true;
115 } else {
116 return false;
117 }
118 }
119
120 # Override for custom handling. If the titles/links are ok, just do feedItemDesc()
121 function feedResult( $row ) {
122 if( isset( $row->cur_title ) ) {
123 $title = Title::MakeTitle( $row->cur_namespace, $row->cur_title );
124 } elseif( isset( $row->old_title ) ) {
125 $title = Title::MakeTitle( $row->old_namespace, $row->old_title );
126 } elseif( isset( $row->rc_title ) ) {
127 $title = Title::MakeTitle( $row->rc_namespace, $row->rc_title );
128 } else {
129 return NULL;
130 }
131 if( $title ) {
132 $date = "";
133 if( isset( $row->cur_timestamp ) ) {
134 $date = $row->cur_timestamp;
135 } elseif( isset( $row->old_timestamp ) ) {
136 $date = $row->old_timestamp;
137 } elseif( isset( $row->rc_cur_timestamp ) ) {
138 $date = $row->rc_cur_timestamp;
139 }
140 return new FeedItem(
141 $title->getText(),
142 $this->feedItemDesc( $row ),
143 $title->getFullURL(),
144 $date,
145 $this->feedItemAuthor( $row ) );
146 } else {
147 return NULL;
148 }
149 }
150
151 function feedItemDesc( $row ) {
152 $text = "";
153 if( isset( $row->cur_comment ) ) {
154 $text = $row->cur_comment;
155 } elseif( isset( $row->old_comment ) ) {
156 $text = $row->old_comment;
157 } elseif( isset( $row->rc_comment ) ) {
158 $text = $row->rc_comment;
159 }
160 $text = htmlspecialchars( $text );
161
162 if( isset( $row->cur_text ) ) {
163 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
164 nl2br( $row->cur_text ) . "</div>";;
165 }
166 return $text;
167 }
168
169 function feedItemAuthor( $row ) {
170 $fields = array( "cur_user_text", "old_user_text", "rc_user_text" );
171 foreach( $fields as $field ) {
172 if( isset( $row->$field ) ) return $row->field;
173 }
174 return "";
175 }
176
177 function feedTitle() {
178 global $wgLanguageCode, $wgSitename, $wgLang;
179 $pages = $wgLang->getValidSpecialPages();
180 $title = $pages[$this->getName()];
181 return "$wgSitename - $title [$wgLanguageCode]";
182 }
183
184 function feedDesc() {
185 return wfMsg( "fromwikipedia" );
186 }
187
188 function feedUrl() {
189 global $wgLang;
190 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
191 return $title->getFullURL();
192 }
193 }
194
195 # This is a subclass for very simple queries that are just looking for page
196 # titles that match some criteria. It formats each result item as a link to
197 # that page.
198
199 class PageQueryPage extends QueryPage {
200
201 function formatResult( $skin, $result ) {
202 return $skin->makeKnownLink( $result->cur_title, "" );
203 }
204 }
205
206 ?>