* Use lazy initialization with loadFile()
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 */
4
5 /**
6 *
7 */
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
10
11 /**
12 * Special handling for image description pages
13 *
14 * @addtogroup Media
15 */
16 class ImagePage extends Article {
17
18 /* private */ var $img; // Image object this page is shown for
19 /* private */ var $current;
20 /* private */ var $repo;
21 /* private */ var $time;
22 /* private */ var $fileLoaded;
23 var $mExtraDescription = false;
24 var $dupes;
25
26 function __construct( $title, $time = null ) {
27 parent::__construct( $title );
28
29 global $wgRequest;
30 $time = is_null($time) ? $time : $wgRequest->getVal( 'filetimestamp' );
31 $this->time = $time;
32 $this->dupes = null;
33 $this->repo = null;
34 }
35
36 protected function loadFile() {
37 if( $this->fileLoaded ) {
38 return true;
39 }
40 $this->img = wfFindFile( $this->mTitle, $this->time );
41 if ( !$this->img ) {
42 $this->img = wfLocalFile( $this->mTitle );
43 $this->current = $this->img;
44 } else {
45 $this->current = $this->time ? wfLocalFile( $this->mTitle ) : $this->img;
46 }
47 $this->repo = $this->img->getRepo();
48 $this->fileLoaded = true;
49 }
50
51 /**
52 * Handler for action=render
53 * Include body text only; none of the image extras
54 */
55 function render() {
56 global $wgOut;
57 $wgOut->setArticleBodyOnly( true );
58 parent::view();
59 }
60
61 function view() {
62 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
63 $this->loadFile();
64
65 if ( $this->mTitle->getNamespace() == NS_IMAGE && $this->img->getRedirected() ) {
66 if ( $this->mTitle->getDBkey() == $this->img->getName() ) {
67 // mTitle is the same as the redirect target so ask Article
68 // to perform the redirect for us.
69 return Article::view();
70 } else {
71 // mTitle is not the same as the redirect target so it is
72 // probably the redirect page itself. Fake the redirect symbol
73 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
74 $this->viewRedirect( Title::makeTitle( NS_IMAGE, $this->img->getName() ),
75 /* $overwriteSubtitle */ true, /* $forceKnown */ true );
76 $this->viewUpdates();
77 return;
78 }
79 }
80
81 $diff = $wgRequest->getVal( 'diff' );
82 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
83
84 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
85 return Article::view();
86
87 if ($wgShowEXIF && $this->img->exists()) {
88 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
89 $formattedMetadata = $this->img->formatMetadata();
90 $showmeta = $formattedMetadata !== false;
91 } else {
92 $showmeta = false;
93 }
94
95 if ($this->img->exists())
96 $wgOut->addHTML($this->showTOC($showmeta));
97
98 $this->openShowImage();
99
100 # No need to display noarticletext, we use our own message, output in openShowImage()
101 if ( $this->getID() ) {
102 Article::view();
103 } else {
104 # Just need to set the right headers
105 $wgOut->setArticleFlag( true );
106 $wgOut->setRobotpolicy( 'noindex,nofollow' );
107 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
108 $this->viewUpdates();
109 }
110
111 # Show shared description, if needed
112 if ( $this->mExtraDescription ) {
113 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
114 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
115 $wgOut->addWikiText( $fol );
116 }
117 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
118 } else {
119 $this->checkSharedConflict();
120 }
121
122 $this->closeShowImage();
123 $this->imageHistory();
124 // TODO: Cleanup the following
125
126 $wgOut->addHTML( Xml::element( 'h2',
127 array( 'id' => 'filelinks' ),
128 wfMsg( 'imagelinks' ) ) . "\n" );
129 $this->imageDupes();
130 // TODO: We may want to find local images redirecting to a foreign
131 // file: "The following local files redirect to this file"
132 if ( $this->img->isLocal() ) $this->imageRedirects();
133 $this->imageLinks();
134
135 if ( $showmeta ) {
136 global $wgStylePath, $wgStyleVersion;
137 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
138 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
139 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
140 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
141 $wgOut->addScriptFile( 'metadata.js' );
142 $wgOut->addHTML(
143 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
144 }
145 }
146
147 public function getRedirectTarget() {
148 $this->loadFile();
149 if ( $this->img->isLocal() ) {
150 return parent::getRedirectTarget();
151 }
152 // Foreign image page
153 $from = $this->img->getRedirected();
154 $to = $this->img->getName();
155 if ( $from == $to ) {
156 return null;
157 }
158 return $this->mRedirectTarget = Title::makeTitle( NS_IMAGE, $to );
159 }
160 public function followRedirect() {
161 $this->loadFile();
162 if ( $this->img->isLocal() ) {
163 return parent::followRedirect();
164 }
165 $from = $this->img->getRedirected();
166 $to = $this->img->getName();
167 if ( $from == $to ) {
168 return false;
169 }
170 return Title::makeTitle( NS_IMAGE, $to );
171 }
172 public function isRedirect( $text = false ) {
173 $this->loadFile();
174 if ( $this->img->isLocal() )
175 return parent::isRedirect( $text );
176
177 return (bool)$this->img->getRedirected();
178 }
179
180 public function isLocal() {
181 $this->loadFile();
182 return $this->img->isLocal();
183 }
184
185 public function getFile() {
186 $this->loadFile();
187 return $this->img;
188 }
189
190 public function getDuplicates() {
191 $this->loadFile();
192 if ( !is_null($this->dupes) ) {
193 return $this->dupes;
194 }
195 if ( !( $hash = $this->img->getSha1() ) ) {
196 return $this->dupes = array();
197 }
198 $dupes = RepoGroup::singleton()->findBySha1( $hash );
199 // Remove duplicates with self and non matching file sizes
200 $self = $this->img->getRepoName().':'.$this->img->getName();
201 $size = $this->img->getSize();
202 foreach ( $dupes as $index => $file ) {
203 $key = $file->getRepoName().':'.$file->getName();
204 if ( $key == $self )
205 unset( $dupes[$index] );
206 if ( $file->getSize() != $size )
207 unset( $dupes[$index] );
208 }
209 return $this->dupes = $dupes;
210
211 }
212
213
214 /**
215 * Create the TOC
216 *
217 * @access private
218 *
219 * @param bool $metadata Whether or not to show the metadata link
220 * @return string
221 */
222 function showTOC( $metadata ) {
223 global $wgLang;
224 $r = '<ul id="filetoc">
225 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
226 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
227 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
228 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
229 </ul>';
230 return $r;
231 }
232
233 /**
234 * Make a table with metadata to be shown in the output page.
235 *
236 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
237 *
238 * @access private
239 *
240 * @param array $exif The array containing the EXIF data
241 * @return string
242 */
243 function makeMetadataTable( $metadata ) {
244 $r = wfMsg( 'metadata-help' ) . "\n\n";
245 $r .= "{| id=mw_metadata class=mw_metadata\n";
246 foreach ( $metadata as $type => $stuff ) {
247 foreach ( $stuff as $v ) {
248 $class = Sanitizer::escapeId( $v['id'] );
249 if( $type == 'collapsed' ) {
250 $class .= ' collapsable';
251 }
252 $r .= "|- class=\"$class\"\n";
253 $r .= "!| {$v['name']}\n";
254 $r .= "|| {$v['value']}\n";
255 }
256 }
257 $r .= '|}';
258 return $r;
259 }
260
261 /**
262 * Overloading Article's getContent method.
263 *
264 * Omit noarticletext if sharedupload; text will be fetched from the
265 * shared upload server if possible.
266 */
267 function getContent() {
268 $this->loadFile();
269 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
270 return '';
271 }
272 return Article::getContent();
273 }
274
275 function openShowImage() {
276 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
277
278 $this->loadFile();
279
280 $full_url = $this->img->getURL();
281 $linkAttribs = false;
282 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
283 if( !isset( $wgImageLimits[$sizeSel] ) ) {
284 $sizeSel = User::getDefaultOption( 'imagesize' );
285
286 // The user offset might still be incorrect, specially if
287 // $wgImageLimits got changed (see bug #8858).
288 if( !isset( $wgImageLimits[$sizeSel] ) ) {
289 // Default to the first offset in $wgImageLimits
290 $sizeSel = 0;
291 }
292 }
293 $max = $wgImageLimits[$sizeSel];
294 $maxWidth = $max[0];
295 $maxHeight = $max[1];
296 $sk = $wgUser->getSkin();
297 $dirmark = $wgContLang->getDirMark();
298
299 if ( $this->img->exists() ) {
300 # image
301 $page = $wgRequest->getIntOrNull( 'page' );
302 if ( is_null( $page ) ) {
303 $params = array();
304 $page = 1;
305 } else {
306 $params = array( 'page' => $page );
307 }
308 $width_orig = $this->img->getWidth();
309 $width = $width_orig;
310 $height_orig = $this->img->getHeight();
311 $height = $height_orig;
312 $mime = $this->img->getMimeType();
313 $showLink = false;
314 $linkAttribs = array( 'href' => $full_url );
315 $longDesc = $this->img->getLongDesc();
316
317 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
318
319 if ( $this->img->allowInlineDisplay() ) {
320 # image
321
322 # "Download high res version" link below the image
323 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
324 # We'll show a thumbnail of this image
325 if ( $width > $maxWidth || $height > $maxHeight ) {
326 # Calculate the thumbnail size.
327 # First case, the limiting factor is the width, not the height.
328 if ( $width / $height >= $maxWidth / $maxHeight ) {
329 $height = round( $height * $maxWidth / $width);
330 $width = $maxWidth;
331 # Note that $height <= $maxHeight now.
332 } else {
333 $newwidth = floor( $width * $maxHeight / $height);
334 $height = round( $height * $newwidth / $width );
335 $width = $newwidth;
336 # Note that $height <= $maxHeight now, but might not be identical
337 # because of rounding.
338 }
339 $msgbig = wfMsgHtml( 'show-big-image' );
340 $msgsmall = wfMsgExt( 'show-big-image-thumb',
341 array( 'parseinline' ), $wgLang->formatNum( $width ), $wgLang->formatNum( $height ) );
342 } else {
343 # Image is small enough to show full size on image page
344 $msgbig = htmlspecialchars( $this->img->getName() );
345 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
346 }
347
348 $params['width'] = $width;
349 $thumbnail = $this->img->transform( $params );
350
351 $anchorclose = "<br />";
352 if( $this->img->mustRender() ) {
353 $showLink = true;
354 } else {
355 $anchorclose .=
356 $msgsmall .
357 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
358 }
359
360 if ( $this->img->isMultipage() ) {
361 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
362 }
363
364 if ( $thumbnail ) {
365 $options = array(
366 'alt' => $this->img->getTitle()->getPrefixedText(),
367 'file-link' => true,
368 );
369 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
370 $thumbnail->toHtml( $options ) .
371 $anchorclose . '</div>' );
372 }
373
374 if ( $this->img->isMultipage() ) {
375 $count = $this->img->pageCount();
376
377 if ( $page > 1 ) {
378 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
379 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
380 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
381 array( 'page' => $page - 1 ) );
382 } else {
383 $thumb1 = '';
384 }
385
386 if ( $page < $count ) {
387 $label = wfMsg( 'imgmultipagenext' );
388 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
389 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
390 array( 'page' => $page + 1 ) );
391 } else {
392 $thumb2 = '';
393 }
394
395 global $wgScript;
396
397 $formParams = array(
398 'name' => 'pageselector',
399 'action' => $wgScript,
400 'onchange' => 'document.pageselector.submit();',
401 );
402
403 $option = array();
404 for ( $i=1; $i <= $count; $i++ ) {
405 $options[] = Xml::option( $wgLang->formatNum($i), $i, $i == $page );
406 }
407 $select = Xml::tags( 'select',
408 array( 'id' => 'pageselector', 'name' => 'page' ),
409 implode( "\n", $options ) );
410
411 $wgOut->addHTML(
412 '</td><td><div class="multipageimagenavbox">' .
413 Xml::openElement( 'form', $formParams ) .
414 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
415 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
416 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
417 Xml::closeElement( 'form' ) .
418 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
419 );
420 }
421 } else {
422 #if direct link is allowed but it's not a renderable image, show an icon.
423 if ($this->img->isSafeFile()) {
424 $icon= $this->img->iconThumb();
425
426 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
427 $icon->toHtml( array( 'desc-link' => true ) ) .
428 '</div>' );
429 }
430
431 $showLink = true;
432 }
433
434
435 if ($showLink) {
436 $filename = wfEscapeWikiText( $this->img->getName() );
437
438 if (!$this->img->isSafeFile()) {
439 $warning = wfMsgNoTrans( 'mediawarning' );
440 $wgOut->addWikiText( <<<EOT
441 <div class="fullMedia">
442 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
443 <span class="fileInfo"> $longDesc</span>
444 </div>
445
446 <div class="mediaWarning">$warning</div>
447 EOT
448 );
449 } else {
450 $wgOut->addWikiText( <<<EOT
451 <div class="fullMedia">
452 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
453 </div>
454 EOT
455 );
456 }
457 }
458
459 if(!$this->img->isLocal()) {
460 $this->printSharedImageText();
461 }
462 } else {
463 # Image does not exist
464
465 $title = SpecialPage::getTitleFor( 'Upload' );
466 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
467 'wpDestFile=' . urlencode( $this->img->getName() ) );
468 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
469 }
470 }
471
472 /**
473 * Show a notice that the file is from a shared repository
474 */
475 function printSharedImageText() {
476 global $wgOut, $wgUser;
477
478 $this->loadFile();
479
480 $descUrl = $this->img->getDescriptionUrl();
481 $descText = $this->img->getDescriptionText();
482 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
483 if ( $descUrl ) {
484 $sk = $wgUser->getSkin();
485 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
486 $msg = ( $descText ) ? 'shareduploadwiki-desc' : 'shareduploadwiki';
487 $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
488 if ( $msg != '-' ) {
489 # Show message only if not voided by local sysops
490 $s .= $msg;
491 }
492 }
493 $s .= "</div>";
494 $wgOut->addHTML( $s );
495
496 if ( $descText ) {
497 $this->mExtraDescription = $descText;
498 }
499 }
500
501 /*
502 * Check for files with the same name on the foreign repos.
503 */
504 function checkSharedConflict() {
505 global $wgOut, $wgUser;
506
507 $repoGroup = RepoGroup::singleton();
508 if( !$repoGroup->hasForeignRepos() ) {
509 return;
510 }
511
512 $this->loadFile();
513 if( !$this->img->isLocal() ) {
514 return;
515 }
516
517 $this->dupFile = null;
518 $repoGroup->forEachForeignRepo( array( $this, 'checkSharedConflictCallback' ) );
519
520 if( !$this->dupFile )
521 return;
522 $dupfile = $this->dupFile;
523 $same = (
524 ($this->img->getSha1() == $dupfile->getSha1()) &&
525 ($this->img->getSize() == $dupfile->getSize())
526 );
527
528 $sk = $wgUser->getSkin();
529 $descUrl = $dupfile->getDescriptionUrl();
530 if( $same ) {
531 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadduplicate-linktext' ) );
532 $wgOut->addHTML( '<div id="shared-image-dup">' . wfMsgWikiHtml( 'shareduploadduplicate', $link ) . '</div>' );
533 } else {
534 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadconflict-linktext' ) );
535 $wgOut->addHTML( '<div id="shared-image-conflict">' . wfMsgWikiHtml( 'shareduploadconflict', $link ) . '</div>' );
536 }
537 }
538
539 function checkSharedConflictCallback( $repo ) {
540 $this->loadFile();
541 $dupfile = $repo->newFile( $this->img->getTitle() );
542 if( $dupfile->exists() ) {
543 $this->dupFile = $dupfile;
544 }
545 return $dupfile->exists();
546 }
547
548 function getUploadUrl() {
549 $this->loadFile();
550 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
551 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
552 }
553
554 /**
555 * Print out the various links at the bottom of the image page, e.g. reupload,
556 * external editing (and instructions link) etc.
557 */
558 function uploadLinksBox() {
559 global $wgUser, $wgOut;
560
561 $this->loadFile();
562 if( !$this->img->isLocal() )
563 return;
564
565 $sk = $wgUser->getSkin();
566
567 $wgOut->addHtml( '<br /><ul>' );
568
569 # "Upload a new version of this file" link
570 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
571 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
572 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
573 }
574
575 # Link to Special:FileDuplicateSearch
576 $dupeLink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'FileDuplicateSearch', $this->mTitle->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
577 $wgOut->addHtml( "<li>{$dupeLink}</li>" );
578
579 # External editing link
580 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
581 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
582
583 $wgOut->addHtml( '</ul>' );
584 }
585
586 function closeShowImage()
587 {
588 # For overloading
589
590 }
591
592 /**
593 * If the page we've just displayed is in the "Image" namespace,
594 * we follow it with an upload history of the image and its usage.
595 */
596 function imageHistory()
597 {
598 global $wgUser, $wgOut, $wgUseExternalEditor;
599
600 $sk = $wgUser->getSkin();
601
602 $this->loadFile();
603 if ( $this->img->exists() ) {
604 $list = new ImageHistoryList( $sk, $this->current, $this->img );
605 $file = $this->current;
606 $dims = $file->getDimensionsString();
607 $s = $list->beginImageHistoryList();
608 $s .= $list->imageHistoryLine( true, $file );
609 // old image versions
610 $hist = $this->img->getHistory();
611 foreach( $hist as $file ) {
612 $dims = $file->getDimensionsString();
613 $s .= $list->imageHistoryLine( false, $file );
614 }
615 $s .= $list->endImageHistoryList();
616 } else { $s=''; }
617 $wgOut->addHTML( $s );
618
619 $this->img->resetHistory(); // free db resources
620
621 # Exist check because we don't want to show this on pages where an image
622 # doesn't exist along with the noimage message, that would suck. -ævar
623 if( $wgUseExternalEditor && $this->img->exists() ) {
624 $this->uploadLinksBox();
625 }
626
627 }
628
629 function imageLinks()
630 {
631 global $wgUser, $wgOut;
632
633 $limit = 100;
634
635 $dbr = wfGetDB( DB_SLAVE );
636
637 $res = $dbr->select(
638 array( 'imagelinks', 'page' ),
639 array( 'page_namespace', 'page_title' ),
640 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
641 __METHOD__,
642 array( 'LIMIT' => $limit + 1)
643 );
644
645 if ( 0 == $dbr->numRows( $res ) ) {
646 $wgOut->addWikiMsg( 'nolinkstoimage' );
647 return;
648 }
649 $wgOut->addWikiMsg( 'linkstoimage' );
650 $wgOut->addHTML( "<ul class='mw-imagepage-linktoimage'>\n" );
651
652 $sk = $wgUser->getSkin();
653 $count = 0;
654 while ( $s = $res->fetchObject() ) {
655 $count++;
656 if ( $count <= $limit ) {
657 // We have not yet reached the extra one that tells us there is more to fetch
658 $name = Title::makeTitle( $s->page_namespace, $s->page_title );
659 $link = $sk->makeKnownLinkObj( $name, "" );
660 $wgOut->addHTML( "<li>{$link}</li>\n" );
661 }
662 }
663 $wgOut->addHTML( "</ul>\n" );
664 $res->free();
665
666 // Add a links to [[Special:Whatlinkshere]]
667 if ( $count > $limit )
668 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
669 }
670
671 function imageRedirects()
672 {
673 global $wgUser, $wgOut;
674
675 $redirects = $this->getTitle()->getRedirectsHere( NS_IMAGE );
676 if ( count( $redirects ) == 0 ) return;
677
678
679 $wgOut->addWikiMsg( 'redirectstofile' );
680 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
681
682 $sk = $wgUser->getSkin();
683 foreach ( $redirects as $title ) {
684 $link = $sk->makeKnownLinkObj( $title, "" );
685 $wgOut->addHTML( "<li>{$link}</li>\n" );
686 }
687 $wgOut->addHTML( "</ul>\n" );
688
689 }
690
691 function imageDupes() {
692 global $wgOut, $wgUser;
693
694 $this->loadFile();
695
696 $dupes = $this->getDuplicates();
697 if ( count( $dupes ) == 0 ) return;
698
699 $wgOut->addWikiMsg( 'duplicatesoffile' );
700 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
701
702 $sk = $wgUser->getSkin();
703 foreach ( $dupes as $file ) {
704 if ( $file->isLocal() )
705 $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
706 else
707 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
708 $file->getTitle()->getPrefixedText() );
709 $wgOut->addHTML( "<li>{$link}</li>\n" );
710 }
711 $wgOut->addHTML( "</ul>\n" );
712 }
713
714 /**
715 * Delete the file, or an earlier version of it
716 */
717 public function delete() {
718 $this->loadFile();
719 if( !$this->img->exists() || !$this->img->isLocal() ) {
720 // Standard article deletion
721 Article::delete();
722 return;
723 }
724 $deleter = new FileDeleteForm( $this->img );
725 $deleter->execute();
726 }
727
728 /**
729 * Revert the file to an earlier version
730 */
731 public function revert() {
732 $this->loadFile();
733 $reverter = new FileRevertForm( $this->img );
734 $reverter->execute();
735 }
736
737 /**
738 * Override handling of action=purge
739 */
740 function doPurge() {
741 $this->loadFile();
742 if( $this->img->exists() ) {
743 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
744 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
745 $update->doUpdate();
746 $this->img->upgradeRow();
747 $this->img->purgeCache();
748 } else {
749 wfDebug( "ImagePage::doPurge no image\n" );
750 }
751 parent::doPurge();
752 }
753
754 /**
755 * Display an error with a wikitext description
756 */
757 function showError( $description ) {
758 global $wgOut;
759 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
760 $wgOut->setRobotpolicy( "noindex,nofollow" );
761 $wgOut->setArticleRelated( false );
762 $wgOut->enableClientCache( false );
763 $wgOut->addWikiText( $description );
764 }
765
766 }
767
768 /**
769 * Builds the image revision log shown on image pages
770 *
771 * @addtogroup Media
772 */
773 class ImageHistoryList {
774
775 protected $img, $skin, $title, $repo;
776
777 public function __construct( $skin, $curimg, $img ) {
778 $this->skin = $skin;
779 $this->current = $curimg;
780 $this->img = $img;
781 $this->title = $img->getTitle();
782 }
783
784 public function beginImageHistoryList() {
785 global $wgOut, $wgUser;
786 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
787 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
788 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
789 . '<tr><td></td>'
790 . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
791 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
792 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
793 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
794 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
795 . "</tr>\n";
796 }
797
798 public function endImageHistoryList() {
799 return "</table>\n";
800 }
801
802 public function imageHistoryLine( $iscur, $file ) {
803 global $wgUser, $wgLang, $wgContLang, $wgTitle;
804
805 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
806 $img = $iscur ? $file->getName() : $file->getArchiveName();
807 $user = $file->getUser('id');
808 $usertext = $file->getUser('text');
809 $size = $file->getSize();
810 $description = $file->getDescription();
811 $dims = $file->getDimensionsString();
812 $sha1 = $file->getSha1();
813
814 $local = $this->current->isLocal();
815 $row = $css = $selected = '';
816
817 // Deletion link
818 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
819 $row .= '<td>';
820 # Link to remove from history
821 if( $wgUser->isAllowed( 'delete' ) ) {
822 $q = array();
823 $q[] = 'action=delete';
824 if( !$iscur )
825 $q[] = 'oldimage=' . urlencode( $img );
826 $row .= $this->skin->makeKnownLinkObj(
827 $this->title,
828 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
829 implode( '&', $q )
830 );
831 }
832 # Link to hide content
833 if( $wgUser->isAllowed( 'deleterevision' ) ) {
834 if( $wgUser->isAllowed('delete') ) {
835 $row .= '<br/>';
836 }
837 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
838 // If file is top revision or locked from this user, don't link
839 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
840 $del = wfMsgHtml( 'rev-delundel' );
841 } else {
842 // If the file was hidden, link to sha-1
843 list($ts,$name) = explode('!',$img,2);
844 $del = $this->skin->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
845 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
846 '&oldimage=' . urlencode( $ts ) );
847 // Bolden oversighted content
848 if( $file->isDeleted(File::DELETED_RESTRICTED) )
849 $del = "<strong>$del</strong>";
850 }
851 $row .= "<tt style='white-space: nowrap;'><small>$del</small></tt>";
852 }
853 $row .= '</td>';
854 }
855
856 // Reversion link/current indicator
857 $row .= '<td>';
858 if( $iscur ) {
859 $row .= wfMsgHtml( 'filehist-current' );
860 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
861 if( $file->isDeleted(File::DELETED_FILE) ) {
862 $row .= wfMsgHtml('filehist-revert');
863 } else {
864 $q = array();
865 $q[] = 'action=revert';
866 $q[] = 'oldimage=' . urlencode( $img );
867 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
868 $row .= $this->skin->makeKnownLinkObj( $this->title,
869 wfMsgHtml( 'filehist-revert' ),
870 implode( '&', $q ) );
871 }
872 }
873 $row .= '</td>';
874
875 // Date/time and image link
876 if( $file->getTimestamp() === $this->img->getTimestamp() ) {
877 $selected = "class='filehistory-selected'";
878 }
879 $row .= "<td $selected style='white-space: nowrap;'>";
880 if( !$file->userCan(File::DELETED_FILE) ) {
881 # Don't link to unviewable files
882 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
883 } else if( $file->isDeleted(File::DELETED_FILE) ) {
884 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
885 # Make a link to review the image
886 $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
887 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->current->getExtension() );
888 $row .= '<span class="history-deleted">'.$url.'</span>';
889 } else {
890 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
891 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
892 }
893
894 $row .= "</td><td>";
895
896 // Image dimensions
897 $row .= htmlspecialchars( $dims );
898
899 // File size
900 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $size ) . ')</span>';
901
902 // Uploading user
903 $row .= '</td><td>';
904 if( $local ) {
905 // Hide deleted usernames
906 if( $file->isDeleted(File::DELETED_USER) ) {
907 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
908 } else {
909 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
910 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
911 }
912 } else {
913 $row .= htmlspecialchars( $usertext );
914 }
915 $row .= '</td><td>';
916
917 // Don't show deleted descriptions
918 if ( $file->isDeleted(File::DELETED_COMMENT) ) {
919 $row .= '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
920 } else {
921 $row .= $this->skin->commentBlock( $description, $this->title );
922 }
923 $row .= '</td>';
924
925 wfRunHooks( 'ImagePageFileHistoryLine', array( &$file, &$row, &$css ) );
926 $trCSS = $css ? " class='$css'" : "";
927
928 return "<tr{$trCSS}>{$row}</tr>\n";
929 }
930 }