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