d594c1e227da9491b67f27c901927cd0f034d0bd
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) )
4 die( 1 );
5
6 /**
7 * Special handling for image description pages
8 *
9 * @ingroup Media
10 */
11 class ImagePage extends Article {
12
13 /* private */ var $img; // Image object
14 /* private */ var $displayImg;
15 /* private */ var $repo;
16 /* private */ var $fileLoaded;
17 var $mExtraDescription = false;
18 var $dupes;
19
20 function __construct( $title ) {
21 parent::__construct( $title );
22 $this->dupes = null;
23 $this->repo = null;
24 }
25
26 public function setFile( $file ) {
27 $this->displayImg = $file;
28 $this->img = $file;
29 $this->fileLoaded = true;
30 }
31
32 protected function loadFile() {
33 if ( $this->fileLoaded ) {
34 return true;
35 }
36 $this->fileLoaded = true;
37
38 $this->displayImg = $this->img = false;
39 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img, &$this->displayImg ) );
40 if ( !$this->img ) {
41 $this->img = wfFindFile( $this->mTitle );
42 if ( !$this->img ) {
43 $this->img = wfLocalFile( $this->mTitle );
44 }
45 }
46 if ( !$this->displayImg ) {
47 $this->displayImg = $this->img;
48 }
49 $this->repo = $this->img->getRepo();
50 }
51
52 /**
53 * Handler for action=render
54 * Include body text only; none of the image extras
55 */
56 public function render() {
57 global $wgOut;
58 $wgOut->setArticleBodyOnly( true );
59 parent::view();
60 }
61
62 public function view() {
63 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
64
65 $diff = $wgRequest->getVal( 'diff' );
66 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
67
68 if ( $this->mTitle->getNamespace() != NS_FILE || ( isset( $diff ) && $diffOnly ) ) {
69 return parent::view();
70 }
71
72 $this->loadFile();
73
74 if ( $this->mTitle->getNamespace() == NS_FILE && $this->img->getRedirected() ) {
75 if ( $this->mTitle->getDBkey() == $this->img->getName() || isset( $diff ) ) {
76 // mTitle is the same as the redirect target so ask Article
77 // to perform the redirect for us.
78 $wgRequest->setVal( 'diffonly', 'true' );
79 return parent::view();
80 } else {
81 // mTitle is not the same as the redirect target so it is
82 // probably the redirect page itself. Fake the redirect symbol
83 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
84 $wgOut->addHTML( $this->viewRedirect( Title::makeTitle( NS_FILE, $this->img->getName() ),
85 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
86 $this->viewUpdates();
87 return;
88 }
89 }
90
91 $this->showRedirectedFromHeader();
92
93 if ( $wgShowEXIF && $this->displayImg->exists() ) {
94 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
95 $formattedMetadata = $this->displayImg->formatMetadata();
96 $showmeta = $formattedMetadata !== false;
97 } else {
98 $showmeta = false;
99 }
100
101 if ( !$diff && $this->displayImg->exists() )
102 $wgOut->addHTML( $this->showTOC( $showmeta ) );
103
104 if ( !$diff )
105 $this->openShowImage();
106
107 # No need to display noarticletext, we use our own message, output in openShowImage()
108 if ( $this->getID() ) {
109 parent::view();
110 } else {
111 # Just need to set the right headers
112 $wgOut->setArticleFlag( true );
113 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
114 $this->viewUpdates();
115 }
116
117 # Show shared description, if needed
118 if ( $this->mExtraDescription ) {
119 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
120 if ( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
121 $wgOut->addWikiText( $fol );
122 }
123 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . "</div>\n" );
124 }
125
126 $this->closeShowImage();
127 $this->imageHistory();
128 // TODO: Cleanup the following
129
130 $wgOut->addHTML( Xml::element( 'h2',
131 array( 'id' => 'filelinks' ),
132 wfMsg( 'imagelinks' ) ) . "\n" );
133 $this->imageDupes();
134 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
135 # Yet we return metadata about the target. Definitely an issue in the FileRepo
136 $this->imageRedirects();
137 $this->imageLinks();
138
139 # Allow extensions to add something after the image links
140 $html = '';
141 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
142 if ( $html )
143 $wgOut->addHTML( $html );
144
145 if ( $showmeta ) {
146 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
147 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
148 $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
149 }
150
151 $css = $this->repo->getDescriptionStylesheetUrl();
152 if ( $css ) {
153 $wgOut->addStyle( $css );
154 }
155 }
156
157 public function getRedirectTarget() {
158 $this->loadFile();
159 if ( $this->img->isLocal() ) {
160 return parent::getRedirectTarget();
161 }
162 // Foreign image page
163 $from = $this->img->getRedirected();
164 $to = $this->img->getName();
165 if ( $from == $to ) {
166 return null;
167 }
168 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
169 }
170 public function followRedirect() {
171 $this->loadFile();
172 if ( $this->img->isLocal() ) {
173 return parent::followRedirect();
174 }
175 $from = $this->img->getRedirected();
176 $to = $this->img->getName();
177 if ( $from == $to ) {
178 return false;
179 }
180 return Title::makeTitle( NS_FILE, $to );
181 }
182 public function isRedirect( $text = false ) {
183 $this->loadFile();
184 if ( $this->img->isLocal() )
185 return parent::isRedirect( $text );
186
187 return (bool)$this->img->getRedirected();
188 }
189
190 public function isLocal() {
191 $this->loadFile();
192 return $this->img->isLocal();
193 }
194
195 public function getFile() {
196 $this->loadFile();
197 return $this->img;
198 }
199
200 public function getDisplayedFile() {
201 $this->loadFile();
202 return $this->displayImg;
203 }
204
205 public function getDuplicates() {
206 $this->loadFile();
207 if ( !is_null( $this->dupes ) ) {
208 return $this->dupes;
209 }
210 if ( !( $hash = $this->img->getSha1() ) ) {
211 return $this->dupes = array();
212 }
213 $dupes = RepoGroup::singleton()->findBySha1( $hash );
214 // Remove duplicates with self and non matching file sizes
215 $self = $this->img->getRepoName() . ':' . $this->img->getName();
216 $size = $this->img->getSize();
217 foreach ( $dupes as $index => $file ) {
218 $key = $file->getRepoName() . ':' . $file->getName();
219 if ( $key == $self )
220 unset( $dupes[$index] );
221 if ( $file->getSize() != $size )
222 unset( $dupes[$index] );
223 }
224 return $this->dupes = $dupes;
225
226 }
227
228
229 /**
230 * Create the TOC
231 *
232 * @param $metadata Boolean: whether or not to show the metadata link
233 * @return String
234 */
235 protected function showTOC( $metadata ) {
236 $r = array(
237 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
238 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
239 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
240 );
241 if ( $metadata ) {
242 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
243 }
244
245 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
246
247 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
248 }
249
250 /**
251 * Make a table with metadata to be shown in the output page.
252 *
253 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
254 *
255 * @param $metadata Array: the array containing the EXIF data
256 * @return String
257 */
258 protected function makeMetadataTable( $metadata ) {
259 $r = "<div class=\"mw-imagepage-section-metadata\">";
260 $r .= wfMsgNoTrans( 'metadata-help' );
261 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
262 foreach ( $metadata as $type => $stuff ) {
263 foreach ( $stuff as $v ) {
264 # FIXME, why is this using escapeId for a class?!
265 $class = Sanitizer::escapeId( $v['id'] );
266 if ( $type == 'collapsed' ) {
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 getContent method.
280 *
281 * Omit noarticletext if sharedupload; text will be fetched from the
282 * shared upload server if possible.
283 */
284 public function getContent() {
285 $this->loadFile();
286 if ( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
287 return '';
288 }
289 return Article::getContent();
290 }
291
292 protected function openShowImage() {
293 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
294 $wgLang, $wgContLang, $wgEnableUploads;
295
296 $this->loadFile();
297
298 $full_url = $this->displayImg->getURL();
299 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
300 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
301 $sizeSel = User::getDefaultOption( 'imagesize' );
302
303 // The user offset might still be incorrect, specially if
304 // $wgImageLimits got changed (see bug #8858).
305 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
306 // Default to the first offset in $wgImageLimits
307 $sizeSel = 0;
308 }
309 }
310 $max = $wgImageLimits[$sizeSel];
311 $maxWidth = $max[0];
312 $maxHeight = $max[1];
313 $sk = $wgUser->getSkin();
314 $dirmark = $wgContLang->getDirMark();
315
316 if ( $this->displayImg->exists() ) {
317 # image
318 $page = $wgRequest->getIntOrNull( 'page' );
319 if ( is_null( $page ) ) {
320 $params = array();
321 $page = 1;
322 } else {
323 $params = array( 'page' => $page );
324 }
325 $width_orig = $this->displayImg->getWidth( $page );
326 $width = $width_orig;
327 $height_orig = $this->displayImg->getHeight( $page );
328 $height = $height_orig;
329
330 $longDesc = $this->displayImg->getLongDesc();
331
332 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
333
334 if ( $this->displayImg->allowInlineDisplay() ) {
335 # image
336
337 # "Download high res version" link below the image
338 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
339 # We'll show a thumbnail of this image
340 if ( $width > $maxWidth || $height > $maxHeight ) {
341 # Calculate the thumbnail size.
342 # First case, the limiting factor is the width, not the height.
343 if ( $width / $height >= $maxWidth / $maxHeight ) {
344 $height = round( $height * $maxWidth / $width );
345 $width = $maxWidth;
346 # Note that $height <= $maxHeight now.
347 } else {
348 $newwidth = floor( $width * $maxHeight / $height );
349 $height = round( $height * $newwidth / $width );
350 $width = $newwidth;
351 # Note that $height <= $maxHeight now, but might not be identical
352 # because of rounding.
353 }
354 $msgbig = wfMsgHtml( 'show-big-image' );
355 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
356 $wgLang->formatNum( $width ),
357 $wgLang->formatNum( $height )
358 );
359 } else {
360 # Image is small enough to show full size on image page
361 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
362 }
363
364 $params['width'] = $width;
365 $thumbnail = $this->displayImg->transform( $params );
366
367 $showLink = true;
368 $anchorclose = '';
369 if ( !$this->displayImg->mustRender() ) {
370 $anchorclose = "<br />" . $msgsmall;
371 }
372
373 $isMulti = $this->displayImg->isMultipage() && $this->displayImg->pageCount() > 1;
374 if ( $isMulti ) {
375 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
376 }
377
378 if ( $thumbnail ) {
379 $options = array(
380 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
381 'file-link' => true,
382 );
383 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
384 $thumbnail->toHtml( $options ) .
385 $anchorclose . "</div>\n" );
386 }
387
388 if ( $isMulti ) {
389 $count = $this->displayImg->pageCount();
390
391 if ( $page > 1 ) {
392 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
393 $link = $sk->link(
394 $this->mTitle,
395 $label,
396 array(),
397 array( 'page' => $page - 1 ),
398 array( 'known', 'noclasses' )
399 );
400 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
401 array( 'page' => $page - 1 ) );
402 } else {
403 $thumb1 = '';
404 }
405
406 if ( $page < $count ) {
407 $label = wfMsg( 'imgmultipagenext' );
408 $link = $sk->link(
409 $this->mTitle,
410 $label,
411 array(),
412 array( 'page' => $page + 1 ),
413 array( 'known', 'noclasses' )
414 );
415 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
416 array( 'page' => $page + 1 ) );
417 } else {
418 $thumb2 = '';
419 }
420
421 global $wgScript;
422
423 $formParams = array(
424 'name' => 'pageselector',
425 'action' => $wgScript,
426 'onchange' => 'document.pageselector.submit();',
427 );
428
429 for ( $i = 1; $i <= $count; $i++ ) {
430 $options[] = Xml::option( $wgLang->formatNum( $i ), $i, $i == $page );
431 }
432 $select = Xml::tags( 'select',
433 array( 'id' => 'pageselector', 'name' => 'page' ),
434 implode( "\n", $options ) );
435
436 $wgOut->addHTML(
437 '</td><td><div class="multipageimagenavbox">' .
438 Xml::openElement( 'form', $formParams ) .
439 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
440 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
441 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
442 Xml::closeElement( 'form' ) .
443 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
444 );
445 }
446 } else {
447 # if direct link is allowed but it's not a renderable image, show an icon.
448 if ( $this->displayImg->isSafeFile() ) {
449 $icon = $this->displayImg->iconThumb();
450
451 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
452 $icon->toHtml( array( 'file-link' => true ) ) .
453 "</div>\n" );
454 }
455
456 $showLink = true;
457 }
458
459
460 if ( $showLink ) {
461 $filename = wfEscapeWikiText( $this->displayImg->getName() );
462 $linktext = $filename;
463 if ( isset( $msgbig ) ) {
464 $linktext = wfEscapeWikiText( $msgbig );
465 }
466 $medialink = "[[Media:$filename|$linktext]]";
467
468 if ( !$this->displayImg->isSafeFile() ) {
469 $warning = wfMsgNoTrans( 'mediawarning' );
470 $wgOut->addWikiText( <<<EOT
471 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
472 <div class="mediaWarning">$warning</div>
473 EOT
474 );
475 } else {
476 $wgOut->addWikiText( <<<EOT
477 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
478 </div>
479 EOT
480 );
481 }
482 }
483
484 if ( !$this->displayImg->isLocal() ) {
485 $this->printSharedImageText();
486 }
487 } else {
488 # Image does not exist
489 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
490 // Only show an upload link if the user can upload
491 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
492 $nofile = array(
493 'filepage-nofile-link',
494 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img->getName() ) )
495 );
496 }
497 else
498 {
499 $nofile = 'filepage-nofile';
500 }
501 $wgOut->setRobotPolicy( 'noindex,nofollow' );
502 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
503 }
504 }
505
506 /**
507 * Show a notice that the file is from a shared repository
508 */
509 protected function printSharedImageText() {
510 global $wgOut;
511
512 $this->loadFile();
513
514 $descUrl = $this->img->getDescriptionUrl();
515 $descText = $this->img->getDescriptionText();
516
517 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
518 $repo = $this->img->getRepo()->getDisplayName();
519
520 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
521 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
522 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
523 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
524 } else {
525 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
526 }
527
528 if ( $descText ) {
529 $this->mExtraDescription = $descText;
530 }
531 }
532
533 public function getUploadUrl() {
534 $this->loadFile();
535 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
536 return $uploadTitle->getFullUrl( array(
537 'wpDestFile' => $this->img->getName(),
538 'wpForReUpload' => 1
539 ) );
540 }
541
542 /**
543 * Print out the various links at the bottom of the image page, e.g. reupload,
544 * external editing (and instructions link) etc.
545 */
546 protected function uploadLinksBox() {
547 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
548
549 if ( !$wgEnableUploads ) { return; }
550
551 $this->loadFile();
552 if ( !$this->img->isLocal() )
553 return;
554
555 $sk = $wgUser->getSkin();
556
557 $wgOut->addHTML( "<br /><ul>\n" );
558
559 # "Upload a new version of this file" link
560 if ( UploadBase::userCanReUpload( $wgUser, $this->img->name ) ) {
561 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
562 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
563 }
564
565 # External editing link
566 if ( $wgUseExternalEditor ) {
567 $elink = $sk->link(
568 $this->mTitle,
569 wfMsgHtml( 'edit-externally' ),
570 array(),
571 array(
572 'action' => 'edit',
573 'externaledit' => 'true',
574 'mode' => 'file'
575 ),
576 array( 'known', 'noclasses' )
577 );
578 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
579 }
580
581 $wgOut->addHTML( "</ul>\n" );
582 }
583
584 protected function closeShowImage() { } # For overloading
585
586 /**
587 * If the page we've just displayed is in the "Image" namespace,
588 * we follow it with an upload history of the image and its usage.
589 */
590 protected function imageHistory() {
591 global $wgOut;
592
593 $this->loadFile();
594 $pager = new ImageHistoryPseudoPager( $this );
595 $wgOut->addHTML( $pager->getBody() );
596
597 $this->img->resetHistory(); // free db resources
598
599 # Exist check because we don't want to show this on pages where an image
600 # doesn't exist along with the noimage message, that would suck. -ævar
601 if ( $this->img->exists() ) {
602 $this->uploadLinksBox();
603 }
604 }
605
606 protected function imageLinks() {
607 global $wgUser, $wgOut, $wgLang;
608
609 $limit = 100;
610
611 $dbr = wfGetDB( DB_SLAVE );
612
613 $res = $dbr->select(
614 array( 'imagelinks', 'page' ),
615 array( 'page_namespace', 'page_title' ),
616 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
617 __METHOD__,
618 array( 'LIMIT' => $limit + 1 )
619 );
620 $count = $dbr->numRows( $res );
621 if ( $count == 0 ) {
622 $wgOut->wrapWikiMsg( Html::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
623 return;
624 }
625
626 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
627 if ( $count <= $limit - 1 ) {
628 $wgOut->addWikiMsg( 'linkstoimage', $count );
629 } else {
630 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
631 $wgOut->addWikiMsg( 'linkstoimage-more',
632 $wgLang->formatNum( $limit ),
633 $this->mTitle->getPrefixedDBkey()
634 );
635 }
636
637 $wgOut->addHTML( Html::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
638 $sk = $wgUser->getSkin();
639 $count = 0;
640 $elements = array();
641 foreach ( $res as $s ) {
642 $count++;
643 if ( $count <= $limit ) {
644 // We have not yet reached the extra one that tells us there is more to fetch
645 $elements[] = $s;
646 }
647 }
648
649 // Sort the list by namespace:title
650 usort ( $elements, array( $this, 'compare' ) );
651
652 // Create links for every element
653 foreach( $elements as $element ) {
654 $link = $sk->linkKnown( Title::makeTitle( $element->page_namespace, $element->page_title ) );
655 $wgOut->addHTML( Html::rawElement(
656 'li',
657 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace ),
658 $link
659 ) . "\n"
660 );
661
662 };
663 $wgOut->addHTML( Html::closeElement( 'ul' ) . "\n" );
664 $res->free();
665
666 // Add a links to [[Special:Whatlinkshere]]
667 if ( $count > $limit ) {
668 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
669 }
670 $wgOut->addHTML( Html::closeElement( 'div' ) . "\n" );
671 }
672
673 protected function imageRedirects() {
674 global $wgUser, $wgOut, $wgLang;
675
676 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
677 if ( count( $redirects ) == 0 ) return;
678
679 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
680 $wgOut->addWikiMsg( 'redirectstofile',
681 $wgLang->formatNum( count( $redirects ) )
682 );
683 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
684
685 $sk = $wgUser->getSkin();
686 foreach ( $redirects as $title ) {
687 $link = $sk->link(
688 $title,
689 null,
690 array(),
691 array( 'redirect' => 'no' ),
692 array( 'known', 'noclasses' )
693 );
694 $wgOut->addHTML( "<li>{$link}</li>\n" );
695 }
696 $wgOut->addHTML( "</ul></div>\n" );
697
698 }
699
700 protected function imageDupes() {
701 global $wgOut, $wgUser, $wgLang;
702
703 $this->loadFile();
704
705 $dupes = $this->getDuplicates();
706 if ( count( $dupes ) == 0 ) return;
707
708 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
709 $wgOut->addWikiMsg( 'duplicatesoffile',
710 $wgLang->formatNum( count( $dupes ) ), $this->mTitle->getDBkey()
711 );
712 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
713
714 $sk = $wgUser->getSkin();
715 foreach ( $dupes as $file ) {
716 $fromSrc = '';
717 if ( $file->isLocal() ) {
718 $link = $sk->link(
719 $file->getTitle(),
720 null,
721 array(),
722 array(),
723 array( 'known', 'noclasses' )
724 );
725 } else {
726 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
727 $file->getTitle()->getPrefixedText() );
728 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
729 }
730 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
731 }
732 $wgOut->addHTML( "</ul></div>\n" );
733 }
734
735 /**
736 * Delete the file, or an earlier version of it
737 */
738 public function delete() {
739 global $wgUploadMaintenance;
740 if ( $wgUploadMaintenance && $this->mTitle && $this->mTitle->getNamespace() == NS_FILE ) {
741 global $wgOut;
742 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
743 return;
744 }
745
746 $this->loadFile();
747 if ( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
748 // Standard article deletion
749 parent::delete();
750 return;
751 }
752 $deleter = new FileDeleteForm( $this->img );
753 $deleter->execute();
754 }
755
756 /**
757 * Revert the file to an earlier version
758 */
759 public function revert() {
760 $this->loadFile();
761 $reverter = new FileRevertForm( $this->img );
762 $reverter->execute();
763 }
764
765 /**
766 * Override handling of action=purge
767 */
768 public function doPurge() {
769 $this->loadFile();
770 if ( $this->img->exists() ) {
771 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
772 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
773 $update->doUpdate();
774 $this->img->upgradeRow();
775 $this->img->purgeCache();
776 } else {
777 wfDebug( "ImagePage::doPurge no image for " . $this->img->getName() . "; limiting purge to cache only\n" );
778 // even if the file supposedly doesn't exist, force any cached information
779 // to be updated (in case the cached information is wrong)
780 $this->img->purgeCache();
781 }
782 parent::doPurge();
783 }
784
785 /**
786 * Display an error with a wikitext description
787 */
788 function showError( $description ) {
789 global $wgOut;
790 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
791 $wgOut->setRobotPolicy( "noindex,nofollow" );
792 $wgOut->setArticleRelated( false );
793 $wgOut->enableClientCache( false );
794 $wgOut->addWikiText( $description );
795 }
796
797
798 /**
799 * Callback for usort() to do link sorts by (namespace, title)
800 * Function copied from Title::compare()
801 *
802 * @param $a object page to compare with
803 * @param $b object page to compare with
804 * @return Integer: result of string comparison, or namespace comparison
805 */
806 protected function compare( $a, $b ) {
807 if ( $a->page_namespace == $b->page_namespace ) {
808 return strcmp( $a->page_title, $b->page_title );
809 } else {
810 return $a->page_namespace - $b->page_namespace;
811 }
812 }
813 }
814
815 /**
816 * Builds the image revision log shown on image pages
817 *
818 * @ingroup Media
819 */
820 class ImageHistoryList {
821
822 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
823
824 public function __construct( $imagePage ) {
825 global $wgUser, $wgShowArchiveThumbnails;
826 $this->skin = $wgUser->getSkin();
827 $this->current = $imagePage->getFile();
828 $this->img = $imagePage->getDisplayedFile();
829 $this->title = $imagePage->getTitle();
830 $this->imagePage = $imagePage;
831 $this->showThumb = $wgShowArchiveThumbnails && $this->img->canRender();
832 }
833
834 public function getImagePage() {
835 return $this->imagePage;
836 }
837
838 public function getSkin() {
839 return $this->skin;
840 }
841
842 public function getFile() {
843 return $this->img;
844 }
845
846 public function beginImageHistoryList( $navLinks = '' ) {
847 global $wgOut, $wgUser;
848 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
849 . "<div id=\"mw-imagepage-section-filehistory\">\n"
850 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
851 . $navLinks . "\n"
852 . Xml::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
853 . '<tr><td></td>'
854 . ( $this->current->isLocal() && ( $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deletedhistory' ) ) ? '<td></td>' : '' )
855 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
856 . ( $this->showThumb ? '<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
857 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
858 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
859 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
860 . "</tr>\n";
861 }
862
863 public function endImageHistoryList( $navLinks = '' ) {
864 return "</table>\n$navLinks\n</div>\n";
865 }
866
867 public function imageHistoryLine( $iscur, $file ) {
868 global $wgUser, $wgLang;
869
870 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
871 $img = $iscur ? $file->getName() : $file->getArchiveName();
872 $user = $file->getUser( 'id' );
873 $usertext = $file->getUser( 'text' );
874 $description = $file->getDescription();
875
876 $local = $this->current->isLocal();
877 $row = $selected = '';
878
879 // Deletion link
880 if ( $local && ( $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deletedhistory' ) ) ) {
881 $row .= '<td>';
882 # Link to remove from history
883 if ( $wgUser->isAllowed( 'delete' ) ) {
884 $q = array( 'action' => 'delete' );
885 if ( !$iscur )
886 $q['oldimage'] = $img;
887 $row .= $this->skin->link(
888 $this->title,
889 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
890 array(), $q, array( 'known' )
891 );
892 }
893 # Link to hide content. Don't show useless link to people who cannot hide revisions.
894 $canHide = $wgUser->isAllowed( 'deleterevision' );
895 if ( $canHide || ( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
896 if ( $wgUser->isAllowed( 'delete' ) ) {
897 $row .= '<br />';
898 }
899 // If file is top revision or locked from this user, don't link
900 if ( $iscur || !$file->userCan( File::DELETED_RESTRICTED ) ) {
901 $del = $this->skin->revDeleteLinkDisabled( $canHide );
902 } else {
903 list( $ts, $name ) = explode( '!', $img, 2 );
904 $query = array(
905 'type' => 'oldimage',
906 'target' => $this->title->getPrefixedText(),
907 'ids' => $ts,
908 );
909 $del = $this->skin->revDeleteLink( $query,
910 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
911 }
912 $row .= $del;
913 }
914 $row .= '</td>';
915 }
916
917 // Reversion link/current indicator
918 $row .= '<td>';
919 if ( $iscur ) {
920 $row .= wfMsgHtml( 'filehist-current' );
921 } elseif ( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
922 if ( $file->isDeleted( File::DELETED_FILE ) ) {
923 $row .= wfMsgHtml( 'filehist-revert' );
924 } else {
925 $row .= $this->skin->link(
926 $this->title,
927 wfMsgHtml( 'filehist-revert' ),
928 array(),
929 array(
930 'action' => 'revert',
931 'oldimage' => $img,
932 'wpEditToken' => $wgUser->editToken( $img )
933 ),
934 array( 'known', 'noclasses' )
935 );
936 }
937 }
938 $row .= '</td>';
939
940 // Date/time and image link
941 if ( $file->getTimestamp() === $this->img->getTimestamp() ) {
942 $selected = "class='filehistory-selected'";
943 }
944 $row .= "<td $selected style='white-space: nowrap;'>";
945 if ( !$file->userCan( File::DELETED_FILE ) ) {
946 # Don't link to unviewable files
947 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
948 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
949 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
950 # Make a link to review the image
951 $url = $this->skin->link(
952 $revdel,
953 $wgLang->timeAndDate( $timestamp, true ),
954 array(),
955 array(
956 'target' => $this->title->getPrefixedText(),
957 'file' => $img,
958 'token' => $wgUser->editToken( $img )
959 ),
960 array( 'known', 'noclasses' )
961 );
962 $row .= '<span class="history-deleted">' . $url . '</span>';
963 } else {
964 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
965 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
966 }
967 $row .= "</td>";
968
969 // Thumbnail
970 if ( $this->showThumb ) {
971 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
972 }
973
974 // Image dimensions + size
975 $row .= '<td>';
976 $row .= htmlspecialchars( $file->getDimensionsString() );
977 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $file->getSize() ) . ')</span>';
978 $row .= '</td>';
979
980 // Uploading user
981 $row .= '<td>';
982 if ( $local ) {
983 // Hide deleted usernames
984 if ( $file->isDeleted( File::DELETED_USER ) ) {
985 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
986 } else {
987 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
988 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
989 }
990 } else {
991 $row .= htmlspecialchars( $usertext );
992 }
993 $row .= '</td><td>';
994
995 // Don't show deleted descriptions
996 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
997 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
998 } else {
999 $row .= $this->skin->commentBlock( $description, $this->title );
1000 }
1001 $row .= '</td>';
1002
1003 $rowClass = null;
1004 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1005 $classAttr = $rowClass ? " class='$rowClass'" : "";
1006
1007 return "<tr{$classAttr}>{$row}</tr>\n";
1008 }
1009
1010 protected function getThumbForLine( $file ) {
1011 global $wgLang;
1012
1013 if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
1014 $params = array(
1015 'width' => '120',
1016 'height' => '120',
1017 );
1018 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
1019
1020 $thumbnail = $file->transform( $params );
1021 $options = array(
1022 'alt' => wfMsg( 'filehist-thumbtext',
1023 $wgLang->timeAndDate( $timestamp, true ),
1024 $wgLang->date( $timestamp, true ),
1025 $wgLang->time( $timestamp, true ) ),
1026 'file-link' => true,
1027 );
1028
1029 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1030
1031 return $thumbnail->toHtml( $options );
1032 } else {
1033 return wfMsgHtml( 'filehist-nothumb' );
1034 }
1035 }
1036 }
1037
1038 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
1039 function __construct( $imagePage ) {
1040 parent::__construct();
1041 $this->mImagePage = $imagePage;
1042 $this->mTitle = clone ( $imagePage->getTitle() );
1043 $this->mTitle->setFragment( '#filehistory' );
1044 $this->mImg = null;
1045 $this->mHist = array();
1046 $this->mRange = array( 0, 0 ); // display range
1047 }
1048
1049 function getTitle() {
1050 return $this->mTitle;
1051 }
1052
1053 function getQueryInfo() {
1054 return false;
1055 }
1056
1057 function getIndexField() {
1058 return '';
1059 }
1060
1061 function formatRow( $row ) {
1062 return '';
1063 }
1064
1065 function getBody() {
1066 $s = '';
1067 $this->doQuery();
1068 if ( count( $this->mHist ) ) {
1069 $list = new ImageHistoryList( $this->mImagePage );
1070 # Generate prev/next links
1071 $navLink = $this->getNavigationBar();
1072 $s = $list->beginImageHistoryList( $navLink );
1073 // Skip rows there just for paging links
1074 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
1075 $file = $this->mHist[$i];
1076 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1077 }
1078 $s .= $list->endImageHistoryList( $navLink );
1079 }
1080 return $s;
1081 }
1082
1083 function doQuery() {
1084 if ( $this->mQueryDone ) return;
1085 $this->mImg = $this->mImagePage->getFile(); // ensure loading
1086 if ( !$this->mImg->exists() ) {
1087 return;
1088 }
1089 $queryLimit = $this->mLimit + 1; // limit plus extra row
1090 if ( $this->mIsBackwards ) {
1091 // Fetch the file history
1092 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
1093 // The current rev may not meet the offset/limit
1094 $numRows = count( $this->mHist );
1095 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
1096 $this->mHist = array_merge( array( $this->mImg ), $this->mHist );
1097 }
1098 } else {
1099 // The current rev may not meet the offset
1100 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
1101 $this->mHist[] = $this->mImg;
1102 }
1103 // Old image versions (fetch extra row for nav links)
1104 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
1105 // Fetch the file history
1106 $this->mHist = array_merge( $this->mHist,
1107 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
1108 }
1109 $numRows = count( $this->mHist ); // Total number of query results
1110 if ( $numRows ) {
1111 # Index value of top item in the list
1112 $firstIndex = $this->mIsBackwards ?
1113 $this->mHist[$numRows - 1]->getTimestamp() : $this->mHist[0]->getTimestamp();
1114 # Discard the extra result row if there is one
1115 if ( $numRows > $this->mLimit && $numRows > 1 ) {
1116 if ( $this->mIsBackwards ) {
1117 # Index value of item past the index
1118 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
1119 # Index value of bottom item in the list
1120 $lastIndex = $this->mHist[1]->getTimestamp();
1121 # Display range
1122 $this->mRange = array( 1, $numRows - 1 );
1123 } else {
1124 # Index value of item past the index
1125 $this->mPastTheEndIndex = $this->mHist[$numRows - 1]->getTimestamp();
1126 # Index value of bottom item in the list
1127 $lastIndex = $this->mHist[$numRows - 2]->getTimestamp();
1128 # Display range
1129 $this->mRange = array( 0, $numRows - 2 );
1130 }
1131 } else {
1132 # Setting indexes to an empty string means that they will be
1133 # omitted if they would otherwise appear in URLs. It just so
1134 # happens that this is the right thing to do in the standard
1135 # UI, in all the relevant cases.
1136 $this->mPastTheEndIndex = '';
1137 # Index value of bottom item in the list
1138 $lastIndex = $this->mIsBackwards ?
1139 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows - 1]->getTimestamp();
1140 # Display range
1141 $this->mRange = array( 0, $numRows - 1 );
1142 }
1143 } else {
1144 $firstIndex = '';
1145 $lastIndex = '';
1146 $this->mPastTheEndIndex = '';
1147 }
1148 if ( $this->mIsBackwards ) {
1149 $this->mIsFirst = ( $numRows < $queryLimit );
1150 $this->mIsLast = ( $this->mOffset == '' );
1151 $this->mLastShown = $firstIndex;
1152 $this->mFirstShown = $lastIndex;
1153 } else {
1154 $this->mIsFirst = ( $this->mOffset == '' );
1155 $this->mIsLast = ( $numRows < $queryLimit );
1156 $this->mLastShown = $lastIndex;
1157 $this->mFirstShown = $firstIndex;
1158 }
1159 $this->mQueryDone = true;
1160 }
1161 }