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