Switch some HTMLForms in special pages to OOUI
[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 /** @var Title */
28 public $title;
29
30 /** @var array */
31 protected $ids;
32
33 /** @var ResultWrapper|bool */
34 protected $res;
35
36 /** @var bool|object */
37 protected $current;
38
39 /**
40 * Construct a revision list for a given title
41 * @param IContextSource $context
42 * @param Title $title
43 */
44 function __construct( IContextSource $context, Title $title ) {
45 $this->setContext( $context );
46 $this->title = $title;
47 }
48
49 /**
50 * Select items only where the ID is any of the specified values
51 * @param array $ids
52 */
53 function filterByIds( array $ids ) {
54 $this->ids = $ids;
55 }
56
57 /**
58 * Get the internal type name of this list. Equal to the table name.
59 * Override this function.
60 * @return null
61 */
62 public function getType() {
63 return null;
64 }
65
66 /**
67 * Initialise the current iteration pointer
68 */
69 protected function initCurrent() {
70 $row = $this->res->current();
71 if ( $row ) {
72 $this->current = $this->newItem( $row );
73 } else {
74 $this->current = false;
75 }
76 }
77
78 /**
79 * Start iteration. This must be called before current() or next().
80 * @return Revision First list item
81 */
82 public function reset() {
83 if ( !$this->res ) {
84 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
85 } else {
86 $this->res->rewind();
87 }
88 $this->initCurrent();
89 return $this->current;
90 }
91
92 /**
93 * Get the current list item, or false if we are at the end
94 * @return Revision
95 */
96 public function current() {
97 return $this->current;
98 }
99
100 /**
101 * Move the iteration pointer to the next list item, and return it.
102 * @return Revision
103 */
104 public function next() {
105 $this->res->next();
106 $this->initCurrent();
107 return $this->current;
108 }
109
110 /**
111 * Get the number of items in the list.
112 * @return int
113 */
114 public function length() {
115 if ( !$this->res ) {
116 return 0;
117 } else {
118 return $this->res->numRows();
119 }
120 }
121
122 /**
123 * Do the DB query to iterate through the objects.
124 * @param DatabaseBase $db DatabaseBase object to use for the query
125 */
126 abstract public function doQuery( $db );
127
128 /**
129 * Create an item object from a DB result row
130 * @param object $row
131 */
132 abstract public function newItem( $row );
133 }
134
135 /**
136 * Abstract base class for revision items
137 */
138 abstract class RevisionItemBase {
139 /** @var RevisionListBase The parent */
140 protected $list;
141
142 /** The database result row */
143 protected $row;
144
145 /**
146 * @param RevisionListBase $list
147 * @param object $row DB result row
148 */
149 public function __construct( $list, $row ) {
150 $this->list = $list;
151 $this->row = $row;
152 }
153
154 /**
155 * Get the DB field name associated with the ID list.
156 * Override this function.
157 * @return null
158 */
159 public function getIdField() {
160 return null;
161 }
162
163 /**
164 * Get the DB field name storing timestamps.
165 * Override this function.
166 * @return bool
167 */
168 public function getTimestampField() {
169 return false;
170 }
171
172 /**
173 * Get the DB field name storing user ids.
174 * Override this function.
175 * @return bool
176 */
177 public function getAuthorIdField() {
178 return false;
179 }
180
181 /**
182 * Get the DB field name storing user names.
183 * Override this function.
184 * @return bool
185 */
186 public function getAuthorNameField() {
187 return false;
188 }
189
190 /**
191 * Get the ID, as it would appear in the ids URL parameter
192 * @return int
193 */
194 public function getId() {
195 $field = $this->getIdField();
196 return $this->row->$field;
197 }
198
199 /**
200 * Get the date, formatted in user's language
201 * @return string
202 */
203 public function formatDate() {
204 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
205 $this->list->getUser() );
206 }
207
208 /**
209 * Get the time, formatted in user's language
210 * @return string
211 */
212 public function formatTime() {
213 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
214 $this->list->getUser() );
215 }
216
217 /**
218 * Get the timestamp in MW 14-char form
219 * @return mixed
220 */
221 public function getTimestamp() {
222 $field = $this->getTimestampField();
223 return wfTimestamp( TS_MW, $this->row->$field );
224 }
225
226 /**
227 * Get the author user ID
228 * @return int
229 */
230 public function getAuthorId() {
231 $field = $this->getAuthorIdField();
232 return intval( $this->row->$field );
233 }
234
235 /**
236 * Get the author user name
237 * @return string
238 */
239 public function getAuthorName() {
240 $field = $this->getAuthorNameField();
241 return strval( $this->row->$field );
242 }
243
244 /**
245 * Returns true if the current user can view the item
246 */
247 abstract public function canView();
248
249 /**
250 * Returns true if the current user can view the item text/file
251 */
252 abstract public function canViewContent();
253
254 /**
255 * Get the HTML of the list item. Should be include "<li></li>" tags.
256 * This is used to show the list in HTML form, by the special page.
257 */
258 abstract public function getHTML();
259 }
260
261 class RevisionList extends RevisionListBase {
262 public function getType() {
263 return 'revision';
264 }
265
266 /**
267 * @param DatabaseBase $db
268 * @return mixed
269 */
270 public function doQuery( $db ) {
271 $conds = array( 'rev_page' => $this->title->getArticleID() );
272 if ( $this->ids !== null ) {
273 $conds['rev_id'] = array_map( 'intval', $this->ids );
274 }
275 return $db->select(
276 array( 'revision', 'page', 'user' ),
277 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
278 $conds,
279 __METHOD__,
280 array( 'ORDER BY' => 'rev_id DESC' ),
281 array(
282 'page' => Revision::pageJoinCond(),
283 'user' => Revision::userJoinCond() )
284 );
285 }
286
287 public function newItem( $row ) {
288 return new RevisionItem( $this, $row );
289 }
290 }
291
292 /**
293 * Item class for a live revision table row
294 */
295 class RevisionItem extends RevisionItemBase {
296 /** @var Revision */
297 protected $revision;
298
299 /** @var RequestContext */
300 protected $context;
301
302 public function __construct( $list, $row ) {
303 parent::__construct( $list, $row );
304 $this->revision = new Revision( $row );
305 $this->context = $list->getContext();
306 }
307
308 public function getIdField() {
309 return 'rev_id';
310 }
311
312 public function getTimestampField() {
313 return 'rev_timestamp';
314 }
315
316 public function getAuthorIdField() {
317 return 'rev_user';
318 }
319
320 public function getAuthorNameField() {
321 return 'rev_user_text';
322 }
323
324 public function canView() {
325 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
326 }
327
328 public function canViewContent() {
329 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
330 }
331
332 public function isDeleted() {
333 return $this->revision->isDeleted( Revision::DELETED_TEXT );
334 }
335
336 /**
337 * Get the HTML link to the revision text.
338 * @todo Essentially a copy of RevDelRevisionItem::getRevisionLink. That class
339 * should inherit from this one, and implement an appropriate interface instead
340 * of extending RevDelItem
341 * @return string
342 */
343 protected function getRevisionLink() {
344 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
345 $this->revision->getTimestamp(), $this->list->getUser() ) );
346
347 if ( $this->isDeleted() && !$this->canViewContent() ) {
348 return $date;
349 }
350 return Linker::linkKnown(
351 $this->list->title,
352 $date,
353 array(),
354 array(
355 'oldid' => $this->revision->getId(),
356 'unhide' => 1
357 )
358 );
359 }
360
361 /**
362 * Get the HTML link to the diff.
363 * @todo Essentially a copy of RevDelRevisionItem::getDiffLink. That class
364 * should inherit from this one, and implement an appropriate interface instead
365 * of extending RevDelItem
366 * @return string
367 */
368 protected function getDiffLink() {
369 if ( $this->isDeleted() && !$this->canViewContent() ) {
370 return $this->context->msg( 'diff' )->escaped();
371 } else {
372 return Linker::linkKnown(
373 $this->list->title,
374 $this->list->msg( 'diff' )->escaped(),
375 array(),
376 array(
377 'diff' => $this->revision->getId(),
378 'oldid' => 'prev',
379 'unhide' => 1
380 )
381 );
382 }
383 }
384
385 /**
386 * @todo Essentially a copy of RevDelRevisionItem::getHTML. That class
387 * should inherit from this one, and implement an appropriate interface instead
388 * of extending RevDelItem
389 * @return string
390 */
391 public function getHTML() {
392 $difflink = $this->context->msg( 'parentheses' )
393 ->rawParams( $this->getDiffLink() )->escaped();
394 $revlink = $this->getRevisionLink();
395 $userlink = Linker::revUserLink( $this->revision );
396 $comment = Linker::revComment( $this->revision );
397 if ( $this->isDeleted() ) {
398 $revlink = "<span class=\"history-deleted\">$revlink</span>";
399 }
400 return "<li>$difflink $revlink $userlink $comment</li>";
401 }
402 }