Fix return type documentation
[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 /**
27 * @param $file File:
28 * @return void
29 */
30 public function setFile( $file ) {
31 $this->displayImg = $file;
32 $this->img = $file;
33 $this->fileLoaded = true;
34 }
35
36 protected function loadFile() {
37 if ( $this->fileLoaded ) {
38 return true;
39 }
40 $this->fileLoaded = true;
41
42 $this->displayImg = $this->img = false;
43 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img, &$this->displayImg ) );
44 if ( !$this->img ) {
45 $this->img = wfFindFile( $this->mTitle );
46 if ( !$this->img ) {
47 $this->img = wfLocalFile( $this->mTitle );
48 }
49 }
50 if ( !$this->displayImg ) {
51 $this->displayImg = $this->img;
52 }
53 $this->repo = $this->img->getRepo();
54 }
55
56 /**
57 * Handler for action=render
58 * Include body text only; none of the image extras
59 */
60 public function render() {
61 global $wgOut;
62 $wgOut->setArticleBodyOnly( true );
63 parent::view();
64 }
65
66 public function view() {
67 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
68
69 $diff = $wgRequest->getVal( 'diff' );
70 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
71
72 if ( $this->mTitle->getNamespace() != NS_FILE || ( isset( $diff ) && $diffOnly ) ) {
73 return parent::view();
74 }
75
76 $this->loadFile();
77
78 if ( $this->mTitle->getNamespace() == NS_FILE && $this->img->getRedirected() ) {
79 if ( $this->mTitle->getDBkey() == $this->img->getName() || isset( $diff ) ) {
80 // mTitle is the same as the redirect target so ask Article
81 // to perform the redirect for us.
82 $wgRequest->setVal( 'diffonly', 'true' );
83 return parent::view();
84 } else {
85 // mTitle is not the same as the redirect target so it is
86 // probably the redirect page itself. Fake the redirect symbol
87 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
88 $wgOut->addHTML( $this->viewRedirect( Title::makeTitle( NS_FILE, $this->img->getName() ),
89 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
90 $this->viewUpdates();
91 return;
92 }
93 }
94
95 $this->showRedirectedFromHeader();
96
97 if ( $wgShowEXIF && $this->displayImg->exists() ) {
98 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
99 $formattedMetadata = $this->displayImg->formatMetadata();
100 $showmeta = $formattedMetadata !== false;
101 } else {
102 $showmeta = false;
103 }
104
105 if ( !$diff && $this->displayImg->exists() )
106 $wgOut->addHTML( $this->showTOC( $showmeta ) );
107
108 if ( !$diff )
109 $this->openShowImage();
110
111 # No need to display noarticletext, we use our own message, output in openShowImage()
112 if ( $this->getID() ) {
113 parent::view();
114 } else {
115 # Just need to set the right headers
116 $wgOut->setArticleFlag( true );
117 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
118 $this->viewUpdates();
119 }
120
121 # Show shared description, if needed
122 if ( $this->mExtraDescription ) {
123 $fol = wfMessage( 'shareddescriptionfollows' );
124 if ( !$fol->isDisabled() ) {
125 $wgOut->addWikiText( $fol->plain() );
126 }
127 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . "</div>\n" );
128 }
129
130 $this->closeShowImage();
131 $this->imageHistory();
132 // TODO: Cleanup the following
133
134 $wgOut->addHTML( Xml::element( 'h2',
135 array( 'id' => 'filelinks' ),
136 wfMsg( 'imagelinks' ) ) . "\n" );
137 $this->imageDupes();
138 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
139 # Yet we return metadata about the target. Definitely an issue in the FileRepo
140 $this->imageRedirects();
141 $this->imageLinks();
142
143 # Allow extensions to add something after the image links
144 $html = '';
145 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
146 if ( $html )
147 $wgOut->addHTML( $html );
148
149 if ( $showmeta ) {
150 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
151 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
152 $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
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 parent::getContent();
294 }
295
296 protected function openShowImage() {
297 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
298 $wgLang, $wgContLang, $wgEnableUploads;
299
300 $this->loadFile();
301
302 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
303 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
304 $sizeSel = User::getDefaultOption( 'imagesize' );
305
306 // The user offset might still be incorrect, specially if
307 // $wgImageLimits got changed (see bug #8858).
308 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
309 // Default to the first offset in $wgImageLimits
310 $sizeSel = 0;
311 }
312 }
313 $max = $wgImageLimits[$sizeSel];
314 $maxWidth = $max[0];
315 $maxHeight = $max[1];
316 $sk = $wgUser->getSkin();
317 $dirmark = $wgContLang->getDirMark();
318
319 if ( $this->displayImg->exists() ) {
320 # image
321 $page = $wgRequest->getIntOrNull( 'page' );
322 if ( is_null( $page ) ) {
323 $params = array();
324 $page = 1;
325 } else {
326 $params = array( 'page' => $page );
327 }
328 $width_orig = $this->displayImg->getWidth( $page );
329 $width = $width_orig;
330 $height_orig = $this->displayImg->getHeight( $page );
331 $height = $height_orig;
332
333 $longDesc = wfMsg( 'parentheses', $this->displayImg->getLongDesc() );
334
335 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
336
337 if ( $this->displayImg->allowInlineDisplay() ) {
338 # image
339
340 # "Download high res version" link below the image
341 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
342 # We'll show a thumbnail of this image
343 if ( $width > $maxWidth || $height > $maxHeight ) {
344 # Calculate the thumbnail size.
345 # First case, the limiting factor is the width, not the height.
346 if ( $width / $height >= $maxWidth / $maxHeight ) {
347 $height = round( $height * $maxWidth / $width );
348 $width = $maxWidth;
349 # Note that $height <= $maxHeight now.
350 } else {
351 $newwidth = floor( $width * $maxHeight / $height );
352 $height = round( $height * $newwidth / $width );
353 $width = $newwidth;
354 # Note that $height <= $maxHeight now, but might not be identical
355 # because of rounding.
356 }
357 $msgbig = wfMsgHtml( 'show-big-image' );
358 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
359 $wgLang->formatNum( $width ),
360 $wgLang->formatNum( $height )
361 );
362 } else {
363 # Image is small enough to show full size on image page
364 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
365 }
366
367 $params['width'] = $width;
368 $thumbnail = $this->displayImg->transform( $params );
369
370 $showLink = true;
371 $anchorclose = "<br />" . $msgsmall;
372
373 $isMulti = $this->displayImg->isMultipage() && $this->displayImg->pageCount() > 1;
374 if ( $isMulti ) {
375 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
376 }
377
378 if ( $thumbnail ) {
379 $options = array(
380 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
381 'file-link' => true,
382 );
383 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
384 $thumbnail->toHtml( $options ) .
385 $anchorclose . "</div>\n" );
386 }
387
388 if ( $isMulti ) {
389 $count = $this->displayImg->pageCount();
390
391 if ( $page > 1 ) {
392 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
393 $link = $sk->link(
394 $this->mTitle,
395 $label,
396 array(),
397 array( 'page' => $page - 1 ),
398 array( 'known', 'noclasses' )
399 );
400 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
401 array( 'page' => $page - 1 ) );
402 } else {
403 $thumb1 = '';
404 }
405
406 if ( $page < $count ) {
407 $label = wfMsg( 'imgmultipagenext' );
408 $link = $sk->link(
409 $this->mTitle,
410 $label,
411 array(),
412 array( 'page' => $page + 1 ),
413 array( 'known', 'noclasses' )
414 );
415 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
416 array( 'page' => $page + 1 ) );
417 } else {
418 $thumb2 = '';
419 }
420
421 global $wgScript;
422
423 $formParams = array(
424 'name' => 'pageselector',
425 'action' => $wgScript,
426 'onchange' => 'document.pageselector.submit();',
427 );
428
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 Html::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 $linktext = $filename;
463 if ( isset( $msgbig ) ) {
464 $linktext = wfEscapeWikiText( $msgbig );
465 }
466 $medialink = "[[Media:$filename|$linktext]]";
467
468 if ( !$this->displayImg->isSafeFile() ) {
469 $warning = wfMsgNoTrans( 'mediawarning' );
470 $wgOut->addWikiText( <<<EOT
471 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
472 <div class="mediaWarning">$warning</div>
473 EOT
474 );
475 } else {
476 $wgOut->addWikiText( <<<EOT
477 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
478 </div>
479 EOT
480 );
481 }
482 }
483
484 if ( !$this->displayImg->isLocal() ) {
485 $this->printSharedImageText();
486 }
487 } else {
488 # Image does not exist
489 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
490 // Only show an upload link if the user can upload
491 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
492 $nofile = array(
493 'filepage-nofile-link',
494 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img->getName() ) )
495 );
496 }
497 else
498 {
499 $nofile = 'filepage-nofile';
500 }
501 // Note, if there is an image description page, but
502 // no image, then this setRobotPolicy is overriden
503 // by Article::View().
504 $wgOut->setRobotPolicy( 'noindex,nofollow' );
505 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
506 if ( !$this->getID() ) {
507 // If there is no image, no shared image, and no description page,
508 // output a 404, to be consistent with articles.
509 $wgRequest->response()->header( "HTTP/1.x 404 Not Found" );
510 }
511 }
512 }
513
514 /**
515 * Show a notice that the file is from a shared repository
516 */
517 protected function printSharedImageText() {
518 global $wgOut;
519
520 $this->loadFile();
521
522 $descUrl = $this->img->getDescriptionUrl();
523 $descText = $this->img->getDescriptionText();
524
525 /* Add canonical to head if there is no local page for this shared file */
526 if( $descUrl && $this->getID() == 0 ) {
527 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
528 }
529
530 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
531 $repo = $this->img->getRepo()->getDisplayName();
532
533 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
534 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
535 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
536 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
537 } else {
538 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
539 }
540
541 if ( $descText ) {
542 $this->mExtraDescription = $descText;
543 }
544 }
545
546 public function getUploadUrl() {
547 $this->loadFile();
548 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
549 return $uploadTitle->getFullUrl( array(
550 'wpDestFile' => $this->img->getName(),
551 'wpForReUpload' => 1
552 ) );
553 }
554
555 /**
556 * Print out the various links at the bottom of the image page, e.g. reupload,
557 * external editing (and instructions link) etc.
558 */
559 protected function uploadLinksBox() {
560 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
561
562 if ( !$wgEnableUploads ) { return; }
563
564 $this->loadFile();
565 if ( !$this->img->isLocal() )
566 return;
567
568 $sk = $wgUser->getSkin();
569
570 $wgOut->addHTML( "<br /><ul>\n" );
571
572 # "Upload a new version of this file" link
573 if ( UploadBase::userCanReUpload( $wgUser, $this->img->name ) ) {
574 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
575 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
576 }
577
578 # External editing link
579 if ( $wgUseExternalEditor ) {
580 $elink = $sk->link(
581 $this->mTitle,
582 wfMsgHtml( 'edit-externally' ),
583 array(),
584 array(
585 'action' => 'edit',
586 'externaledit' => 'true',
587 'mode' => 'file'
588 ),
589 array( 'known', 'noclasses' )
590 );
591 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
592 }
593
594 $wgOut->addHTML( "</ul>\n" );
595 }
596
597 protected function closeShowImage() { } # For overloading
598
599 /**
600 * If the page we've just displayed is in the "Image" namespace,
601 * we follow it with an upload history of the image and its usage.
602 */
603 protected function imageHistory() {
604 global $wgOut;
605
606 $this->loadFile();
607 $pager = new ImageHistoryPseudoPager( $this );
608 $wgOut->addHTML( $pager->getBody() );
609 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
610
611 $this->img->resetHistory(); // free db resources
612
613 # Exist check because we don't want to show this on pages where an image
614 # doesn't exist along with the noimage message, that would suck. -ævar
615 if ( $this->img->exists() ) {
616 $this->uploadLinksBox();
617 }
618 }
619
620 protected function imageLinks() {
621 global $wgUser, $wgOut, $wgLang;
622
623 $limit = 100;
624
625 $dbr = wfGetDB( DB_SLAVE );
626
627 $res = $dbr->select(
628 array( 'imagelinks', 'page' ),
629 array( 'page_namespace', 'page_title' ),
630 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
631 __METHOD__,
632 array( 'LIMIT' => $limit + 1 )
633 );
634 $count = $dbr->numRows( $res );
635 if ( $count == 0 ) {
636 $wgOut->wrapWikiMsg( Html::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
637 return;
638 }
639
640 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
641 if ( $count <= $limit - 1 ) {
642 $wgOut->addWikiMsg( 'linkstoimage', $count );
643 } else {
644 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
645 $wgOut->addWikiMsg( 'linkstoimage-more',
646 $wgLang->formatNum( $limit ),
647 $this->mTitle->getPrefixedDBkey()
648 );
649 }
650
651 $wgOut->addHTML( Html::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
652 $sk = $wgUser->getSkin();
653 $count = 0;
654 $elements = array();
655 foreach ( $res as $s ) {
656 $count++;
657 if ( $count <= $limit ) {
658 // We have not yet reached the extra one that tells us there is more to fetch
659 $elements[] = $s;
660 }
661 }
662
663 // Sort the list by namespace:title
664 usort ( $elements, array( $this, 'compare' ) );
665
666 // Create links for every element
667 foreach( $elements as $element ) {
668 $link = $sk->linkKnown( Title::makeTitle( $element->page_namespace, $element->page_title ) );
669 $wgOut->addHTML( Html::rawElement(
670 'li',
671 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace ),
672 $link
673 ) . "\n"
674 );
675
676 };
677 $wgOut->addHTML( Html::closeElement( 'ul' ) . "\n" );
678 $res->free();
679
680 // Add a links to [[Special:Whatlinkshere]]
681 if ( $count > $limit ) {
682 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
683 }
684 $wgOut->addHTML( Html::closeElement( 'div' ) . "\n" );
685 }
686
687 protected function imageRedirects() {
688 global $wgUser, $wgOut, $wgLang;
689
690 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
691 if ( count( $redirects ) == 0 ) return;
692
693 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
694 $wgOut->addWikiMsg( 'redirectstofile',
695 $wgLang->formatNum( count( $redirects ) )
696 );
697 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
698
699 $sk = $wgUser->getSkin();
700 foreach ( $redirects as $title ) {
701 $link = $sk->link(
702 $title,
703 null,
704 array(),
705 array( 'redirect' => 'no' ),
706 array( 'known', 'noclasses' )
707 );
708 $wgOut->addHTML( "<li>{$link}</li>\n" );
709 }
710 $wgOut->addHTML( "</ul></div>\n" );
711
712 }
713
714 protected function imageDupes() {
715 global $wgOut, $wgUser, $wgLang;
716
717 $this->loadFile();
718
719 $dupes = $this->getDuplicates();
720 if ( count( $dupes ) == 0 ) return;
721
722 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
723 $wgOut->addWikiMsg( 'duplicatesoffile',
724 $wgLang->formatNum( count( $dupes ) ), $this->mTitle->getDBkey()
725 );
726 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
727
728 $sk = $wgUser->getSkin();
729 foreach ( $dupes as $file ) {
730 $fromSrc = '';
731 if ( $file->isLocal() ) {
732 $link = $sk->link(
733 $file->getTitle(),
734 null,
735 array(),
736 array(),
737 array( 'known', 'noclasses' )
738 );
739 } else {
740 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
741 $file->getTitle()->getPrefixedText() );
742 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
743 }
744 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
745 }
746 $wgOut->addHTML( "</ul></div>\n" );
747 }
748
749 /**
750 * Delete the file, or an earlier version of it
751 */
752 public function delete() {
753 global $wgUploadMaintenance;
754 if ( $wgUploadMaintenance && $this->mTitle && $this->mTitle->getNamespace() == NS_FILE ) {
755 global $wgOut;
756 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
757 return;
758 }
759
760 $this->loadFile();
761 if ( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
762 // Standard article deletion
763 parent::delete();
764 return;
765 }
766 $deleter = new FileDeleteForm( $this->img );
767 $deleter->execute();
768 }
769
770 /**
771 * Revert the file to an earlier version
772 */
773 public function revert() {
774 $this->loadFile();
775 $reverter = new FileRevertForm( $this->img );
776 $reverter->execute();
777 }
778
779 /**
780 * Override handling of action=purge
781 */
782 public function doPurge() {
783 $this->loadFile();
784 if ( $this->img->exists() ) {
785 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
786 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
787 $update->doUpdate();
788 $this->img->upgradeRow();
789 $this->img->purgeCache();
790 } else {
791 wfDebug( "ImagePage::doPurge no image for " . $this->img->getName() . "; limiting purge to cache only\n" );
792 // even if the file supposedly doesn't exist, force any cached information
793 // to be updated (in case the cached information is wrong)
794 $this->img->purgeCache();
795 }
796 parent::doPurge();
797 }
798
799 /**
800 * Display an error with a wikitext description
801 */
802 function showError( $description ) {
803 global $wgOut;
804 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
805 $wgOut->setRobotPolicy( "noindex,nofollow" );
806 $wgOut->setArticleRelated( false );
807 $wgOut->enableClientCache( false );
808 $wgOut->addWikiText( $description );
809 }
810
811
812 /**
813 * Callback for usort() to do link sorts by (namespace, title)
814 * Function copied from Title::compare()
815 *
816 * @param $a object page to compare with
817 * @param $b object page to compare with
818 * @return Integer: result of string comparison, or namespace comparison
819 */
820 protected function compare( $a, $b ) {
821 if ( $a->page_namespace == $b->page_namespace ) {
822 return strcmp( $a->page_title, $b->page_title );
823 } else {
824 return $a->page_namespace - $b->page_namespace;
825 }
826 }
827 }
828
829 /**
830 * Builds the image revision log shown on image pages
831 *
832 * @ingroup Media
833 */
834 class ImageHistoryList {
835
836 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
837 protected $preventClickjacking = false;
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;
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 = $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 $this->preventClickjacking();
965 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
966 # Make a link to review the image
967 $url = $this->skin->link(
968 $revdel,
969 $wgLang->timeAndDate( $timestamp, true ),
970 array(),
971 array(
972 'target' => $this->title->getPrefixedText(),
973 'file' => $img,
974 'token' => $wgUser->editToken( $img )
975 ),
976 array( 'known', 'noclasses' )
977 );
978 $row .= '<span class="history-deleted">' . $url . '</span>';
979 } else {
980 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
981 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
982 }
983 $row .= "</td>";
984
985 // Thumbnail
986 if ( $this->showThumb ) {
987 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
988 }
989
990 // Image dimensions + size
991 $row .= '<td>';
992 $row .= htmlspecialchars( $file->getDimensionsString() );
993 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $file->getSize() ) . ')</span>';
994 $row .= '</td>';
995
996 // Uploading user
997 $row .= '<td>';
998 if ( $local ) {
999 // Hide deleted usernames
1000 if ( $file->isDeleted( File::DELETED_USER ) ) {
1001 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1002 } else {
1003 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
1004 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
1005 }
1006 } else {
1007 $row .= htmlspecialchars( $usertext );
1008 }
1009 $row .= '</td><td>';
1010
1011 // Don't show deleted descriptions
1012 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1013 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1014 } else {
1015 $row .= $this->skin->commentBlock( $description, $this->title );
1016 }
1017 $row .= '</td>';
1018
1019 $rowClass = null;
1020 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1021 $classAttr = $rowClass ? " class='$rowClass'" : "";
1022
1023 return "<tr{$classAttr}>{$row}</tr>\n";
1024 }
1025
1026 protected function getThumbForLine( $file ) {
1027 global $wgLang;
1028
1029 if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
1030 $params = array(
1031 'width' => '120',
1032 'height' => '120',
1033 );
1034 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
1035
1036 $thumbnail = $file->transform( $params );
1037 $options = array(
1038 'alt' => wfMsg( 'filehist-thumbtext',
1039 $wgLang->timeAndDate( $timestamp, true ),
1040 $wgLang->date( $timestamp, true ),
1041 $wgLang->time( $timestamp, true ) ),
1042 'file-link' => true,
1043 );
1044
1045 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1046
1047 return $thumbnail->toHtml( $options );
1048 } else {
1049 return wfMsgHtml( 'filehist-nothumb' );
1050 }
1051 }
1052
1053 protected function preventClickjacking( $enable = true ) {
1054 $this->preventClickjacking = $enable;
1055 }
1056
1057 public function getPreventClickjacking() {
1058 return $this->preventClickjacking;
1059 }
1060 }
1061
1062 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
1063 protected $preventClickjacking = false;
1064
1065 function __construct( $imagePage ) {
1066 parent::__construct();
1067 $this->mImagePage = $imagePage;
1068 $this->mTitle = clone ( $imagePage->getTitle() );
1069 $this->mTitle->setFragment( '#filehistory' );
1070 $this->mImg = null;
1071 $this->mHist = array();
1072 $this->mRange = array( 0, 0 ); // display range
1073 }
1074
1075 function getTitle() {
1076 return $this->mTitle;
1077 }
1078
1079 function getQueryInfo() {
1080 return false;
1081 }
1082
1083 function getIndexField() {
1084 return '';
1085 }
1086
1087 function formatRow( $row ) {
1088 return '';
1089 }
1090
1091 function getBody() {
1092 $s = '';
1093 $this->doQuery();
1094 if ( count( $this->mHist ) ) {
1095 $list = new ImageHistoryList( $this->mImagePage );
1096 # Generate prev/next links
1097 $navLink = $this->getNavigationBar();
1098 $s = $list->beginImageHistoryList( $navLink );
1099 // Skip rows there just for paging links
1100 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
1101 $file = $this->mHist[$i];
1102 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1103 }
1104 $s .= $list->endImageHistoryList( $navLink );
1105
1106 if ( $list->getPreventClickjacking() ) {
1107 $this->preventClickjacking();
1108 }
1109 }
1110 return $s;
1111 }
1112
1113 function doQuery() {
1114 if ( $this->mQueryDone ) return;
1115 $this->mImg = $this->mImagePage->getFile(); // ensure loading
1116 if ( !$this->mImg->exists() ) {
1117 return;
1118 }
1119 $queryLimit = $this->mLimit + 1; // limit plus extra row
1120 if ( $this->mIsBackwards ) {
1121 // Fetch the file history
1122 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
1123 // The current rev may not meet the offset/limit
1124 $numRows = count( $this->mHist );
1125 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
1126 $this->mHist = array_merge( array( $this->mImg ), $this->mHist );
1127 }
1128 } else {
1129 // The current rev may not meet the offset
1130 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
1131 $this->mHist[] = $this->mImg;
1132 }
1133 // Old image versions (fetch extra row for nav links)
1134 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
1135 // Fetch the file history
1136 $this->mHist = array_merge( $this->mHist,
1137 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
1138 }
1139 $numRows = count( $this->mHist ); // Total number of query results
1140 if ( $numRows ) {
1141 # Index value of top item in the list
1142 $firstIndex = $this->mIsBackwards ?
1143 $this->mHist[$numRows - 1]->getTimestamp() : $this->mHist[0]->getTimestamp();
1144 # Discard the extra result row if there is one
1145 if ( $numRows > $this->mLimit && $numRows > 1 ) {
1146 if ( $this->mIsBackwards ) {
1147 # Index value of item past the index
1148 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
1149 # Index value of bottom item in the list
1150 $lastIndex = $this->mHist[1]->getTimestamp();
1151 # Display range
1152 $this->mRange = array( 1, $numRows - 1 );
1153 } else {
1154 # Index value of item past the index
1155 $this->mPastTheEndIndex = $this->mHist[$numRows - 1]->getTimestamp();
1156 # Index value of bottom item in the list
1157 $lastIndex = $this->mHist[$numRows - 2]->getTimestamp();
1158 # Display range
1159 $this->mRange = array( 0, $numRows - 2 );
1160 }
1161 } else {
1162 # Setting indexes to an empty string means that they will be
1163 # omitted if they would otherwise appear in URLs. It just so
1164 # happens that this is the right thing to do in the standard
1165 # UI, in all the relevant cases.
1166 $this->mPastTheEndIndex = '';
1167 # Index value of bottom item in the list
1168 $lastIndex = $this->mIsBackwards ?
1169 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows - 1]->getTimestamp();
1170 # Display range
1171 $this->mRange = array( 0, $numRows - 1 );
1172 }
1173 } else {
1174 $firstIndex = '';
1175 $lastIndex = '';
1176 $this->mPastTheEndIndex = '';
1177 }
1178 if ( $this->mIsBackwards ) {
1179 $this->mIsFirst = ( $numRows < $queryLimit );
1180 $this->mIsLast = ( $this->mOffset == '' );
1181 $this->mLastShown = $firstIndex;
1182 $this->mFirstShown = $lastIndex;
1183 } else {
1184 $this->mIsFirst = ( $this->mOffset == '' );
1185 $this->mIsLast = ( $numRows < $queryLimit );
1186 $this->mLastShown = $lastIndex;
1187 $this->mFirstShown = $firstIndex;
1188 }
1189 $this->mQueryDone = true;
1190 }
1191
1192 protected function preventClickjacking( $enable = true ) {
1193 $this->preventClickjacking = $enable;
1194 }
1195
1196 public function getPreventClickjacking() {
1197 return $this->preventClickjacking;
1198 }
1199
1200 }