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