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