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