JOIN to get user_name in Contributions, HistoryPage, RevisionList, RevisionDelete...
[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 IContextSource
12 */
13 var $context;
14
15 var $ids, $res, $current;
16
17 /**
18 * Construct a revision list for a given title
19 * @param $context IContextSource
20 * @param $title Title
21 */
22 function __construct( IContextSource $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 in user's languae
188 */
189 public function formatDate() {
190 return $this->list->context->getLang()->userDate( $this->getTimestamp(),
191 $this->list->context->getUser() );
192 }
193
194 /**
195 * Get the time, formatted in user's languae
196 */
197 public function formatTime() {
198 return $this->list->context->getLang()->userTime( $this->getTimestamp(),
199 $this->list->context->getUser() );
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( 'rev_page' => $this->title->getArticleID() );
254 if ( $this->ids !== null ) {
255 $conds['rev_id'] = array_map( 'intval', $this->ids );
256 }
257 return $db->select(
258 array( 'revision', 'page', 'user' ),
259 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
260 $conds,
261 __METHOD__,
262 array( 'ORDER BY' => 'rev_id DESC' ),
263 array( 'page' => array( 'INNER JOIN', 'rev_page = page_id' ),
264 'user' => array( 'LEFT JOIN', 'user_id = rev_user' ) )
265 );
266 }
267
268 public function newItem( $row ) {
269 return new RevisionItem( $this, $row );
270 }
271 }
272
273 /**
274 * Item class for a live revision table row
275 */
276 class RevisionItem extends RevisionItemBase {
277 var $revision, $context;
278
279 public function __construct( $list, $row ) {
280 parent::__construct( $list, $row );
281 $this->revision = new Revision( $row );
282 $this->context = $list->context;
283 }
284
285 public function getIdField() {
286 return 'rev_id';
287 }
288
289 public function getTimestampField() {
290 return 'rev_timestamp';
291 }
292
293 public function getAuthorIdField() {
294 return 'rev_user';
295 }
296
297 public function getAuthorNameField() {
298 return 'rev_user_name'; // see Revision::selectUserFields()
299 }
300
301 public function canView() {
302 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
303 }
304
305 public function canViewContent() {
306 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
307 }
308
309 public function isDeleted() {
310 return $this->revision->isDeleted( Revision::DELETED_TEXT );
311 }
312
313 /**
314 * Get the HTML link to the revision text.
315 * Overridden by RevDel_ArchiveItem.
316 */
317 protected function getRevisionLink() {
318 $date = $this->list->getLang()->timeanddate( $this->revision->getTimestamp(), true );
319 if ( $this->isDeleted() && !$this->canViewContent() ) {
320 return $date;
321 }
322 return Linker::link(
323 $this->list->title,
324 $date,
325 array(),
326 array(
327 'oldid' => $this->revision->getId(),
328 'unhide' => 1
329 )
330 );
331 }
332
333 /**
334 * Get the HTML link to the diff.
335 * Overridden by RevDel_ArchiveItem
336 */
337 protected function getDiffLink() {
338 if ( $this->isDeleted() && !$this->canViewContent() ) {
339 return wfMsgHtml('diff');
340 } else {
341 return
342 Linker::link(
343 $this->list->title,
344 wfMsgHtml('diff'),
345 array(),
346 array(
347 'diff' => $this->revision->getId(),
348 'oldid' => 'prev',
349 'unhide' => 1
350 ),
351 array(
352 'known',
353 'noclasses'
354 )
355 );
356 }
357 }
358
359 public function getHTML() {
360 $difflink = $this->getDiffLink();
361 $revlink = $this->getRevisionLink();
362 $userlink = Linker::revUserLink( $this->revision );
363 $comment = Linker::revComment( $this->revision );
364 if ( $this->isDeleted() ) {
365 $revlink = "<span class=\"history-deleted\">$revlink</span>";
366 }
367 return "<li>($difflink) $revlink $userlink $comment</li>";
368 }
369 }