399fc1cad0d3c4855eaa95aefee5703d6b6de56b
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 */
4
5 /**
6 *
7 */
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
10
11 /**
12 * Special handling for image description pages
13 *
14 * @addtogroup Media
15 */
16 class ImagePage extends Article {
17
18 /* private */ var $img; // Image object this page is shown for
19 /* private */ var $repo;
20 var $mExtraDescription = false;
21
22 function __construct( $title, $time = false ) {
23 parent::__construct( $title );
24 $this->img = wfFindFile( $this->mTitle, $time );
25 if ( !$this->img ) {
26 $this->img = wfLocalFile( $this->mTitle );
27 $this->current = $this->img;
28 } else {
29 $this->current = $time ? wfLocalFile( $this->mTitle ) : $this->img;
30 }
31 $this->repo = $this->img->repo;
32 }
33
34 /**
35 * Handler for action=render
36 * Include body text only; none of the image extras
37 */
38 function render() {
39 global $wgOut;
40 $wgOut->setArticleBodyOnly( true );
41 parent::view();
42 }
43
44 function view() {
45 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
46
47 if ( $this->mTitle->getNamespace() == NS_IMAGE && $this->img->getRedirected() ) {
48 if ( $this->mTitle->getDBkey() == $this->img->getName() ) {
49 // mTitle is the same as the redirect target so ask Article
50 // to perform the redirect for us.
51 return Article::view();
52 } else {
53 // mTitle is not the same as the redirect target so it is
54 // probably the redirect page itself. Fake the redirect symbol
55 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
56 $this->viewRedirect( Title::makeTitle( NS_IMAGE, $this->img->getName() ),
57 /* $overwriteSubtitle */ true, /* $forceKnown */ true );
58 $this->viewUpdates();
59 return;
60 }
61 }
62
63 $diff = $wgRequest->getVal( 'diff' );
64 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
65
66 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
67 return Article::view();
68
69 if ($wgShowEXIF && $this->img->exists()) {
70 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
71 $formattedMetadata = $this->img->formatMetadata();
72 $showmeta = $formattedMetadata !== false;
73 } else {
74 $showmeta = false;
75 }
76
77 if ($this->img->exists())
78 $wgOut->addHTML($this->showTOC($showmeta));
79
80 $this->openShowImage();
81
82 # No need to display noarticletext, we use our own message, output in openShowImage()
83 if ( $this->getID() ) {
84 Article::view();
85 } else {
86 # Just need to set the right headers
87 $wgOut->setArticleFlag( true );
88 $wgOut->setRobotpolicy( 'noindex,nofollow' );
89 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
90 $this->viewUpdates();
91 }
92
93 # Show shared description, if needed
94 if ( $this->mExtraDescription ) {
95 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
96 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
97 $wgOut->addWikiText( $fol );
98 }
99 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
100 } else {
101 $this->checkSharedConflict();
102 }
103
104 $this->closeShowImage();
105 $this->imageHistory();
106 $this->imageLinks();
107 if ( $this->img->isLocal() ) $this->imageRedirects();
108
109 if ( $showmeta ) {
110 global $wgStylePath, $wgStyleVersion;
111 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
112 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
113 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
114 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
115 $wgOut->addScriptFile( 'metadata.js' );
116 $wgOut->addHTML(
117 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
118 }
119 }
120
121 public function getRedirectTarget() {
122 if ( $this->img->isLocal() )
123 return parent::getRedirectTarget();
124
125 // Foreign image page
126 $from = $this->img->getRedirected();
127 $to = $this->img->getName();
128 if ($from == $to) return null;
129 return $this->mRedirectTarget = Title::makeTitle( NS_IMAGE, $to );
130 }
131 public function followRedirect() {
132 if ( $this->img->isLocal() )
133 return parent::followRedirect();
134
135 $from = $this->img->getRedirected();
136 $to = $this->img->getName();
137 if ($from == $to) return false;
138 return Title::makeTitle( NS_IMAGE, $to );
139 }
140 public function isRedirect( $text = false ) {
141 if ( $this->img->isLocal() )
142 return parent::isRedirect( $text );
143
144 return (bool)$this->img->getRedirected();
145 }
146
147 public function isLocal() {
148 return $this->img->isLocal();
149 }
150
151 public function getFile() {
152 return $this->img;
153 }
154
155 /**
156 * Create the TOC
157 *
158 * @access private
159 *
160 * @param bool $metadata Whether or not to show the metadata link
161 * @return string
162 */
163 function showTOC( $metadata ) {
164 global $wgLang;
165 $r = '<ul id="filetoc">
166 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
167 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
168 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
169 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
170 </ul>';
171 return $r;
172 }
173
174 /**
175 * Make a table with metadata to be shown in the output page.
176 *
177 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
178 *
179 * @access private
180 *
181 * @param array $exif The array containing the EXIF data
182 * @return string
183 */
184 function makeMetadataTable( $metadata ) {
185 $r = wfMsg( 'metadata-help' ) . "\n\n";
186 $r .= "{| id=mw_metadata class=mw_metadata\n";
187 foreach ( $metadata as $type => $stuff ) {
188 foreach ( $stuff as $v ) {
189 $class = Sanitizer::escapeId( $v['id'] );
190 if( $type == 'collapsed' ) {
191 $class .= ' collapsable';
192 }
193 $r .= "|- class=\"$class\"\n";
194 $r .= "!| {$v['name']}\n";
195 $r .= "|| {$v['value']}\n";
196 }
197 }
198 $r .= '|}';
199 return $r;
200 }
201
202 /**
203 * Overloading Article's getContent method.
204 *
205 * Omit noarticletext if sharedupload; text will be fetched from the
206 * shared upload server if possible.
207 */
208 function getContent() {
209 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
210 return '';
211 }
212 return Article::getContent();
213 }
214
215 function openShowImage() {
216 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
217
218 $full_url = $this->img->getURL();
219 $linkAttribs = false;
220 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
221 if( !isset( $wgImageLimits[$sizeSel] ) ) {
222 $sizeSel = User::getDefaultOption( 'imagesize' );
223
224 // The user offset might still be incorrect, specially if
225 // $wgImageLimits got changed (see bug #8858).
226 if( !isset( $wgImageLimits[$sizeSel] ) ) {
227 // Default to the first offset in $wgImageLimits
228 $sizeSel = 0;
229 }
230 }
231 $max = $wgImageLimits[$sizeSel];
232 $maxWidth = $max[0];
233 $maxHeight = $max[1];
234 $sk = $wgUser->getSkin();
235 $dirmark = $wgContLang->getDirMark();
236
237 if ( $this->img->exists() ) {
238 # image
239 $page = $wgRequest->getIntOrNull( 'page' );
240 if ( is_null( $page ) ) {
241 $params = array();
242 $page = 1;
243 } else {
244 $params = array( 'page' => $page );
245 }
246 $width_orig = $this->img->getWidth();
247 $width = $width_orig;
248 $height_orig = $this->img->getHeight();
249 $height = $height_orig;
250 $mime = $this->img->getMimeType();
251 $showLink = false;
252 $linkAttribs = array( 'href' => $full_url );
253 $longDesc = $this->img->getLongDesc();
254
255 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
256
257 if ( $this->img->allowInlineDisplay() ) {
258 # image
259
260 # "Download high res version" link below the image
261 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
262 # We'll show a thumbnail of this image
263 if ( $width > $maxWidth || $height > $maxHeight ) {
264 # Calculate the thumbnail size.
265 # First case, the limiting factor is the width, not the height.
266 if ( $width / $height >= $maxWidth / $maxHeight ) {
267 $height = round( $height * $maxWidth / $width);
268 $width = $maxWidth;
269 # Note that $height <= $maxHeight now.
270 } else {
271 $newwidth = floor( $width * $maxHeight / $height);
272 $height = round( $height * $newwidth / $width );
273 $width = $newwidth;
274 # Note that $height <= $maxHeight now, but might not be identical
275 # because of rounding.
276 }
277 $msgbig = wfMsgHtml( 'show-big-image' );
278 $msgsmall = wfMsgExt( 'show-big-image-thumb',
279 array( 'parseinline' ), $wgLang->formatNum( $width ), $wgLang->formatNum( $height ) );
280 } else {
281 # Image is small enough to show full size on image page
282 $msgbig = htmlspecialchars( $this->img->getName() );
283 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
284 }
285
286 $params['width'] = $width;
287 $thumbnail = $this->img->transform( $params );
288
289 $anchorclose = "<br />";
290 if( $this->img->mustRender() ) {
291 $showLink = true;
292 } else {
293 $anchorclose .=
294 $msgsmall .
295 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
296 }
297
298 if ( $this->img->isMultipage() ) {
299 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
300 }
301
302 if ( $thumbnail ) {
303 $options = array(
304 'alt' => $this->img->getTitle()->getPrefixedText(),
305 'file-link' => true,
306 );
307 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
308 $thumbnail->toHtml( $options ) .
309 $anchorclose . '</div>' );
310 }
311
312 if ( $this->img->isMultipage() ) {
313 $count = $this->img->pageCount();
314
315 if ( $page > 1 ) {
316 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
317 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
318 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
319 array( 'page' => $page - 1 ) );
320 } else {
321 $thumb1 = '';
322 }
323
324 if ( $page < $count ) {
325 $label = wfMsg( 'imgmultipagenext' );
326 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
327 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
328 array( 'page' => $page + 1 ) );
329 } else {
330 $thumb2 = '';
331 }
332
333 global $wgScript;
334
335 $formParams = array(
336 'name' => 'pageselector',
337 'action' => $wgScript,
338 'onchange' => 'document.pageselector.submit();',
339 );
340
341 $option = array();
342 for ( $i=1; $i <= $count; $i++ ) {
343 $options[] = Xml::option( $wgLang->formatNum($i), $i, $i == $page );
344 }
345 $select = Xml::tags( 'select',
346 array( 'id' => 'pageselector', 'name' => 'page' ),
347 implode( "\n", $options ) );
348
349 $wgOut->addHTML(
350 '</td><td><div class="multipageimagenavbox">' .
351 Xml::openElement( 'form', $formParams ) .
352 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
353 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
354 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
355 Xml::closeElement( 'form' ) .
356 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
357 );
358 }
359 } else {
360 #if direct link is allowed but it's not a renderable image, show an icon.
361 if ($this->img->isSafeFile()) {
362 $icon= $this->img->iconThumb();
363
364 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
365 $icon->toHtml( array( 'desc-link' => true ) ) .
366 '</div>' );
367 }
368
369 $showLink = true;
370 }
371
372
373 if ($showLink) {
374 $filename = wfEscapeWikiText( $this->img->getName() );
375
376 if (!$this->img->isSafeFile()) {
377 $warning = wfMsgNoTrans( 'mediawarning' );
378 $wgOut->addWikiText( <<<EOT
379 <div class="fullMedia">
380 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
381 <span class="fileInfo"> $longDesc</span>
382 </div>
383
384 <div class="mediaWarning">$warning</div>
385 EOT
386 );
387 } else {
388 $wgOut->addWikiText( <<<EOT
389 <div class="fullMedia">
390 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
391 </div>
392 EOT
393 );
394 }
395 }
396
397 if(!$this->img->isLocal()) {
398 $this->printSharedImageText();
399 }
400 } else {
401 # Image does not exist
402
403 $title = SpecialPage::getTitleFor( 'Upload' );
404 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
405 'wpDestFile=' . urlencode( $this->img->getName() ) );
406 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
407 }
408 }
409
410 /**
411 * Show a notice that the file is from a shared repository
412 */
413 function printSharedImageText() {
414 global $wgOut, $wgUser;
415
416 $descUrl = $this->img->getDescriptionUrl();
417 $descText = $this->img->getDescriptionText();
418 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
419 if ( $descUrl ) {
420 $sk = $wgUser->getSkin();
421 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
422 $msg = ( $descText ) ? 'shareduploadwiki-desc' : 'shareduploadwiki';
423 $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
424 if ( $msg != '-' ) {
425 # Show message only if not voided by local sysops
426 $s .= $msg;
427 }
428 }
429 $s .= "</div>";
430 $wgOut->addHTML( $s );
431
432 if ( $descText ) {
433 $this->mExtraDescription = $descText;
434 }
435 }
436
437 function checkSharedConflict() {
438 global $wgOut, $wgUser;
439 $repoGroup = RepoGroup::singleton();
440 if( !$repoGroup->hasForeignRepos() ) {
441 return;
442 }
443 if( !$this->img->isLocal() ) {
444 return;
445 }
446
447 $this->dupFile = null;
448 $repoGroup->forEachForeignRepo( array( $this, 'checkSharedConflictCallback' ) );
449
450 if( !$this->dupFile )
451 return;
452 $dupfile = $this->dupFile;
453 $same = (
454 ($this->img->getSha1() == $dupfile->getSha1()) &&
455 ($this->img->getSize() == $dupfile->getSize())
456 );
457
458 $sk = $wgUser->getSkin();
459 $descUrl = $dupfile->getDescriptionUrl();
460 if( $same ) {
461 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadduplicate-linktext' ) );
462 $wgOut->addHTML( '<div id="shared-image-dup">' . wfMsgWikiHtml( 'shareduploadduplicate', $link ) . '</div>' );
463 } else {
464 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadconflict-linktext' ) );
465 $wgOut->addHTML( '<div id="shared-image-conflict">' . wfMsgWikiHtml( 'shareduploadconflict', $link ) . '</div>' );
466 }
467 }
468
469 function checkSharedConflictCallback( $repo ) {
470 $dupfile = $repo->newFile( $this->img->getTitle() );
471 if( $dupfile->exists() )
472 $this->dupFile = $dupfile;
473 return $dupfile->exists();
474 }
475
476 function getUploadUrl() {
477 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
478 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
479 }
480
481 /**
482 * Print out the various links at the bottom of the image page, e.g. reupload,
483 * external editing (and instructions link) etc.
484 */
485 function uploadLinksBox() {
486 global $wgUser, $wgOut;
487
488 if( !$this->img->isLocal() )
489 return;
490
491 $sk = $wgUser->getSkin();
492
493 $wgOut->addHtml( '<br /><ul>' );
494
495 # "Upload a new version of this file" link
496 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
497 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
498 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
499 }
500
501 # Link to Special:FileDuplicateSearch
502 $dupeLink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'FileDuplicateSearch', $this->mTitle->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
503 $wgOut->addHtml( "<li>{$dupeLink}</li>" );
504
505 # External editing link
506 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
507 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
508
509 $wgOut->addHtml( '</ul>' );
510 }
511
512 function closeShowImage()
513 {
514 # For overloading
515
516 }
517
518 /**
519 * If the page we've just displayed is in the "Image" namespace,
520 * we follow it with an upload history of the image and its usage.
521 */
522 function imageHistory()
523 {
524 global $wgUser, $wgOut, $wgUseExternalEditor;
525
526 $sk = $wgUser->getSkin();
527
528 if ( $this->img->exists() ) {
529 $list = new ImageHistoryList( $sk, $this->current );
530 $file = $this->current;
531 $dims = $file->getDimensionsString();
532 $s = $list->beginImageHistoryList() .
533 $list->imageHistoryLine( true, $file );
534 // old image versions
535 $hist = $this->img->getHistory();
536 foreach( $hist as $file ) {
537 $dims = $file->getDimensionsString();
538 $s .= $list->imageHistoryLine( false, $file );
539 }
540 $s .= $list->endImageHistoryList();
541 } else { $s=''; }
542 $wgOut->addHTML( $s );
543
544 $this->img->resetHistory(); // free db resources
545
546 # Exist check because we don't want to show this on pages where an image
547 # doesn't exist along with the noimage message, that would suck. -ævar
548 if( $wgUseExternalEditor && $this->img->exists() ) {
549 $this->uploadLinksBox();
550 }
551
552 }
553
554 function imageLinks()
555 {
556 global $wgUser, $wgOut;
557
558 $limit = 100;
559
560 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
561
562 $dbr = wfGetDB( DB_SLAVE );
563
564 $res = $dbr->select(
565 array( 'imagelinks', 'page' ),
566 array( 'page_namespace', 'page_title' ),
567 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
568 __METHOD__,
569 array( 'LIMIT' => $limit + 1)
570 );
571
572 if ( 0 == $dbr->numRows( $res ) ) {
573 $wgOut->addWikiMsg( 'nolinkstoimage' );
574 return;
575 }
576 $wgOut->addWikiMsg( 'linkstoimage' );
577 $wgOut->addHTML( "<ul>\n" );
578
579 $sk = $wgUser->getSkin();
580 $count = 0;
581 while ( $s = $res->fetchObject() ) {
582 $count++;
583 if ( $count <= $limit ) {
584 // We have not yet reached the extra one that tells us there is more to fetch
585 $name = Title::makeTitle( $s->page_namespace, $s->page_title );
586 $link = $sk->makeKnownLinkObj( $name, "" );
587 $wgOut->addHTML( "<li>{$link}</li>\n" );
588 }
589 }
590 $wgOut->addHTML( "</ul>\n" );
591 $res->free();
592
593 // Add a links to [[Special:Whatlinkshere]]
594 if ( $count > $limit )
595 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
596 }
597
598 function imageRedirects()
599 {
600 global $wgUser, $wgOut;
601
602 $dbr = wfGetDB( DB_SLAVE );
603 $res = $dbr->select(
604 array( 'redirect', 'page' ),
605 array( 'page_title' ),
606 array(
607 'rd_namespace' => NS_IMAGE,
608 'rd_title' => $this->mTitle->getDBkey(),
609 'page_namespace' => NS_IMAGE,
610 'rd_from = page_id'
611 ),
612 __METHOD__
613 );
614
615
616 if ( 0 == $dbr->numRows( $res ) )
617 return;
618
619 $wgOut->addWikiMsg( 'redirectstofile' );
620 $wgOut->addHTML( "<ul>\n" );
621
622 $sk = $wgUser->getSkin();
623 while ( $row = $dbr->fetchObject( $res ) ) {
624 $name = Title::makeTitle( NS_IMAGE, $row->page_title );
625 $link = $sk->makeKnownLinkObj( $name, "" );
626 wfDebug("Image redirect: {$row->page_title}\n");
627 $wgOut->addHTML( "<li>{$link}</li>\n" );
628 }
629 $wgOut->addHTML( "</ul>\n" );
630
631 $res->free();
632 }
633
634 /**
635 * Delete the file, or an earlier version of it
636 */
637 public function delete() {
638 if( !$this->img->exists() || !$this->img->isLocal() ) {
639 // Standard article deletion
640 Article::delete();
641 return;
642 }
643 $deleter = new FileDeleteForm( $this->img );
644 $deleter->execute();
645 }
646
647 /**
648 * Revert the file to an earlier version
649 */
650 public function revert() {
651 $reverter = new FileRevertForm( $this->img );
652 $reverter->execute();
653 }
654
655 /**
656 * Override handling of action=purge
657 */
658 function doPurge() {
659 if( $this->img->exists() ) {
660 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
661 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
662 $update->doUpdate();
663 $this->img->upgradeRow();
664 $this->img->purgeCache();
665 } else {
666 wfDebug( "ImagePage::doPurge no image\n" );
667 }
668 parent::doPurge();
669 }
670
671 /**
672 * Display an error with a wikitext description
673 */
674 function showError( $description ) {
675 global $wgOut;
676 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
677 $wgOut->setRobotpolicy( "noindex,nofollow" );
678 $wgOut->setArticleRelated( false );
679 $wgOut->enableClientCache( false );
680 $wgOut->addWikiText( $description );
681 }
682
683 }
684
685 /**
686 * Builds the image revision log shown on image pages
687 *
688 * @addtogroup Media
689 */
690 class ImageHistoryList {
691
692 protected $img, $skin, $title, $repo;
693
694 public function __construct( $skin, $img ) {
695 $this->skin = $skin;
696 $this->img = $img;
697 $this->title = $img->getTitle();
698 }
699
700 public function beginImageHistoryList() {
701 global $wgOut, $wgUser;
702 $deleteColumn = $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deleterevision' );
703 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
704 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
705 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
706 . '<tr><td></td>'
707 . ( $this->img->isLocal() && $deleteColumn ? '<td></td>' : '' )
708 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
709 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
710 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
711 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
712 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
713 . "</tr>\n";
714 }
715
716 public function endImageHistoryList() {
717 return "</table>\n";
718 }
719
720 public function imageHistoryLine( $iscur, $file ) {
721 global $wgUser, $wgLang, $wgContLang, $wgTitle;
722
723 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
724 $img = $iscur ? $file->getName() : $file->getArchiveName();
725 $user = $file->getUser('id');
726 $usertext = $file->getUser('text');
727 $size = $file->getSize();
728 $description = $file->getDescription();
729 $dims = $file->getDimensionsString();
730 $sha1 = $file->getSha1();
731
732 $local = $this->img->isLocal();
733 $row = '';
734
735 // Deletion link
736 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
737 $row .= '<td>';
738 # Link to remove from history
739 if( $wgUser->isAllowed( 'delete' ) ) {
740 $q = array();
741 $q[] = 'action=delete';
742 if( !$iscur )
743 $q[] = 'oldimage=' . urlencode( $img );
744 $row .= $this->skin->makeKnownLinkObj(
745 $this->title,
746 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
747 implode( '&', $q )
748 );
749 $row .= '<br/>';
750 }
751 # Link to hide content
752 if( $wgUser->isAllowed( 'deleterevision' ) ) {
753 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
754 // If file is top revision or locked from this user, don't link
755 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
756 $del = wfMsgHtml( 'rev-delundel' );
757 } else {
758 // If the file was hidden, link to sha-1
759 list($ts,$name) = explode('!',$img,2);
760 $del = $this->skin->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
761 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
762 '&oldimage=' . urlencode( $ts ) );
763 // Bolden oversighted content
764 if( $file->isDeleted(File::DELETED_RESTRICTED) )
765 $del = "<strong>$del</strong>";
766 }
767 $row .= "<tt><small>$del</small></tt>";
768 }
769 $row .= '</td>';
770 }
771
772 // Reversion link/current indicator
773 $row .= '<td>';
774 if( $iscur ) {
775 $row .= wfMsgHtml( 'filehist-current' );
776 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
777 if( $file->isDeleted(File::DELETED_FILE) ) {
778 $row .= wfMsgHtml('filehist-revert');
779 } else {
780 $q = array();
781 $q[] = 'action=revert';
782 $q[] = 'oldimage=' . urlencode( $img );
783 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
784 $row .= $this->skin->makeKnownLinkObj( $this->title,
785 wfMsgHtml( 'filehist-revert' ),
786 implode( '&', $q ) );
787 }
788 }
789 $row .= '</td>';
790
791 // Date/time and image link
792 $row .= '<td>';
793 if( !$file->userCan(File::DELETED_FILE) ) {
794 # Don't link to unviewable files
795 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
796 } else if( $file->isDeleted(File::DELETED_FILE) ) {
797 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
798 # Make a link to review the image
799 $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
800 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->img->getExtension() );
801 $row .= '<span class="history-deleted">'.$url.'</span>';
802 } else {
803 $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
804 $row .= Xml::element( 'a',
805 array( 'href' => $url ),
806 $wgLang->timeAndDate( $timestamp, true ) );
807 }
808
809 $row .= '</td>';
810
811 // Uploading user
812 $row .= '<td>';
813 if( $local ) {
814 // Hide deleted usernames
815 if( $file->isDeleted(File::DELETED_USER) )
816 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
817 else
818 $row .= $this->skin->userLink( $user, $usertext ) .
819 $this->skin->userToolLinks( $user, $usertext );
820 } else {
821 $row .= htmlspecialchars( $usertext );
822 }
823 $row .= '</td>';
824
825 // Image dimensions
826 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
827
828 // File size
829 $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
830
831 // Don't show deleted descriptions
832 if ( $file->isDeleted(File::DELETED_COMMENT) )
833 $row .= '<td><span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span></td>';
834 else
835 $row .= '<td>' . $this->skin->commentBlock( $description, $this->title ) . '</td>';
836
837 return "<tr>{$row}</tr>\n";
838 }
839 }