Commit of various live hacks
[mediawiki.git] / includes / RevisionList.php
bloba61120e79f0749e39f9308b5e10e2582b23133c9
1 <?php
2 /**
3 * List for revision table items for a single page
4 */
5 abstract class RevisionListBase extends ContextSource {
6 /**
7 * @var Title
8 */
9 var $title;
11 var $ids, $res, $current;
13 /**
14 * Construct a revision list for a given title
15 * @param $context IContextSource
16 * @param $title Title
18 function __construct( IContextSource $context, Title $title ) {
19 $this->setContext( $context );
20 $this->title = $title;
23 /**
24 * Select items only where the ID is any of the specified values
25 * @param $ids Array
27 function filterByIds( array $ids ) {
28 $this->ids = $ids;
31 /**
32 * Get the internal type name of this list. Equal to the table name.
33 * Override this function.
34 * @return null
36 public function getType() {
37 return null;
40 /**
41 * Initialise the current iteration pointer
43 protected function initCurrent() {
44 $row = $this->res->current();
45 if ( $row ) {
46 $this->current = $this->newItem( $row );
47 } else {
48 $this->current = false;
52 /**
53 * Start iteration. This must be called before current() or next().
54 * @return First list item
56 public function reset() {
57 if ( !$this->res ) {
58 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
59 } else {
60 $this->res->rewind();
62 $this->initCurrent();
63 return $this->current;
66 /**
67 * Get the current list item, or false if we are at the end
69 public function current() {
70 return $this->current;
73 /**
74 * Move the iteration pointer to the next list item, and return it.
76 public function next() {
77 $this->res->next();
78 $this->initCurrent();
79 return $this->current;
82 /**
83 * Get the number of items in the list.
84 * @return int
86 public function length() {
87 if( !$this->res ) {
88 return 0;
89 } else {
90 return $this->res->numRows();
94 /**
95 * Do the DB query to iterate through the objects.
96 * @param $db DatabaseBase object to use for the query
98 abstract public function doQuery( $db );
101 * Create an item object from a DB result row
102 * @param $row stdclass
104 abstract public function newItem( $row );
108 * Abstract base class for revision items
110 abstract class RevisionItemBase {
111 /** The parent RevisionListBase */
112 var $list;
114 /** The DB result row */
115 var $row;
118 * @param $list RevisionListBase
119 * @param $row DB result row
121 public function __construct( $list, $row ) {
122 $this->list = $list;
123 $this->row = $row;
127 * Get the DB field name associated with the ID list.
128 * Override this function.
129 * @return null
131 public function getIdField() {
132 return null;
136 * Get the DB field name storing timestamps.
137 * Override this function.
138 * @return bool
140 public function getTimestampField() {
141 return false;
145 * Get the DB field name storing user ids.
146 * Override this function.
147 * @return bool
149 public function getAuthorIdField() {
150 return false;
154 * Get the DB field name storing user names.
155 * Override this function.
156 * @return bool
158 public function getAuthorNameField() {
159 return false;
163 * Get the ID, as it would appear in the ids URL parameter
164 * @return
166 public function getId() {
167 $field = $this->getIdField();
168 return $this->row->$field;
172 * Get the date, formatted in user's languae
173 * @return String
175 public function formatDate() {
176 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
177 $this->list->getUser() );
181 * Get the time, formatted in user's languae
182 * @return String
184 public function formatTime() {
185 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
186 $this->list->getUser() );
190 * Get the timestamp in MW 14-char form
191 * @return Mixed
193 public function getTimestamp() {
194 $field = $this->getTimestampField();
195 return wfTimestamp( TS_MW, $this->row->$field );
199 * Get the author user ID
200 * @return int
202 public function getAuthorId() {
203 $field = $this->getAuthorIdField();
204 return intval( $this->row->$field );
208 * Get the author user name
209 * @return string
211 public function getAuthorName() {
212 $field = $this->getAuthorNameField();
213 return strval( $this->row->$field );
217 * Returns true if the current user can view the item
219 abstract public function canView();
222 * Returns true if the current user can view the item text/file
224 abstract public function canViewContent();
227 * Get the HTML of the list item. Should be include <li></li> tags.
228 * This is used to show the list in HTML form, by the special page.
230 abstract public function getHTML();
233 class RevisionList extends RevisionListBase {
234 public function getType() {
235 return 'revision';
239 * @param $db DatabaseBase
240 * @return mixed
242 public function doQuery( $db ) {
243 $conds = array( 'rev_page' => $this->title->getArticleID() );
244 if ( $this->ids !== null ) {
245 $conds['rev_id'] = array_map( 'intval', $this->ids );
247 return $db->select(
248 array( 'revision', 'page', 'user' ),
249 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
250 $conds,
251 __METHOD__,
252 array( 'ORDER BY' => 'rev_id DESC' ),
253 array(
254 'page' => Revision::pageJoinCond(),
255 'user' => Revision::userJoinCond() )
259 public function newItem( $row ) {
260 return new RevisionItem( $this, $row );
265 * Item class for a live revision table row
267 class RevisionItem extends RevisionItemBase {
268 var $revision, $context;
270 public function __construct( $list, $row ) {
271 parent::__construct( $list, $row );
272 $this->revision = new Revision( $row );
273 $this->context = $list->getContext();
276 public function getIdField() {
277 return 'rev_id';
280 public function getTimestampField() {
281 return 'rev_timestamp';
284 public function getAuthorIdField() {
285 return 'rev_user';
288 public function getAuthorNameField() {
289 return 'user_name'; // see Revision::selectUserFields()
292 public function canView() {
293 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
296 public function canViewContent() {
297 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
300 public function isDeleted() {
301 return $this->revision->isDeleted( Revision::DELETED_TEXT );
305 * Get the HTML link to the revision text.
306 * Overridden by RevDel_ArchiveItem.
307 * @return string
309 protected function getRevisionLink() {
310 $date = $this->list->getLanguage()->timeanddate( $this->revision->getTimestamp(), true );
311 if ( $this->isDeleted() && !$this->canViewContent() ) {
312 return $date;
314 return Linker::link(
315 $this->list->title,
316 $date,
317 array(),
318 array(
319 'oldid' => $this->revision->getId(),
320 'unhide' => 1
326 * Get the HTML link to the diff.
327 * Overridden by RevDel_ArchiveItem
328 * @return string
330 protected function getDiffLink() {
331 if ( $this->isDeleted() && !$this->canViewContent() ) {
332 return wfMsgHtml('diff');
333 } else {
334 return
335 Linker::link(
336 $this->list->title,
337 wfMsgHtml('diff'),
338 array(),
339 array(
340 'diff' => $this->revision->getId(),
341 'oldid' => 'prev',
342 'unhide' => 1
344 array(
345 'known',
346 'noclasses'
352 public function getHTML() {
353 $difflink = $this->getDiffLink();
354 $revlink = $this->getRevisionLink();
355 $userlink = Linker::revUserLink( $this->revision );
356 $comment = Linker::revComment( $this->revision );
357 if ( $this->isDeleted() ) {
358 $revlink = "<span class=\"history-deleted\">$revlink</span>";
360 return "<li>($difflink) $revlink $userlink $comment</li>";