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