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
{
35 /** @var bool|object */
39 * Construct a revision list for a given title
40 * @param IContextSource $context
43 function __construct( IContextSource
$context, Title
$title ) {
44 $this->setContext( $context );
45 $this->title
= $title;
49 * Select items only where the ID is any of the specified values
52 function filterByIds( array $ids ) {
57 * Get the internal type name of this list. Equal to the table name.
58 * Override this function.
61 public function getType() {
66 * Initialise the current iteration pointer
68 protected function initCurrent() {
69 $row = $this->res
->current();
71 $this->current
= $this->newItem( $row );
73 $this->current
= false;
78 * Start iteration. This must be called before current() or next().
79 * @return Revision First list item
81 public function reset() {
83 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
88 return $this->current
;
92 * Get the current list item, or false if we are at the end
95 public function current() {
96 return $this->current
;
100 * Move the iteration pointer to the next list item, and return it.
103 public function next() {
105 $this->initCurrent();
106 return $this->current
;
110 * Get the number of items in the list.
113 public function length() {
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
131 abstract public function newItem( $row );
135 * Abstract base class for revision items
137 abstract class RevisionItemBase
{
138 /** @var RevisionListBase The parent */
141 /** The database result row */
145 * @param RevisionListBase $list
146 * @param object $row DB result row
148 public function __construct( $list, $row ) {
154 * Get the DB field name associated with the ID list.
155 * Override this function.
158 public function getIdField() {
163 * Get the DB field name storing timestamps.
164 * Override this function.
167 public function getTimestampField() {
172 * Get the DB field name storing user ids.
173 * Override this function.
176 public function getAuthorIdField() {
181 * Get the DB field name storing user names.
182 * Override this function.
185 public function getAuthorNameField() {
190 * Get the ID, as it would appear in the ids URL parameter
193 public function getId() {
194 $field = $this->getIdField();
195 return $this->row
->$field;
199 * Get the date, formatted in user's language
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
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
220 public function getTimestamp() {
221 $field = $this->getTimestampField();
222 return wfTimestamp( TS_MW
, $this->row
->$field );
226 * Get the author user ID
229 public function getAuthorId() {
230 $field = $this->getAuthorIdField();
231 return intval( $this->row
->$field );
235 * Get the author user name
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() {
266 * @param DatabaseBase $db
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
);
275 array( 'revision', 'page', 'user' ),
276 array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
279 array( 'ORDER BY' => 'rev_id DESC' ),
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
{
298 /** @var RequestContext */
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() {
311 public function getTimestampField() {
312 return 'rev_timestamp';
315 public function getAuthorIdField() {
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.
340 protected function getRevisionLink() {
341 $date = $this->list->getLanguage()->timeanddate( $this->revision
->getTimestamp(), true );
342 if ( $this->isDeleted() && !$this->canViewContent() ) {
350 'oldid' => $this->revision
->getId(),
357 * Get the HTML link to the diff.
358 * Overridden by RevDelArchiveItem
361 protected function getDiffLink() {
362 if ( $this->isDeleted() && !$this->canViewContent() ) {
363 return $this->context
->msg( 'diff' )->escaped();
367 $this->context
->msg( 'diff' )->escaped(),
370 'diff' => $this->revision
->getId(),
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>";