* Use Linker::link()
[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
442 if( !$this->displayImg->isSafeFile() ) {
443 $warning = wfMsgNoTrans( 'mediawarning' );
444 $wgOut->addWikiText( <<<EOT
445 <div class="fullMedia">
446 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
447 <span class="fileInfo">$longDesc</span>
448 </div>
449 <div class="mediaWarning">$warning</div>
450 EOT
451 );
452 } else {
453 $wgOut->addWikiText( <<<EOT
454 <div class="fullMedia">
455 [[Media:$filename|$filename]]$dirmark
456 <span class="fileInfo">$longDesc</span>
457 </div>
458 EOT
459 );
460 }
461 }
462
463 if( !$this->displayImg->isLocal() ) {
464 $this->printSharedImageText();
465 }
466 } else {
467 # Image does not exist
468
469 $title = SpecialPage::getTitleFor( 'Upload' );
470 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
471 'wpDestFile=' . urlencode( $this->displayImg->getName() ) );
472 $wgOut->setRobotPolicy( 'noindex,nofollow' );
473 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
474 }
475 }
476
477 /**
478 * Show a notice that the file is from a shared repository
479 */
480 protected function printSharedImageText() {
481 global $wgOut, $wgUser;
482
483 $this->loadFile();
484
485 $descUrl = $this->img->getDescriptionUrl();
486 $descText = $this->img->getDescriptionText();
487
488 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
489 $repo = $this->img->getRepo()->getDisplayName();
490
491 $msg = '';
492 if( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
493 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
494 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
495 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
496 } else {
497 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
498 }
499
500 if( $descText ) {
501 $this->mExtraDescription = $descText;
502 }
503 }
504
505 public function getUploadUrl() {
506 $this->loadFile();
507 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
508 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) . '&wpForReUpload=1' );
509 }
510
511 /**
512 * Print out the various links at the bottom of the image page, e.g. reupload,
513 * external editing (and instructions link) etc.
514 */
515 protected function uploadLinksBox() {
516 global $wgUser, $wgOut;
517
518 $this->loadFile();
519 if( !$this->img->isLocal() )
520 return;
521
522 $sk = $wgUser->getSkin();
523
524 $wgOut->addHTML( "<br /><ul>\n" );
525
526 # "Upload a new version of this file" link
527 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
528 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
529 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
530 }
531
532 # External editing link
533 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
534 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
535
536 $wgOut->addHTML( "</ul>\n" );
537 }
538
539 protected function closeShowImage() {} # For overloading
540
541 /**
542 * If the page we've just displayed is in the "Image" namespace,
543 * we follow it with an upload history of the image and its usage.
544 */
545 protected function imageHistory() {
546 global $wgOut, $wgUseExternalEditor;
547
548 $this->loadFile();
549 $pager = new ImageHistoryPseudoPager( $this );
550 $wgOut->addHTML( $pager->getBody() );
551
552 $this->img->resetHistory(); // free db resources
553
554 # Exist check because we don't want to show this on pages where an image
555 # doesn't exist along with the noimage message, that would suck. -ævar
556 if( $wgUseExternalEditor && $this->img->exists() ) {
557 $this->uploadLinksBox();
558 }
559 }
560
561 protected function imageLinks() {
562 global $wgUser, $wgOut, $wgLang;
563
564 $limit = 100;
565
566 $dbr = wfGetDB( DB_SLAVE );
567
568 $res = $dbr->select(
569 array( 'imagelinks', 'page' ),
570 array( 'page_namespace', 'page_title' ),
571 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
572 __METHOD__,
573 array( 'LIMIT' => $limit + 1)
574 );
575 $count = $dbr->numRows( $res );
576 if( $count == 0 ) {
577 $wgOut->addHTML( "<div id='mw-imagepage-nolinkstoimage'>\n" );
578 $wgOut->addWikiMsg( 'nolinkstoimage' );
579 $wgOut->addHTML( "</div>\n" );
580 return;
581 }
582
583 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
584 if( $count <= $limit - 1 ) {
585 $wgOut->addWikiMsg( 'linkstoimage', $count );
586 } else {
587 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
588 $wgOut->addWikiMsg( 'linkstoimage-more',
589 $wgLang->formatNum( $limit ),
590 $this->mTitle->getPrefixedDBkey()
591 );
592 }
593
594 $wgOut->addHTML( "<ul class='mw-imagepage-linkstoimage'>\n" );
595 $sk = $wgUser->getSkin();
596 $count = 0;
597 while ( $s = $res->fetchObject() ) {
598 $count++;
599 if( $count <= $limit ) {
600 // We have not yet reached the extra one that tells us there is more to fetch
601 $name = Title::makeTitle( $s->page_namespace, $s->page_title );
602 $link = $sk->makeKnownLinkObj( $name, "" );
603 $wgOut->addHTML( "<li>{$link}</li>\n" );
604 }
605 }
606 $wgOut->addHTML( "</ul>\n" );
607 $res->free();
608
609 // Add a links to [[Special:Whatlinkshere]]
610 if( $count > $limit )
611 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
612 $wgOut->addHTML( "</div>\n" );
613 }
614
615 protected function imageRedirects() {
616 global $wgUser, $wgOut, $wgLang;
617
618 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
619 if( count( $redirects ) == 0 ) return;
620
621 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
622 $wgOut->addWikiMsg( 'redirectstofile',
623 $wgLang->formatNum( count( $redirects ) )
624 );
625 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
626
627 $sk = $wgUser->getSkin();
628 foreach ( $redirects as $title ) {
629 $link = $sk->makeKnownLinkObj( $title, "", "redirect=no" );
630 $wgOut->addHTML( "<li>{$link}</li>\n" );
631 }
632 $wgOut->addHTML( "</ul></div>\n" );
633
634 }
635
636 protected function imageDupes() {
637 global $wgOut, $wgUser, $wgLang;
638
639 $this->loadFile();
640
641 $dupes = $this->getDuplicates();
642 if( count( $dupes ) == 0 ) return;
643
644 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
645 $wgOut->addWikiMsg( 'duplicatesoffile',
646 $wgLang->formatNum( count( $dupes ) ), $this->mTitle->getDBkey()
647 );
648 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
649
650 $sk = $wgUser->getSkin();
651 foreach ( $dupes as $file ) {
652 $fromSrc = '';
653 if( $file->isLocal() )
654 $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
655 else {
656 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
657 $file->getTitle()->getPrefixedText() );
658 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
659 }
660 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
661 }
662 $wgOut->addHTML( "</ul></div>\n" );
663 }
664
665 /**
666 * Delete the file, or an earlier version of it
667 */
668 public function delete() {
669 $this->loadFile();
670 if( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
671 // Standard article deletion
672 Article::delete();
673 return;
674 }
675 $deleter = new FileDeleteForm( $this->img );
676 $deleter->execute();
677 }
678
679 /**
680 * Revert the file to an earlier version
681 */
682 public function revert() {
683 $this->loadFile();
684 $reverter = new FileRevertForm( $this->img );
685 $reverter->execute();
686 }
687
688 /**
689 * Override handling of action=purge
690 */
691 public function doPurge() {
692 $this->loadFile();
693 if( $this->img->exists() ) {
694 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
695 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
696 $update->doUpdate();
697 $this->img->upgradeRow();
698 $this->img->purgeCache();
699 } else {
700 wfDebug( "ImagePage::doPurge no image\n" );
701 }
702 parent::doPurge();
703 }
704
705 /**
706 * Display an error with a wikitext description
707 */
708 function showError( $description ) {
709 global $wgOut;
710 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
711 $wgOut->setRobotPolicy( "noindex,nofollow" );
712 $wgOut->setArticleRelated( false );
713 $wgOut->enableClientCache( false );
714 $wgOut->addWikiText( $description );
715 }
716
717 }
718
719 /**
720 * Builds the image revision log shown on image pages
721 *
722 * @ingroup Media
723 */
724 class ImageHistoryList {
725
726 protected $imagePage, $img, $skin, $title, $repo;
727
728 public function __construct( $imagePage ) {
729 global $wgUser;
730 $this->skin = $wgUser->getSkin();
731 $this->current = $imagePage->getFile();
732 $this->img = $imagePage->getDisplayedFile();
733 $this->title = $imagePage->getTitle();
734 $this->imagePage = $imagePage;
735 }
736
737 public function getImagePage() {
738 return $this->imagePage;
739 }
740
741 public function getSkin() {
742 return $this->skin;
743 }
744
745 public function getFile() {
746 return $this->img;
747 }
748
749 public function beginImageHistoryList( $navLinks = '' ) {
750 global $wgOut, $wgUser;
751 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
752 . "<div id=\"mw-imagepage-section-filehistory\">\n"
753 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
754 . $navLinks . "\n"
755 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
756 . '<tr><td></td>'
757 . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
758 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
759 . '<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>'
760 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
761 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
762 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
763 . "</tr>\n";
764 }
765
766 public function endImageHistoryList( $navLinks = '' ) {
767 return "</table>\n$navLinks\n</div>\n";
768 }
769
770 public function imageHistoryLine( $iscur, $file ) {
771 global $wgUser, $wgLang, $wgContLang, $wgTitle;
772
773 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
774 $img = $iscur ? $file->getName() : $file->getArchiveName();
775 $user = $file->getUser('id');
776 $usertext = $file->getUser('text');
777 $size = $file->getSize();
778 $description = $file->getDescription();
779 $dims = $file->getDimensionsString();
780 $sha1 = $file->getSha1();
781
782 $local = $this->current->isLocal();
783 $row = $css = $selected = '';
784
785 // Deletion link
786 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
787 $row .= '<td>';
788 # Link to remove from history
789 if( $wgUser->isAllowed( 'delete' ) ) {
790 $q = array( 'action' => 'delete' );
791 if( !$iscur )
792 $q['oldimage'] = $img;
793 $row .= $this->skin->link(
794 $this->title,
795 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
796 array(), $q, array( 'known' )
797 );
798 }
799 # Link to hide content
800 if( $wgUser->isAllowed( 'deleterevision' ) ) {
801 if( $wgUser->isAllowed('delete') ) {
802 $row .= '<br/>';
803 }
804 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
805 // If file is top revision or locked from this user, don't link
806 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
807 $del = wfMsgHtml( 'rev-delundel' );
808 } else {
809 // If the file was hidden, link to sha-1
810 list( $ts, $name ) = explode( '!', $img, 2 );
811 $del = $this->skin->link( $revdel, wfMsgHtml( 'rev-delundel' ),
812 array(),
813 array( 'target' => $wgTitle->getPrefixedText(), 'oldimage' => $ts ),
814 array( 'known' )
815 );
816 // Bolden oversighted content
817 if( $file->isDeleted(File::DELETED_RESTRICTED) )
818 $del = "<strong>$del</strong>";
819 }
820 $row .= "<tt style='white-space: nowrap;'><small>$del</small></tt>";
821 }
822 $row .= '</td>';
823 }
824
825 // Reversion link/current indicator
826 $row .= '<td>';
827 if( $iscur ) {
828 $row .= wfMsgHtml( 'filehist-current' );
829 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
830 if( $file->isDeleted(File::DELETED_FILE) ) {
831 $row .= wfMsgHtml('filehist-revert');
832 } else {
833 $q = array();
834 $q[] = 'action=revert';
835 $q[] = 'oldimage=' . urlencode( $img );
836 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
837 $row .= $this->skin->makeKnownLinkObj( $this->title,
838 wfMsgHtml( 'filehist-revert' ),
839 implode( '&', $q ) );
840 }
841 }
842 $row .= '</td>';
843
844 // Date/time and image link
845 if( $file->getTimestamp() === $this->img->getTimestamp() ) {
846 $selected = "class='filehistory-selected'";
847 }
848 $row .= "<td $selected style='white-space: nowrap;'>";
849 if( !$file->userCan(File::DELETED_FILE) ) {
850 # Don't link to unviewable files
851 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
852 } else if( $file->isDeleted(File::DELETED_FILE) ) {
853 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
854 # Make a link to review the image
855 $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
856 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->current->getExtension() );
857 $row .= '<span class="history-deleted">'.$url.'</span>';
858 } else {
859 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
860 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
861 }
862
863 // Thumbnail
864 if( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
865 $params = array(
866 'width' => '120',
867 'height' => '120',
868 );
869 $thumbnail = $file->transform( $params );
870 $options = array(
871 'alt' => wfMsg( 'filehist-thumbtext', $wgLang->timeAndDate( $timestamp, true ) ),
872 'file-link' => true,
873 );
874 $row .= '</td><td>' . ( $thumbnail ? $thumbnail->toHtml( $options ) :
875 wfMsgHtml( 'filehist-nothumb' ) );
876 } else {
877 $row .= '</td><td>' . wfMsgHtml( 'filehist-nothumb' );
878 }
879 $row .= "</td><td>";
880
881 // Image dimensions
882 $row .= htmlspecialchars( $dims );
883
884 // File size
885 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $size ) . ')</span>';
886
887 // Uploading user
888 $row .= '</td><td>';
889 if( $local ) {
890 // Hide deleted usernames
891 if( $file->isDeleted(File::DELETED_USER) ) {
892 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
893 } else {
894 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
895 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
896 }
897 } else {
898 $row .= htmlspecialchars( $usertext );
899 }
900 $row .= '</td><td>';
901
902 // Don't show deleted descriptions
903 if( $file->isDeleted(File::DELETED_COMMENT) ) {
904 $row .= '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
905 } else {
906 $row .= $this->skin->commentBlock( $description, $this->title );
907 }
908 $row .= '</td>';
909
910 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
911 $classAttr = $rowClass ? " class='$rowClass'" : "";
912
913 return "<tr{$classAttr}>{$row}</tr>\n";
914 }
915 }
916
917 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
918 function __construct( $imagePage ) {
919 parent::__construct();
920 $this->mImagePage = $imagePage;
921 $this->mTitle = clone( $imagePage->getTitle() );
922 $this->mTitle->setFragment( '#filehistory' );
923 $this->mImg = NULL;
924 $this->mHist = array();
925 $this->mRange = array( 0, 0 ); // display range
926 }
927
928 function getTitle() {
929 return $this->mTitle;
930 }
931
932 function getQueryInfo() {
933 return false;
934 }
935
936 function getIndexField() {
937 return '';
938 }
939
940 function formatRow( $row ) {
941 return '';
942 }
943
944 function getBody() {
945 $s = '';
946 $this->doQuery();
947 if( count($this->mHist) ) {
948 $list = new ImageHistoryList( $this->mImagePage );
949 # Generate prev/next links
950 $navLink = $this->getNavigationBar();
951 $s = $list->beginImageHistoryList($navLink);
952 // Skip rows there just for paging links
953 for( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
954 $file = $this->mHist[$i];
955 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
956 }
957 $s .= $list->endImageHistoryList($navLink);
958 }
959 return $s;
960 }
961
962 function doQuery() {
963 if( $this->mQueryDone ) return;
964 $this->mImg = $this->mImagePage->getFile(); // ensure loading
965 if( !$this->mImg->exists() ) {
966 return;
967 }
968 $queryLimit = $this->mLimit + 1; // limit plus extra row
969 if( $this->mIsBackwards ) {
970 // Fetch the file history
971 $this->mHist = $this->mImg->getHistory($queryLimit,null,$this->mOffset,false);
972 // The current rev may not meet the offset/limit
973 $numRows = count($this->mHist);
974 if( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
975 $this->mHist = array_merge( array($this->mImg), $this->mHist );
976 }
977 } else {
978 // The current rev may not meet the offset
979 if( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
980 $this->mHist[] = $this->mImg;
981 }
982 // Old image versions (fetch extra row for nav links)
983 $oiLimit = count($this->mHist) ? $this->mLimit : $this->mLimit+1;
984 // Fetch the file history
985 $this->mHist = array_merge( $this->mHist,
986 $this->mImg->getHistory($oiLimit,$this->mOffset,null,false) );
987 }
988 $numRows = count($this->mHist); // Total number of query results
989 if( $numRows ) {
990 # Index value of top item in the list
991 $firstIndex = $this->mIsBackwards ?
992 $this->mHist[$numRows-1]->getTimestamp() : $this->mHist[0]->getTimestamp();
993 # Discard the extra result row if there is one
994 if( $numRows > $this->mLimit && $numRows > 1 ) {
995 if( $this->mIsBackwards ) {
996 # Index value of item past the index
997 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
998 # Index value of bottom item in the list
999 $lastIndex = $this->mHist[1]->getTimestamp();
1000 # Display range
1001 $this->mRange = array( 1, $numRows-1 );
1002 } else {
1003 # Index value of item past the index
1004 $this->mPastTheEndIndex = $this->mHist[$numRows-1]->getTimestamp();
1005 # Index value of bottom item in the list
1006 $lastIndex = $this->mHist[$numRows-2]->getTimestamp();
1007 # Display range
1008 $this->mRange = array( 0, $numRows-2 );
1009 }
1010 } else {
1011 # Setting indexes to an empty string means that they will be
1012 # omitted if they would otherwise appear in URLs. It just so
1013 # happens that this is the right thing to do in the standard
1014 # UI, in all the relevant cases.
1015 $this->mPastTheEndIndex = '';
1016 # Index value of bottom item in the list
1017 $lastIndex = $this->mIsBackwards ?
1018 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows-1]->getTimestamp();
1019 # Display range
1020 $this->mRange = array( 0, $numRows-1 );
1021 }
1022 } else {
1023 $firstIndex = '';
1024 $lastIndex = '';
1025 $this->mPastTheEndIndex = '';
1026 }
1027 if( $this->mIsBackwards ) {
1028 $this->mIsFirst = ( $numRows < $queryLimit );
1029 $this->mIsLast = ( $this->mOffset == '' );
1030 $this->mLastShown = $firstIndex;
1031 $this->mFirstShown = $lastIndex;
1032 } else {
1033 $this->mIsFirst = ( $this->mOffset == '' );
1034 $this->mIsLast = ( $numRows < $queryLimit );
1035 $this->mLastShown = $lastIndex;
1036 $this->mFirstShown = $firstIndex;
1037 }
1038 $this->mQueryDone = true;
1039 }
1040 }