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