(bug 13943) Fix image redirect behaviour on image pages:
[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->addHTML(
103 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
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 $select = '<form name="pageselector" action="' .
318 htmlspecialchars( $wgScript ) .
319 '" method="get" onchange="document.pageselector.submit();">' .
320 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
321 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
322 ' <select id="pageselector" name="page">';
323 for ( $i=1; $i <= $count; $i++ ) {
324 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
325 $i == $page );
326 }
327 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
328 '<input type="submit" value="' .
329 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
330
331 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
332 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
333 }
334 } else {
335 #if direct link is allowed but it's not a renderable image, show an icon.
336 if ($this->img->isSafeFile()) {
337 $icon= $this->img->iconThumb();
338
339 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
340 $icon->toHtml( array( 'desc-link' => true ) ) .
341 '</div>' );
342 }
343
344 $showLink = true;
345 }
346
347
348 if ($showLink) {
349 $filename = wfEscapeWikiText( $this->img->getName() );
350
351 if (!$this->img->isSafeFile()) {
352 $warning = wfMsgNoTrans( 'mediawarning' );
353 $wgOut->addWikiText( <<<EOT
354 <div class="fullMedia">
355 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
356 <span class="fileInfo"> $longDesc</span>
357 </div>
358
359 <div class="mediaWarning">$warning</div>
360 EOT
361 );
362 } else {
363 $wgOut->addWikiText( <<<EOT
364 <div class="fullMedia">
365 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
366 </div>
367 EOT
368 );
369 }
370 }
371
372 if(!$this->img->isLocal()) {
373 $this->printSharedImageText();
374 }
375 } else {
376 # Image does not exist
377
378 $title = SpecialPage::getTitleFor( 'Upload' );
379 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
380 'wpDestFile=' . urlencode( $this->img->getName() ) );
381 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
382 }
383 }
384
385 /**
386 * Show a notice that the file is from a shared repository
387 */
388 function printSharedImageText() {
389 global $wgOut, $wgUser;
390
391 $descUrl = $this->img->getDescriptionUrl();
392 $descText = $this->img->getDescriptionText();
393 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
394 if ( $descUrl ) {
395 $sk = $wgUser->getSkin();
396 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
397 $msg = ( $descText ) ? 'shareduploadwiki-desc' : 'shareduploadwiki';
398 $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
399 if ( $msg != '-' ) {
400 # Show message only if not voided by local sysops
401 $s .= $msg;
402 }
403 }
404 $s .= "</div>";
405 $wgOut->addHTML( $s );
406
407 if ( $descText ) {
408 $this->mExtraDescription = $descText;
409 }
410 }
411
412 function checkSharedConflict() {
413 global $wgOut, $wgUser;
414 $repoGroup = RepoGroup::singleton();
415 if( !$repoGroup->hasForeignRepos() ) {
416 return;
417 }
418 if( !$this->img->isLocal() ) {
419 return;
420 }
421
422 $this->dupFile = null;
423 $repoGroup->forEachForeignRepo( array( $this, 'checkSharedConflictCallback' ) );
424
425 if( !$this->dupFile )
426 return;
427 $dupfile = $this->dupFile;
428 $same = (
429 ($this->img->getSha1() == $dupfile->getSha1()) &&
430 ($this->img->getSize() == $dupfile->getSize())
431 );
432
433 $sk = $wgUser->getSkin();
434 $descUrl = $dupfile->getDescriptionUrl();
435 if( $same ) {
436 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadduplicate-linktext' ) );
437 $wgOut->addHTML( '<div id="shared-image-dup">' . wfMsgWikiHtml( 'shareduploadduplicate', $link ) . '</div>' );
438 } else {
439 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadconflict-linktext' ) );
440 $wgOut->addHTML( '<div id="shared-image-conflict">' . wfMsgWikiHtml( 'shareduploadconflict', $link ) . '</div>' );
441 }
442 }
443
444 function checkSharedConflictCallback( $repo ) {
445 $dupfile = $repo->newFile( $this->img->getTitle() );
446 if( $dupfile->exists() )
447 $this->dupFile = $dupfile;
448 return $dupfile->exists();
449 }
450
451 function getUploadUrl() {
452 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
453 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
454 }
455
456 /**
457 * Print out the various links at the bottom of the image page, e.g. reupload,
458 * external editing (and instructions link) etc.
459 */
460 function uploadLinksBox() {
461 global $wgUser, $wgOut;
462
463 if( !$this->img->isLocal() )
464 return;
465
466 $sk = $wgUser->getSkin();
467
468 $wgOut->addHtml( '<br /><ul>' );
469
470 # "Upload a new version of this file" link
471 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
472 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
473 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
474 }
475
476 # Link to Special:FileDuplicateSearch
477 $dupeLink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'FileDuplicateSearch', $this->mTitle->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
478 $wgOut->addHtml( "<li>{$dupeLink}</li>" );
479
480 # External editing link
481 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
482 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
483
484 $wgOut->addHtml( '</ul>' );
485 }
486
487 function closeShowImage()
488 {
489 # For overloading
490
491 }
492
493 /**
494 * If the page we've just displayed is in the "Image" namespace,
495 * we follow it with an upload history of the image and its usage.
496 */
497 function imageHistory()
498 {
499 global $wgUser, $wgOut, $wgUseExternalEditor;
500
501 $sk = $wgUser->getSkin();
502
503 if ( $this->img->exists() ) {
504 $list = new ImageHistoryList( $sk, $this->current );
505 $file = $this->current;
506 $dims = $file->getDimensionsString();
507 $s = $list->beginImageHistoryList() .
508 $list->imageHistoryLine( true, $file );
509 // old image versions
510 $hist = $this->img->getHistory();
511 foreach( $hist as $file ) {
512 $dims = $file->getDimensionsString();
513 $s .= $list->imageHistoryLine( false, $file );
514 }
515 $s .= $list->endImageHistoryList();
516 } else { $s=''; }
517 $wgOut->addHTML( $s );
518
519 $this->img->resetHistory(); // free db resources
520
521 # Exist check because we don't want to show this on pages where an image
522 # doesn't exist along with the noimage message, that would suck. -ævar
523 if( $wgUseExternalEditor && $this->img->exists() ) {
524 $this->uploadLinksBox();
525 }
526
527 }
528
529 function imageLinks()
530 {
531 global $wgUser, $wgOut;
532
533 $limit = 100;
534
535 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
536
537 $dbr = wfGetDB( DB_SLAVE );
538
539 $res = $dbr->select(
540 array( 'imagelinks', 'page' ),
541 array( 'page_namespace', 'page_title' ),
542 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
543 __METHOD__,
544 array( 'LIMIT' => $limit + 1)
545 );
546
547 if ( 0 == $dbr->numRows( $res ) ) {
548 $wgOut->addWikiMsg( 'nolinkstoimage' );
549 return;
550 }
551 $wgOut->addWikiMsg( 'linkstoimage' );
552 $wgOut->addHTML( "<ul>\n" );
553
554 $sk = $wgUser->getSkin();
555 $count = 0;
556 while ( $s = $res->fetchObject() ) {
557 $count++;
558 if ( $count <= $limit ) {
559 // We have not yet reached the extra one that tells us there is more to fetch
560 $name = Title::makeTitle( $s->page_namespace, $s->page_title );
561 $link = $sk->makeKnownLinkObj( $name, "" );
562 $wgOut->addHTML( "<li>{$link}</li>\n" );
563 }
564 }
565 $wgOut->addHTML( "</ul>\n" );
566 $res->free();
567
568 // Add a links to [[Special:Whatlinkshere]]
569 if ( $count > $limit )
570 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
571 }
572
573 function imageRedirects()
574 {
575 global $wgUser, $wgOut;
576
577 $dbr = wfGetDB( DB_SLAVE );
578 $res = $dbr->select(
579 array( 'redirect', 'page' ),
580 array( 'page_title' ),
581 array(
582 'rd_namespace' => NS_IMAGE,
583 'rd_title' => $this->mTitle->getDBkey(),
584 'page_namespace' => NS_IMAGE,
585 'rd_from = page_id'
586 ),
587 __METHOD__
588 );
589
590
591 if ( 0 == $dbr->numRows( $res ) )
592 return;
593
594 $wgOut->addWikiMsg( 'redirectstofile' );
595 $wgOut->addHTML( "<ul>\n" );
596
597 $sk = $wgUser->getSkin();
598 while ( $row = $dbr->fetchObject( $res ) ) {
599 $name = Title::makeTitle( NS_IMAGE, $row->page_title );
600 $link = $sk->makeKnownLinkObj( $name, "" );
601 wfDebug("Image redirect: {$row->page_title}\n");
602 $wgOut->addHTML( "<li>{$link}</li>\n" );
603 }
604 $wgOut->addHTML( "</ul>\n" );
605
606 $res->free();
607 }
608
609 /**
610 * Delete the file, or an earlier version of it
611 */
612 public function delete() {
613 if( !$this->img->exists() || !$this->img->isLocal() ) {
614 // Standard article deletion
615 Article::delete();
616 return;
617 }
618 $deleter = new FileDeleteForm( $this->img );
619 $deleter->execute();
620 }
621
622 /**
623 * Revert the file to an earlier version
624 */
625 public function revert() {
626 $reverter = new FileRevertForm( $this->img );
627 $reverter->execute();
628 }
629
630 /**
631 * Override handling of action=purge
632 */
633 function doPurge() {
634 if( $this->img->exists() ) {
635 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
636 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
637 $update->doUpdate();
638 $this->img->upgradeRow();
639 $this->img->purgeCache();
640 } else {
641 wfDebug( "ImagePage::doPurge no image\n" );
642 }
643 parent::doPurge();
644 }
645
646 /**
647 * Display an error with a wikitext description
648 */
649 function showError( $description ) {
650 global $wgOut;
651 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
652 $wgOut->setRobotpolicy( "noindex,nofollow" );
653 $wgOut->setArticleRelated( false );
654 $wgOut->enableClientCache( false );
655 $wgOut->addWikiText( $description );
656 }
657
658 }
659
660 /**
661 * Builds the image revision log shown on image pages
662 *
663 * @addtogroup Media
664 */
665 class ImageHistoryList {
666
667 protected $img, $skin, $title, $repo;
668
669 public function __construct( $skin, $img ) {
670 $this->skin = $skin;
671 $this->img = $img;
672 $this->title = $img->getTitle();
673 }
674
675 public function beginImageHistoryList() {
676 global $wgOut, $wgUser;
677 $deleteColumn = $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deleterevision' );
678 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
679 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
680 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
681 . '<tr><td></td>'
682 . ( $this->img->isLocal() && $deleteColumn ? '<td></td>' : '' )
683 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
684 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
685 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
686 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
687 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
688 . "</tr>\n";
689 }
690
691 public function endImageHistoryList() {
692 return "</table>\n";
693 }
694
695 public function imageHistoryLine( $iscur, $file ) {
696 global $wgUser, $wgLang, $wgContLang, $wgTitle;
697
698 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
699 $img = $iscur ? $file->getName() : $file->getArchiveName();
700 $user = $file->getUser('id');
701 $usertext = $file->getUser('text');
702 $size = $file->getSize();
703 $description = $file->getDescription();
704 $dims = $file->getDimensionsString();
705 $sha1 = $file->getSha1();
706
707 $local = $this->img->isLocal();
708 $row = '';
709
710 // Deletion link
711 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
712 $row .= '<td>';
713 # Link to remove from history
714 if( $wgUser->isAllowed( 'delete' ) ) {
715 $q = array();
716 $q[] = 'action=delete';
717 if( !$iscur )
718 $q[] = 'oldimage=' . urlencode( $img );
719 $row .= $this->skin->makeKnownLinkObj(
720 $this->title,
721 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
722 implode( '&', $q )
723 );
724 $row .= '<br/>';
725 }
726 # Link to hide content
727 if( $wgUser->isAllowed( 'deleterevision' ) ) {
728 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
729 // If file is top revision or locked from this user, don't link
730 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
731 $del = wfMsgHtml( 'rev-delundel' );
732 } else {
733 // If the file was hidden, link to sha-1
734 list($ts,$name) = explode('!',$img,2);
735 $del = $this->skin->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
736 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
737 '&oldimage=' . urlencode( $ts ) );
738 // Bolden oversighted content
739 if( $file->isDeleted(File::DELETED_RESTRICTED) )
740 $del = "<strong>$del</strong>";
741 }
742 $row .= "<tt><small>$del</small></tt>";
743 }
744 $row .= '</td>';
745 }
746
747 // Reversion link/current indicator
748 $row .= '<td>';
749 if( $iscur ) {
750 $row .= wfMsgHtml( 'filehist-current' );
751 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
752 if( $file->isDeleted(File::DELETED_FILE) ) {
753 $row .= wfMsgHtml('filehist-revert');
754 } else {
755 $q = array();
756 $q[] = 'action=revert';
757 $q[] = 'oldimage=' . urlencode( $img );
758 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
759 $row .= $this->skin->makeKnownLinkObj( $this->title,
760 wfMsgHtml( 'filehist-revert' ),
761 implode( '&', $q ) );
762 }
763 }
764 $row .= '</td>';
765
766 // Date/time and image link
767 $row .= '<td>';
768 if( !$file->userCan(File::DELETED_FILE) ) {
769 # Don't link to unviewable files
770 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
771 } else if( $file->isDeleted(File::DELETED_FILE) ) {
772 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
773 # Make a link to review the image
774 $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
775 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->img->getExtension() );
776 $row .= '<span class="history-deleted">'.$url.'</span>';
777 } else {
778 $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
779 $row .= Xml::element( 'a',
780 array( 'href' => $url ),
781 $wgLang->timeAndDate( $timestamp, true ) );
782 }
783
784 $row .= '</td>';
785
786 // Uploading user
787 $row .= '<td>';
788 if( $local ) {
789 // Hide deleted usernames
790 if( $file->isDeleted(File::DELETED_USER) )
791 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
792 else
793 $row .= $this->skin->userLink( $user, $usertext ) .
794 $this->skin->userToolLinks( $user, $usertext );
795 } else {
796 $row .= htmlspecialchars( $usertext );
797 }
798 $row .= '</td>';
799
800 // Image dimensions
801 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
802
803 // File size
804 $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
805
806 // Don't show deleted descriptions
807 if ( $file->isDeleted(File::DELETED_COMMENT) )
808 $row .= '<td><span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span></td>';
809 else
810 $row .= '<td>' . $this->skin->commentBlock( $description, $this->title ) . '</td>';
811
812 return "<tr>{$row}</tr>\n";
813 }
814 }