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