Renamed Rev_List/Rev_Item classes to RevisionListBase/RevisionItemBase
[lhc/web/wiklou.git] / includes / RevisionList.php
1 <?php
2 /**
3 * List for revision table items for a single page
4 */
5 abstract class RevisionListBase {
6 /**
7 * @var Title
8 */
9 var $title;
10 /**
11 * @var RequestContext
12 */
13 var $context;
14
15 var $ids, $res, $current;
16
17 /**
18 * Construct a revision list for a given title
19 * @param $context RequestContext
20 * @param $title Title
21 */
22 function __construct( RequestContext $context, Title $title ) {
23 $this->context = $context;
24 $this->title = $title;
25 }
26
27 /**
28 * Select items only where the ID is any of the specified values
29 * @param $ids Array
30 */
31 function filterByIds( array $ids ) {
32 $this->ids = $ids;
33 }
34
35 /**
36 * Get the internal type name of this list. Equal to the table name.
37 * Override this function.
38 */
39 public function getType() {
40 return null;
41 }
42
43 /**
44 * Initialise the current iteration pointer
45 */
46 protected function initCurrent() {
47 $row = $this->res->current();
48 if ( $row ) {
49 $this->current = $this->newItem( $row );
50 } else {
51 $this->current = false;
52 }
53 }
54
55 /**
56 * Start iteration. This must be called before current() or next().
57 * @return First list item
58 */
59 public function reset() {
60 if ( !$this->res ) {
61 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
62 } else {
63 $this->res->rewind();
64 }
65 $this->initCurrent();
66 return $this->current;
67 }
68
69 /**
70 * Get the current list item, or false if we are at the end
71 */
72 public function current() {
73 return $this->current;
74 }
75
76 /**
77 * Move the iteration pointer to the next list item, and return it.
78 */
79 public function next() {
80 $this->res->next();
81 $this->initCurrent();
82 return $this->current;
83 }
84
85 /**
86 * Get the number of items in the list.
87 */
88 public function length() {
89 if( !$this->res ) {
90 return 0;
91 } else {
92 return $this->res->numRows();
93 }
94 }
95
96 /**
97 * Do the DB query to iterate through the objects.
98 * @param $db DatabaseBase object to use for the query
99 */
100 abstract public function doQuery( $db );
101
102 /**
103 * Create an item object from a DB result row
104 * @param $row stdclass
105 */
106 abstract public function newItem( $row );
107
108 /**
109 * Get the language of the user doing the action
110 *
111 * @return Language object
112 */
113 public function getLang() {
114 return $this->context->getLang();
115 }
116
117 /**
118 * Get the user doing the action
119 *
120 * @return User object
121 */
122 public function getUser() {
123 return $this->context->getUser();
124 }
125 }
126
127 /**
128 * Abstract base class for revision items
129 */
130 abstract class RevisionItemBase {
131 /** The parent RevisionListBase */
132 var $list;
133
134 /** The DB result row */
135 var $row;
136
137 /**
138 * @param $list RevisionListBase
139 * @param $row DB result row
140 */
141 public function __construct( $list, $row ) {
142 $this->list = $list;
143 $this->row = $row;
144 }
145
146 /**
147 * Get the DB field name associated with the ID list.
148 * Override this function.
149 */
150 public function getIdField() {
151 return null;
152 }
153
154 /**
155 * Get the DB field name storing timestamps.
156 * Override this function.
157 */
158 public function getTimestampField() {
159 return false;
160 }
161
162 /**
163 * Get the DB field name storing user ids.
164 * Override this function.
165 */
166 public function getAuthorIdField() {
167 return false;
168 }
169
170 /**
171 * Get the DB field name storing user names.
172 * Override this function.
173 */
174 public function getAuthorNameField() {
175 return false;
176 }
177
178 /**
179 * Get the ID, as it would appear in the ids URL parameter
180 */
181 public function getId() {
182 $field = $this->getIdField();
183 return $this->row->$field;
184 }
185
186 /**
187 * Get the date, formatted with $wgLang
188 */
189 public function formatDate() {
190 global $wgLang;
191 return $wgLang->date( $this->getTimestamp() );
192 }
193
194 /**
195 * Get the time, formatted with $wgLang
196 */
197 public function formatTime() {
198 global $wgLang;
199 return $wgLang->time( $this->getTimestamp() );
200 }
201
202 /**
203 * Get the timestamp in MW 14-char form
204 */
205 public function getTimestamp() {
206 $field = $this->getTimestampField();
207 return wfTimestamp( TS_MW, $this->row->$field );
208 }
209
210 /**
211 * Get the author user ID
212 */
213 public function getAuthorId() {
214 $field = $this->getAuthorIdField();
215 return intval( $this->row->$field );
216 }
217
218 /**
219 * Get the author user name
220 */
221 public function getAuthorName() {
222 $field = $this->getAuthorNameField();
223 return strval( $this->row->$field );
224 }
225
226 /**
227 * Returns true if the current user can view the item
228 */
229 abstract public function canView();
230
231 /**
232 * Returns true if the current user can view the item text/file
233 */
234 abstract public function canViewContent();
235
236 /**
237 * Get the HTML of the list item. Should be include <li></li> tags.
238 * This is used to show the list in HTML form, by the special page.
239 */
240 abstract public function getHTML();
241 }
242
243 class RevisionList extends RevisionListBase {
244 public function getType() {
245 return 'revision';
246 }
247
248 /**
249 * @param $db DatabaseBase
250 * @return mixed
251 */
252 public function doQuery( $db ) {
253 $conds = array(
254 'rev_page' => $this->title->getArticleID(),
255 'rev_page = page_id'
256 );
257 if ( $this->ids !== null ) {
258 $conds['rev_id'] = array_map( 'intval', $this->ids );
259 }
260 return $db->select(
261 array( 'revision', 'page' ),
262 '*',
263 $conds,
264 __METHOD__,
265 array( 'ORDER BY' => 'rev_id DESC' )
266 );
267 }
268
269 public function newItem( $row ) {
270 return new RevisionItem( $this, $row );
271 }
272 }
273
274 /**
275 * Item class for a live revision table row
276 */
277 class RevisionItem extends RevisionItemBase {
278 var $revision, $context;
279
280 public function __construct( $list, $row ) {
281 parent::__construct( $list, $row );
282 $this->revision = new Revision( $row );
283 $this->context = $list->context;
284 }
285
286 public function getIdField() {
287 return 'rev_id';
288 }
289
290 public function getTimestampField() {
291 return 'rev_timestamp';
292 }
293
294 public function getAuthorIdField() {
295 return 'rev_user';
296 }
297
298 public function getAuthorNameField() {
299 return 'rev_user_text';
300 }
301
302 public function canView() {
303 return $this->revision->userCan( Revision::DELETED_RESTRICTED );
304 }
305
306 public function canViewContent() {
307 return $this->revision->userCan( Revision::DELETED_TEXT );
308 }
309
310 public function isDeleted() {
311 return $this->revision->isDeleted( Revision::DELETED_TEXT );
312 }
313
314 /**
315 * Get the HTML link to the revision text.
316 * Overridden by RevDel_ArchiveItem.
317 */
318 protected function getRevisionLink() {
319 $date = $this->list->getLang()->timeanddate( $this->revision->getTimestamp(), true );
320 if ( $this->isDeleted() && !$this->canViewContent() ) {
321 return $date;
322 }
323 return Linker::link(
324 $this->list->title,
325 $date,
326 array(),
327 array(
328 'oldid' => $this->revision->getId(),
329 'unhide' => 1
330 )
331 );
332 }
333
334 /**
335 * Get the HTML link to the diff.
336 * Overridden by RevDel_ArchiveItem
337 */
338 protected function getDiffLink() {
339 if ( $this->isDeleted() && !$this->canViewContent() ) {
340 return wfMsgHtml('diff');
341 } else {
342 return
343 Linker::link(
344 $this->list->title,
345 wfMsgHtml('diff'),
346 array(),
347 array(
348 'diff' => $this->revision->getId(),
349 'oldid' => 'prev',
350 'unhide' => 1
351 ),
352 array(
353 'known',
354 'noclasses'
355 )
356 );
357 }
358 }
359
360 public function getHTML() {
361 $difflink = $this->getDiffLink();
362 $revlink = $this->getRevisionLink();
363 $userlink = Linker::revUserLink( $this->revision );
364 $comment = Linker::revComment( $this->revision );
365 if ( $this->isDeleted() ) {
366 $revlink = "<span class=\"history-deleted\">$revlink</span>";
367 }
368 return "<li>($difflink) $revlink $userlink $comment</li>";
369 }
370 }