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
{
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
;
93 * Get the current list item, or false if we are at the end
96 public function current() {
97 return $this->current
;
101 * Move the iteration pointer to the next list item, and return it.
104 public function next() {
106 $this->initCurrent();
107 return $this->current
;
111 * Get the number of items in the list.
114 public function length() {
118 return $this->res
->numRows();
123 * Do the DB query to iterate through the objects.
124 * @param IDatabase $db DB object to use for the query
126 abstract public function doQuery( $db );
129 * Create an item object from a DB result row
132 abstract public function newItem( $row );
136 * Abstract base class for revision items
138 abstract class RevisionItemBase
{
139 /** @var RevisionListBase The parent */
142 /** The database result row */
146 * @param RevisionListBase $list
147 * @param object $row DB result row
149 public function __construct( $list, $row ) {
155 * Get the DB field name associated with the ID list.
156 * Override this function.
159 public function getIdField() {
164 * Get the DB field name storing timestamps.
165 * Override this function.
168 public function getTimestampField() {
173 * Get the DB field name storing user ids.
174 * Override this function.
177 public function getAuthorIdField() {
182 * Get the DB field name storing user names.
183 * Override this function.
186 public function getAuthorNameField() {
191 * Get the ID, as it would appear in the ids URL parameter
194 public function getId() {
195 $field = $this->getIdField();
196 return $this->row
->$field;
200 * Get the date, formatted in user's language
203 public function formatDate() {
204 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
205 $this->list->getUser() );
209 * Get the time, formatted in user's language
212 public function formatTime() {
213 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
214 $this->list->getUser() );
218 * Get the timestamp in MW 14-char form
221 public function getTimestamp() {
222 $field = $this->getTimestampField();
223 return wfTimestamp( TS_MW
, $this->row
->$field );
227 * Get the author user ID
230 public function getAuthorId() {
231 $field = $this->getAuthorIdField();
232 return intval( $this->row
->$field );
236 * Get the author user name
239 public function getAuthorName() {
240 $field = $this->getAuthorNameField();
241 return strval( $this->row
->$field );
245 * Returns true if the current user can view the item
247 abstract public function canView();
250 * Returns true if the current user can view the item text/file
252 abstract public function canViewContent();
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.
258 abstract public function getHTML();
261 class RevisionList
extends RevisionListBase
{
262 public function getType() {
267 * @param IDatabase $db
270 public function doQuery( $db ) {
271 $conds = [ 'rev_page' => $this->title
->getArticleID() ];
272 if ( $this->ids
!== null ) {
273 $conds['rev_id'] = array_map( 'intval', $this->ids
);
276 [ 'revision', 'page', 'user' ],
277 array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
280 [ 'ORDER BY' => 'rev_id DESC' ],
282 'page' => Revision
::pageJoinCond(),
283 'user' => Revision
::userJoinCond() ]
287 public function newItem( $row ) {
288 return new RevisionItem( $this, $row );
293 * Item class for a live revision table row
295 class RevisionItem
extends RevisionItemBase
{
299 /** @var RequestContext */
302 public function __construct( $list, $row ) {
303 parent
::__construct( $list, $row );
304 $this->revision
= new Revision( $row );
305 $this->context
= $list->getContext();
308 public function getIdField() {
312 public function getTimestampField() {
313 return 'rev_timestamp';
316 public function getAuthorIdField() {
320 public function getAuthorNameField() {
321 return 'rev_user_text';
324 public function canView() {
325 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
, $this->context
->getUser() );
328 public function canViewContent() {
329 return $this->revision
->userCan( Revision
::DELETED_TEXT
, $this->context
->getUser() );
332 public function isDeleted() {
333 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
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
343 protected function getRevisionLink() {
344 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
345 $this->revision
->getTimestamp(), $this->list->getUser() ) );
347 if ( $this->isDeleted() && !$this->canViewContent() ) {
350 return Linker
::linkKnown(
355 'oldid' => $this->revision
->getId(),
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
368 protected function getDiffLink() {
369 if ( $this->isDeleted() && !$this->canViewContent() ) {
370 return $this->context
->msg( 'diff' )->escaped();
372 return Linker
::linkKnown(
374 $this->list->msg( 'diff' )->escaped(),
377 'diff' => $this->revision
->getId(),
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
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>";
400 return "<li>$difflink $revlink $userlink $comment</li>";