3 * Holders of revision list for a single page
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.
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.
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
24 * List for revision table items for a single page
26 abstract class RevisionListBase
extends ContextSource
implements Iterator
{
33 /** @var ResultWrapper|bool */
36 /** @var bool|object */
40 * Construct a revision list for a given title
41 * @param IContextSource $context
44 function __construct( IContextSource
$context, Title
$title ) {
45 $this->setContext( $context );
46 $this->title
= $title;
50 * Select items only where the ID is any of the specified values
53 function filterByIds( array $ids ) {
58 * Get the internal type name of this list. Equal to the table name.
59 * Override this function.
62 public function getType() {
67 * Initialise the current iteration pointer
69 protected function initCurrent() {
70 $row = $this->res
->current();
72 $this->current
= $this->newItem( $row );
74 $this->current
= false;
79 * Start iteration. This must be called before current() or next().
80 * @return Revision First list item
82 public function reset() {
84 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
89 return $this->current
;
92 public function rewind() {
97 * Get the current list item, or false if we are at the end
100 public function current() {
101 return $this->current
;
105 * Move the iteration pointer to the next list item, and return it.
108 public function next() {
110 $this->initCurrent();
111 return $this->current
;
114 public function key() {
115 return $this->res ?
$this->res
->key(): 0;
118 public function valid() {
119 return $this->res ?
$this->res
->valid() : false;
123 * Get the number of items in the list.
126 public function length() {
130 return $this->res
->numRows();
135 * Do the DB query to iterate through the objects.
136 * @param IDatabase $db DB object to use for the query
138 abstract public function doQuery( $db );
141 * Create an item object from a DB result row
144 abstract public function newItem( $row );
148 * Abstract base class for revision items
150 abstract class RevisionItemBase
{
151 /** @var RevisionListBase The parent */
154 /** The database result row */
158 * @param RevisionListBase $list
159 * @param object $row DB result row
161 public function __construct( $list, $row ) {
167 * Get the DB field name associated with the ID list.
168 * Override this function.
171 public function getIdField() {
176 * Get the DB field name storing timestamps.
177 * Override this function.
180 public function getTimestampField() {
185 * Get the DB field name storing user ids.
186 * Override this function.
189 public function getAuthorIdField() {
194 * Get the DB field name storing user names.
195 * Override this function.
198 public function getAuthorNameField() {
203 * Get the ID, as it would appear in the ids URL parameter
206 public function getId() {
207 $field = $this->getIdField();
208 return $this->row
->$field;
212 * Get the date, formatted in user's language
215 public function formatDate() {
216 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
217 $this->list->getUser() );
221 * Get the time, formatted in user's language
224 public function formatTime() {
225 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
226 $this->list->getUser() );
230 * Get the timestamp in MW 14-char form
233 public function getTimestamp() {
234 $field = $this->getTimestampField();
235 return wfTimestamp( TS_MW
, $this->row
->$field );
239 * Get the author user ID
242 public function getAuthorId() {
243 $field = $this->getAuthorIdField();
244 return intval( $this->row
->$field );
248 * Get the author user name
251 public function getAuthorName() {
252 $field = $this->getAuthorNameField();
253 return strval( $this->row
->$field );
257 * Returns true if the current user can view the item
259 abstract public function canView();
262 * Returns true if the current user can view the item text/file
264 abstract public function canViewContent();
267 * Get the HTML of the list item. Should be include "<li></li>" tags.
268 * This is used to show the list in HTML form, by the special page.
270 abstract public function getHTML();
273 class RevisionList
extends RevisionListBase
{
274 public function getType() {
279 * @param IDatabase $db
282 public function doQuery( $db ) {
283 $conds = [ 'rev_page' => $this->title
->getArticleID() ];
284 if ( $this->ids
!== null ) {
285 $conds['rev_id'] = array_map( 'intval', $this->ids
);
288 [ 'revision', 'page', 'user' ],
289 array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
292 [ 'ORDER BY' => 'rev_id DESC' ],
294 'page' => Revision
::pageJoinCond(),
295 'user' => Revision
::userJoinCond() ]
299 public function newItem( $row ) {
300 return new RevisionItem( $this, $row );
305 * Item class for a live revision table row
307 class RevisionItem
extends RevisionItemBase
{
311 /** @var RequestContext */
314 public function __construct( $list, $row ) {
315 parent
::__construct( $list, $row );
316 $this->revision
= new Revision( $row );
317 $this->context
= $list->getContext();
320 public function getIdField() {
324 public function getTimestampField() {
325 return 'rev_timestamp';
328 public function getAuthorIdField() {
332 public function getAuthorNameField() {
333 return 'rev_user_text';
336 public function canView() {
337 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
, $this->context
->getUser() );
340 public function canViewContent() {
341 return $this->revision
->userCan( Revision
::DELETED_TEXT
, $this->context
->getUser() );
344 public function isDeleted() {
345 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
349 * Get the HTML link to the revision text.
350 * @todo Essentially a copy of RevDelRevisionItem::getRevisionLink. That class
351 * should inherit from this one, and implement an appropriate interface instead
352 * of extending RevDelItem
355 protected function getRevisionLink() {
356 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
357 $this->revision
->getTimestamp(), $this->list->getUser() ) );
359 if ( $this->isDeleted() && !$this->canViewContent() ) {
362 return Linker
::linkKnown(
367 'oldid' => $this->revision
->getId(),
374 * Get the HTML link to the diff.
375 * @todo Essentially a copy of RevDelRevisionItem::getDiffLink. That class
376 * should inherit from this one, and implement an appropriate interface instead
377 * of extending RevDelItem
380 protected function getDiffLink() {
381 if ( $this->isDeleted() && !$this->canViewContent() ) {
382 return $this->context
->msg( 'diff' )->escaped();
384 return Linker
::linkKnown(
386 $this->list->msg( 'diff' )->escaped(),
389 'diff' => $this->revision
->getId(),
398 * @todo Essentially a copy of RevDelRevisionItem::getHTML. That class
399 * should inherit from this one, and implement an appropriate interface instead
400 * of extending RevDelItem
403 public function getHTML() {
404 $difflink = $this->context
->msg( 'parentheses' )
405 ->rawParams( $this->getDiffLink() )->escaped();
406 $revlink = $this->getRevisionLink();
407 $userlink = Linker
::revUserLink( $this->revision
);
408 $comment = Linker
::revComment( $this->revision
);
409 if ( $this->isDeleted() ) {
410 $revlink = "<span class=\"history-deleted\">$revlink</span>";
412 return "<li>$difflink $revlink $userlink $comment</li>";