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