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