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