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