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