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