Add CASCADINGSOURCES parser function
[lhc/web/wiklou.git] / includes / RevisionList.php
1 <?php
2 /**
3 * Holders of revision list for a single page
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * List for revision table items for a single page
25 */
26 abstract class RevisionListBase extends ContextSource {
27 /**
28 * @var Title
29 */
30 var $title;
31
32 var $ids, $res, $current;
33
34 /**
35 * Construct a revision list for a given title
36 * @param $context IContextSource
37 * @param $title Title
38 */
39 function __construct( IContextSource $context, Title $title ) {
40 $this->setContext( $context );
41 $this->title = $title;
42 }
43
44 /**
45 * Select items only where the ID is any of the specified values
46 * @param $ids Array
47 */
48 function filterByIds( array $ids ) {
49 $this->ids = $ids;
50 }
51
52 /**
53 * Get the internal type name of this list. Equal to the table name.
54 * Override this function.
55 * @return null
56 */
57 public function getType() {
58 return null;
59 }
60
61 /**
62 * Initialise the current iteration pointer
63 */
64 protected function initCurrent() {
65 $row = $this->res->current();
66 if ( $row ) {
67 $this->current = $this->newItem( $row );
68 } else {
69 $this->current = false;
70 }
71 }
72
73 /**
74 * Start iteration. This must be called before current() or next().
75 * @return First list item
76 */
77 public function reset() {
78 if ( !$this->res ) {
79 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
80 } else {
81 $this->res->rewind();
82 }
83 $this->initCurrent();
84 return $this->current;
85 }
86
87 /**
88 * Get the current list item, or false if we are at the end
89 */
90 public function current() {
91 return $this->current;
92 }
93
94 /**
95 * Move the iteration pointer to the next list item, and return it.
96 */
97 public function next() {
98 $this->res->next();
99 $this->initCurrent();
100 return $this->current;
101 }
102
103 /**
104 * Get the number of items in the list.
105 * @return int
106 */
107 public function length() {
108 if ( !$this->res ) {
109 return 0;
110 } else {
111 return $this->res->numRows();
112 }
113 }
114
115 /**
116 * Do the DB query to iterate through the objects.
117 * @param $db DatabaseBase object to use for the query
118 */
119 abstract public function doQuery( $db );
120
121 /**
122 * Create an item object from a DB result row
123 * @param $row stdclass
124 */
125 abstract public function newItem( $row );
126 }
127
128 /**
129 * Abstract base class for revision items
130 */
131 abstract class RevisionItemBase {
132 /** The parent RevisionListBase */
133 var $list;
134
135 /** The DB result row */
136 var $row;
137
138 /**
139 * @param $list RevisionListBase
140 * @param $row DB result row
141 */
142 public function __construct( $list, $row ) {
143 $this->list = $list;
144 $this->row = $row;
145 }
146
147 /**
148 * Get the DB field name associated with the ID list.
149 * Override this function.
150 * @return null
151 */
152 public function getIdField() {
153 return null;
154 }
155
156 /**
157 * Get the DB field name storing timestamps.
158 * Override this function.
159 * @return bool
160 */
161 public function getTimestampField() {
162 return false;
163 }
164
165 /**
166 * Get the DB field name storing user ids.
167 * Override this function.
168 * @return bool
169 */
170 public function getAuthorIdField() {
171 return false;
172 }
173
174 /**
175 * Get the DB field name storing user names.
176 * Override this function.
177 * @return bool
178 */
179 public function getAuthorNameField() {
180 return false;
181 }
182
183 /**
184 * Get the ID, as it would appear in the ids URL parameter
185 * @return
186 */
187 public function getId() {
188 $field = $this->getIdField();
189 return $this->row->$field;
190 }
191
192 /**
193 * Get the date, formatted in user's language
194 * @return String
195 */
196 public function formatDate() {
197 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
198 $this->list->getUser() );
199 }
200
201 /**
202 * Get the time, formatted in user's language
203 * @return String
204 */
205 public function formatTime() {
206 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
207 $this->list->getUser() );
208 }
209
210 /**
211 * Get the timestamp in MW 14-char form
212 * @return Mixed
213 */
214 public function getTimestamp() {
215 $field = $this->getTimestampField();
216 return wfTimestamp( TS_MW, $this->row->$field );
217 }
218
219 /**
220 * Get the author user ID
221 * @return int
222 */
223 public function getAuthorId() {
224 $field = $this->getAuthorIdField();
225 return intval( $this->row->$field );
226 }
227
228 /**
229 * Get the author user name
230 * @return string
231 */
232 public function getAuthorName() {
233 $field = $this->getAuthorNameField();
234 return strval( $this->row->$field );
235 }
236
237 /**
238 * Returns true if the current user can view the item
239 */
240 abstract public function canView();
241
242 /**
243 * Returns true if the current user can view the item text/file
244 */
245 abstract public function canViewContent();
246
247 /**
248 * Get the HTML of the list item. Should be include "<li></li>" tags.
249 * This is used to show the list in HTML form, by the special page.
250 */
251 abstract public function getHTML();
252 }
253
254 class RevisionList extends RevisionListBase {
255 public function getType() {
256 return 'revision';
257 }
258
259 /**
260 * @param $db DatabaseBase
261 * @return mixed
262 */
263 public function doQuery( $db ) {
264 $conds = array( 'rev_page' => $this->title->getArticleID() );
265 if ( $this->ids !== null ) {
266 $conds['rev_id'] = array_map( 'intval', $this->ids );
267 }
268 return $db->select(
269 array( 'revision', 'page', 'user' ),
270 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
271 $conds,
272 __METHOD__,
273 array( 'ORDER BY' => 'rev_id DESC' ),
274 array(
275 'page' => Revision::pageJoinCond(),
276 'user' => Revision::userJoinCond() )
277 );
278 }
279
280 public function newItem( $row ) {
281 return new RevisionItem( $this, $row );
282 }
283 }
284
285 /**
286 * Item class for a live revision table row
287 */
288 class RevisionItem extends RevisionItemBase {
289 var $revision, $context;
290
291 public function __construct( $list, $row ) {
292 parent::__construct( $list, $row );
293 $this->revision = new Revision( $row );
294 $this->context = $list->getContext();
295 }
296
297 public function getIdField() {
298 return 'rev_id';
299 }
300
301 public function getTimestampField() {
302 return 'rev_timestamp';
303 }
304
305 public function getAuthorIdField() {
306 return 'rev_user';
307 }
308
309 public function getAuthorNameField() {
310 return 'user_name'; // see Revision::selectUserFields()
311 }
312
313 public function canView() {
314 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
315 }
316
317 public function canViewContent() {
318 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
319 }
320
321 public function isDeleted() {
322 return $this->revision->isDeleted( Revision::DELETED_TEXT );
323 }
324
325 /**
326 * Get the HTML link to the revision text.
327 * Overridden by RevDel_ArchiveItem.
328 * @return string
329 */
330 protected function getRevisionLink() {
331 $date = $this->list->getLanguage()->timeanddate( $this->revision->getTimestamp(), true );
332 if ( $this->isDeleted() && !$this->canViewContent() ) {
333 return $date;
334 }
335 return Linker::link(
336 $this->list->title,
337 $date,
338 array(),
339 array(
340 'oldid' => $this->revision->getId(),
341 'unhide' => 1
342 )
343 );
344 }
345
346 /**
347 * Get the HTML link to the diff.
348 * Overridden by RevDel_ArchiveItem
349 * @return string
350 */
351 protected function getDiffLink() {
352 if ( $this->isDeleted() && !$this->canViewContent() ) {
353 return $this->context->msg( 'diff' )->escaped();
354 } else {
355 return Linker::link(
356 $this->list->title,
357 $this->context->msg( 'diff' )->escaped(),
358 array(),
359 array(
360 'diff' => $this->revision->getId(),
361 'oldid' => 'prev',
362 'unhide' => 1
363 ),
364 array(
365 'known',
366 'noclasses'
367 )
368 );
369 }
370 }
371
372 public function getHTML() {
373 $difflink = $this->context->msg( 'parentheses' )
374 ->rawParams( $this->getDiffLink() )->escaped();
375 $revlink = $this->getRevisionLink();
376 $userlink = Linker::revUserLink( $this->revision );
377 $comment = Linker::revComment( $this->revision );
378 if ( $this->isDeleted() ) {
379 $revlink = "<span class=\"history-deleted\">$revlink</span>";
380 }
381 return "<li>$difflink $revlink $userlink $comment</li>";
382 }
383 }