Removal of unused globals
[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 $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->addScriptFile( 'metadata.js' );
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 $linkAttribs = false;
304 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
305 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
306 $sizeSel = User::getDefaultOption( 'imagesize' );
307
308 // The user offset might still be incorrect, specially if
309 // $wgImageLimits got changed (see bug #8858).
310 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
311 // Default to the first offset in $wgImageLimits
312 $sizeSel = 0;
313 }
314 }
315 $max = $wgImageLimits[$sizeSel];
316 $maxWidth = $max[0];
317 $maxHeight = $max[1];
318 $sk = $wgUser->getSkin();
319 $dirmark = $wgContLang->getDirMark();
320
321 if ( $this->displayImg->exists() ) {
322 # image
323 $page = $wgRequest->getIntOrNull( 'page' );
324 if ( is_null( $page ) ) {
325 $params = array();
326 $page = 1;
327 } else {
328 $params = array( 'page' => $page );
329 }
330 $width_orig = $this->displayImg->getWidth( $page );
331 $width = $width_orig;
332 $height_orig = $this->displayImg->getHeight( $page );
333 $height = $height_orig;
334 $mime = $this->displayImg->getMimeType();
335 $showLink = false;
336 $linkAttribs = array( 'href' => $full_url );
337 $longDesc = $this->displayImg->getLongDesc();
338
339 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
340
341 if ( $this->displayImg->allowInlineDisplay() ) {
342 # image
343
344 # "Download high res version" link below the image
345 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
346 # We'll show a thumbnail of this image
347 if ( $width > $maxWidth || $height > $maxHeight ) {
348 # Calculate the thumbnail size.
349 # First case, the limiting factor is the width, not the height.
350 if ( $width / $height >= $maxWidth / $maxHeight ) {
351 $height = round( $height * $maxWidth / $width );
352 $width = $maxWidth;
353 # Note that $height <= $maxHeight now.
354 } else {
355 $newwidth = floor( $width * $maxHeight / $height );
356 $height = round( $height * $newwidth / $width );
357 $width = $newwidth;
358 # Note that $height <= $maxHeight now, but might not be identical
359 # because of rounding.
360 }
361 $msgbig = wfMsgHtml( 'show-big-image' );
362 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
363 $wgLang->formatNum( $width ),
364 $wgLang->formatNum( $height )
365 );
366 } else {
367 # Image is small enough to show full size on image page
368 $msgbig = htmlspecialchars( $this->displayImg->getName() );
369 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
370 }
371
372 $params['width'] = $width;
373 $thumbnail = $this->displayImg->transform( $params );
374
375 $anchorclose = "<br />";
376 if ( $this->displayImg->mustRender() ) {
377 $showLink = true;
378 } else {
379 $anchorclose .=
380 $msgsmall .
381 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
382 }
383
384 $isMulti = $this->displayImg->isMultipage() && $this->displayImg->pageCount() > 1;
385 if ( $isMulti ) {
386 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
387 }
388
389 if ( $thumbnail ) {
390 $options = array(
391 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
392 'file-link' => true,
393 );
394 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
395 $thumbnail->toHtml( $options ) .
396 $anchorclose . "</div>\n" );
397 }
398
399 if ( $isMulti ) {
400 $count = $this->displayImg->pageCount();
401
402 if ( $page > 1 ) {
403 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
404 $link = $sk->link(
405 $this->mTitle,
406 $label,
407 array(),
408 array( 'page' => $page - 1 ),
409 array( 'known', 'noclasses' )
410 );
411 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
412 array( 'page' => $page - 1 ) );
413 } else {
414 $thumb1 = '';
415 }
416
417 if ( $page < $count ) {
418 $label = wfMsg( 'imgmultipagenext' );
419 $link = $sk->link(
420 $this->mTitle,
421 $label,
422 array(),
423 array( 'page' => $page + 1 ),
424 array( 'known', 'noclasses' )
425 );
426 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
427 array( 'page' => $page + 1 ) );
428 } else {
429 $thumb2 = '';
430 }
431
432 global $wgScript;
433
434 $formParams = array(
435 'name' => 'pageselector',
436 'action' => $wgScript,
437 'onchange' => 'document.pageselector.submit();',
438 );
439
440 $option = array();
441 for ( $i = 1; $i <= $count; $i++ ) {
442 $options[] = Xml::option( $wgLang->formatNum( $i ), $i, $i == $page );
443 }
444 $select = Xml::tags( 'select',
445 array( 'id' => 'pageselector', 'name' => 'page' ),
446 implode( "\n", $options ) );
447
448 $wgOut->addHTML(
449 '</td><td><div class="multipageimagenavbox">' .
450 Xml::openElement( 'form', $formParams ) .
451 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
452 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
453 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
454 Xml::closeElement( 'form' ) .
455 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
456 );
457 }
458 } else {
459 # if direct link is allowed but it's not a renderable image, show an icon.
460 if ( $this->displayImg->isSafeFile() ) {
461 $icon = $this->displayImg->iconThumb();
462
463 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
464 $icon->toHtml( array( 'file-link' => true ) ) .
465 "</div>\n" );
466 }
467
468 $showLink = true;
469 }
470
471
472 if ( $showLink ) {
473 $filename = wfEscapeWikiText( $this->displayImg->getName() );
474 $medialink = "[[Media:$filename|$filename]]";
475
476 if ( !$this->displayImg->isSafeFile() ) {
477 $warning = wfMsgNoTrans( 'mediawarning' );
478 $wgOut->addWikiText( <<<EOT
479 <div class="fullMedia">
480 <span class="dangerousLink">{$medialink}</span>$dirmark
481 <span class="fileInfo">$longDesc</span>
482 </div>
483 <div class="mediaWarning">$warning</div>
484 EOT
485 );
486 } else {
487 $wgOut->addWikiText( <<<EOT
488 <div class="fullMedia">
489 {$medialink}{$dirmark}
490 <span class="fileInfo">$longDesc</span>
491 </div>
492 EOT
493 );
494 }
495 }
496
497 if ( !$this->displayImg->isLocal() ) {
498 $this->printSharedImageText();
499 }
500 } else {
501 # Image does not exist
502 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
503 // Only show an upload link if the user can upload
504 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
505 $nofile = array(
506 'filepage-nofile-link',
507 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img->getName() ) )
508 );
509 }
510 else
511 {
512 $nofile = 'filepage-nofile';
513 }
514 $wgOut->setRobotPolicy( 'noindex,nofollow' );
515 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
516 }
517 }
518
519 /**
520 * Show a notice that the file is from a shared repository
521 */
522 protected function printSharedImageText() {
523 global $wgOut;
524
525 $this->loadFile();
526
527 $descUrl = $this->img->getDescriptionUrl();
528 $descText = $this->img->getDescriptionText();
529
530 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
531 $repo = $this->img->getRepo()->getDisplayName();
532
533 $msg = '';
534 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
535 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
536 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
537 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
538 } else {
539 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
540 }
541
542 if ( $descText ) {
543 $this->mExtraDescription = $descText;
544 }
545 }
546
547 public function getUploadUrl() {
548 $this->loadFile();
549 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
550 return $uploadTitle->getFullUrl( array(
551 'wpDestFile' => $this->img->getName(),
552 'wpForReUpload' => 1
553 ) );
554 }
555
556 /**
557 * Print out the various links at the bottom of the image page, e.g. reupload,
558 * external editing (and instructions link) etc.
559 */
560 protected function uploadLinksBox() {
561 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
562
563 if ( !$wgEnableUploads ) { return; }
564
565 $this->loadFile();
566 if ( !$this->img->isLocal() )
567 return;
568
569 $sk = $wgUser->getSkin();
570
571 $wgOut->addHTML( "<br /><ul>\n" );
572
573 # "Upload a new version of this file" link
574 if ( UploadBase::userCanReUpload( $wgUser, $this->img->name ) ) {
575 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
576 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
577 }
578
579 # External editing link
580 if ( $wgUseExternalEditor ) {
581 $elink = $sk->link(
582 $this->mTitle,
583 wfMsgHtml( 'edit-externally' ),
584 array(),
585 array(
586 'action' => 'edit',
587 'externaledit' => 'true',
588 'mode' => 'file'
589 ),
590 array( 'known', 'noclasses' )
591 );
592 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
593 }
594
595 $wgOut->addHTML( "</ul>\n" );
596 }
597
598 protected function closeShowImage() { } # For overloading
599
600 /**
601 * If the page we've just displayed is in the "Image" namespace,
602 * we follow it with an upload history of the image and its usage.
603 */
604 protected function imageHistory() {
605 global $wgOut;
606
607 $this->loadFile();
608 $pager = new ImageHistoryPseudoPager( $this );
609 $wgOut->addHTML( $pager->getBody() );
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 while ( $s = $res->fetchObject() ) {
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 Article::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
838 public function __construct( $imagePage ) {
839 global $wgUser, $wgShowArchiveThumbnails;
840 $this->skin = $wgUser->getSkin();
841 $this->current = $imagePage->getFile();
842 $this->img = $imagePage->getDisplayedFile();
843 $this->title = $imagePage->getTitle();
844 $this->imagePage = $imagePage;
845 $this->showThumb = $wgShowArchiveThumbnails && $this->img->canRender();
846 }
847
848 public function getImagePage() {
849 return $this->imagePage;
850 }
851
852 public function getSkin() {
853 return $this->skin;
854 }
855
856 public function getFile() {
857 return $this->img;
858 }
859
860 public function beginImageHistoryList( $navLinks = '' ) {
861 global $wgOut, $wgUser;
862 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
863 . "<div id=\"mw-imagepage-section-filehistory\">\n"
864 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
865 . $navLinks . "\n"
866 . Xml::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
867 . '<tr><td></td>'
868 . ( $this->current->isLocal() && ( $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deletedhistory' ) ) ? '<td></td>' : '' )
869 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
870 . ( $this->showThumb ? '<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
871 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
872 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
873 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
874 . "</tr>\n";
875 }
876
877 public function endImageHistoryList( $navLinks = '' ) {
878 return "</table>\n$navLinks\n</div>\n";
879 }
880
881 public function imageHistoryLine( $iscur, $file ) {
882 global $wgUser, $wgLang;
883
884 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
885 $img = $iscur ? $file->getName() : $file->getArchiveName();
886 $user = $file->getUser( 'id' );
887 $usertext = $file->getUser( 'text' );
888 $description = $file->getDescription();
889
890 $local = $this->current->isLocal();
891 $row = $css = $selected = '';
892
893 // Deletion link
894 if ( $local && ( $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deletedhistory' ) ) ) {
895 $row .= '<td>';
896 # Link to remove from history
897 if ( $wgUser->isAllowed( 'delete' ) ) {
898 $q = array( 'action' => 'delete' );
899 if ( !$iscur )
900 $q['oldimage'] = $img;
901 $row .= $this->skin->link(
902 $this->title,
903 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
904 array(), $q, array( 'known' )
905 );
906 }
907 # Link to hide content. Don't show useless link to people who cannot hide revisions.
908 $canHide = $wgUser->isAllowed( 'deleterevision' );
909 if ( $canHide || ( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
910 if ( $wgUser->isAllowed( 'delete' ) ) {
911 $row .= '<br />';
912 }
913 // If file is top revision or locked from this user, don't link
914 if ( $iscur || !$file->userCan( File::DELETED_RESTRICTED ) ) {
915 $del = $this->skin->revDeleteLinkDisabled( $canHide );
916 } else {
917 list( $ts, $name ) = explode( '!', $img, 2 );
918 $query = array(
919 'type' => 'oldimage',
920 'target' => $this->title->getPrefixedText(),
921 'ids' => $ts,
922 );
923 $del = $this->skin->revDeleteLink( $query,
924 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
925 }
926 $row .= $del;
927 }
928 $row .= '</td>';
929 }
930
931 // Reversion link/current indicator
932 $row .= '<td>';
933 if ( $iscur ) {
934 $row .= wfMsgHtml( 'filehist-current' );
935 } elseif ( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
936 if ( $file->isDeleted( File::DELETED_FILE ) ) {
937 $row .= wfMsgHtml( 'filehist-revert' );
938 } else {
939 $row .= $this->skin->link(
940 $this->title,
941 wfMsgHtml( 'filehist-revert' ),
942 array(),
943 array(
944 'action' => 'revert',
945 'oldimage' => $img,
946 'wpEditToken' => $wgUser->editToken( $img )
947 ),
948 array( 'known', 'noclasses' )
949 );
950 }
951 }
952 $row .= '</td>';
953
954 // Date/time and image link
955 if ( $file->getTimestamp() === $this->img->getTimestamp() ) {
956 $selected = "class='filehistory-selected'";
957 }
958 $row .= "<td $selected style='white-space: nowrap;'>";
959 if ( !$file->userCan( File::DELETED_FILE ) ) {
960 # Don't link to unviewable files
961 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
962 } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
963 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
964 # Make a link to review the image
965 $url = $this->skin->link(
966 $revdel,
967 $wgLang->timeAndDate( $timestamp, true ),
968 array(),
969 array(
970 'target' => $this->title->getPrefixedText(),
971 'file' => $img,
972 'token' => $wgUser->editToken( $img )
973 ),
974 array( 'known', 'noclasses' )
975 );
976 $row .= '<span class="history-deleted">' . $url . '</span>';
977 } else {
978 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
979 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
980 }
981 $row .= "</td>";
982
983 // Thumbnail
984 if ( $this->showThumb ) {
985 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
986 }
987
988 // Image dimensions + size
989 $row .= '<td>';
990 $row .= htmlspecialchars( $file->getDimensionsString() );
991 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $file->getSize() ) . ')</span>';
992 $row .= '</td>';
993
994 // Uploading user
995 $row .= '<td>';
996 if ( $local ) {
997 // Hide deleted usernames
998 if ( $file->isDeleted( File::DELETED_USER ) ) {
999 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1000 } else {
1001 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
1002 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
1003 }
1004 } else {
1005 $row .= htmlspecialchars( $usertext );
1006 }
1007 $row .= '</td><td>';
1008
1009 // Don't show deleted descriptions
1010 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1011 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1012 } else {
1013 $row .= $this->skin->commentBlock( $description, $this->title );
1014 }
1015 $row .= '</td>';
1016
1017 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1018 $classAttr = $rowClass ? " class='$rowClass'" : "";
1019
1020 return "<tr{$classAttr}>{$row}</tr>\n";
1021 }
1022
1023 protected function getThumbForLine( $file ) {
1024 global $wgLang;
1025
1026 if ( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
1027 $params = array(
1028 'width' => '120',
1029 'height' => '120',
1030 );
1031 $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
1032
1033 $thumbnail = $file->transform( $params );
1034 $options = array(
1035 'alt' => wfMsg( 'filehist-thumbtext',
1036 $wgLang->timeAndDate( $timestamp, true ),
1037 $wgLang->date( $timestamp, true ),
1038 $wgLang->time( $timestamp, true ) ),
1039 'file-link' => true,
1040 );
1041
1042 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1043
1044 return $thumbnail->toHtml( $options );
1045 } else {
1046 return wfMsgHtml( 'filehist-nothumb' );
1047 }
1048 }
1049 }
1050
1051 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
1052 function __construct( $imagePage ) {
1053 parent::__construct();
1054 $this->mImagePage = $imagePage;
1055 $this->mTitle = clone ( $imagePage->getTitle() );
1056 $this->mTitle->setFragment( '#filehistory' );
1057 $this->mImg = null;
1058 $this->mHist = array();
1059 $this->mRange = array( 0, 0 ); // display range
1060 }
1061
1062 function getTitle() {
1063 return $this->mTitle;
1064 }
1065
1066 function getQueryInfo() {
1067 return false;
1068 }
1069
1070 function getIndexField() {
1071 return '';
1072 }
1073
1074 function formatRow( $row ) {
1075 return '';
1076 }
1077
1078 function getBody() {
1079 $s = '';
1080 $this->doQuery();
1081 if ( count( $this->mHist ) ) {
1082 $list = new ImageHistoryList( $this->mImagePage );
1083 # Generate prev/next links
1084 $navLink = $this->getNavigationBar();
1085 $s = $list->beginImageHistoryList( $navLink );
1086 // Skip rows there just for paging links
1087 for ( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
1088 $file = $this->mHist[$i];
1089 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1090 }
1091 $s .= $list->endImageHistoryList( $navLink );
1092 }
1093 return $s;
1094 }
1095
1096 function doQuery() {
1097 if ( $this->mQueryDone ) return;
1098 $this->mImg = $this->mImagePage->getFile(); // ensure loading
1099 if ( !$this->mImg->exists() ) {
1100 return;
1101 }
1102 $queryLimit = $this->mLimit + 1; // limit plus extra row
1103 if ( $this->mIsBackwards ) {
1104 // Fetch the file history
1105 $this->mHist = $this->mImg->getHistory( $queryLimit, null, $this->mOffset, false );
1106 // The current rev may not meet the offset/limit
1107 $numRows = count( $this->mHist );
1108 if ( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
1109 $this->mHist = array_merge( array( $this->mImg ), $this->mHist );
1110 }
1111 } else {
1112 // The current rev may not meet the offset
1113 if ( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
1114 $this->mHist[] = $this->mImg;
1115 }
1116 // Old image versions (fetch extra row for nav links)
1117 $oiLimit = count( $this->mHist ) ? $this->mLimit : $this->mLimit + 1;
1118 // Fetch the file history
1119 $this->mHist = array_merge( $this->mHist,
1120 $this->mImg->getHistory( $oiLimit, $this->mOffset, null, false ) );
1121 }
1122 $numRows = count( $this->mHist ); // Total number of query results
1123 if ( $numRows ) {
1124 # Index value of top item in the list
1125 $firstIndex = $this->mIsBackwards ?
1126 $this->mHist[$numRows - 1]->getTimestamp() : $this->mHist[0]->getTimestamp();
1127 # Discard the extra result row if there is one
1128 if ( $numRows > $this->mLimit && $numRows > 1 ) {
1129 if ( $this->mIsBackwards ) {
1130 # Index value of item past the index
1131 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
1132 # Index value of bottom item in the list
1133 $lastIndex = $this->mHist[1]->getTimestamp();
1134 # Display range
1135 $this->mRange = array( 1, $numRows - 1 );
1136 } else {
1137 # Index value of item past the index
1138 $this->mPastTheEndIndex = $this->mHist[$numRows - 1]->getTimestamp();
1139 # Index value of bottom item in the list
1140 $lastIndex = $this->mHist[$numRows - 2]->getTimestamp();
1141 # Display range
1142 $this->mRange = array( 0, $numRows - 2 );
1143 }
1144 } else {
1145 # Setting indexes to an empty string means that they will be
1146 # omitted if they would otherwise appear in URLs. It just so
1147 # happens that this is the right thing to do in the standard
1148 # UI, in all the relevant cases.
1149 $this->mPastTheEndIndex = '';
1150 # Index value of bottom item in the list
1151 $lastIndex = $this->mIsBackwards ?
1152 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows - 1]->getTimestamp();
1153 # Display range
1154 $this->mRange = array( 0, $numRows - 1 );
1155 }
1156 } else {
1157 $firstIndex = '';
1158 $lastIndex = '';
1159 $this->mPastTheEndIndex = '';
1160 }
1161 if ( $this->mIsBackwards ) {
1162 $this->mIsFirst = ( $numRows < $queryLimit );
1163 $this->mIsLast = ( $this->mOffset == '' );
1164 $this->mLastShown = $firstIndex;
1165 $this->mFirstShown = $lastIndex;
1166 } else {
1167 $this->mIsFirst = ( $this->mOffset == '' );
1168 $this->mIsLast = ( $numRows < $queryLimit );
1169 $this->mLastShown = $lastIndex;
1170 $this->mFirstShown = $firstIndex;
1171 }
1172 $this->mQueryDone = true;
1173 }
1174 }