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