(bug 25717) Fix "Hide/show extended details" toggle in image metadata table
[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 $showLink = false;
331 $linkAttribs = array( 'href' => $full_url );
332 $longDesc = $this->displayImg->getLongDesc();
333
334 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
335
336 if ( $this->displayImg->allowInlineDisplay() ) {
337 # image
338
339 # "Download high res version" link below the image
340 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
341 # We'll show a thumbnail of this image
342 if ( $width > $maxWidth || $height > $maxHeight ) {
343 # Calculate the thumbnail size.
344 # First case, the limiting factor is the width, not the height.
345 if ( $width / $height >= $maxWidth / $maxHeight ) {
346 $height = round( $height * $maxWidth / $width );
347 $width = $maxWidth;
348 # Note that $height <= $maxHeight now.
349 } else {
350 $newwidth = floor( $width * $maxHeight / $height );
351 $height = round( $height * $newwidth / $width );
352 $width = $newwidth;
353 # Note that $height <= $maxHeight now, but might not be identical
354 # because of rounding.
355 }
356 $msgbig = wfMsgHtml( 'show-big-image' );
357 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
358 $wgLang->formatNum( $width ),
359 $wgLang->formatNum( $height )
360 );
361 } else {
362 # Image is small enough to show full size on image page
363 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
364 }
365
366 $params['width'] = $width;
367 $thumbnail = $this->displayImg->transform( $params );
368
369 $showLink = true;
370 $anchorclose = '';
371 if ( !$this->displayImg->mustRender() ) {
372 $anchorclose = "<br />" . $msgsmall;
373 }
374
375 $isMulti = $this->displayImg->isMultipage() && $this->displayImg->pageCount() > 1;
376 if ( $isMulti ) {
377 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
378 }
379
380 if ( $thumbnail ) {
381 $options = array(
382 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
383 'file-link' => true,
384 );
385 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
386 $thumbnail->toHtml( $options ) .
387 $anchorclose . "</div>\n" );
388 }
389
390 if ( $isMulti ) {
391 $count = $this->displayImg->pageCount();
392
393 if ( $page > 1 ) {
394 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
395 $link = $sk->link(
396 $this->mTitle,
397 $label,
398 array(),
399 array( 'page' => $page - 1 ),
400 array( 'known', 'noclasses' )
401 );
402 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
403 array( 'page' => $page - 1 ) );
404 } else {
405 $thumb1 = '';
406 }
407
408 if ( $page < $count ) {
409 $label = wfMsg( 'imgmultipagenext' );
410 $link = $sk->link(
411 $this->mTitle,
412 $label,
413 array(),
414 array( 'page' => $page + 1 ),
415 array( 'known', 'noclasses' )
416 );
417 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
418 array( 'page' => $page + 1 ) );
419 } else {
420 $thumb2 = '';
421 }
422
423 global $wgScript;
424
425 $formParams = array(
426 'name' => 'pageselector',
427 'action' => $wgScript,
428 'onchange' => 'document.pageselector.submit();',
429 );
430
431 for ( $i = 1; $i <= $count; $i++ ) {
432 $options[] = Xml::option( $wgLang->formatNum( $i ), $i, $i == $page );
433 }
434 $select = Xml::tags( 'select',
435 array( 'id' => 'pageselector', 'name' => 'page' ),
436 implode( "\n", $options ) );
437
438 $wgOut->addHTML(
439 '</td><td><div class="multipageimagenavbox">' .
440 Xml::openElement( 'form', $formParams ) .
441 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
442 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
443 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
444 Xml::closeElement( 'form' ) .
445 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
446 );
447 }
448 } else {
449 # if direct link is allowed but it's not a renderable image, show an icon.
450 if ( $this->displayImg->isSafeFile() ) {
451 $icon = $this->displayImg->iconThumb();
452
453 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
454 $icon->toHtml( array( 'file-link' => true ) ) .
455 "</div>\n" );
456 }
457
458 $showLink = true;
459 }
460
461
462 if ( $showLink ) {
463 $filename = wfEscapeWikiText( $this->displayImg->getName() );
464 $linktext = $filename;
465 if ( isset( $msgbig ) ) {
466 $linktext = wfEscapeWikiText( $msgbig );
467 }
468 $medialink = "[[Media:$filename|$linktext]]";
469
470 if ( !$this->displayImg->isSafeFile() ) {
471 $warning = wfMsgNoTrans( 'mediawarning' );
472 $wgOut->addWikiText( <<<EOT
473 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
474 <div class="mediaWarning">$warning</div>
475 EOT
476 );
477 } else {
478 $wgOut->addWikiText( <<<EOT
479 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
480 </div>
481 EOT
482 );
483 }
484 }
485
486 if ( !$this->displayImg->isLocal() ) {
487 $this->printSharedImageText();
488 }
489 } else {
490 # Image does not exist
491 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
492 // Only show an upload link if the user can upload
493 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
494 $nofile = array(
495 'filepage-nofile-link',
496 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img->getName() ) )
497 );
498 }
499 else
500 {
501 $nofile = 'filepage-nofile';
502 }
503 $wgOut->setRobotPolicy( 'noindex,nofollow' );
504 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
505 }
506 }
507
508 /**
509 * Show a notice that the file is from a shared repository
510 */
511 protected function printSharedImageText() {
512 global $wgOut;
513
514 $this->loadFile();
515
516 $descUrl = $this->img->getDescriptionUrl();
517 $descText = $this->img->getDescriptionText();
518
519 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
520 $repo = $this->img->getRepo()->getDisplayName();
521
522 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
523 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
524 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
525 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
526 } else {
527 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
528 }
529
530 if ( $descText ) {
531 $this->mExtraDescription = $descText;
532 }
533 }
534
535 public function getUploadUrl() {
536 $this->loadFile();
537 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
538 return $uploadTitle->getFullUrl( array(
539 'wpDestFile' => $this->img->getName(),
540 'wpForReUpload' => 1
541 ) );
542 }
543
544 /**
545 * Print out the various links at the bottom of the image page, e.g. reupload,
546 * external editing (and instructions link) etc.
547 */
548 protected function uploadLinksBox() {
549 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
550
551 if ( !$wgEnableUploads ) { return; }
552
553 $this->loadFile();
554 if ( !$this->img->isLocal() )
555 return;
556
557 $sk = $wgUser->getSkin();
558
559 $wgOut->addHTML( "<br /><ul>\n" );
560
561 # "Upload a new version of this file" link
562 if ( UploadBase::userCanReUpload( $wgUser, $this->img->name ) ) {
563 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
564 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
565 }
566
567 # External editing link
568 if ( $wgUseExternalEditor ) {
569 $elink = $sk->link(
570 $this->mTitle,
571 wfMsgHtml( 'edit-externally' ),
572 array(),
573 array(
574 'action' => 'edit',
575 'externaledit' => 'true',
576 'mode' => 'file'
577 ),
578 array( 'known', 'noclasses' )
579 );
580 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
581 }
582
583 $wgOut->addHTML( "</ul>\n" );
584 }
585
586 protected function closeShowImage() { } # For overloading
587
588 /**
589 * If the page we've just displayed is in the "Image" namespace,
590 * we follow it with an upload history of the image and its usage.
591 */
592 protected function imageHistory() {
593 global $wgOut;
594
595 $this->loadFile();
596 $pager = new ImageHistoryPseudoPager( $this );
597 $wgOut->addHTML( $pager->getBody() );
598
599 $this->img->resetHistory(); // free db resources
600
601 # Exist check because we don't want to show this on pages where an image
602 # doesn't exist along with the noimage message, that would suck. -ævar
603 if ( $this->img->exists() ) {
604 $this->uploadLinksBox();
605 }
606 }
607
608 protected function imageLinks() {
609 global $wgUser, $wgOut, $wgLang;
610
611 $limit = 100;
612
613 $dbr = wfGetDB( DB_SLAVE );
614
615 $res = $dbr->select(
616 array( 'imagelinks', 'page' ),
617 array( 'page_namespace', 'page_title' ),
618 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
619 __METHOD__,
620 array( 'LIMIT' => $limit + 1 )
621 );
622 $count = $dbr->numRows( $res );
623 if ( $count == 0 ) {
624 $wgOut->wrapWikiMsg( Html::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
625 return;
626 }
627
628 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
629 if ( $count <= $limit - 1 ) {
630 $wgOut->addWikiMsg( 'linkstoimage', $count );
631 } else {
632 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
633 $wgOut->addWikiMsg( 'linkstoimage-more',
634 $wgLang->formatNum( $limit ),
635 $this->mTitle->getPrefixedDBkey()
636 );
637 }
638
639 $wgOut->addHTML( Html::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
640 $sk = $wgUser->getSkin();
641 $count = 0;
642 $elements = array();
643 foreach ( $res as $s ) {
644 $count++;
645 if ( $count <= $limit ) {
646 // We have not yet reached the extra one that tells us there is more to fetch
647 $elements[] = $s;
648 }
649 }
650
651 // Sort the list by namespace:title
652 usort ( $elements, array( $this, 'compare' ) );
653
654 // Create links for every element
655 foreach( $elements as $element ) {
656 $link = $sk->linkKnown( Title::makeTitle( $element->page_namespace, $element->page_title ) );
657 $wgOut->addHTML( Html::rawElement(
658 'li',
659 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace ),
660 $link
661 ) . "\n"
662 );
663
664 };
665 $wgOut->addHTML( Html::closeElement( 'ul' ) . "\n" );
666 $res->free();
667
668 // Add a links to [[Special:Whatlinkshere]]
669 if ( $count > $limit ) {
670 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
671 }
672 $wgOut->addHTML( Html::closeElement( 'div' ) . "\n" );
673 }
674
675 protected function imageRedirects() {
676 global $wgUser, $wgOut, $wgLang;
677
678 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
679 if ( count( $redirects ) == 0 ) return;
680
681 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
682 $wgOut->addWikiMsg( 'redirectstofile',
683 $wgLang->formatNum( count( $redirects ) )
684 );
685 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
686
687 $sk = $wgUser->getSkin();
688 foreach ( $redirects as $title ) {
689 $link = $sk->link(
690 $title,
691 null,
692 array(),
693 array( 'redirect' => 'no' ),
694 array( 'known', 'noclasses' )
695 );
696 $wgOut->addHTML( "<li>{$link}</li>\n" );
697 }
698 $wgOut->addHTML( "</ul></div>\n" );
699
700 }
701
702 protected function imageDupes() {
703 global $wgOut, $wgUser, $wgLang;
704
705 $this->loadFile();
706
707 $dupes = $this->getDuplicates();
708 if ( count( $dupes ) == 0 ) return;
709
710 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
711 $wgOut->addWikiMsg( 'duplicatesoffile',
712 $wgLang->formatNum( count( $dupes ) ), $this->mTitle->getDBkey()
713 );
714 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
715
716 $sk = $wgUser->getSkin();
717 foreach ( $dupes as $file ) {
718 $fromSrc = '';
719 if ( $file->isLocal() ) {
720 $link = $sk->link(
721 $file->getTitle(),
722 null,
723 array(),
724 array(),
725 array( 'known', 'noclasses' )
726 );
727 } else {
728 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
729 $file->getTitle()->getPrefixedText() );
730 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
731 }
732 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
733 }
734 $wgOut->addHTML( "</ul></div>\n" );
735 }
736
737 /**
738 * Delete the file, or an earlier version of it
739 */
740 public function delete() {
741 global $wgUploadMaintenance;
742 if ( $wgUploadMaintenance && $this->mTitle && $this->mTitle->getNamespace() == NS_FILE ) {
743 global $wgOut;
744 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
745 return;
746 }
747
748 $this->loadFile();
749 if ( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
750 // Standard article deletion
751 parent::delete();
752 return;
753 }
754 $deleter = new FileDeleteForm( $this->img );
755 $deleter->execute();
756 }
757
758 /**
759 * Revert the file to an earlier version
760 */
761 public function revert() {
762 $this->loadFile();
763 $reverter = new FileRevertForm( $this->img );
764 $reverter->execute();
765 }
766
767 /**
768 * Override handling of action=purge
769 */
770 public function doPurge() {
771 $this->loadFile();
772 if ( $this->img->exists() ) {
773 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
774 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
775 $update->doUpdate();
776 $this->img->upgradeRow();
777 $this->img->purgeCache();
778 } else {
779 wfDebug( "ImagePage::doPurge no image for " . $this->img->getName() . "; limiting purge to cache only\n" );
780 // even if the file supposedly doesn't exist, force any cached information
781 // to be updated (in case the cached information is wrong)
782 $this->img->purgeCache();
783 }
784 parent::doPurge();
785 }
786
787 /**
788 * Display an error with a wikitext description
789 */
790 function showError( $description ) {
791 global $wgOut;
792 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
793 $wgOut->setRobotPolicy( "noindex,nofollow" );
794 $wgOut->setArticleRelated( false );
795 $wgOut->enableClientCache( false );
796 $wgOut->addWikiText( $description );
797 }
798
799
800 /**
801 * Callback for usort() to do link sorts by (namespace, title)
802 * Function copied from Title::compare()
803 *
804 * @param $a object page to compare with
805 * @param $b object page to compare with
806 * @return Integer: result of string comparison, or namespace comparison
807 */
808 protected function compare( $a, $b ) {
809 if ( $a->page_namespace == $b->page_namespace ) {
810 return strcmp( $a->page_title, $b->page_title );
811 } else {
812 return $a->page_namespace - $b->page_namespace;
813 }
814 }
815 }
816
817 /**
818 * Builds the image revision log shown on image pages
819 *
820 * @ingroup Media
821 */
822 class ImageHistoryList {
823
824 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
825
826 public function __construct( $imagePage ) {
827 global $wgUser, $wgShowArchiveThumbnails;
828 $this->skin = $wgUser->getSkin();
829 $this->current = $imagePage->getFile();
830 $this->img = $imagePage->getDisplayedFile();
831 $this->title = $imagePage->getTitle();
832 $this->imagePage = $imagePage;
833 $this->showThumb = $wgShowArchiveThumbnails && $this->img->canRender();
834 }
835
836 public function getImagePage() {
837 return $this->imagePage;
838 }
839
840 public function getSkin() {
841 return $this->skin;
842 }
843
844 public function getFile() {
845 return $this->img;
846 }
847
848 public function beginImageHistoryList( $navLinks = '' ) {
849 global $wgOut, $wgUser;
850 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
851 . "<div id=\"mw-imagepage-section-filehistory\">\n"
852 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
853 . $navLinks . "\n"
854 . Xml::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
855 . '<tr><td></td>'
856 . ( $this->current->isLocal() && ( $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deletedhistory' ) ) ? '<td></td>' : '' )
857 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
858 . ( $this->showThumb ? '<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
859 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
860 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
861 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
862 . "</tr>\n";
863 }
864
865 public function endImageHistoryList( $navLinks = '' ) {
866 return "</table>\n$navLinks\n</div>\n";
867 }
868
869 public function imageHistoryLine( $iscur, $file ) {
870 global $wgUser, $wgLang;
871
872 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
873 $img = $iscur ? $file->getName() : $file->getArchiveName();
874 $user = $file->getUser( 'id' );
875 $usertext = $file->getUser( 'text' );
876 $description = $file->getDescription();
877
878 $local = $this->current->isLocal();
879 $row = $selected = '';
880
881 // Deletion link
882 if ( $local && ( $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deletedhistory' ) ) ) {
883 $row .= '<td>';
884 # Link to remove from history
885 if ( $wgUser->isAllowed( 'delete' ) ) {
886 $q = array( 'action' => 'delete' );
887 if ( !$iscur )
888 $q['oldimage'] = $img;
889 $row .= $this->skin->link(
890 $this->title,
891 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
892 array(), $q, array( 'known' )
893 );
894 }
895 # Link to hide content. Don't show useless link to people who cannot hide revisions.
896 $canHide = $wgUser->isAllowed( 'deleterevision' );
897 if ( $canHide || ( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
898 if ( $wgUser->isAllowed( 'delete' ) ) {
899 $row .= '<br />';
900 }
901 // If file is top revision or locked from this user, don't link
902 if ( $iscur || !$file->userCan( File::DELETED_RESTRICTED ) ) {
903 $del = $this->skin->revDeleteLinkDisabled( $canHide );
904 } else {
905 list( $ts, $name ) = explode( '!', $img, 2 );
906 $query = array(
907 'type' => 'oldimage',
908 'target' => $this->title->getPrefixedText(),
909 'ids' => $ts,
910 );
911 $del = $this->skin->revDeleteLink( $query,
912 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
913 }
914 $row .= $del;
915 }
916 $row .= '</td>';
917 }
918
919 // Reversion link/current indicator
920 $row .= '<td>';
921 if ( $iscur ) {
922 $row .= wfMsgHtml( 'filehist-current' );
923 } elseif ( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
924 if ( $file->isDeleted( File::DELETED_FILE ) ) {
925 $row .= wfMsgHtml( 'filehist-revert' );
926 } else {
927 $row .= $this->skin->link(
928 $this->title,
929 wfMsgHtml( 'filehist-revert' ),
930 array(),
931 array(
932 'action' => 'revert',
933 'oldimage' => $img,
934 'wpEditToken' => $wgUser->editToken( $img )
935 ),
936 array( 'known', 'noclasses' )
937 );
938 }
939 }
940 $row .= '</td>';
941
942 // Date/time and image link
943 if ( $file->getTimestamp() === $this->img->getTimestamp() ) {
944 $selected = "class='filehistory-selected'";
945 }
946 $row .= "<td $selected style='white-space: nowrap;'>";
947 if ( !$file->userCan( File::DELETED_FILE ) ) {
948 # Don't link to unviewable files
949 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
950 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
951 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
952 # Make a link to review the image
953 $url = $this->skin->link(
954 $revdel,
955 $wgLang->timeAndDate( $timestamp, true ),
956 array(),
957 array(
958 'target' => $this->title->getPrefixedText(),
959 'file' => $img,
960 'token' => $wgUser->editToken( $img )
961 ),
962 array( 'known', 'noclasses' )
963 );
964 $row .= '<span class="history-deleted">' . $url . '</span>';
965 } else {
966 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
967 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
968 }
969 $row .= "</td>";
970
971 // Thumbnail
972 if ( $this->showThumb ) {
973 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
974 }
975
976 // Image dimensions + size
977 $row .= '<td>';
978 $row .= htmlspecialchars( $file->getDimensionsString() );
979 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $file->getSize() ) . ')</span>';
980 $row .= '</td>';
981
982 // Uploading user
983 $row .= '<td>';
984 if ( $local ) {
985 // Hide deleted usernames
986 if ( $file->isDeleted( File::DELETED_USER ) ) {
987 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
988 } else {
989 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
990 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
991 }
992 } else {
993 $row .= htmlspecialchars( $usertext );
994 }
995 $row .= '</td><td>';
996
997 // Don't show deleted descriptions
998 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
999 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1000 } else {
1001 $row .= $this->skin->commentBlock( $description, $this->title );
1002 }
1003 $row .= '</td>';
1004
1005 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1006 $classAttr = $rowClass ? " class='$rowClass'" : "";
1007
1008 return "<tr{$classAttr}>{$row}</tr>\n";
1009 }
1010
1011 protected function getThumbForLine( $file ) {
1012 global $wgLang;
1013
1014 if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
1015 $params = array(
1016 'width' => '120',
1017 'height' => '120',
1018 );
1019 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
1020
1021 $thumbnail = $file->transform( $params );
1022 $options = array(
1023 'alt' => wfMsg( 'filehist-thumbtext',
1024 $wgLang->timeAndDate( $timestamp, true ),
1025 $wgLang->date( $timestamp, true ),
1026 $wgLang->time( $timestamp, true ) ),
1027 'file-link' => true,
1028 );
1029
1030 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1031
1032 return $thumbnail->toHtml( $options );
1033 } else {
1034 return wfMsgHtml( 'filehist-nothumb' );
1035 }
1036 }
1037 }
1038
1039 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
1040 function __construct( $imagePage ) {
1041 parent::__construct();
1042 $this->mImagePage = $imagePage;
1043 $this->mTitle = clone ( $imagePage->getTitle() );
1044 $this->mTitle->setFragment( '#filehistory' );
1045 $this->mImg = null;
1046 $this->mHist = array();
1047 $this->mRange = array( 0, 0 ); // display range
1048 }
1049
1050 function getTitle() {
1051 return $this->mTitle;
1052 }
1053
1054 function getQueryInfo() {
1055 return false;
1056 }
1057
1058 function getIndexField() {
1059 return '';
1060 }
1061
1062 function formatRow( $row ) {
1063 return '';
1064 }
1065
1066 function getBody() {
1067 $s = '';
1068 $this->doQuery();
1069 if ( count( $this->mHist ) ) {
1070 $list = new ImageHistoryList( $this->mImagePage );
1071 # Generate prev/next links
1072 $navLink = $this->getNavigationBar();
1073 $s = $list->beginImageHistoryList( $navLink );
1074 // Skip rows there just for paging links
1075 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
1076 $file = $this->mHist[$i];
1077 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1078 }
1079 $s .= $list->endImageHistoryList( $navLink );
1080 }
1081 return $s;
1082 }
1083
1084 function doQuery() {
1085 if ( $this->mQueryDone ) return;
1086 $this->mImg = $this->mImagePage->getFile(); // ensure loading
1087 if ( !$this->mImg->exists() ) {
1088 return;
1089 }
1090 $queryLimit = $this->mLimit + 1; // limit plus extra row
1091 if ( $this->mIsBackwards ) {
1092 // Fetch the file history
1093 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
1094 // The current rev may not meet the offset/limit
1095 $numRows = count( $this->mHist );
1096 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
1097 $this->mHist = array_merge( array( $this->mImg ), $this->mHist );
1098 }
1099 } else {
1100 // The current rev may not meet the offset
1101 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
1102 $this->mHist[] = $this->mImg;
1103 }
1104 // Old image versions (fetch extra row for nav links)
1105 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
1106 // Fetch the file history
1107 $this->mHist = array_merge( $this->mHist,
1108 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
1109 }
1110 $numRows = count( $this->mHist ); // Total number of query results
1111 if ( $numRows ) {
1112 # Index value of top item in the list
1113 $firstIndex = $this->mIsBackwards ?
1114 $this->mHist[$numRows - 1]->getTimestamp() : $this->mHist[0]->getTimestamp();
1115 # Discard the extra result row if there is one
1116 if ( $numRows > $this->mLimit && $numRows > 1 ) {
1117 if ( $this->mIsBackwards ) {
1118 # Index value of item past the index
1119 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
1120 # Index value of bottom item in the list
1121 $lastIndex = $this->mHist[1]->getTimestamp();
1122 # Display range
1123 $this->mRange = array( 1, $numRows - 1 );
1124 } else {
1125 # Index value of item past the index
1126 $this->mPastTheEndIndex = $this->mHist[$numRows - 1]->getTimestamp();
1127 # Index value of bottom item in the list
1128 $lastIndex = $this->mHist[$numRows - 2]->getTimestamp();
1129 # Display range
1130 $this->mRange = array( 0, $numRows - 2 );
1131 }
1132 } else {
1133 # Setting indexes to an empty string means that they will be
1134 # omitted if they would otherwise appear in URLs. It just so
1135 # happens that this is the right thing to do in the standard
1136 # UI, in all the relevant cases.
1137 $this->mPastTheEndIndex = '';
1138 # Index value of bottom item in the list
1139 $lastIndex = $this->mIsBackwards ?
1140 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows - 1]->getTimestamp();
1141 # Display range
1142 $this->mRange = array( 0, $numRows - 1 );
1143 }
1144 } else {
1145 $firstIndex = '';
1146 $lastIndex = '';
1147 $this->mPastTheEndIndex = '';
1148 }
1149 if ( $this->mIsBackwards ) {
1150 $this->mIsFirst = ( $numRows < $queryLimit );
1151 $this->mIsLast = ( $this->mOffset == '' );
1152 $this->mLastShown = $firstIndex;
1153 $this->mFirstShown = $lastIndex;
1154 } else {
1155 $this->mIsFirst = ( $this->mOffset == '' );
1156 $this->mIsLast = ( $numRows < $queryLimit );
1157 $this->mLastShown = $lastIndex;
1158 $this->mFirstShown = $firstIndex;
1159 }
1160 $this->mQueryDone = true;
1161 }
1162 }