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