HTMLForm: Add "cloner" type
[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 IContextSource $context
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 array $ids
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 Revision 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 * @return Revision
90 */
91 public function current() {
92 return $this->current;
93 }
94
95 /**
96 * Move the iteration pointer to the next list item, and return it.
97 * @return Revision
98 */
99 public function next() {
100 $this->res->next();
101 $this->initCurrent();
102 return $this->current;
103 }
104
105 /**
106 * Get the number of items in the list.
107 * @return int
108 */
109 public function length() {
110 if ( !$this->res ) {
111 return 0;
112 } else {
113 return $this->res->numRows();
114 }
115 }
116
117 /**
118 * Do the DB query to iterate through the objects.
119 * @param DatabaseBase $db DatabaseBase object to use for the query
120 */
121 abstract public function doQuery( $db );
122
123 /**
124 * Create an item object from a DB result row
125 * @param object $row
126 */
127 abstract public function newItem( $row );
128 }
129
130 /**
131 * Abstract base class for revision items
132 */
133 abstract class RevisionItemBase {
134 /** The parent RevisionListBase */
135 var $list;
136
137 /** The DB result row */
138 var $row;
139
140 /**
141 * @param RevisionListBase $list
142 * @param object $row DB result row
143 */
144 public function __construct( $list, $row ) {
145 $this->list = $list;
146 $this->row = $row;
147 }
148
149 /**
150 * Get the DB field name associated with the ID list.
151 * Override this function.
152 * @return null
153 */
154 public function getIdField() {
155 return null;
156 }
157
158 /**
159 * Get the DB field name storing timestamps.
160 * Override this function.
161 * @return bool
162 */
163 public function getTimestampField() {
164 return false;
165 }
166
167 /**
168 * Get the DB field name storing user ids.
169 * Override this function.
170 * @return bool
171 */
172 public function getAuthorIdField() {
173 return false;
174 }
175
176 /**
177 * Get the DB field name storing user names.
178 * Override this function.
179 * @return bool
180 */
181 public function getAuthorNameField() {
182 return false;
183 }
184
185 /**
186 * Get the ID, as it would appear in the ids URL parameter
187 * @return int
188 */
189 public function getId() {
190 $field = $this->getIdField();
191 return $this->row->$field;
192 }
193
194 /**
195 * Get the date, formatted in user's language
196 * @return string
197 */
198 public function formatDate() {
199 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
200 $this->list->getUser() );
201 }
202
203 /**
204 * Get the time, formatted in user's language
205 * @return string
206 */
207 public function formatTime() {
208 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
209 $this->list->getUser() );
210 }
211
212 /**
213 * Get the timestamp in MW 14-char form
214 * @return mixed
215 */
216 public function getTimestamp() {
217 $field = $this->getTimestampField();
218 return wfTimestamp( TS_MW, $this->row->$field );
219 }
220
221 /**
222 * Get the author user ID
223 * @return int
224 */
225 public function getAuthorId() {
226 $field = $this->getAuthorIdField();
227 return intval( $this->row->$field );
228 }
229
230 /**
231 * Get the author user name
232 * @return string
233 */
234 public function getAuthorName() {
235 $field = $this->getAuthorNameField();
236 return strval( $this->row->$field );
237 }
238
239 /**
240 * Returns true if the current user can view the item
241 */
242 abstract public function canView();
243
244 /**
245 * Returns true if the current user can view the item text/file
246 */
247 abstract public function canViewContent();
248
249 /**
250 * Get the HTML of the list item. Should be include "<li></li>" tags.
251 * This is used to show the list in HTML form, by the special page.
252 */
253 abstract public function getHTML();
254 }
255
256 class RevisionList extends RevisionListBase {
257 public function getType() {
258 return 'revision';
259 }
260
261 /**
262 * @param DatabaseBase $db
263 * @return mixed
264 */
265 public function doQuery( $db ) {
266 $conds = array( 'rev_page' => $this->title->getArticleID() );
267 if ( $this->ids !== null ) {
268 $conds['rev_id'] = array_map( 'intval', $this->ids );
269 }
270 return $db->select(
271 array( 'revision', 'page', 'user' ),
272 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
273 $conds,
274 __METHOD__,
275 array( 'ORDER BY' => 'rev_id DESC' ),
276 array(
277 'page' => Revision::pageJoinCond(),
278 'user' => Revision::userJoinCond() )
279 );
280 }
281
282 public function newItem( $row ) {
283 return new RevisionItem( $this, $row );
284 }
285 }
286
287 /**
288 * Item class for a live revision table row
289 */
290 class RevisionItem extends RevisionItemBase {
291 var $revision, $context;
292
293 public function __construct( $list, $row ) {
294 parent::__construct( $list, $row );
295 $this->revision = new Revision( $row );
296 $this->context = $list->getContext();
297 }
298
299 public function getIdField() {
300 return 'rev_id';
301 }
302
303 public function getTimestampField() {
304 return 'rev_timestamp';
305 }
306
307 public function getAuthorIdField() {
308 return 'rev_user';
309 }
310
311 public function getAuthorNameField() {
312 return 'user_name'; // see Revision::selectUserFields()
313 }
314
315 public function canView() {
316 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
317 }
318
319 public function canViewContent() {
320 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
321 }
322
323 public function isDeleted() {
324 return $this->revision->isDeleted( Revision::DELETED_TEXT );
325 }
326
327 /**
328 * Get the HTML link to the revision text.
329 * Overridden by RevDel_ArchiveItem.
330 * @return string
331 */
332 protected function getRevisionLink() {
333 $date = $this->list->getLanguage()->timeanddate( $this->revision->getTimestamp(), true );
334 if ( $this->isDeleted() && !$this->canViewContent() ) {
335 return $date;
336 }
337 return Linker::link(
338 $this->list->title,
339 $date,
340 array(),
341 array(
342 'oldid' => $this->revision->getId(),
343 'unhide' => 1
344 )
345 );
346 }
347
348 /**
349 * Get the HTML link to the diff.
350 * Overridden by RevDel_ArchiveItem
351 * @return string
352 */
353 protected function getDiffLink() {
354 if ( $this->isDeleted() && !$this->canViewContent() ) {
355 return $this->context->msg( 'diff' )->escaped();
356 } else {
357 return Linker::link(
358 $this->list->title,
359 $this->context->msg( 'diff' )->escaped(),
360 array(),
361 array(
362 'diff' => $this->revision->getId(),
363 'oldid' => 'prev',
364 'unhide' => 1
365 ),
366 array(
367 'known',
368 'noclasses'
369 )
370 );
371 }
372 }
373
374 public function getHTML() {
375 $difflink = $this->context->msg( 'parentheses' )
376 ->rawParams( $this->getDiffLink() )->escaped();
377 $revlink = $this->getRevisionLink();
378 $userlink = Linker::revUserLink( $this->revision );
379 $comment = Linker::revComment( $this->revision );
380 if ( $this->isDeleted() ) {
381 $revlink = "<span class=\"history-deleted\">$revlink</span>";
382 }
383 return "<li>$difflink $revlink $userlink $comment</li>";
384 }
385 }