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