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