Merge "Add support for 'hu-formal'"
[lhc/web/wiklou.git] / includes / RevisionList.php
1 <?php
2 /**
3 * Holders of revision list for a single page
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24 use Wikimedia\Rdbms\ResultWrapper;
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
28 * List for revision table items for a single page
29 */
30 abstract class RevisionListBase extends ContextSource implements Iterator {
31 /** @var Title */
32 public $title;
33
34 /** @var array */
35 protected $ids;
36
37 /** @var ResultWrapper|bool */
38 protected $res;
39
40 /** @var bool|Revision */
41 protected $current;
42
43 /**
44 * Construct a revision list for a given title
45 * @param IContextSource $context
46 * @param Title $title
47 */
48 function __construct( IContextSource $context, Title $title ) {
49 $this->setContext( $context );
50 $this->title = $title;
51 }
52
53 /**
54 * Select items only where the ID is any of the specified values
55 * @param array $ids
56 */
57 function filterByIds( array $ids ) {
58 $this->ids = $ids;
59 }
60
61 /**
62 * Get the internal type name of this list. Equal to the table name.
63 * Override this function.
64 * @return null
65 */
66 public function getType() {
67 return null;
68 }
69
70 /**
71 * Initialise the current iteration pointer
72 */
73 protected function initCurrent() {
74 $row = $this->res->current();
75 if ( $row ) {
76 $this->current = $this->newItem( $row );
77 } else {
78 $this->current = false;
79 }
80 }
81
82 /**
83 * Start iteration. This must be called before current() or next().
84 * @return Revision First list item
85 */
86 public function reset() {
87 if ( !$this->res ) {
88 $this->res = $this->doQuery( wfGetDB( DB_REPLICA ) );
89 } else {
90 $this->res->rewind();
91 }
92 $this->initCurrent();
93 return $this->current;
94 }
95
96 public function rewind() {
97 $this->reset();
98 }
99
100 /**
101 * Get the current list item, or false if we are at the end
102 * @return Revision
103 */
104 public function current() {
105 return $this->current;
106 }
107
108 /**
109 * Move the iteration pointer to the next list item, and return it.
110 * @return Revision
111 */
112 public function next() {
113 $this->res->next();
114 $this->initCurrent();
115 return $this->current;
116 }
117
118 public function key() {
119 return $this->res ? $this->res->key() : 0;
120 }
121
122 public function valid() {
123 return $this->res ? $this->res->valid() : false;
124 }
125
126 /**
127 * Get the number of items in the list.
128 * @return int
129 */
130 public function length() {
131 if ( !$this->res ) {
132 return 0;
133 } else {
134 return $this->res->numRows();
135 }
136 }
137
138 /**
139 * Do the DB query to iterate through the objects.
140 * @param IDatabase $db DB object to use for the query
141 */
142 abstract public function doQuery( $db );
143
144 /**
145 * Create an item object from a DB result row
146 * @param object $row
147 */
148 abstract public function newItem( $row );
149 }
150
151 /**
152 * Abstract base class for revision items
153 */
154 abstract class RevisionItemBase {
155 /** @var RevisionListBase The parent */
156 protected $list;
157
158 /** The database result row */
159 protected $row;
160
161 /**
162 * @param RevisionListBase $list
163 * @param object $row DB result row
164 */
165 public function __construct( $list, $row ) {
166 $this->list = $list;
167 $this->row = $row;
168 }
169
170 /**
171 * Get the DB field name associated with the ID list.
172 * Override this function.
173 * @return null
174 */
175 public function getIdField() {
176 return null;
177 }
178
179 /**
180 * Get the DB field name storing timestamps.
181 * Override this function.
182 * @return bool
183 */
184 public function getTimestampField() {
185 return false;
186 }
187
188 /**
189 * Get the DB field name storing user ids.
190 * Override this function.
191 * @return bool
192 */
193 public function getAuthorIdField() {
194 return false;
195 }
196
197 /**
198 * Get the DB field name storing user names.
199 * Override this function.
200 * @return bool
201 */
202 public function getAuthorNameField() {
203 return false;
204 }
205
206 /**
207 * Get the DB field name storing actor ids.
208 * Override this function.
209 * @since 1.31
210 * @return bool
211 */
212 public function getAuthorActorField() {
213 return false;
214 }
215
216 /**
217 * Get the ID, as it would appear in the ids URL parameter
218 * @return int
219 */
220 public function getId() {
221 $field = $this->getIdField();
222 return $this->row->$field;
223 }
224
225 /**
226 * Get the date, formatted in user's language
227 * @return string
228 */
229 public function formatDate() {
230 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
231 $this->list->getUser() );
232 }
233
234 /**
235 * Get the time, formatted in user's language
236 * @return string
237 */
238 public function formatTime() {
239 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
240 $this->list->getUser() );
241 }
242
243 /**
244 * Get the timestamp in MW 14-char form
245 * @return mixed
246 */
247 public function getTimestamp() {
248 $field = $this->getTimestampField();
249 return wfTimestamp( TS_MW, $this->row->$field );
250 }
251
252 /**
253 * Get the author user ID
254 * @return int
255 */
256 public function getAuthorId() {
257 $field = $this->getAuthorIdField();
258 return intval( $this->row->$field );
259 }
260
261 /**
262 * Get the author user name
263 * @return string
264 */
265 public function getAuthorName() {
266 $field = $this->getAuthorNameField();
267 return strval( $this->row->$field );
268 }
269
270 /**
271 * Get the author actor ID
272 * @since 1.31
273 * @return string
274 */
275 public function getAuthorActor() {
276 $field = $this->getAuthorActorField();
277 return strval( $this->row->$field );
278 }
279
280 /**
281 * Returns true if the current user can view the item
282 */
283 abstract public function canView();
284
285 /**
286 * Returns true if the current user can view the item text/file
287 */
288 abstract public function canViewContent();
289
290 /**
291 * Get the HTML of the list item. Should be include "<li></li>" tags.
292 * This is used to show the list in HTML form, by the special page.
293 */
294 abstract public function getHTML();
295
296 /**
297 * Returns an instance of LinkRenderer
298 * @return \MediaWiki\Linker\LinkRenderer
299 */
300 protected function getLinkRenderer() {
301 return MediaWikiServices::getInstance()->getLinkRenderer();
302 }
303 }
304
305 class RevisionList extends RevisionListBase {
306 public function getType() {
307 return 'revision';
308 }
309
310 /**
311 * @param IDatabase $db
312 * @return mixed
313 */
314 public function doQuery( $db ) {
315 $conds = [ 'rev_page' => $this->title->getArticleID() ];
316 if ( $this->ids !== null ) {
317 $conds['rev_id'] = array_map( 'intval', $this->ids );
318 }
319 $revQuery = Revision::getQueryInfo( [ 'page', 'user' ] );
320 return $db->select(
321 $revQuery['tables'],
322 $revQuery['fields'],
323 $conds,
324 __METHOD__,
325 [ 'ORDER BY' => 'rev_id DESC' ],
326 $revQuery['joins']
327 );
328 }
329
330 public function newItem( $row ) {
331 return new RevisionItem( $this, $row );
332 }
333 }
334
335 /**
336 * Item class for a live revision table row
337 */
338 class RevisionItem extends RevisionItemBase {
339 /** @var Revision */
340 protected $revision;
341
342 /** @var RequestContext */
343 protected $context;
344
345 public function __construct( $list, $row ) {
346 parent::__construct( $list, $row );
347 $this->revision = new Revision( $row );
348 $this->context = $list->getContext();
349 }
350
351 public function getIdField() {
352 return 'rev_id';
353 }
354
355 public function getTimestampField() {
356 return 'rev_timestamp';
357 }
358
359 public function getAuthorIdField() {
360 return 'rev_user';
361 }
362
363 public function getAuthorNameField() {
364 return 'rev_user_text';
365 }
366
367 public function canView() {
368 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
369 }
370
371 public function canViewContent() {
372 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
373 }
374
375 public function isDeleted() {
376 return $this->revision->isDeleted( Revision::DELETED_TEXT );
377 }
378
379 /**
380 * Get the HTML link to the revision text.
381 * @todo Essentially a copy of RevDelRevisionItem::getRevisionLink. That class
382 * should inherit from this one, and implement an appropriate interface instead
383 * of extending RevDelItem
384 * @return string
385 */
386 protected function getRevisionLink() {
387 $date = $this->list->getLanguage()->userTimeAndDate(
388 $this->revision->getTimestamp(), $this->list->getUser() );
389
390 if ( $this->isDeleted() && !$this->canViewContent() ) {
391 return htmlspecialchars( $date );
392 }
393 $linkRenderer = $this->getLinkRenderer();
394 return $linkRenderer->makeKnownLink(
395 $this->list->title,
396 $date,
397 [],
398 [
399 'oldid' => $this->revision->getId(),
400 'unhide' => 1
401 ]
402 );
403 }
404
405 /**
406 * Get the HTML link to the diff.
407 * @todo Essentially a copy of RevDelRevisionItem::getDiffLink. That class
408 * should inherit from this one, and implement an appropriate interface instead
409 * of extending RevDelItem
410 * @return string
411 */
412 protected function getDiffLink() {
413 if ( $this->isDeleted() && !$this->canViewContent() ) {
414 return $this->context->msg( 'diff' )->escaped();
415 } else {
416 $linkRenderer = $this->getLinkRenderer();
417 return $linkRenderer->makeKnownLink(
418 $this->list->title,
419 $this->list->msg( 'diff' )->text(),
420 [],
421 [
422 'diff' => $this->revision->getId(),
423 'oldid' => 'prev',
424 'unhide' => 1
425 ]
426 );
427 }
428 }
429
430 /**
431 * @todo Essentially a copy of RevDelRevisionItem::getHTML. That class
432 * should inherit from this one, and implement an appropriate interface instead
433 * of extending RevDelItem
434 * @return string
435 */
436 public function getHTML() {
437 $difflink = $this->context->msg( 'parentheses' )
438 ->rawParams( $this->getDiffLink() )->escaped();
439 $revlink = $this->getRevisionLink();
440 $userlink = Linker::revUserLink( $this->revision );
441 $comment = Linker::revComment( $this->revision );
442 if ( $this->isDeleted() ) {
443 $revlink = "<span class=\"history-deleted\">$revlink</span>";
444 }
445 return "<li>$difflink $revlink $userlink $comment</li>";
446 }
447 }