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