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