Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / includes / page / ImageHistoryList.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * Builds the image revision log shown on image pages
25 *
26 * @ingroup Media
27 */
28 class ImageHistoryList extends ContextSource {
29
30 /**
31 * @var Title
32 */
33 protected $title;
34
35 /**
36 * @var File
37 */
38 protected $img;
39
40 /**
41 * @var ImagePage
42 */
43 protected $imagePage;
44
45 /**
46 * @var File
47 */
48 protected $current;
49
50 protected $repo, $showThumb;
51 protected $preventClickjacking = false;
52
53 /**
54 * @param ImagePage $imagePage
55 */
56 public function __construct( $imagePage ) {
57 $context = $imagePage->getContext();
58 $this->current = $imagePage->getPage()->getFile();
59 $this->img = $imagePage->getDisplayedFile();
60 $this->title = $imagePage->getTitle();
61 $this->imagePage = $imagePage;
62 $this->showThumb = $context->getConfig()->get( 'ShowArchiveThumbnails' ) &&
63 $this->img->canRender();
64 $this->setContext( $context );
65 }
66
67 /**
68 * @return ImagePage
69 */
70 public function getImagePage() {
71 return $this->imagePage;
72 }
73
74 /**
75 * @return File
76 */
77 public function getFile() {
78 return $this->img;
79 }
80
81 /**
82 * @param string $navLinks
83 * @return string
84 */
85 public function beginImageHistoryList( $navLinks = '' ) {
86 return Xml::element( 'h2', [ 'id' => 'filehistory' ], $this->msg( 'filehist' )->text() )
87 . "\n"
88 . "<div id=\"mw-imagepage-section-filehistory\">\n"
89 . $this->msg( 'filehist-help' )->parseAsBlock()
90 . $navLinks . "\n"
91 . Xml::openElement( 'table', [ 'class' => 'wikitable filehistory' ] ) . "\n"
92 . '<tr><th></th>'
93 . ( $this->current->isLocal()
94 && ( MediaWikiServices::getInstance()
95 ->getPermissionManager()
96 ->userHasAnyRight( $this->getUser(), 'delete', 'deletedhistory' ) ) ? '<th></th>' : '' )
97 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
98 . ( $this->showThumb ? '<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
99 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
100 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
101 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
102 . "</tr>\n";
103 }
104
105 /**
106 * @param string $navLinks
107 * @return string
108 */
109 public function endImageHistoryList( $navLinks = '' ) {
110 return "</table>\n$navLinks\n</div>\n";
111 }
112
113 /**
114 * @param bool $iscur
115 * @param File $file
116 * @return string
117 */
118 public function imageHistoryLine( $iscur, $file ) {
119 $user = $this->getUser();
120 $lang = $this->getLanguage();
121 $pm = MediaWikiServices::getInstance()->getPermissionManager();
122 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
123 $img = $iscur ? $file->getName() : $file->getArchiveName();
124 $userId = $file->getUser( 'id' );
125 $userText = $file->getUser( 'text' );
126 $description = $file->getDescription( File::FOR_THIS_USER, $user );
127
128 $local = $this->current->isLocal();
129 $row = $selected = '';
130
131 // Deletion link
132 if ( $local && ( $pm->userHasAnyRight( $user, 'delete', 'deletedhistory' ) )
133 ) {
134 $row .= '<td>';
135 # Link to remove from history
136 if ( $user->isAllowed( 'delete' ) ) {
137 $q = [ 'action' => 'delete' ];
138 if ( !$iscur ) {
139 $q['oldimage'] = $img;
140 }
141 $row .= Linker::linkKnown(
142 $this->title,
143 $this->msg( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
144 [], $q
145 );
146 }
147 # Link to hide content. Don't show useless link to people who cannot hide revisions.
148 $canHide = $user->isAllowed( 'deleterevision' );
149 if ( $canHide || ( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
150 if ( $user->isAllowed( 'delete' ) ) {
151 $row .= '<br />';
152 }
153 // If file is top revision or locked from this user, don't link
154 if ( $iscur || !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
155 $del = Linker::revDeleteLinkDisabled( $canHide );
156 } else {
157 list( $ts, ) = explode( '!', $img, 2 );
158 $query = [
159 'type' => 'oldimage',
160 'target' => $this->title->getPrefixedText(),
161 'ids' => $ts,
162 ];
163 $del = Linker::revDeleteLink( $query,
164 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
165 }
166 $row .= $del;
167 }
168 $row .= '</td>';
169 }
170
171 // Reversion link/current indicator
172 $row .= '<td>';
173 if ( $iscur ) {
174 $row .= $this->msg( 'filehist-current' )->escaped();
175 } elseif ( $local && $pm->quickUserCan( 'edit', $user, $this->title )
176 && $pm->quickUserCan( 'upload', $user, $this->title )
177 ) {
178 if ( $file->isDeleted( File::DELETED_FILE ) ) {
179 $row .= $this->msg( 'filehist-revert' )->escaped();
180 } else {
181 $row .= Linker::linkKnown(
182 $this->title,
183 $this->msg( 'filehist-revert' )->escaped(),
184 [],
185 [
186 'action' => 'revert',
187 'oldimage' => $img,
188 ]
189 );
190 }
191 }
192 $row .= '</td>';
193
194 // Date/time and image link
195 if ( $file->getTimestamp() === $this->img->getTimestamp() ) {
196 $selected = "class='filehistory-selected'";
197 }
198 $row .= "<td $selected style='white-space: nowrap;'>";
199 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
200 # Don't link to unviewable files
201 $row .= Html::element( 'span', [ 'class' => 'history-deleted' ],
202 $lang->userTimeAndDate( $timestamp, $user )
203 );
204 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
205 $timeAndDate = htmlspecialchars( $lang->userTimeAndDate( $timestamp, $user ) );
206 if ( $local ) {
207 $this->preventClickjacking();
208 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
209 # Make a link to review the image
210 $url = Linker::linkKnown(
211 $revdel,
212 $timeAndDate,
213 [],
214 [
215 'target' => $this->title->getPrefixedText(),
216 'file' => $img,
217 'token' => $user->getEditToken( $img )
218 ]
219 );
220 } else {
221 $url = $timeAndDate;
222 }
223 $row .= '<span class="history-deleted">' . $url . '</span>';
224 } elseif ( !$file->exists() ) {
225 $row .= Html::element( 'span', [ 'class' => 'mw-file-missing' ],
226 $lang->userTimeAndDate( $timestamp, $user )
227 );
228 } else {
229 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
230 $row .= Xml::element(
231 'a',
232 [ 'href' => $url ],
233 $lang->userTimeAndDate( $timestamp, $user )
234 );
235 }
236 $row .= "</td>";
237
238 // Thumbnail
239 if ( $this->showThumb ) {
240 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
241 }
242
243 // Image dimensions + size
244 $row .= '<td>';
245 $row .= htmlspecialchars( $file->getDimensionsString() );
246 $row .= $this->msg( 'word-separator' )->escaped();
247 $row .= '<span style="white-space: nowrap;">';
248 $row .= $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->escaped();
249 $row .= '</span>';
250 $row .= '</td>';
251
252 // Uploading user
253 $row .= '<td>';
254 // Hide deleted usernames
255 if ( $file->isDeleted( File::DELETED_USER ) ) {
256 $row .= '<span class="history-deleted">'
257 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
258 } else {
259 if ( $local ) {
260 $row .= Linker::userLink( $userId, $userText );
261 $row .= '<span style="white-space: nowrap;">';
262 $row .= Linker::userToolLinks( $userId, $userText );
263 $row .= '</span>';
264 } else {
265 $row .= htmlspecialchars( $userText );
266 }
267 }
268 $row .= '</td>';
269
270 // Don't show deleted descriptions
271 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
272 $row .= '<td><span class="history-deleted">' .
273 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
274 } else {
275 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
276 $row .= Html::rawElement(
277 'td',
278 [ 'dir' => $contLang->getDir() ],
279 Linker::formatComment( $description, $this->title )
280 );
281 }
282
283 $rowClass = null;
284 Hooks::run( 'ImagePageFileHistoryLine', [ $this, $file, &$row, &$rowClass ] );
285 $classAttr = $rowClass ? " class='$rowClass'" : '';
286
287 return "<tr{$classAttr}>{$row}</tr>\n";
288 }
289
290 /**
291 * @param File $file
292 * @return string
293 */
294 protected function getThumbForLine( $file ) {
295 $lang = $this->getLanguage();
296 $user = $this->getUser();
297 if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE, $user )
298 && !$file->isDeleted( File::DELETED_FILE )
299 ) {
300 $params = [
301 'width' => '120',
302 'height' => '120',
303 ];
304 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
305
306 $thumbnail = $file->transform( $params );
307 $options = [
308 'alt' => $this->msg( 'filehist-thumbtext',
309 $lang->userTimeAndDate( $timestamp, $user ),
310 $lang->userDate( $timestamp, $user ),
311 $lang->userTime( $timestamp, $user ) )->text(),
312 'file-link' => true,
313 ];
314
315 if ( !$thumbnail ) {
316 return $this->msg( 'filehist-nothumb' )->escaped();
317 }
318
319 return $thumbnail->toHtml( $options );
320 } else {
321 return $this->msg( 'filehist-nothumb' )->escaped();
322 }
323 }
324
325 /**
326 * @param bool $enable
327 */
328 protected function preventClickjacking( $enable = true ) {
329 $this->preventClickjacking = $enable;
330 }
331
332 /**
333 * @return bool
334 */
335 public function getPreventClickjacking() {
336 return $this->preventClickjacking;
337 }
338 }