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