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