Merge "Swap isSpecialPage for canExist()"
[mediawiki.git] / includes / RevisionList.php
blob3c5cfa8ea28b81b7a130f5994e6140c8bb6067cc
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 /**
28 * @var Title
30 var $title;
32 var $ids, $res, $current;
34 /**
35 * Construct a revision list for a given title
36 * @param $context IContextSource
37 * @param $title Title
39 function __construct( IContextSource $context, Title $title ) {
40 $this->setContext( $context );
41 $this->title = $title;
44 /**
45 * Select items only where the ID is any of the specified values
46 * @param $ids Array
48 function filterByIds( array $ids ) {
49 $this->ids = $ids;
52 /**
53 * Get the internal type name of this list. Equal to the table name.
54 * Override this function.
55 * @return null
57 public function getType() {
58 return null;
61 /**
62 * Initialise the current iteration pointer
64 protected function initCurrent() {
65 $row = $this->res->current();
66 if ( $row ) {
67 $this->current = $this->newItem( $row );
68 } else {
69 $this->current = false;
73 /**
74 * Start iteration. This must be called before current() or next().
75 * @return First list item
77 public function reset() {
78 if ( !$this->res ) {
79 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
80 } else {
81 $this->res->rewind();
83 $this->initCurrent();
84 return $this->current;
87 /**
88 * Get the current list item, or false if we are at the end
90 public function current() {
91 return $this->current;
94 /**
95 * Move the iteration pointer to the next list item, and return it.
97 public function next() {
98 $this->res->next();
99 $this->initCurrent();
100 return $this->current;
104 * Get the number of items in the list.
105 * @return int
107 public function length() {
108 if( !$this->res ) {
109 return 0;
110 } else {
111 return $this->res->numRows();
116 * Do the DB query to iterate through the objects.
117 * @param $db DatabaseBase object to use for the query
119 abstract public function doQuery( $db );
122 * Create an item object from a DB result row
123 * @param $row stdclass
125 abstract public function newItem( $row );
129 * Abstract base class for revision items
131 abstract class RevisionItemBase {
132 /** The parent RevisionListBase */
133 var $list;
135 /** The DB result row */
136 var $row;
139 * @param $list RevisionListBase
140 * @param $row DB result row
142 public function __construct( $list, $row ) {
143 $this->list = $list;
144 $this->row = $row;
148 * Get the DB field name associated with the ID list.
149 * Override this function.
150 * @return null
152 public function getIdField() {
153 return null;
157 * Get the DB field name storing timestamps.
158 * Override this function.
159 * @return bool
161 public function getTimestampField() {
162 return false;
166 * Get the DB field name storing user ids.
167 * Override this function.
168 * @return bool
170 public function getAuthorIdField() {
171 return false;
175 * Get the DB field name storing user names.
176 * Override this function.
177 * @return bool
179 public function getAuthorNameField() {
180 return false;
184 * Get the ID, as it would appear in the ids URL parameter
185 * @return
187 public function getId() {
188 $field = $this->getIdField();
189 return $this->row->$field;
193 * Get the date, formatted in user's languae
194 * @return String
196 public function formatDate() {
197 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
198 $this->list->getUser() );
202 * Get the time, formatted in user's languae
203 * @return String
205 public function formatTime() {
206 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
207 $this->list->getUser() );
211 * Get the timestamp in MW 14-char form
212 * @return Mixed
214 public function getTimestamp() {
215 $field = $this->getTimestampField();
216 return wfTimestamp( TS_MW, $this->row->$field );
220 * Get the author user ID
221 * @return int
223 public function getAuthorId() {
224 $field = $this->getAuthorIdField();
225 return intval( $this->row->$field );
229 * Get the author user name
230 * @return string
232 public function getAuthorName() {
233 $field = $this->getAuthorNameField();
234 return strval( $this->row->$field );
238 * Returns true if the current user can view the item
240 abstract public function canView();
243 * Returns true if the current user can view the item text/file
245 abstract public function canViewContent();
248 * Get the HTML of the list item. Should be include "<li></li>" tags.
249 * This is used to show the list in HTML form, by the special page.
251 abstract public function getHTML();
254 class RevisionList extends RevisionListBase {
255 public function getType() {
256 return 'revision';
260 * @param $db DatabaseBase
261 * @return mixed
263 public function doQuery( $db ) {
264 $conds = array( 'rev_page' => $this->title->getArticleID() );
265 if ( $this->ids !== null ) {
266 $conds['rev_id'] = array_map( 'intval', $this->ids );
268 return $db->select(
269 array( 'revision', 'page', 'user' ),
270 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
271 $conds,
272 __METHOD__,
273 array( 'ORDER BY' => 'rev_id DESC' ),
274 array(
275 'page' => Revision::pageJoinCond(),
276 'user' => Revision::userJoinCond() )
280 public function newItem( $row ) {
281 return new RevisionItem( $this, $row );
286 * Item class for a live revision table row
288 class RevisionItem extends RevisionItemBase {
289 var $revision, $context;
291 public function __construct( $list, $row ) {
292 parent::__construct( $list, $row );
293 $this->revision = new Revision( $row );
294 $this->context = $list->getContext();
297 public function getIdField() {
298 return 'rev_id';
301 public function getTimestampField() {
302 return 'rev_timestamp';
305 public function getAuthorIdField() {
306 return 'rev_user';
309 public function getAuthorNameField() {
310 return 'user_name'; // see Revision::selectUserFields()
313 public function canView() {
314 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
317 public function canViewContent() {
318 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
321 public function isDeleted() {
322 return $this->revision->isDeleted( Revision::DELETED_TEXT );
326 * Get the HTML link to the revision text.
327 * Overridden by RevDel_ArchiveItem.
328 * @return string
330 protected function getRevisionLink() {
331 $date = $this->list->getLanguage()->timeanddate( $this->revision->getTimestamp(), true );
332 if ( $this->isDeleted() && !$this->canViewContent() ) {
333 return $date;
335 return Linker::link(
336 $this->list->title,
337 $date,
338 array(),
339 array(
340 'oldid' => $this->revision->getId(),
341 'unhide' => 1
347 * Get the HTML link to the diff.
348 * Overridden by RevDel_ArchiveItem
349 * @return string
351 protected function getDiffLink() {
352 if ( $this->isDeleted() && !$this->canViewContent() ) {
353 return $this->context->msg( 'diff' )->escaped();
354 } else {
355 return
356 Linker::link(
357 $this->list->title,
358 $this->context->msg( 'diff' )->escaped(),
359 array(),
360 array(
361 'diff' => $this->revision->getId(),
362 'oldid' => 'prev',
363 'unhide' => 1
365 array(
366 'known',
367 'noclasses'
373 public function getHTML() {
374 $difflink = $this->context->msg( 'parentheses' )
375 ->rawParams( $this->getDiffLink() )->escaped();
376 $revlink = $this->getRevisionLink();
377 $userlink = Linker::revUserLink( $this->revision );
378 $comment = Linker::revComment( $this->revision );
379 if ( $this->isDeleted() ) {
380 $revlink = "<span class=\"history-deleted\">$revlink</span>";
382 return "<li>$difflink $revlink $userlink $comment</li>";