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