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