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