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
23 use MediaWiki\MediaWikiServices
;
26 * List for revision table items for a single page
28 abstract class RevisionListBase
extends ContextSource
implements Iterator
{
35 /** @var ResultWrapper|bool */
38 /** @var bool|object */
42 * Construct a revision list for a given title
43 * @param IContextSource $context
46 function __construct( IContextSource
$context, Title
$title ) {
47 $this->setContext( $context );
48 $this->title
= $title;
52 * Select items only where the ID is any of the specified values
55 function filterByIds( array $ids ) {
60 * Get the internal type name of this list. Equal to the table name.
61 * Override this function.
64 public function getType() {
69 * Initialise the current iteration pointer
71 protected function initCurrent() {
72 $row = $this->res
->current();
74 $this->current
= $this->newItem( $row );
76 $this->current
= false;
81 * Start iteration. This must be called before current() or next().
82 * @return Revision First list item
84 public function reset() {
86 $this->res
= $this->doQuery( wfGetDB( DB_REPLICA
) );
91 return $this->current
;
94 public function rewind() {
99 * Get the current list item, or false if we are at the end
102 public function current() {
103 return $this->current
;
107 * Move the iteration pointer to the next list item, and return it.
110 public function next() {
112 $this->initCurrent();
113 return $this->current
;
116 public function key() {
117 return $this->res ?
$this->res
->key(): 0;
120 public function valid() {
121 return $this->res ?
$this->res
->valid() : false;
125 * Get the number of items in the list.
128 public function length() {
132 return $this->res
->numRows();
137 * Do the DB query to iterate through the objects.
138 * @param IDatabase $db DB object to use for the query
140 abstract public function doQuery( $db );
143 * Create an item object from a DB result row
146 abstract public function newItem( $row );
150 * Abstract base class for revision items
152 abstract class RevisionItemBase
{
153 /** @var RevisionListBase The parent */
156 /** The database result row */
160 * @param RevisionListBase $list
161 * @param object $row DB result row
163 public function __construct( $list, $row ) {
169 * Get the DB field name associated with the ID list.
170 * Override this function.
173 public function getIdField() {
178 * Get the DB field name storing timestamps.
179 * Override this function.
182 public function getTimestampField() {
187 * Get the DB field name storing user ids.
188 * Override this function.
191 public function getAuthorIdField() {
196 * Get the DB field name storing user names.
197 * Override this function.
200 public function getAuthorNameField() {
205 * Get the ID, as it would appear in the ids URL parameter
208 public function getId() {
209 $field = $this->getIdField();
210 return $this->row
->$field;
214 * Get the date, formatted in user's language
217 public function formatDate() {
218 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
219 $this->list->getUser() );
223 * Get the time, formatted in user's language
226 public function formatTime() {
227 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
228 $this->list->getUser() );
232 * Get the timestamp in MW 14-char form
235 public function getTimestamp() {
236 $field = $this->getTimestampField();
237 return wfTimestamp( TS_MW
, $this->row
->$field );
241 * Get the author user ID
244 public function getAuthorId() {
245 $field = $this->getAuthorIdField();
246 return intval( $this->row
->$field );
250 * Get the author user name
253 public function getAuthorName() {
254 $field = $this->getAuthorNameField();
255 return strval( $this->row
->$field );
259 * Returns true if the current user can view the item
261 abstract public function canView();
264 * Returns true if the current user can view the item text/file
266 abstract public function canViewContent();
269 * Get the HTML of the list item. Should be include "<li></li>" tags.
270 * This is used to show the list in HTML form, by the special page.
272 abstract public function getHTML();
275 * Returns an instance of LinkRenderer
276 * @return \MediaWiki\Linker\LinkRenderer
278 protected function getLinkRenderer() {
279 return MediaWikiServices
::getInstance()->getLinkRenderer();
283 class RevisionList
extends RevisionListBase
{
284 public function getType() {
289 * @param IDatabase $db
292 public function doQuery( $db ) {
293 $conds = [ 'rev_page' => $this->title
->getArticleID() ];
294 if ( $this->ids
!== null ) {
295 $conds['rev_id'] = array_map( 'intval', $this->ids
);
298 [ 'revision', 'page', 'user' ],
299 array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
302 [ 'ORDER BY' => 'rev_id DESC' ],
304 'page' => Revision
::pageJoinCond(),
305 'user' => Revision
::userJoinCond() ]
309 public function newItem( $row ) {
310 return new RevisionItem( $this, $row );
315 * Item class for a live revision table row
317 class RevisionItem
extends RevisionItemBase
{
321 /** @var RequestContext */
324 public function __construct( $list, $row ) {
325 parent
::__construct( $list, $row );
326 $this->revision
= new Revision( $row );
327 $this->context
= $list->getContext();
330 public function getIdField() {
334 public function getTimestampField() {
335 return 'rev_timestamp';
338 public function getAuthorIdField() {
342 public function getAuthorNameField() {
343 return 'rev_user_text';
346 public function canView() {
347 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
, $this->context
->getUser() );
350 public function canViewContent() {
351 return $this->revision
->userCan( Revision
::DELETED_TEXT
, $this->context
->getUser() );
354 public function isDeleted() {
355 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
359 * Get the HTML link to the revision text.
360 * @todo Essentially a copy of RevDelRevisionItem::getRevisionLink. That class
361 * should inherit from this one, and implement an appropriate interface instead
362 * of extending RevDelItem
365 protected function getRevisionLink() {
366 $date = $this->list->getLanguage()->userTimeAndDate(
367 $this->revision
->getTimestamp(), $this->list->getUser() );
369 if ( $this->isDeleted() && !$this->canViewContent() ) {
370 return htmlspecialchars( $date );
372 $linkRenderer = $this->getLinkRenderer();
373 return $linkRenderer->makeKnownLink(
378 'oldid' => $this->revision
->getId(),
385 * Get the HTML link to the diff.
386 * @todo Essentially a copy of RevDelRevisionItem::getDiffLink. That class
387 * should inherit from this one, and implement an appropriate interface instead
388 * of extending RevDelItem
391 protected function getDiffLink() {
392 if ( $this->isDeleted() && !$this->canViewContent() ) {
393 return $this->context
->msg( 'diff' )->escaped();
395 $linkRenderer = $this->getLinkRenderer();
396 return $linkRenderer->makeKnownLink(
398 $this->list->msg( 'diff' )->text(),
401 'diff' => $this->revision
->getId(),
410 * @todo Essentially a copy of RevDelRevisionItem::getHTML. That class
411 * should inherit from this one, and implement an appropriate interface instead
412 * of extending RevDelItem
415 public function getHTML() {
416 $difflink = $this->context
->msg( 'parentheses' )
417 ->rawParams( $this->getDiffLink() )->escaped();
418 $revlink = $this->getRevisionLink();
419 $userlink = Linker
::revUserLink( $this->revision
);
420 $comment = Linker
::revComment( $this->revision
);
421 if ( $this->isDeleted() ) {
422 $revlink = "<span class=\"history-deleted\">$revlink</span>";
424 return "<li>$difflink $revlink $userlink $comment</li>";