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