Merge "Request-local caching of DjVu dimensions"
[mediawiki.git] / includes / RevisionList.php
blob731d1b3e5dc2a9a19d28cce174b353d95b43d327
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 /** @var ResultWrapper|bool */
34 protected $res;
36 /** @var bool|object */
37 protected $current;
39 /**
40 * Construct a revision list for a given title
41 * @param IContextSource $context
42 * @param Title $title
44 function __construct( IContextSource $context, Title $title ) {
45 $this->setContext( $context );
46 $this->title = $title;
49 /**
50 * Select items only where the ID is any of the specified values
51 * @param array $ids
53 function filterByIds( array $ids ) {
54 $this->ids = $ids;
57 /**
58 * Get the internal type name of this list. Equal to the table name.
59 * Override this function.
60 * @return null
62 public function getType() {
63 return null;
66 /**
67 * Initialise the current iteration pointer
69 protected function initCurrent() {
70 $row = $this->res->current();
71 if ( $row ) {
72 $this->current = $this->newItem( $row );
73 } else {
74 $this->current = false;
78 /**
79 * Start iteration. This must be called before current() or next().
80 * @return Revision First list item
82 public function reset() {
83 if ( !$this->res ) {
84 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
85 } else {
86 $this->res->rewind();
88 $this->initCurrent();
89 return $this->current;
92 /**
93 * Get the current list item, or false if we are at the end
94 * @return Revision
96 public function current() {
97 return $this->current;
101 * Move the iteration pointer to the next list item, and return it.
102 * @return Revision
104 public function next() {
105 $this->res->next();
106 $this->initCurrent();
107 return $this->current;
111 * Get the number of items in the list.
112 * @return int
114 public function length() {
115 if ( !$this->res ) {
116 return 0;
117 } else {
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
130 * @param object $row
132 abstract public function newItem( $row );
136 * Abstract base class for revision items
138 abstract class RevisionItemBase {
139 /** @var RevisionListBase The parent */
140 protected $list;
142 /** The database result row */
143 protected $row;
146 * @param RevisionListBase $list
147 * @param object $row DB result row
149 public function __construct( $list, $row ) {
150 $this->list = $list;
151 $this->row = $row;
155 * Get the DB field name associated with the ID list.
156 * Override this function.
157 * @return null
159 public function getIdField() {
160 return null;
164 * Get the DB field name storing timestamps.
165 * Override this function.
166 * @return bool
168 public function getTimestampField() {
169 return false;
173 * Get the DB field name storing user ids.
174 * Override this function.
175 * @return bool
177 public function getAuthorIdField() {
178 return false;
182 * Get the DB field name storing user names.
183 * Override this function.
184 * @return bool
186 public function getAuthorNameField() {
187 return false;
191 * Get the ID, as it would appear in the ids URL parameter
192 * @return int
194 public function getId() {
195 $field = $this->getIdField();
196 return $this->row->$field;
200 * Get the date, formatted in user's language
201 * @return string
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
210 * @return string
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
219 * @return mixed
221 public function getTimestamp() {
222 $field = $this->getTimestampField();
223 return wfTimestamp( TS_MW, $this->row->$field );
227 * Get the author user ID
228 * @return int
230 public function getAuthorId() {
231 $field = $this->getAuthorIdField();
232 return intval( $this->row->$field );
236 * Get the author user name
237 * @return string
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() {
263 return 'revision';
267 * @param IDatabase $db
268 * @return mixed
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 );
275 return $db->select(
276 [ 'revision', 'page', 'user' ],
277 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
278 $conds,
279 __METHOD__,
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 {
296 /** @var Revision */
297 protected $revision;
299 /** @var RequestContext */
300 protected $context;
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() {
309 return 'rev_id';
312 public function getTimestampField() {
313 return 'rev_timestamp';
316 public function getAuthorIdField() {
317 return 'rev_user';
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
341 * @return string
343 protected function getRevisionLink() {
344 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
345 $this->revision->getTimestamp(), $this->list->getUser() ) );
347 if ( $this->isDeleted() && !$this->canViewContent() ) {
348 return $date;
350 return Linker::linkKnown(
351 $this->list->title,
352 $date,
355 'oldid' => $this->revision->getId(),
356 'unhide' => 1
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
366 * @return string
368 protected function getDiffLink() {
369 if ( $this->isDeleted() && !$this->canViewContent() ) {
370 return $this->context->msg( 'diff' )->escaped();
371 } else {
372 return Linker::linkKnown(
373 $this->list->title,
374 $this->list->msg( 'diff' )->escaped(),
377 'diff' => $this->revision->getId(),
378 'oldid' => 'prev',
379 'unhide' => 1
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
389 * @return string
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>";