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