b7901adb9369b335b22e1766ccfe2980e4e81e3c
[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 # 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( $class == "rss" ) {
102 $feed = new RSSFeed(
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 return new FeedItem(
144 $title->getText(),
145 $this->feedItemDesc( $row ),
146 $title->getFullURL(),
147 $date,
148 $this->feedItemAuthor( $row ) );
149 } else {
150 return NULL;
151 }
152 }
153
154 function feedItemDesc( $row ) {
155 $text = "";
156 if( isset( $row->cur_comment ) ) {
157 $text = $row->cur_comment;
158 } elseif( isset( $row->old_comment ) ) {
159 $text = $row->old_comment;
160 } elseif( isset( $row->rc_comment ) ) {
161 $text = $row->rc_comment;
162 }
163 $text = htmlspecialchars( $text );
164
165 if( isset( $row->cur_text ) ) {
166 $text = "<p>" . htmlspecialchars( wfMsg( "summary" ) ) . ": " . $text . "</p>\n<hr />\n<div>" .
167 nl2br( $row->cur_text ) . "</div>";;
168 }
169 return $text;
170 }
171
172 function feedItemAuthor( $row ) {
173 $fields = array( "cur_user_text", "old_user_text", "rc_user_text" );
174 foreach( $fields as $field ) {
175 if( isset( $row->$field ) ) return $row->field;
176 }
177 return "";
178 }
179
180 function feedTitle() {
181 global $wgLanguageCode, $wgSitename, $wgLang;
182 $pages = $wgLang->getValidSpecialPages();
183 $title = $pages[$this->getName()];
184 return "$wgSitename - $title [$wgLanguageCode]";
185 }
186
187 function feedDesc() {
188 return wfMsg( "fromwikipedia" );
189 }
190
191 function feedUrl() {
192 global $wgLang;
193 $title = Title::MakeTitle( NS_SPECIAL, $this->getName() );
194 return $title->getFullURL();
195 }
196 }
197
198 # This is a subclass for very simple queries that are just looking for page
199 # titles that match some criteria. It formats each result item as a link to
200 # that page.
201
202 class PageQueryPage extends QueryPage {
203
204 function formatResult( $skin, $result ) {
205 return $skin->makeKnownLink( $result->cur_title, "" );
206 }
207 }
208
209 ?>