Do not suggest alternate svg sizes bigger than $wgMaxSVGSize on img pg
[lhc/web/wiklou.git] / includes / page / ImagePage.php
1 <?php
2 /**
3 * Special handling for file description pages.
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 /**
24 * Class for viewing MediaWiki file description pages
25 *
26 * @ingroup Media
27 */
28 class ImagePage extends Article {
29 /** @var File */
30 private $displayImg;
31
32 /** @var FileRepo */
33 private $repo;
34
35 /** @var bool */
36 private $fileLoaded;
37
38 /** @var bool */
39 protected $mExtraDescription = false;
40
41 /**
42 * @param Title $title
43 * @return WikiFilePage
44 */
45 protected function newPage( Title $title ) {
46 // Overload mPage with a file-specific page
47 return new WikiFilePage( $title );
48 }
49
50 /**
51 * Constructor from a page id
52 * @param int $id Article ID to load
53 * @return ImagePage|null
54 */
55 public static function newFromID( $id ) {
56 $t = Title::newFromID( $id );
57 # @todo FIXME: Doesn't inherit right
58 return $t == null ? null : new self( $t );
59 # return $t == null ? null : new static( $t ); // PHP 5.3
60 }
61
62 /**
63 * @param File $file
64 * @return void
65 */
66 public function setFile( $file ) {
67 $this->mPage->setFile( $file );
68 $this->displayImg = $file;
69 $this->fileLoaded = true;
70 }
71
72 protected function loadFile() {
73 if ( $this->fileLoaded ) {
74 return;
75 }
76 $this->fileLoaded = true;
77
78 $this->displayImg = $img = false;
79 Hooks::run( 'ImagePageFindFile', array( $this, &$img, &$this->displayImg ) );
80 if ( !$img ) { // not set by hook?
81 $img = wfFindFile( $this->getTitle() );
82 if ( !$img ) {
83 $img = wfLocalFile( $this->getTitle() );
84 }
85 }
86 $this->mPage->setFile( $img );
87 if ( !$this->displayImg ) { // not set by hook?
88 $this->displayImg = $img;
89 }
90 $this->repo = $img->getRepo();
91 }
92
93 /**
94 * Handler for action=render
95 * Include body text only; none of the image extras
96 */
97 public function render() {
98 $this->getContext()->getOutput()->setArticleBodyOnly( true );
99 parent::view();
100 }
101
102 public function view() {
103 global $wgShowEXIF;
104
105 $out = $this->getContext()->getOutput();
106 $request = $this->getContext()->getRequest();
107 $diff = $request->getVal( 'diff' );
108 $diffOnly = $request->getBool(
109 'diffonly',
110 $this->getContext()->getUser()->getOption( 'diffonly' )
111 );
112
113 if ( $this->getTitle()->getNamespace() != NS_FILE || ( $diff !== null && $diffOnly ) ) {
114 parent::view();
115 return;
116 }
117
118 $this->loadFile();
119
120 if ( $this->getTitle()->getNamespace() == NS_FILE && $this->mPage->getFile()->getRedirected() ) {
121 if ( $this->getTitle()->getDBkey() == $this->mPage->getFile()->getName() || $diff !== null ) {
122 // mTitle is the same as the redirect target so ask Article
123 // to perform the redirect for us.
124 $request->setVal( 'diffonly', 'true' );
125 parent::view();
126 return;
127 } else {
128 // mTitle is not the same as the redirect target so it is
129 // probably the redirect page itself. Fake the redirect symbol
130 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
131 $out->addHTML( $this->viewRedirect(
132 Title::makeTitle( NS_FILE, $this->mPage->getFile()->getName() ),
133 /* $appendSubtitle */ true,
134 /* $forceKnown */ true )
135 );
136 $this->mPage->doViewUpdates( $this->getContext()->getUser(), $this->getOldID() );
137 return;
138 }
139 }
140
141 if ( $wgShowEXIF && $this->displayImg->exists() ) {
142 // @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
143 $formattedMetadata = $this->displayImg->formatMetadata( $this->getContext() );
144 $showmeta = $formattedMetadata !== false;
145 } else {
146 $showmeta = false;
147 }
148
149 if ( !$diff && $this->displayImg->exists() ) {
150 $out->addHTML( $this->showTOC( $showmeta ) );
151 }
152
153 if ( !$diff ) {
154 $this->openShowImage();
155 }
156
157 # No need to display noarticletext, we use our own message, output in openShowImage()
158 if ( $this->mPage->getID() ) {
159 # NS_FILE is in the user language, but this section (the actual wikitext)
160 # should be in page content language
161 $pageLang = $this->getTitle()->getPageViewLanguage();
162 $out->addHTML( Xml::openElement( 'div', array( 'id' => 'mw-imagepage-content',
163 'lang' => $pageLang->getHtmlCode(), 'dir' => $pageLang->getDir(),
164 'class' => 'mw-content-' . $pageLang->getDir() ) ) );
165
166 parent::view();
167
168 $out->addHTML( Xml::closeElement( 'div' ) );
169 } else {
170 # Just need to set the right headers
171 $out->setArticleFlag( true );
172 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
173 $this->mPage->doViewUpdates( $this->getContext()->getUser(), $this->getOldID() );
174 }
175
176 # Show shared description, if needed
177 if ( $this->mExtraDescription ) {
178 $fol = $this->getContext()->msg( 'shareddescriptionfollows' );
179 if ( !$fol->isDisabled() ) {
180 $out->addWikiText( $fol->plain() );
181 }
182 $out->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . "</div>\n" );
183 }
184
185 $this->closeShowImage();
186 $this->imageHistory();
187 // TODO: Cleanup the following
188
189 $out->addHTML( Xml::element( 'h2',
190 array( 'id' => 'filelinks' ),
191 $this->getContext()->msg( 'imagelinks' )->text() ) . "\n" );
192 $this->imageDupes();
193 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
194 # Yet we return metadata about the target. Definitely an issue in the FileRepo
195 $this->imageLinks();
196
197 # Allow extensions to add something after the image links
198 $html = '';
199 Hooks::run( 'ImagePageAfterImageLinks', array( $this, &$html ) );
200 if ( $html ) {
201 $out->addHTML( $html );
202 }
203
204 if ( $showmeta ) {
205 $out->addHTML( Xml::element(
206 'h2',
207 array( 'id' => 'metadata' ),
208 $this->getContext()->msg( 'metadata' )->text() ) . "\n" );
209 $out->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
210 $out->addModules( array( 'mediawiki.action.view.metadata' ) );
211 }
212
213 // Add remote Filepage.css
214 if ( !$this->repo->isLocal() ) {
215 $css = $this->repo->getDescriptionStylesheetUrl();
216 if ( $css ) {
217 $out->addStyle( $css );
218 }
219 }
220 // always show the local local Filepage.css, bug 29277
221 $out->addModuleStyles( 'filepage' );
222 }
223
224 /**
225 * @return File
226 */
227 public function getDisplayedFile() {
228 $this->loadFile();
229 return $this->displayImg;
230 }
231
232 /**
233 * Create the TOC
234 *
235 * @param bool $metadata Whether or not to show the metadata link
236 * @return string
237 */
238 protected function showTOC( $metadata ) {
239 $r = array(
240 '<li><a href="#file">' . $this->getContext()->msg( 'file-anchor-link' )->escaped() . '</a></li>',
241 '<li><a href="#filehistory">' . $this->getContext()->msg( 'filehist' )->escaped() . '</a></li>',
242 '<li><a href="#filelinks">' . $this->getContext()->msg( 'imagelinks' )->escaped() . '</a></li>',
243 );
244
245 Hooks::run( 'ImagePageShowTOC', array( $this, &$r ) );
246
247 if ( $metadata ) {
248 $r[] = '<li><a href="#metadata">' . $this->getContext()->msg( 'metadata' )->escaped() . '</a></li>';
249 }
250
251 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
252 }
253
254 /**
255 * Make a table with metadata to be shown in the output page.
256 *
257 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
258 *
259 * @param array $metadata The array containing the Exif data
260 * @return string The metadata table. This is treated as Wikitext (!)
261 */
262 protected function makeMetadataTable( $metadata ) {
263 $r = "<div class=\"mw-imagepage-section-metadata\">";
264 $r .= $this->getContext()->msg( 'metadata-help' )->plain();
265 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
266 foreach ( $metadata as $type => $stuff ) {
267 foreach ( $stuff as $v ) {
268 # @todo FIXME: Why is this using escapeId for a class?!
269 $class = Sanitizer::escapeId( $v['id'] );
270 if ( $type == 'collapsed' ) {
271 // Handled by mediawiki.action.view.metadata module.
272 $class .= ' collapsable';
273 }
274 $r .= "<tr class=\"$class\">\n";
275 $r .= "<th>{$v['name']}</th>\n";
276 $r .= "<td>{$v['value']}</td>\n</tr>";
277 }
278 }
279 $r .= "</table>\n</div>\n";
280 return $r;
281 }
282
283 /**
284 * Overloading Article's getContentObject method.
285 *
286 * Omit noarticletext if sharedupload; text will be fetched from the
287 * shared upload server if possible.
288 * @return string
289 */
290 public function getContentObject() {
291 $this->loadFile();
292 if ( $this->mPage->getFile() && !$this->mPage->getFile()->isLocal() && 0 == $this->getID() ) {
293 return null;
294 }
295 return parent::getContentObject();
296 }
297
298 protected function openShowImage() {
299 global $wgEnableUploads, $wgSend404Code, $wgSVGMaxSize;
300
301 $this->loadFile();
302 $out = $this->getContext()->getOutput();
303 $user = $this->getContext()->getUser();
304 $lang = $this->getContext()->getLanguage();
305 $dirmark = $lang->getDirMarkEntity();
306 $request = $this->getContext()->getRequest();
307
308 $max = $this->getImageLimitsFromOption( $user, 'imagesize' );
309 $maxWidth = $max[0];
310 $maxHeight = $max[1];
311
312 if ( $this->displayImg->exists() ) {
313 # image
314 $page = $request->getIntOrNull( 'page' );
315 if ( is_null( $page ) ) {
316 $params = array();
317 $page = 1;
318 } else {
319 $params = array( 'page' => $page );
320 }
321
322 $renderLang = $request->getVal( 'lang' );
323 if ( !is_null( $renderLang ) ) {
324 $handler = $this->displayImg->getHandler();
325 if ( $handler && $handler->validateParam( 'lang', $renderLang ) ) {
326 $params['lang'] = $renderLang;
327 } else {
328 $renderLang = null;
329 }
330 }
331
332 $width_orig = $this->displayImg->getWidth( $page );
333 $width = $width_orig;
334 $height_orig = $this->displayImg->getHeight( $page );
335 $height = $height_orig;
336
337 $filename = wfEscapeWikiText( $this->displayImg->getName() );
338 $linktext = $filename;
339
340 Hooks::run( 'ImageOpenShowImageInlineBefore', array( &$this, &$out ) );
341
342 if ( $this->displayImg->allowInlineDisplay() ) {
343 # image
344 # "Download high res version" link below the image
345 # $msgsize = $this->getContext()->msg( 'file-info-size', $width_orig, $height_orig,
346 # Linker::formatSize( $this->displayImg->getSize() ), $mime )->escaped();
347 # We'll show a thumbnail of this image
348 if ( $width > $maxWidth || $height > $maxHeight || $this->displayImg->isVectorized() ) {
349 list( $width, $height ) = $this->getDisplayWidthHeight(
350 $maxWidth, $maxHeight, $width, $height
351 );
352 $linktext = $this->getContext()->msg( 'show-big-image' )->escaped();
353
354 $thumbSizes = $this->getThumbSizes( $width_orig, $height_orig );
355 # Generate thumbnails or thumbnail links as needed...
356 $otherSizes = array();
357 foreach ( $thumbSizes as $size ) {
358 // We include a thumbnail size in the list, if it is
359 // less than or equal to the original size of the image
360 // asset ($width_orig/$height_orig). We also exclude
361 // the current thumbnail's size ($width/$height)
362 // since that is added to the message separately, so
363 // it can be denoted as the current size being shown.
364 // Vectorized images are limited by $wgSVGMaxSize big,
365 // so all thumbs less than or equal that are shown.
366 if ( ( ( $size[0] <= $width_orig && $size[1] <= $height_orig )
367 || ( $this->displayImg->isVectorized()
368 && max( $size[0], $size[1] ) <= $wgSVGMaxSize )
369 )
370 && $size[0] != $width && $size[1] != $height
371 ) {
372 $sizeLink = $this->makeSizeLink( $params, $size[0], $size[1] );
373 if ( $sizeLink ) {
374 $otherSizes[] = $sizeLink;
375 }
376 }
377 }
378 $otherSizes = array_unique( $otherSizes );
379
380 $msgsmall = '';
381 $sizeLinkBigImagePreview = $this->makeSizeLink( $params, $width, $height );
382 if ( $sizeLinkBigImagePreview ) {
383 $msgsmall .= $this->getContext()->msg( 'show-big-image-preview' )->
384 rawParams( $sizeLinkBigImagePreview )->
385 parse();
386 }
387 if ( count( $otherSizes ) ) {
388 $msgsmall .= ' ' .
389 Html::rawElement( 'span', array( 'class' => 'mw-filepage-other-resolutions' ),
390 $this->getContext()->msg( 'show-big-image-other' )->rawParams( $lang->pipeList( $otherSizes ) )->
391 params( count( $otherSizes ) )->parse()
392 );
393 }
394 } elseif ( $width == 0 && $height == 0 ) {
395 # Some sort of audio file that doesn't have dimensions
396 # Don't output a no hi res message for such a file
397 $msgsmall = '';
398 } else {
399 # Image is small enough to show full size on image page
400 $msgsmall = $this->getContext()->msg( 'file-nohires' )->parse();
401 }
402
403 $params['width'] = $width;
404 $params['height'] = $height;
405 $thumbnail = $this->displayImg->transform( $params );
406 Linker::processResponsiveImages( $this->displayImg, $thumbnail, $params );
407
408 $anchorclose = Html::rawElement(
409 'div',
410 array( 'class' => 'mw-filepage-resolutioninfo' ),
411 $msgsmall
412 );
413
414 $isMulti = $this->displayImg->isMultipage() && $this->displayImg->pageCount() > 1;
415 if ( $isMulti ) {
416 $out->addModules( 'mediawiki.page.image.pagination' );
417 $out->addHTML( '<table class="multipageimage"><tr><td>' );
418 }
419
420 if ( $thumbnail ) {
421 $options = array(
422 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
423 'file-link' => true,
424 );
425 $out->addHTML( '<div class="fullImageLink" id="file">' .
426 $thumbnail->toHtml( $options ) .
427 $anchorclose . "</div>\n" );
428 }
429
430 if ( $isMulti ) {
431 $count = $this->displayImg->pageCount();
432
433 if ( $page > 1 ) {
434 $label = $out->parse( $this->getContext()->msg( 'imgmultipageprev' )->text(), false );
435 // on the client side, this link is generated in ajaxifyPageNavigation()
436 // in the mediawiki.page.image.pagination module
437 $link = Linker::linkKnown(
438 $this->getTitle(),
439 $label,
440 array(),
441 array( 'page' => $page - 1 )
442 );
443 $thumb1 = Linker::makeThumbLinkObj(
444 $this->getTitle(),
445 $this->displayImg,
446 $link,
447 $label,
448 'none',
449 array( 'page' => $page - 1 )
450 );
451 } else {
452 $thumb1 = '';
453 }
454
455 if ( $page < $count ) {
456 $label = $this->getContext()->msg( 'imgmultipagenext' )->text();
457 $link = Linker::linkKnown(
458 $this->getTitle(),
459 $label,
460 array(),
461 array( 'page' => $page + 1 )
462 );
463 $thumb2 = Linker::makeThumbLinkObj(
464 $this->getTitle(),
465 $this->displayImg,
466 $link,
467 $label,
468 'none',
469 array( 'page' => $page + 1 )
470 );
471 } else {
472 $thumb2 = '';
473 }
474
475 global $wgScript;
476
477 $formParams = array(
478 'name' => 'pageselector',
479 'action' => $wgScript,
480 );
481 $options = array();
482 for ( $i = 1; $i <= $count; $i++ ) {
483 $options[] = Xml::option( $lang->formatNum( $i ), $i, $i == $page );
484 }
485 $select = Xml::tags( 'select',
486 array( 'id' => 'pageselector', 'name' => 'page' ),
487 implode( "\n", $options ) );
488
489 $out->addHTML(
490 '</td><td><div class="multipageimagenavbox">' .
491 Xml::openElement( 'form', $formParams ) .
492 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
493 $this->getContext()->msg( 'imgmultigoto' )->rawParams( $select )->parse() .
494 Xml::submitButton( $this->getContext()->msg( 'imgmultigo' )->text() ) .
495 Xml::closeElement( 'form' ) .
496 "<hr />$thumb1\n$thumb2<br style=\"clear: both\" /></div></td></tr></table>"
497 );
498 }
499 } elseif ( $this->displayImg->isSafeFile() ) {
500 # if direct link is allowed but it's not a renderable image, show an icon.
501 $icon = $this->displayImg->iconThumb();
502
503 $out->addHTML( '<div class="fullImageLink" id="file">' .
504 $icon->toHtml( array( 'file-link' => true ) ) .
505 "</div>\n" );
506 }
507
508 $longDesc = $this->getContext()->msg( 'parentheses', $this->displayImg->getLongDesc() )->text();
509
510 $handler = $this->displayImg->getHandler();
511
512 // If this is a filetype with potential issues, warn the user.
513 if ( $handler ) {
514 $warningConfig = $handler->getWarningConfig( $this->displayImg );
515
516 if ( $warningConfig !== null ) {
517 // The warning will be displayed via CSS and JavaScript.
518 // We just need to tell the client side what message to use.
519 $output = $this->getContext()->getOutput();
520 $output->addJsConfigVars( 'wgFileWarning', $warningConfig );
521 $output->addModules( $warningConfig['module'] );
522 $output->addModules( 'mediawiki.filewarning' );
523 }
524 }
525
526 $medialink = "[[Media:$filename|$linktext]]";
527
528 if ( !$this->displayImg->isSafeFile() ) {
529 $warning = $this->getContext()->msg( 'mediawarning' )->plain();
530 // dirmark is needed here to separate the file name, which
531 // most likely ends in Latin characters, from the description,
532 // which may begin with the file type. In RTL environment
533 // this will get messy.
534 // The dirmark, however, must not be immediately adjacent
535 // to the filename, because it can get copied with it.
536 // See bug 25277.
537 // @codingStandardsIgnoreStart Ignore long line
538 $out->addWikiText( <<<EOT
539 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span> $dirmark<span class="fileInfo">$longDesc</span></div>
540 <div class="mediaWarning">$warning</div>
541 EOT
542 );
543 // @codingStandardsIgnoreEnd
544 } else {
545 $out->addWikiText( <<<EOT
546 <div class="fullMedia">{$medialink} {$dirmark}<span class="fileInfo">$longDesc</span>
547 </div>
548 EOT
549 );
550 }
551
552 $renderLangOptions = $this->displayImg->getAvailableLanguages();
553 if ( count( $renderLangOptions ) >= 1 ) {
554 $currentLanguage = $renderLang;
555 $defaultLang = $this->displayImg->getDefaultRenderLanguage();
556 if ( is_null( $currentLanguage ) ) {
557 $currentLanguage = $defaultLang;
558 }
559 $out->addHtml( $this->doRenderLangOpt( $renderLangOptions, $currentLanguage, $defaultLang ) );
560 }
561
562 // Add cannot animate thumbnail warning
563 if ( !$this->displayImg->canAnimateThumbIfAppropriate() ) {
564 // Include the extension so wiki admins can
565 // customize it on a per file-type basis
566 // (aka say things like use format X instead).
567 // additionally have a specific message for
568 // file-no-thumb-animation-gif
569 $ext = $this->displayImg->getExtension();
570 $noAnimMesg = wfMessageFallback(
571 'file-no-thumb-animation-' . $ext,
572 'file-no-thumb-animation'
573 )->plain();
574
575 $out->addWikiText( <<<EOT
576 <div class="mw-noanimatethumb">{$noAnimMesg}</div>
577 EOT
578 );
579 }
580
581 if ( !$this->displayImg->isLocal() ) {
582 $this->printSharedImageText();
583 }
584 } else {
585 # Image does not exist
586 if ( !$this->getID() ) {
587 # No article exists either
588 # Show deletion log to be consistent with normal articles
589 LogEventsList::showLogExtract(
590 $out,
591 array( 'delete', 'move' ),
592 $this->getTitle()->getPrefixedText(),
593 '',
594 array( 'lim' => 10,
595 'conds' => array( "log_action != 'revision'" ),
596 'showIfEmpty' => false,
597 'msgKey' => array( 'moveddeleted-notice' )
598 )
599 );
600 }
601
602 if ( $wgEnableUploads && $user->isAllowed( 'upload' ) ) {
603 // Only show an upload link if the user can upload
604 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
605 $nofile = array(
606 'filepage-nofile-link',
607 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage->getFile()->getName() ) )
608 );
609 } else {
610 $nofile = 'filepage-nofile';
611 }
612 // Note, if there is an image description page, but
613 // no image, then this setRobotPolicy is overridden
614 // by Article::View().
615 $out->setRobotPolicy( 'noindex,nofollow' );
616 $out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
617 if ( !$this->getID() && $wgSend404Code ) {
618 // If there is no image, no shared image, and no description page,
619 // output a 404, to be consistent with Article::showMissingArticle.
620 $request->response()->statusHeader( 404 );
621 }
622 }
623 $out->setFileVersion( $this->displayImg );
624 }
625
626 /**
627 * Creates an thumbnail of specified size and returns an HTML link to it
628 * @param array $params Scaler parameters
629 * @param int $width
630 * @param int $height
631 * @return string
632 */
633 private function makeSizeLink( $params, $width, $height ) {
634 $params['width'] = $width;
635 $params['height'] = $height;
636 $thumbnail = $this->displayImg->transform( $params );
637 if ( $thumbnail && !$thumbnail->isError() ) {
638 return Html::rawElement( 'a', array(
639 'href' => $thumbnail->getUrl(),
640 'class' => 'mw-thumbnail-link'
641 ), $this->getContext()->msg( 'show-big-image-size' )->numParams(
642 $thumbnail->getWidth(), $thumbnail->getHeight()
643 )->parse() );
644 } else {
645 return '';
646 }
647 }
648
649 /**
650 * Show a notice that the file is from a shared repository
651 */
652 protected function printSharedImageText() {
653 $out = $this->getContext()->getOutput();
654 $this->loadFile();
655
656 $descUrl = $this->mPage->getFile()->getDescriptionUrl();
657 $descText = $this->mPage->getFile()->getDescriptionText( $this->getContext()->getLanguage() );
658
659 /* Add canonical to head if there is no local page for this shared file */
660 if ( $descUrl && $this->mPage->getID() == 0 ) {
661 $out->setCanonicalUrl( $descUrl );
662 }
663
664 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
665 $repo = $this->mPage->getFile()->getRepo()->getDisplayName();
666
667 if ( $descUrl && $descText && $this->getContext()->msg( 'sharedupload-desc-here' )->plain() !== '-' ) {
668 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
669 } elseif ( $descUrl && $this->getContext()->msg( 'sharedupload-desc-there' )->plain() !== '-' ) {
670 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
671 } else {
672 $out->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
673 }
674
675 if ( $descText ) {
676 $this->mExtraDescription = $descText;
677 }
678 }
679
680 public function getUploadUrl() {
681 $this->loadFile();
682 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
683 return $uploadTitle->getFullURL( array(
684 'wpDestFile' => $this->mPage->getFile()->getName(),
685 'wpForReUpload' => 1
686 ) );
687 }
688
689 /**
690 * Print out the various links at the bottom of the image page, e.g. reupload,
691 * external editing (and instructions link) etc.
692 */
693 protected function uploadLinksBox() {
694 global $wgEnableUploads;
695
696 if ( !$wgEnableUploads ) {
697 return;
698 }
699
700 $this->loadFile();
701 if ( !$this->mPage->getFile()->isLocal() ) {
702 return;
703 }
704
705 $out = $this->getContext()->getOutput();
706 $out->addHTML( "<ul>\n" );
707
708 # "Upload a new version of this file" link
709 $canUpload = $this->getTitle()->userCan( 'upload', $this->getContext()->getUser() );
710 if ( $canUpload && UploadBase::userCanReUpload(
711 $this->getContext()->getUser(),
712 $this->mPage->getFile()->name )
713 ) {
714 $ulink = Linker::makeExternalLink(
715 $this->getUploadUrl(),
716 $this->getContext()->msg( 'uploadnewversion-linktext' )->text()
717 );
718 $out->addHTML( "<li id=\"mw-imagepage-reupload-link\">"
719 . "<div class=\"plainlinks\">{$ulink}</div></li>\n" );
720 } else {
721 $out->addHTML( "<li id=\"mw-imagepage-upload-disallowed\">"
722 . $this->getContext()->msg( 'upload-disallowed-here' )->escaped() . "</li>\n" );
723 }
724
725 $out->addHTML( "</ul>\n" );
726 }
727
728 /**
729 * For overloading
730 */
731 protected function closeShowImage() {
732 }
733
734 /**
735 * If the page we've just displayed is in the "Image" namespace,
736 * we follow it with an upload history of the image and its usage.
737 */
738 protected function imageHistory() {
739 $this->loadFile();
740 $out = $this->getContext()->getOutput();
741 $pager = new ImageHistoryPseudoPager( $this );
742 $out->addHTML( $pager->getBody() );
743 $out->preventClickjacking( $pager->getPreventClickjacking() );
744
745 $this->mPage->getFile()->resetHistory(); // free db resources
746
747 # Exist check because we don't want to show this on pages where an image
748 # doesn't exist along with the noimage message, that would suck. -ævar
749 if ( $this->mPage->getFile()->exists() ) {
750 $this->uploadLinksBox();
751 }
752 }
753
754 /**
755 * @param string $target
756 * @param int $limit
757 * @return ResultWrapper
758 */
759 protected function queryImageLinks( $target, $limit ) {
760 $dbr = wfGetDB( DB_SLAVE );
761
762 return $dbr->select(
763 array( 'imagelinks', 'page' ),
764 array( 'page_namespace', 'page_title', 'il_to' ),
765 array( 'il_to' => $target, 'il_from = page_id' ),
766 __METHOD__,
767 array( 'LIMIT' => $limit + 1, 'ORDER BY' => 'il_from', )
768 );
769 }
770
771 protected function imageLinks() {
772 $limit = 100;
773
774 $out = $this->getContext()->getOutput();
775
776 $rows = array();
777 $redirects = array();
778 foreach ( $this->getTitle()->getRedirectsHere( NS_FILE ) as $redir ) {
779 $redirects[$redir->getDBkey()] = array();
780 $rows[] = (object)array(
781 'page_namespace' => NS_FILE,
782 'page_title' => $redir->getDBkey(),
783 );
784 }
785
786 $res = $this->queryImageLinks( $this->getTitle()->getDBkey(), $limit + 1 );
787 foreach ( $res as $row ) {
788 $rows[] = $row;
789 }
790 $count = count( $rows );
791
792 $hasMore = $count > $limit;
793 if ( !$hasMore && count( $redirects ) ) {
794 $res = $this->queryImageLinks( array_keys( $redirects ),
795 $limit - count( $rows ) + 1 );
796 foreach ( $res as $row ) {
797 $redirects[$row->il_to][] = $row;
798 $count++;
799 }
800 $hasMore = ( $res->numRows() + count( $rows ) ) > $limit;
801 }
802
803 if ( $count == 0 ) {
804 $out->wrapWikiMsg(
805 Html::rawElement( 'div',
806 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
807 'nolinkstoimage'
808 );
809 return;
810 }
811
812 $out->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
813 if ( !$hasMore ) {
814 $out->addWikiMsg( 'linkstoimage', $count );
815 } else {
816 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
817 $out->addWikiMsg( 'linkstoimage-more',
818 $this->getContext()->getLanguage()->formatNum( $limit ),
819 $this->getTitle()->getPrefixedDBkey()
820 );
821 }
822
823 $out->addHTML(
824 Html::openElement( 'ul',
825 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
826 );
827 $count = 0;
828
829 // Sort the list by namespace:title
830 usort( $rows, array( $this, 'compare' ) );
831
832 // Create links for every element
833 $currentCount = 0;
834 foreach ( $rows as $element ) {
835 $currentCount++;
836 if ( $currentCount > $limit ) {
837 break;
838 }
839
840 $query = array();
841 # Add a redirect=no to make redirect pages reachable
842 if ( isset( $redirects[$element->page_title] ) ) {
843 $query['redirect'] = 'no';
844 }
845 $link = Linker::linkKnown(
846 Title::makeTitle( $element->page_namespace, $element->page_title ),
847 null, array(), $query
848 );
849 if ( !isset( $redirects[$element->page_title] ) ) {
850 # No redirects
851 $liContents = $link;
852 } elseif ( count( $redirects[$element->page_title] ) === 0 ) {
853 # Redirect without usages
854 $liContents = $this->getContext()->msg( 'linkstoimage-redirect' )->rawParams( $link, '' )->parse();
855 } else {
856 # Redirect with usages
857 $li = '';
858 foreach ( $redirects[$element->page_title] as $row ) {
859 $currentCount++;
860 if ( $currentCount > $limit ) {
861 break;
862 }
863
864 $link2 = Linker::linkKnown( Title::makeTitle( $row->page_namespace, $row->page_title ) );
865 $li .= Html::rawElement(
866 'li',
867 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace ),
868 $link2
869 ) . "\n";
870 }
871
872 $ul = Html::rawElement(
873 'ul',
874 array( 'class' => 'mw-imagepage-redirectstofile' ),
875 $li
876 ) . "\n";
877 $liContents = $this->getContext()->msg( 'linkstoimage-redirect' )->rawParams(
878 $link, $ul )->parse();
879 }
880 $out->addHTML( Html::rawElement(
881 'li',
882 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace ),
883 $liContents
884 ) . "\n"
885 );
886
887 };
888 $out->addHTML( Html::closeElement( 'ul' ) . "\n" );
889 $res->free();
890
891 // Add a links to [[Special:Whatlinkshere]]
892 if ( $count > $limit ) {
893 $out->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
894 }
895 $out->addHTML( Html::closeElement( 'div' ) . "\n" );
896 }
897
898 protected function imageDupes() {
899 $this->loadFile();
900 $out = $this->getContext()->getOutput();
901
902 $dupes = $this->mPage->getDuplicates();
903 if ( count( $dupes ) == 0 ) {
904 return;
905 }
906
907 $out->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
908 $out->addWikiMsg( 'duplicatesoffile',
909 $this->getContext()->getLanguage()->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
910 );
911 $out->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
912
913 /**
914 * @var $file File
915 */
916 foreach ( $dupes as $file ) {
917 $fromSrc = '';
918 if ( $file->isLocal() ) {
919 $link = Linker::linkKnown( $file->getTitle() );
920 } else {
921 $link = Linker::makeExternalLink( $file->getDescriptionUrl(),
922 $file->getTitle()->getPrefixedText() );
923 $fromSrc = $this->getContext()->msg( 'shared-repo-from', $file->getRepo()->getDisplayName() )->text();
924 }
925 $out->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
926 }
927 $out->addHTML( "</ul></div>\n" );
928 }
929
930 /**
931 * Delete the file, or an earlier version of it
932 */
933 public function delete() {
934 $file = $this->mPage->getFile();
935 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
936 // Standard article deletion
937 parent::delete();
938 return;
939 }
940
941 $deleter = new FileDeleteForm( $file );
942 $deleter->execute();
943 }
944
945 /**
946 * Display an error with a wikitext description
947 *
948 * @param string $description
949 */
950 function showError( $description ) {
951 $out = $this->getContext()->getOutput();
952 $out->setPageTitle( $this->getContext()->msg( 'internalerror' ) );
953 $out->setRobotPolicy( 'noindex,nofollow' );
954 $out->setArticleRelated( false );
955 $out->enableClientCache( false );
956 $out->addWikiText( $description );
957 }
958
959 /**
960 * Callback for usort() to do link sorts by (namespace, title)
961 * Function copied from Title::compare()
962 *
963 * @param object $a Object page to compare with
964 * @param object $b Object page to compare with
965 * @return int Result of string comparison, or namespace comparison
966 */
967 protected function compare( $a, $b ) {
968 if ( $a->page_namespace == $b->page_namespace ) {
969 return strcmp( $a->page_title, $b->page_title );
970 } else {
971 return $a->page_namespace - $b->page_namespace;
972 }
973 }
974
975 /**
976 * Returns the corresponding $wgImageLimits entry for the selected user option
977 *
978 * @param User $user
979 * @param string $optionName Name of a option to check, typically imagesize or thumbsize
980 * @return array
981 * @since 1.21
982 */
983 public function getImageLimitsFromOption( $user, $optionName ) {
984 global $wgImageLimits;
985
986 $option = $user->getIntOption( $optionName );
987 if ( !isset( $wgImageLimits[$option] ) ) {
988 $option = User::getDefaultOption( $optionName );
989 }
990
991 // The user offset might still be incorrect, specially if
992 // $wgImageLimits got changed (see bug #8858).
993 if ( !isset( $wgImageLimits[$option] ) ) {
994 // Default to the first offset in $wgImageLimits
995 $option = 0;
996 }
997
998 return isset( $wgImageLimits[$option] )
999 ? $wgImageLimits[$option]
1000 : array( 800, 600 ); // if nothing is set, fallback to a hardcoded default
1001 }
1002
1003 /**
1004 * Output a drop-down box for language options for the file
1005 *
1006 * @param array $langChoices Array of string language codes
1007 * @param string $curLang Language code file is being viewed in.
1008 * @param string $defaultLang Language code that image is rendered in by default
1009 * @return string HTML to insert underneath image.
1010 */
1011 protected function doRenderLangOpt( array $langChoices, $curLang, $defaultLang ) {
1012 global $wgScript;
1013 sort( $langChoices );
1014 $curLang = wfBCP47( $curLang );
1015 $defaultLang = wfBCP47( $defaultLang );
1016 $opts = '';
1017 $haveCurrentLang = false;
1018 $haveDefaultLang = false;
1019
1020 // We make a list of all the language choices in the file.
1021 // Additionally if the default language to render this file
1022 // is not included as being in this file (for example, in svgs
1023 // usually the fallback content is the english content) also
1024 // include a choice for that. Last of all, if we're viewing
1025 // the file in a language not on the list, add it as a choice.
1026 foreach ( $langChoices as $lang ) {
1027 $code = wfBCP47( $lang );
1028 $name = Language::fetchLanguageName( $code, $this->getContext()->getLanguage()->getCode() );
1029 if ( $name !== '' ) {
1030 $display = $this->getContext()->msg( 'img-lang-opt', $code, $name )->text();
1031 } else {
1032 $display = $code;
1033 }
1034 $opts .= "\n" . Xml::option( $display, $code, $curLang === $code );
1035 if ( $curLang === $code ) {
1036 $haveCurrentLang = true;
1037 }
1038 if ( $defaultLang === $code ) {
1039 $haveDefaultLang = true;
1040 }
1041 }
1042 if ( !$haveDefaultLang ) {
1043 // Its hard to know if the content is really in the default language, or
1044 // if its just unmarked content that could be in any language.
1045 $opts = Xml::option(
1046 $this->getContext()->msg( 'img-lang-default' )->text(),
1047 $defaultLang,
1048 $defaultLang === $curLang
1049 ) . $opts;
1050 }
1051 if ( !$haveCurrentLang && $defaultLang !== $curLang ) {
1052 $name = Language::fetchLanguageName( $curLang, $this->getContext()->getLanguage()->getCode() );
1053 if ( $name !== '' ) {
1054 $display = $this->getContext()->msg( 'img-lang-opt', $curLang, $name )->text();
1055 } else {
1056 $display = $curLang;
1057 }
1058 $opts = Xml::option( $display, $curLang, true ) . $opts;
1059 }
1060
1061 $select = Html::rawElement(
1062 'select',
1063 array( 'id' => 'mw-imglangselector', 'name' => 'lang' ),
1064 $opts
1065 );
1066 $submit = Xml::submitButton( $this->getContext()->msg( 'img-lang-go' )->text() );
1067
1068 $formContents = $this->getContext()->msg( 'img-lang-info' )->rawParams( $select, $submit )->parse()
1069 . Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() );
1070
1071 $langSelectLine = Html::rawElement( 'div', array( 'id' => 'mw-imglangselector-line' ),
1072 Html::rawElement( 'form', array( 'action' => $wgScript ), $formContents )
1073 );
1074 return $langSelectLine;
1075 }
1076
1077 /**
1078 * Get the width and height to display image at.
1079 *
1080 * @note This method assumes that it is only called if one
1081 * of the dimensions are bigger than the max, or if the
1082 * image is vectorized.
1083 *
1084 * @param int $maxWidth Max width to display at
1085 * @param int $maxHeight Max height to display at
1086 * @param int $width Actual width of the image
1087 * @param int $height Actual height of the image
1088 * @throws MWException
1089 * @return array Array (width, height)
1090 */
1091 protected function getDisplayWidthHeight( $maxWidth, $maxHeight, $width, $height ) {
1092 if ( !$maxWidth || !$maxHeight ) {
1093 // should never happen
1094 throw new MWException( 'Using a choice from $wgImageLimits that is 0x0' );
1095 }
1096
1097 if ( !$width || !$height ) {
1098 return array( 0, 0 );
1099 }
1100
1101 # Calculate the thumbnail size.
1102 if ( $width <= $maxWidth && $height <= $maxHeight ) {
1103 // Vectorized image, do nothing.
1104 } elseif ( $width / $height >= $maxWidth / $maxHeight ) {
1105 # The limiting factor is the width, not the height.
1106 $height = round( $height * $maxWidth / $width );
1107 $width = $maxWidth;
1108 # Note that $height <= $maxHeight now.
1109 } else {
1110 $newwidth = floor( $width * $maxHeight / $height );
1111 $height = round( $height * $newwidth / $width );
1112 $width = $newwidth;
1113 # Note that $height <= $maxHeight now, but might not be identical
1114 # because of rounding.
1115 }
1116 return array( $width, $height );
1117 }
1118
1119 /**
1120 * Get alternative thumbnail sizes.
1121 *
1122 * @note This will only list several alternatives if thumbnails are rendered on 404
1123 * @param int $origWidth Actual width of image
1124 * @param int $origHeight Actual height of image
1125 * @return array An array of [width, height] pairs.
1126 */
1127 protected function getThumbSizes( $origWidth, $origHeight ) {
1128 global $wgImageLimits;
1129 if ( $this->displayImg->getRepo()->canTransformVia404() ) {
1130 $thumbSizes = $wgImageLimits;
1131 // Also include the full sized resolution in the list, so
1132 // that users know they can get it. This will link to the
1133 // original file asset if mustRender() === false. In the case
1134 // that we mustRender, some users have indicated that they would
1135 // find it useful to have the full size image in the rendered
1136 // image format.
1137 $thumbSizes[] = array( $origWidth, $origHeight );
1138 } else {
1139 # Creating thumb links triggers thumbnail generation.
1140 # Just generate the thumb for the current users prefs.
1141 $thumbSizes = array( $this->getImageLimitsFromOption( $this->getContext()->getUser(), 'thumbsize' ) );
1142 if ( !$this->displayImg->mustRender() ) {
1143 // We can safely include a link to the "full-size" preview,
1144 // without actually rendering.
1145 $thumbSizes[] = array( $origWidth, $origHeight );
1146 }
1147 }
1148 return $thumbSizes;
1149 }
1150
1151 }
1152
1153 /**
1154 * Builds the image revision log shown on image pages
1155 *
1156 * @ingroup Media
1157 */
1158 class ImageHistoryList extends ContextSource {
1159
1160 /**
1161 * @var Title
1162 */
1163 protected $title;
1164
1165 /**
1166 * @var File
1167 */
1168 protected $img;
1169
1170 /**
1171 * @var ImagePage
1172 */
1173 protected $imagePage;
1174
1175 /**
1176 * @var File
1177 */
1178 protected $current;
1179
1180 protected $repo, $showThumb;
1181 protected $preventClickjacking = false;
1182
1183 /**
1184 * @param ImagePage $imagePage
1185 */
1186 public function __construct( $imagePage ) {
1187 global $wgShowArchiveThumbnails;
1188 $this->current = $imagePage->getFile();
1189 $this->img = $imagePage->getDisplayedFile();
1190 $this->title = $imagePage->getTitle();
1191 $this->imagePage = $imagePage;
1192 $this->showThumb = $wgShowArchiveThumbnails && $this->img->canRender();
1193 $this->setContext( $imagePage->getContext() );
1194 }
1195
1196 /**
1197 * @return ImagePage
1198 */
1199 public function getImagePage() {
1200 return $this->imagePage;
1201 }
1202
1203 /**
1204 * @return File
1205 */
1206 public function getFile() {
1207 return $this->img;
1208 }
1209
1210 /**
1211 * @param string $navLinks
1212 * @return string
1213 */
1214 public function beginImageHistoryList( $navLinks = '' ) {
1215 return Xml::element( 'h2', array( 'id' => 'filehistory' ), $this->msg( 'filehist' )->text() )
1216 . "\n"
1217 . "<div id=\"mw-imagepage-section-filehistory\">\n"
1218 . $this->msg( 'filehist-help' )->parseAsBlock()
1219 . $navLinks . "\n"
1220 . Xml::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
1221 . '<tr><th></th>'
1222 . ( $this->current->isLocal()
1223 && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ? '<th></th>' : '' )
1224 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
1225 . ( $this->showThumb ? '<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
1226 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
1227 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
1228 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
1229 . "</tr>\n";
1230 }
1231
1232 /**
1233 * @param string $navLinks
1234 * @return string
1235 */
1236 public function endImageHistoryList( $navLinks = '' ) {
1237 return "</table>\n$navLinks\n</div>\n";
1238 }
1239
1240 /**
1241 * @param bool $iscur
1242 * @param File $file
1243 * @return string
1244 */
1245 public function imageHistoryLine( $iscur, $file ) {
1246 global $wgContLang;
1247
1248 $user = $this->getUser();
1249 $lang = $this->getLanguage();
1250 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
1251 $img = $iscur ? $file->getName() : $file->getArchiveName();
1252 $userId = $file->getUser( 'id' );
1253 $userText = $file->getUser( 'text' );
1254 $description = $file->getDescription( File::FOR_THIS_USER, $user );
1255
1256 $local = $this->current->isLocal();
1257 $row = $selected = '';
1258
1259 // Deletion link
1260 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
1261 $row .= '<td>';
1262 # Link to remove from history
1263 if ( $user->isAllowed( 'delete' ) ) {
1264 $q = array( 'action' => 'delete' );
1265 if ( !$iscur ) {
1266 $q['oldimage'] = $img;
1267 }
1268 $row .= Linker::linkKnown(
1269 $this->title,
1270 $this->msg( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
1271 array(), $q
1272 );
1273 }
1274 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1275 $canHide = $user->isAllowed( 'deleterevision' );
1276 if ( $canHide || ( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1277 if ( $user->isAllowed( 'delete' ) ) {
1278 $row .= '<br />';
1279 }
1280 // If file is top revision or locked from this user, don't link
1281 if ( $iscur || !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1282 $del = Linker::revDeleteLinkDisabled( $canHide );
1283 } else {
1284 list( $ts, ) = explode( '!', $img, 2 );
1285 $query = array(
1286 'type' => 'oldimage',
1287 'target' => $this->title->getPrefixedText(),
1288 'ids' => $ts,
1289 );
1290 $del = Linker::revDeleteLink( $query,
1291 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1292 }
1293 $row .= $del;
1294 }
1295 $row .= '</td>';
1296 }
1297
1298 // Reversion link/current indicator
1299 $row .= '<td>';
1300 if ( $iscur ) {
1301 $row .= $this->msg( 'filehist-current' )->escaped();
1302 } elseif ( $local && $this->title->quickUserCan( 'edit', $user )
1303 && $this->title->quickUserCan( 'upload', $user )
1304 ) {
1305 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1306 $row .= $this->msg( 'filehist-revert' )->escaped();
1307 } else {
1308 $row .= Linker::linkKnown(
1309 $this->title,
1310 $this->msg( 'filehist-revert' )->escaped(),
1311 array(),
1312 array(
1313 'action' => 'revert',
1314 'oldimage' => $img,
1315 'wpEditToken' => $user->getEditToken( $img )
1316 )
1317 );
1318 }
1319 }
1320 $row .= '</td>';
1321
1322 // Date/time and image link
1323 if ( $file->getTimestamp() === $this->img->getTimestamp() ) {
1324 $selected = "class='filehistory-selected'";
1325 }
1326 $row .= "<td $selected style='white-space: nowrap;'>";
1327 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1328 # Don't link to unviewable files
1329 $row .= '<span class="history-deleted">'
1330 . $lang->userTimeAndDate( $timestamp, $user ) . '</span>';
1331 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
1332 if ( $local ) {
1333 $this->preventClickjacking();
1334 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
1335 # Make a link to review the image
1336 $url = Linker::linkKnown(
1337 $revdel,
1338 $lang->userTimeAndDate( $timestamp, $user ),
1339 array(),
1340 array(
1341 'target' => $this->title->getPrefixedText(),
1342 'file' => $img,
1343 'token' => $user->getEditToken( $img )
1344 )
1345 );
1346 } else {
1347 $url = $lang->userTimeAndDate( $timestamp, $user );
1348 }
1349 $row .= '<span class="history-deleted">' . $url . '</span>';
1350 } elseif ( !$file->exists() ) {
1351 $row .= '<span class="mw-file-missing">'
1352 . $lang->userTimeAndDate( $timestamp, $user ) . '</span>';
1353 } else {
1354 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
1355 $row .= Xml::element(
1356 'a',
1357 array( 'href' => $url ),
1358 $lang->userTimeAndDate( $timestamp, $user )
1359 );
1360 }
1361 $row .= "</td>";
1362
1363 // Thumbnail
1364 if ( $this->showThumb ) {
1365 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1366 }
1367
1368 // Image dimensions + size
1369 $row .= '<td>';
1370 $row .= htmlspecialchars( $file->getDimensionsString() );
1371 $row .= $this->msg( 'word-separator' )->escaped();
1372 $row .= '<span style="white-space: nowrap;">';
1373 $row .= $this->msg( 'parentheses' )->sizeParams( $file->getSize() )->escaped();
1374 $row .= '</span>';
1375 $row .= '</td>';
1376
1377 // Uploading user
1378 $row .= '<td>';
1379 // Hide deleted usernames
1380 if ( $file->isDeleted( File::DELETED_USER ) ) {
1381 $row .= '<span class="history-deleted">'
1382 . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1383 } else {
1384 if ( $local ) {
1385 $row .= Linker::userLink( $userId, $userText );
1386 $row .= '<span style="white-space: nowrap;">';
1387 $row .= Linker::userToolLinks( $userId, $userText );
1388 $row .= '</span>';
1389 } else {
1390 $row .= htmlspecialchars( $userText );
1391 }
1392 }
1393 $row .= '</td>';
1394
1395 // Don't show deleted descriptions
1396 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1397 $row .= '<td><span class="history-deleted">' .
1398 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
1399 } else {
1400 $row .= '<td dir="' . $wgContLang->getDir() . '">' .
1401 Linker::formatComment( $description, $this->title ) . '</td>';
1402 }
1403
1404 $rowClass = null;
1405 Hooks::run( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1406 $classAttr = $rowClass ? " class='$rowClass'" : '';
1407
1408 return "<tr{$classAttr}>{$row}</tr>\n";
1409 }
1410
1411 /**
1412 * @param File $file
1413 * @return string
1414 */
1415 protected function getThumbForLine( $file ) {
1416 $lang = $this->getLanguage();
1417 $user = $this->getUser();
1418 if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE, $user )
1419 && !$file->isDeleted( File::DELETED_FILE )
1420 ) {
1421 $params = array(
1422 'width' => '120',
1423 'height' => '120',
1424 );
1425 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
1426
1427 $thumbnail = $file->transform( $params );
1428 $options = array(
1429 'alt' => $this->msg( 'filehist-thumbtext',
1430 $lang->userTimeAndDate( $timestamp, $user ),
1431 $lang->userDate( $timestamp, $user ),
1432 $lang->userTime( $timestamp, $user ) )->text(),
1433 'file-link' => true,
1434 );
1435
1436 if ( !$thumbnail ) {
1437 return $this->msg( 'filehist-nothumb' )->escaped();
1438 }
1439
1440 return $thumbnail->toHtml( $options );
1441 } else {
1442 return $this->msg( 'filehist-nothumb' )->escaped();
1443 }
1444 }
1445
1446 /**
1447 * @param bool $enable
1448 */
1449 protected function preventClickjacking( $enable = true ) {
1450 $this->preventClickjacking = $enable;
1451 }
1452
1453 /**
1454 * @return bool
1455 */
1456 public function getPreventClickjacking() {
1457 return $this->preventClickjacking;
1458 }
1459 }
1460
1461 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
1462 protected $preventClickjacking = false;
1463
1464 /**
1465 * @var File
1466 */
1467 protected $mImg;
1468
1469 /**
1470 * @var Title
1471 */
1472 protected $mTitle;
1473
1474 /**
1475 * @param ImagePage $imagePage
1476 */
1477 function __construct( $imagePage ) {
1478 parent::__construct( $imagePage->getContext() );
1479 $this->mImagePage = $imagePage;
1480 $this->mTitle = clone ( $imagePage->getTitle() );
1481 $this->mTitle->setFragment( '#filehistory' );
1482 $this->mImg = null;
1483 $this->mHist = array();
1484 $this->mRange = array( 0, 0 ); // display range
1485 }
1486
1487 /**
1488 * @return Title
1489 */
1490 function getTitle() {
1491 return $this->mTitle;
1492 }
1493
1494 function getQueryInfo() {
1495 return false;
1496 }
1497
1498 /**
1499 * @return string
1500 */
1501 function getIndexField() {
1502 return '';
1503 }
1504
1505 /**
1506 * @param object $row
1507 * @return string
1508 */
1509 function formatRow( $row ) {
1510 return '';
1511 }
1512
1513 /**
1514 * @return string
1515 */
1516 function getBody() {
1517 $s = '';
1518 $this->doQuery();
1519 if ( count( $this->mHist ) ) {
1520 if ( $this->mImg->isLocal() ) {
1521 // Do a batch existence check for user pages and talkpages
1522 $linkBatch = new LinkBatch();
1523 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
1524 $file = $this->mHist[$i];
1525 $user = $file->getUser( 'text' );
1526 $linkBatch->add( NS_USER, $user );
1527 $linkBatch->add( NS_USER_TALK, $user );
1528 }
1529 $linkBatch->execute();
1530 }
1531
1532 $list = new ImageHistoryList( $this->mImagePage );
1533 # Generate prev/next links
1534 $navLink = $this->getNavigationBar();
1535 $s = $list->beginImageHistoryList( $navLink );
1536 // Skip rows there just for paging links
1537 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
1538 $file = $this->mHist[$i];
1539 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1540 }
1541 $s .= $list->endImageHistoryList( $navLink );
1542
1543 if ( $list->getPreventClickjacking() ) {
1544 $this->preventClickjacking();
1545 }
1546 }
1547 return $s;
1548 }
1549
1550 function doQuery() {
1551 if ( $this->mQueryDone ) {
1552 return;
1553 }
1554 $this->mImg = $this->mImagePage->getFile(); // ensure loading
1555 if ( !$this->mImg->exists() ) {
1556 return;
1557 }
1558 $queryLimit = $this->mLimit + 1; // limit plus extra row
1559 if ( $this->mIsBackwards ) {
1560 // Fetch the file history
1561 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
1562 // The current rev may not meet the offset/limit
1563 $numRows = count( $this->mHist );
1564 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
1565 $this->mHist = array_merge( array( $this->mImg ), $this->mHist );
1566 }
1567 } else {
1568 // The current rev may not meet the offset
1569 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
1570 $this->mHist[] = $this->mImg;
1571 }
1572 // Old image versions (fetch extra row for nav links)
1573 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
1574 // Fetch the file history
1575 $this->mHist = array_merge( $this->mHist,
1576 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
1577 }
1578 $numRows = count( $this->mHist ); // Total number of query results
1579 if ( $numRows ) {
1580 # Index value of top item in the list
1581 $firstIndex = $this->mIsBackwards ?
1582 $this->mHist[$numRows - 1]->getTimestamp() : $this->mHist[0]->getTimestamp();
1583 # Discard the extra result row if there is one
1584 if ( $numRows > $this->mLimit && $numRows > 1 ) {
1585 if ( $this->mIsBackwards ) {
1586 # Index value of item past the index
1587 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
1588 # Index value of bottom item in the list
1589 $lastIndex = $this->mHist[1]->getTimestamp();
1590 # Display range
1591 $this->mRange = array( 1, $numRows - 1 );
1592 } else {
1593 # Index value of item past the index
1594 $this->mPastTheEndIndex = $this->mHist[$numRows - 1]->getTimestamp();
1595 # Index value of bottom item in the list
1596 $lastIndex = $this->mHist[$numRows - 2]->getTimestamp();
1597 # Display range
1598 $this->mRange = array( 0, $numRows - 2 );
1599 }
1600 } else {
1601 # Setting indexes to an empty string means that they will be
1602 # omitted if they would otherwise appear in URLs. It just so
1603 # happens that this is the right thing to do in the standard
1604 # UI, in all the relevant cases.
1605 $this->mPastTheEndIndex = '';
1606 # Index value of bottom item in the list
1607 $lastIndex = $this->mIsBackwards ?
1608 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows - 1]->getTimestamp();
1609 # Display range
1610 $this->mRange = array( 0, $numRows - 1 );
1611 }
1612 } else {
1613 $firstIndex = '';
1614 $lastIndex = '';
1615 $this->mPastTheEndIndex = '';
1616 }
1617 if ( $this->mIsBackwards ) {
1618 $this->mIsFirst = ( $numRows < $queryLimit );
1619 $this->mIsLast = ( $this->mOffset == '' );
1620 $this->mLastShown = $firstIndex;
1621 $this->mFirstShown = $lastIndex;
1622 } else {
1623 $this->mIsFirst = ( $this->mOffset == '' );
1624 $this->mIsLast = ( $numRows < $queryLimit );
1625 $this->mLastShown = $lastIndex;
1626 $this->mFirstShown = $firstIndex;
1627 }
1628 $this->mQueryDone = true;
1629 }
1630
1631 /**
1632 * @param bool $enable
1633 */
1634 protected function preventClickjacking( $enable = true ) {
1635 $this->preventClickjacking = $enable;
1636 }
1637
1638 /**
1639 * @return bool
1640 */
1641 public function getPreventClickjacking() {
1642 return $this->preventClickjacking;
1643 }
1644
1645 }