adding support for <a> tags as a parser tag hook, in order to support rdfa output
[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 global $wgLang;
234 $r = '<ul id="filetoc">
235 <li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>
236 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
237 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . "</a></li>\n" .
238 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . "
239 </ul>\n";
240 return $r;
241 }
242
243 /**
244 * Make a table with metadata to be shown in the output page.
245 *
246 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
247 *
248 * @param array $exif The array containing the EXIF data
249 * @return string
250 */
251 protected function makeMetadataTable( $metadata ) {
252 $r = "<div class=\"mw-imagepage-section-metadata\">";
253 $r .= wfMsgNoTrans( 'metadata-help' );
254 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
255 foreach ( $metadata as $type => $stuff ) {
256 foreach ( $stuff as $v ) {
257 # FIXME, why is this using escapeId for a class?!
258 $class = Sanitizer::escapeId( $v['id'] );
259 if( $type == 'collapsed' ) {
260 $class .= ' collapsable';
261 }
262 $r .= "<tr class=\"$class\">\n";
263 $r .= "<th>{$v['name']}</th>\n";
264 $r .= "<td>{$v['value']}</td>\n</tr>";
265 }
266 }
267 $r .= "</table>\n</div>\n";
268 return $r;
269 }
270
271 /**
272 * Overloading Article's getContent method.
273 *
274 * Omit noarticletext if sharedupload; text will be fetched from the
275 * shared upload server if possible.
276 */
277 public function getContent() {
278 $this->loadFile();
279 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
280 return '';
281 }
282 return Article::getContent();
283 }
284
285 protected function openShowImage() {
286 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
287 $wgLang, $wgContLang, $wgEnableUploads;
288
289 $this->loadFile();
290
291 $full_url = $this->displayImg->getURL();
292 $linkAttribs = false;
293 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
294 if( !isset( $wgImageLimits[$sizeSel] ) ) {
295 $sizeSel = User::getDefaultOption( 'imagesize' );
296
297 // The user offset might still be incorrect, specially if
298 // $wgImageLimits got changed (see bug #8858).
299 if( !isset( $wgImageLimits[$sizeSel] ) ) {
300 // Default to the first offset in $wgImageLimits
301 $sizeSel = 0;
302 }
303 }
304 $max = $wgImageLimits[$sizeSel];
305 $maxWidth = $max[0];
306 $maxHeight = $max[1];
307 $sk = $wgUser->getSkin();
308 $dirmark = $wgContLang->getDirMark();
309
310 if( $this->displayImg->exists() ) {
311 # image
312 $page = $wgRequest->getIntOrNull( 'page' );
313 if( is_null( $page ) ) {
314 $params = array();
315 $page = 1;
316 } else {
317 $params = array( 'page' => $page );
318 }
319 $width_orig = $this->displayImg->getWidth();
320 $width = $width_orig;
321 $height_orig = $this->displayImg->getHeight();
322 $height = $height_orig;
323 $mime = $this->displayImg->getMimeType();
324 $showLink = false;
325 $linkAttribs = array( 'href' => $full_url );
326 $longDesc = $this->displayImg->getLongDesc();
327
328 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
329
330 if( $this->displayImg->allowInlineDisplay() ) {
331 # image
332
333 # "Download high res version" link below the image
334 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
335 # We'll show a thumbnail of this image
336 if( $width > $maxWidth || $height > $maxHeight ) {
337 # Calculate the thumbnail size.
338 # First case, the limiting factor is the width, not the height.
339 if( $width / $height >= $maxWidth / $maxHeight ) {
340 $height = round( $height * $maxWidth / $width);
341 $width = $maxWidth;
342 # Note that $height <= $maxHeight now.
343 } else {
344 $newwidth = floor( $width * $maxHeight / $height);
345 $height = round( $height * $newwidth / $width );
346 $width = $newwidth;
347 # Note that $height <= $maxHeight now, but might not be identical
348 # because of rounding.
349 }
350 $msgbig = wfMsgHtml( 'show-big-image' );
351 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
352 $wgLang->formatNum( $width ),
353 $wgLang->formatNum( $height )
354 );
355 } else {
356 # Image is small enough to show full size on image page
357 $msgbig = htmlspecialchars( $this->displayImg->getName() );
358 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
359 }
360
361 $params['width'] = $width;
362 $thumbnail = $this->displayImg->transform( $params );
363
364 $anchorclose = "<br />";
365 if( $this->displayImg->mustRender() ) {
366 $showLink = true;
367 } else {
368 $anchorclose .=
369 $msgsmall .
370 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
371 }
372
373 if( $this->displayImg->isMultipage() ) {
374 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
375 }
376
377 if( $thumbnail ) {
378 $options = array(
379 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
380 'file-link' => true,
381 );
382 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
383 $thumbnail->toHtml( $options ) .
384 $anchorclose . "</div>\n" );
385 }
386
387 if( $this->displayImg->isMultipage() ) {
388 $count = $this->displayImg->pageCount();
389
390 if( $page > 1 ) {
391 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
392 $link = $sk->link(
393 $this->mTitle,
394 $label,
395 array(),
396 array( 'page' => $page - 1 ),
397 array( 'known', 'noclasses' )
398 );
399 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
400 array( 'page' => $page - 1 ) );
401 } else {
402 $thumb1 = '';
403 }
404
405 if( $page < $count ) {
406 $label = wfMsg( 'imgmultipagenext' );
407 $link = $sk->link(
408 $this->mTitle,
409 $label,
410 array(),
411 array( 'page' => $page + 1 ),
412 array( 'known', 'noclasses' )
413 );
414 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
415 array( 'page' => $page + 1 ) );
416 } else {
417 $thumb2 = '';
418 }
419
420 global $wgScript;
421
422 $formParams = array(
423 'name' => 'pageselector',
424 'action' => $wgScript,
425 'onchange' => 'document.pageselector.submit();',
426 );
427
428 $option = array();
429 for ( $i=1; $i <= $count; $i++ ) {
430 $options[] = Xml::option( $wgLang->formatNum($i), $i, $i == $page );
431 }
432 $select = Xml::tags( 'select',
433 array( 'id' => 'pageselector', 'name' => 'page' ),
434 implode( "\n", $options ) );
435
436 $wgOut->addHTML(
437 '</td><td><div class="multipageimagenavbox">' .
438 Xml::openElement( 'form', $formParams ) .
439 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
440 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
441 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
442 Xml::closeElement( 'form' ) .
443 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
444 );
445 }
446 } else {
447 #if direct link is allowed but it's not a renderable image, show an icon.
448 if( $this->displayImg->isSafeFile() ) {
449 $icon= $this->displayImg->iconThumb();
450
451 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
452 $icon->toHtml( array( 'file-link' => true ) ) .
453 "</div>\n" );
454 }
455
456 $showLink = true;
457 }
458
459
460 if($showLink) {
461 $filename = wfEscapeWikiText( $this->displayImg->getName() );
462 $medialink = "[[Media:$filename|$filename]]";
463
464 if( !$this->displayImg->isSafeFile() ) {
465 $warning = wfMsgNoTrans( 'mediawarning' );
466 $wgOut->addWikiText( <<<EOT
467 <div class="fullMedia">
468 <span class="dangerousLink">{$medialink}</span>$dirmark
469 <span class="fileInfo">$longDesc</span>
470 </div>
471 <div class="mediaWarning">$warning</div>
472 EOT
473 );
474 } else {
475 $wgOut->addWikiText( <<<EOT
476 <div class="fullMedia">
477 {$medialink}{$dirmark}
478 <span class="fileInfo">$longDesc</span>
479 </div>
480 EOT
481 );
482 }
483 }
484
485 if( !$this->displayImg->isLocal() ) {
486 $this->printSharedImageText();
487 }
488 } else {
489 # Image does not exist
490 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
491 // Only show an upload link if the user can upload
492 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
493 $nofile = array(
494 'filepage-nofile-link',
495 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img->getName() ) )
496 );
497 }
498 else
499 {
500 $nofile = 'filepage-nofile';
501 }
502 $wgOut->setRobotPolicy( 'noindex,nofollow' );
503 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
504 }
505 }
506
507 /**
508 * Show a notice that the file is from a shared repository
509 */
510 protected function printSharedImageText() {
511 global $wgOut;
512
513 $this->loadFile();
514
515 $descUrl = $this->img->getDescriptionUrl();
516 $descText = $this->img->getDescriptionText();
517
518 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
519 $repo = $this->img->getRepo()->getDisplayName();
520
521 $msg = '';
522 if( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
523 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
524 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
525 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
526 } else {
527 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
528 }
529
530 if( $descText ) {
531 $this->mExtraDescription = $descText;
532 }
533 }
534
535 public function getUploadUrl() {
536 $this->loadFile();
537 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
538 return $uploadTitle->getFullUrl( array(
539 'wpDestFile' => $this->img->getName(),
540 'wpForReUpload' => 1
541 ) );
542 }
543
544 /**
545 * Print out the various links at the bottom of the image page, e.g. reupload,
546 * external editing (and instructions link) etc.
547 */
548 protected function uploadLinksBox() {
549 global $wgUser, $wgOut, $wgEnableUploads;
550
551 if( !$wgEnableUploads ) { return; }
552
553 $this->loadFile();
554 if( !$this->img->isLocal() )
555 return;
556
557 $sk = $wgUser->getSkin();
558
559 $wgOut->addHTML( "<br /><ul>\n" );
560
561 # "Upload a new version of this file" link
562 if( UploadBase::userCanReUpload($wgUser,$this->img->name) ) {
563 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
564 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
565 }
566
567 # External editing link
568 $elink = $sk->link(
569 $this->mTitle,
570 wfMsgHtml( 'edit-externally' ),
571 array(),
572 array(
573 'action' => 'edit',
574 'externaledit' => 'true',
575 'mode' => 'file'
576 ),
577 array( 'known', 'noclasses' )
578 );
579 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
580
581 $wgOut->addHTML( "</ul>\n" );
582 }
583
584 protected function closeShowImage() {} # For overloading
585
586 /**
587 * If the page we've just displayed is in the "Image" namespace,
588 * we follow it with an upload history of the image and its usage.
589 */
590 protected function imageHistory() {
591 global $wgOut, $wgUseExternalEditor;
592
593 $this->loadFile();
594 $pager = new ImageHistoryPseudoPager( $this );
595 $wgOut->addHTML( $pager->getBody() );
596
597 $this->img->resetHistory(); // free db resources
598
599 # Exist check because we don't want to show this on pages where an image
600 # doesn't exist along with the noimage message, that would suck. -ævar
601 if( $wgUseExternalEditor && $this->img->exists() ) {
602 $this->uploadLinksBox();
603 }
604 }
605
606 protected function imageLinks() {
607 global $wgUser, $wgOut, $wgLang;
608
609 $limit = 100;
610
611 $dbr = wfGetDB( DB_SLAVE );
612
613 $res = $dbr->select(
614 array( 'imagelinks', 'page' ),
615 array( 'page_namespace', 'page_title' ),
616 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
617 __METHOD__,
618 array( 'LIMIT' => $limit + 1)
619 );
620 $count = $dbr->numRows( $res );
621 if( $count == 0 ) {
622 $wgOut->addHTML( "<div id='mw-imagepage-nolinkstoimage'>\n" );
623 $wgOut->addWikiMsg( 'nolinkstoimage' );
624 $wgOut->addHTML( "</div>\n" );
625 return;
626 }
627
628 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
629 if( $count <= $limit - 1 ) {
630 $wgOut->addWikiMsg( 'linkstoimage', $count );
631 } else {
632 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
633 $wgOut->addWikiMsg( 'linkstoimage-more',
634 $wgLang->formatNum( $limit ),
635 $this->mTitle->getPrefixedDBkey()
636 );
637 }
638
639 $wgOut->addHTML( "<ul class='mw-imagepage-linkstoimage'>\n" );
640 $sk = $wgUser->getSkin();
641 $count = 0;
642 while ( $s = $res->fetchObject() ) {
643 $count++;
644 if( $count <= $limit ) {
645 // We have not yet reached the extra one that tells us there is more to fetch
646 $link = $sk->link(
647 Title::makeTitle( $s->page_namespace, $s->page_title ),
648 null,
649 array(),
650 array(),
651 array( 'known', 'noclasses' )
652 );
653 $wgOut->addHTML( "<li>{$link}</li>\n" );
654 }
655 }
656 $wgOut->addHTML( "</ul>\n" );
657 $res->free();
658
659 // Add a links to [[Special:Whatlinkshere]]
660 if( $count > $limit )
661 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
662 $wgOut->addHTML( "</div>\n" );
663 }
664
665 protected function imageRedirects() {
666 global $wgUser, $wgOut, $wgLang;
667
668 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
669 if( count( $redirects ) == 0 ) return;
670
671 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
672 $wgOut->addWikiMsg( 'redirectstofile',
673 $wgLang->formatNum( count( $redirects ) )
674 );
675 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
676
677 $sk = $wgUser->getSkin();
678 foreach ( $redirects as $title ) {
679 $link = $sk->link(
680 $title,
681 null,
682 array(),
683 array( 'redirect' => 'no' ),
684 array( 'known', 'noclasses' )
685 );
686 $wgOut->addHTML( "<li>{$link}</li>\n" );
687 }
688 $wgOut->addHTML( "</ul></div>\n" );
689
690 }
691
692 protected function imageDupes() {
693 global $wgOut, $wgUser, $wgLang;
694
695 $this->loadFile();
696
697 $dupes = $this->getDuplicates();
698 if( count( $dupes ) == 0 ) return;
699
700 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
701 $wgOut->addWikiMsg( 'duplicatesoffile',
702 $wgLang->formatNum( count( $dupes ) ), $this->mTitle->getDBkey()
703 );
704 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
705
706 $sk = $wgUser->getSkin();
707 foreach ( $dupes as $file ) {
708 $fromSrc = '';
709 if( $file->isLocal() ) {
710 $link = $sk->link(
711 $file->getTitle(),
712 null,
713 array(),
714 array(),
715 array( 'known', 'noclasses' )
716 );
717 } else {
718 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
719 $file->getTitle()->getPrefixedText() );
720 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
721 }
722 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
723 }
724 $wgOut->addHTML( "</ul></div>\n" );
725 }
726
727 /**
728 * Delete the file, or an earlier version of it
729 */
730 public function delete() {
731 global $wgUploadMaintenance;
732 if( $wgUploadMaintenance && $this->mTitle && $this->mTitle->getNamespace() == NS_FILE ) {
733 global $wgOut;
734 $wgOut->wrapWikiMsg( "<div class='error'>\n$1</div>\n", array( 'filedelete-maintenance' ) );
735 return;
736 }
737
738 $this->loadFile();
739 if( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
740 // Standard article deletion
741 Article::delete();
742 return;
743 }
744 $deleter = new FileDeleteForm( $this->img );
745 $deleter->execute();
746 }
747
748 /**
749 * Revert the file to an earlier version
750 */
751 public function revert() {
752 $this->loadFile();
753 $reverter = new FileRevertForm( $this->img );
754 $reverter->execute();
755 }
756
757 /**
758 * Override handling of action=purge
759 */
760 public function doPurge() {
761 $this->loadFile();
762 if( $this->img->exists() ) {
763 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
764 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
765 $update->doUpdate();
766 $this->img->upgradeRow();
767 $this->img->purgeCache();
768 } else {
769 wfDebug( "ImagePage::doPurge no image for " . $this->img->getName() . "; limiting purge to cache only\n" );
770 // even if the file supposedly doesn't exist, force any cached information
771 // to be updated (in case the cached information is wrong)
772 $this->img->purgeCache();
773 }
774 parent::doPurge();
775 }
776
777 /**
778 * Display an error with a wikitext description
779 */
780 function showError( $description ) {
781 global $wgOut;
782 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
783 $wgOut->setRobotPolicy( "noindex,nofollow" );
784 $wgOut->setArticleRelated( false );
785 $wgOut->enableClientCache( false );
786 $wgOut->addWikiText( $description );
787 }
788
789 }
790
791 /**
792 * Builds the image revision log shown on image pages
793 *
794 * @ingroup Media
795 */
796 class ImageHistoryList {
797
798 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
799
800 public function __construct( $imagePage ) {
801 global $wgUser, $wgShowArchiveThumbnails;
802 $this->skin = $wgUser->getSkin();
803 $this->current = $imagePage->getFile();
804 $this->img = $imagePage->getDisplayedFile();
805 $this->title = $imagePage->getTitle();
806 $this->imagePage = $imagePage;
807 $this->showThumb = $wgShowArchiveThumbnails && $this->img->canRender();
808 }
809
810 public function getImagePage() {
811 return $this->imagePage;
812 }
813
814 public function getSkin() {
815 return $this->skin;
816 }
817
818 public function getFile() {
819 return $this->img;
820 }
821
822 public function beginImageHistoryList( $navLinks = '' ) {
823 global $wgOut, $wgUser;
824 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
825 . "<div id=\"mw-imagepage-section-filehistory\">\n"
826 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
827 . $navLinks . "\n"
828 . Xml::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
829 . '<tr><td></td>'
830 . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deletedhistory') ) ? '<td></td>' : '' )
831 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
832 . ( $this->showThumb ? '<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
833 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
834 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
835 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
836 . "</tr>\n";
837 }
838
839 public function endImageHistoryList( $navLinks = '' ) {
840 return "</table>\n$navLinks\n</div>\n";
841 }
842
843 public function imageHistoryLine( $iscur, $file ) {
844 global $wgUser, $wgLang, $wgContLang, $wgTitle;
845
846 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
847 $img = $iscur ? $file->getName() : $file->getArchiveName();
848 $user = $file->getUser('id');
849 $usertext = $file->getUser('text');
850 $description = $file->getDescription();
851
852 $local = $this->current->isLocal();
853 $row = $css = $selected = '';
854
855 // Deletion link
856 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deletedhistory') ) ) {
857 $row .= '<td>';
858 # Link to remove from history
859 if( $wgUser->isAllowed( 'delete' ) ) {
860 $q = array( 'action' => 'delete' );
861 if( !$iscur )
862 $q['oldimage'] = $img;
863 $row .= $this->skin->link(
864 $this->title,
865 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
866 array(), $q, array( 'known' )
867 );
868 }
869 # Link to hide content. Don't show useless link to people who cannot hide revisions.
870 $canHide = $wgUser->isAllowed( 'deleterevision' );
871 if( $canHide || ($wgUser->isAllowed('deletedhistory') && $file->getVisibility()) ) {
872 if( $wgUser->isAllowed('delete') ) {
873 $row .= '<br/>';
874 }
875 // If file is top revision or locked from this user, don't link
876 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
877 $del = $this->skin->revDeleteLinkDisabled( $canHide );
878 } else {
879 list( $ts, $name ) = explode( '!', $img, 2 );
880 $query = array(
881 'type' => 'oldimage',
882 'target' => $wgTitle->getPrefixedText(),
883 'ids' => $ts,
884 );
885 $del = $this->skin->revDeleteLink( $query,
886 $file->isDeleted(File::DELETED_RESTRICTED), $canHide );
887 }
888 $row .= $del;
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 }