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