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