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