resourceloader: Add @covers and minor clean up of test suites
[mediawiki.git] / includes / RevisionList.php
blobd10b5412cb19d48f09ffb815ef16b178fc0de0ef
1 <?php
2 /**
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
20 * @file
23 /**
24 * List for revision table items for a single page
26 abstract class RevisionListBase extends ContextSource {
27 /** @var Title */
28 public $title;
30 /** @var array */
31 protected $ids;
33 protected $res;
35 /** @var bool|object */
36 protected $current;
38 /**
39 * Construct a revision list for a given title
40 * @param IContextSource $context
41 * @param Title $title
43 function __construct( IContextSource $context, Title $title ) {
44 $this->setContext( $context );
45 $this->title = $title;
48 /**
49 * Select items only where the ID is any of the specified values
50 * @param array $ids
52 function filterByIds( array $ids ) {
53 $this->ids = $ids;
56 /**
57 * Get the internal type name of this list. Equal to the table name.
58 * Override this function.
59 * @return null
61 public function getType() {
62 return null;
65 /**
66 * Initialise the current iteration pointer
68 protected function initCurrent() {
69 $row = $this->res->current();
70 if ( $row ) {
71 $this->current = $this->newItem( $row );
72 } else {
73 $this->current = false;
77 /**
78 * Start iteration. This must be called before current() or next().
79 * @return Revision First list item
81 public function reset() {
82 if ( !$this->res ) {
83 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
84 } else {
85 $this->res->rewind();
87 $this->initCurrent();
88 return $this->current;
91 /**
92 * Get the current list item, or false if we are at the end
93 * @return Revision
95 public function current() {
96 return $this->current;
99 /**
100 * Move the iteration pointer to the next list item, and return it.
101 * @return Revision
103 public function next() {
104 $this->res->next();
105 $this->initCurrent();
106 return $this->current;
110 * Get the number of items in the list.
111 * @return int
113 public function length() {
114 if ( !$this->res ) {
115 return 0;
116 } else {
117 return $this->res->numRows();
122 * Do the DB query to iterate through the objects.
123 * @param DatabaseBase $db DatabaseBase object to use for the query
125 abstract public function doQuery( $db );
128 * Create an item object from a DB result row
129 * @param object $row
131 abstract public function newItem( $row );
135 * Abstract base class for revision items
137 abstract class RevisionItemBase {
138 /** @var RevisionListBase The parent */
139 protected $list;
141 /** The database result row */
142 protected $row;
145 * @param RevisionListBase $list
146 * @param object $row DB result row
148 public function __construct( $list, $row ) {
149 $this->list = $list;
150 $this->row = $row;
154 * Get the DB field name associated with the ID list.
155 * Override this function.
156 * @return null
158 public function getIdField() {
159 return null;
163 * Get the DB field name storing timestamps.
164 * Override this function.
165 * @return bool
167 public function getTimestampField() {
168 return false;
172 * Get the DB field name storing user ids.
173 * Override this function.
174 * @return bool
176 public function getAuthorIdField() {
177 return false;
181 * Get the DB field name storing user names.
182 * Override this function.
183 * @return bool
185 public function getAuthorNameField() {
186 return false;
190 * Get the ID, as it would appear in the ids URL parameter
191 * @return int
193 public function getId() {
194 $field = $this->getIdField();
195 return $this->row->$field;
199 * Get the date, formatted in user's language
200 * @return string
202 public function formatDate() {
203 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
204 $this->list->getUser() );
208 * Get the time, formatted in user's language
209 * @return string
211 public function formatTime() {
212 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
213 $this->list->getUser() );
217 * Get the timestamp in MW 14-char form
218 * @return mixed
220 public function getTimestamp() {
221 $field = $this->getTimestampField();
222 return wfTimestamp( TS_MW, $this->row->$field );
226 * Get the author user ID
227 * @return int
229 public function getAuthorId() {
230 $field = $this->getAuthorIdField();
231 return intval( $this->row->$field );
235 * Get the author user name
236 * @return string
238 public function getAuthorName() {
239 $field = $this->getAuthorNameField();
240 return strval( $this->row->$field );
244 * Returns true if the current user can view the item
246 abstract public function canView();
249 * Returns true if the current user can view the item text/file
251 abstract public function canViewContent();
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.
257 abstract public function getHTML();
260 class RevisionList extends RevisionListBase {
261 public function getType() {
262 return 'revision';
266 * @param DatabaseBase $db
267 * @return mixed
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 );
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() )
286 public function newItem( $row ) {
287 return new RevisionItem( $this, $row );
292 * Item class for a live revision table row
294 class RevisionItem extends RevisionItemBase {
295 /** @var Revision */
296 protected $revision;
298 /** @var RequestContext */
299 protected $context;
301 public function __construct( $list, $row ) {
302 parent::__construct( $list, $row );
303 $this->revision = new Revision( $row );
304 $this->context = $list->getContext();
307 public function getIdField() {
308 return 'rev_id';
311 public function getTimestampField() {
312 return 'rev_timestamp';
315 public function getAuthorIdField() {
316 return 'rev_user';
319 public function getAuthorNameField() {
320 return 'user_name'; // see Revision::selectUserFields()
323 public function canView() {
324 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
327 public function canViewContent() {
328 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
331 public function isDeleted() {
332 return $this->revision->isDeleted( Revision::DELETED_TEXT );
336 * Get the HTML link to the revision text.
337 * Overridden by RevDelArchiveItem.
338 * @return string
340 protected function getRevisionLink() {
341 $date = $this->list->getLanguage()->timeanddate( $this->revision->getTimestamp(), true );
342 if ( $this->isDeleted() && !$this->canViewContent() ) {
343 return $date;
345 return Linker::link(
346 $this->list->title,
347 $date,
348 array(),
349 array(
350 'oldid' => $this->revision->getId(),
351 'unhide' => 1
357 * Get the HTML link to the diff.
358 * Overridden by RevDelArchiveItem
359 * @return string
361 protected function getDiffLink() {
362 if ( $this->isDeleted() && !$this->canViewContent() ) {
363 return $this->context->msg( 'diff' )->escaped();
364 } else {
365 return Linker::link(
366 $this->list->title,
367 $this->context->msg( 'diff' )->escaped(),
368 array(),
369 array(
370 'diff' => $this->revision->getId(),
371 'oldid' => 'prev',
372 'unhide' => 1
374 array(
375 'known',
376 'noclasses'
382 public function getHTML() {
383 $difflink = $this->context->msg( 'parentheses' )
384 ->rawParams( $this->getDiffLink() )->escaped();
385 $revlink = $this->getRevisionLink();
386 $userlink = Linker::revUserLink( $this->revision );
387 $comment = Linker::revComment( $this->revision );
388 if ( $this->isDeleted() ) {
389 $revlink = "<span class=\"history-deleted\">$revlink</span>";
391 return "<li>$difflink $revlink $userlink $comment</li>";