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