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