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