Minor comment tweak
[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->exists() ) {
556 $this->dupFile = $dupfile;
557 }
558 return $dupfile->exists();
559 }
560
561 function getUploadUrl() {
562 $this->loadFile();
563 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
564 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
565 }
566
567 /**
568 * Print out the various links at the bottom of the image page, e.g. reupload,
569 * external editing (and instructions link) etc.
570 */
571 function uploadLinksBox() {
572 global $wgUser, $wgOut;
573
574 $this->loadFile();
575 if( !$this->img->isLocal() )
576 return;
577
578 $sk = $wgUser->getSkin();
579
580 $wgOut->addHtml( '<br /><ul>' );
581
582 # "Upload a new version of this file" link
583 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
584 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
585 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
586 }
587
588 # Link to Special:FileDuplicateSearch
589 $dupeLink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'FileDuplicateSearch', $this->mTitle->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
590 $wgOut->addHtml( "<li>{$dupeLink}</li>" );
591
592 # External editing link
593 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
594 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
595
596 $wgOut->addHtml( '</ul>' );
597 }
598
599 function closeShowImage()
600 {
601 # For overloading
602
603 }
604
605 /**
606 * If the page we've just displayed is in the "Image" namespace,
607 * we follow it with an upload history of the image and its usage.
608 */
609 function imageHistory()
610 {
611 global $wgUser, $wgOut, $wgUseExternalEditor;
612
613 $sk = $wgUser->getSkin();
614
615 $this->loadFile();
616 if ( $this->img->exists() ) {
617 $list = new ImageHistoryList( $sk, $this->img, $this->displayImg );
618 $file = $this->img;
619 $dims = $file->getDimensionsString();
620 $s = $list->beginImageHistoryList();
621 $s .= $list->imageHistoryLine( true, $file );
622 // old image versions
623 $hist = $this->img->getHistory();
624 foreach( $hist as $file ) {
625 $dims = $file->getDimensionsString();
626 $s .= $list->imageHistoryLine( false, $file );
627 }
628 $s .= $list->endImageHistoryList();
629 } else { $s=''; }
630 $wgOut->addHTML( $s );
631
632 $this->img->resetHistory(); // free db resources
633
634 # Exist check because we don't want to show this on pages where an image
635 # doesn't exist along with the noimage message, that would suck. -ævar
636 if( $wgUseExternalEditor && $this->img->exists() ) {
637 $this->uploadLinksBox();
638 }
639
640 }
641
642 function imageLinks()
643 {
644 global $wgUser, $wgOut;
645
646 $limit = 100;
647
648 $dbr = wfGetDB( DB_SLAVE );
649
650 $res = $dbr->select(
651 array( 'imagelinks', 'page' ),
652 array( 'page_namespace', 'page_title' ),
653 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
654 __METHOD__,
655 array( 'LIMIT' => $limit + 1)
656 );
657
658 if ( 0 == $dbr->numRows( $res ) ) {
659 $wgOut->addWikiMsg( 'nolinkstoimage' );
660 return;
661 }
662 $wgOut->addWikiMsg( 'linkstoimage' );
663 $wgOut->addHTML( "<ul class='mw-imagepage-linktoimage'>\n" );
664
665 $sk = $wgUser->getSkin();
666 $count = 0;
667 while ( $s = $res->fetchObject() ) {
668 $count++;
669 if ( $count <= $limit ) {
670 // We have not yet reached the extra one that tells us there is more to fetch
671 $name = Title::makeTitle( $s->page_namespace, $s->page_title );
672 $link = $sk->makeKnownLinkObj( $name, "" );
673 $wgOut->addHTML( "<li>{$link}</li>\n" );
674 }
675 }
676 $wgOut->addHTML( "</ul>\n" );
677 $res->free();
678
679 // Add a links to [[Special:Whatlinkshere]]
680 if ( $count > $limit )
681 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
682 }
683
684 function imageRedirects()
685 {
686 global $wgUser, $wgOut;
687
688 $redirects = $this->getTitle()->getRedirectsHere( NS_IMAGE );
689 if ( count( $redirects ) == 0 ) return;
690
691
692 $wgOut->addWikiMsg( 'redirectstofile' );
693 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
694
695 $sk = $wgUser->getSkin();
696 foreach ( $redirects as $title ) {
697 $link = $sk->makeKnownLinkObj( $title, "" );
698 $wgOut->addHTML( "<li>{$link}</li>\n" );
699 }
700 $wgOut->addHTML( "</ul>\n" );
701
702 }
703
704 function imageDupes() {
705 global $wgOut, $wgUser;
706
707 $this->loadFile();
708
709 $dupes = $this->getDuplicates();
710 if ( count( $dupes ) == 0 ) return;
711
712 $wgOut->addWikiMsg( 'duplicatesoffile' );
713 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
714
715 $sk = $wgUser->getSkin();
716 foreach ( $dupes as $file ) {
717 if ( $file->isLocal() )
718 $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
719 else
720 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
721 $file->getTitle()->getPrefixedText() );
722 $wgOut->addHTML( "<li>{$link}</li>\n" );
723 }
724 $wgOut->addHTML( "</ul>\n" );
725 }
726
727 /**
728 * Delete the file, or an earlier version of it
729 */
730 public function delete() {
731 $this->loadFile();
732 if( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
733 // Standard article deletion
734 Article::delete();
735 return;
736 }
737 $deleter = new FileDeleteForm( $this->img );
738 $deleter->execute();
739 }
740
741 /**
742 * Revert the file to an earlier version
743 */
744 public function revert() {
745 $this->loadFile();
746 $reverter = new FileRevertForm( $this->img );
747 $reverter->execute();
748 }
749
750 /**
751 * Override handling of action=purge
752 */
753 function doPurge() {
754 $this->loadFile();
755 if( $this->img->exists() ) {
756 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
757 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
758 $update->doUpdate();
759 $this->img->upgradeRow();
760 $this->img->purgeCache();
761 } else {
762 wfDebug( "ImagePage::doPurge no image\n" );
763 }
764 parent::doPurge();
765 }
766
767 /**
768 * Display an error with a wikitext description
769 */
770 function showError( $description ) {
771 global $wgOut;
772 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
773 $wgOut->setRobotpolicy( "noindex,nofollow" );
774 $wgOut->setArticleRelated( false );
775 $wgOut->enableClientCache( false );
776 $wgOut->addWikiText( $description );
777 }
778
779 }
780
781 /**
782 * Builds the image revision log shown on image pages
783 *
784 * @ingroup Media
785 */
786 class ImageHistoryList {
787
788 protected $img, $skin, $title, $repo;
789
790 public function __construct( $skin, $curimg, $img ) {
791 $this->skin = $skin;
792 $this->current = $curimg;
793 $this->img = $img;
794 $this->title = $img->getTitle();
795 }
796
797 public function beginImageHistoryList() {
798 global $wgOut, $wgUser;
799 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
800 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
801 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
802 . '<tr><td></td>'
803 . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
804 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
805 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
806 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
807 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
808 . "</tr>\n";
809 }
810
811 public function endImageHistoryList() {
812 return "</table>\n";
813 }
814
815 public function imageHistoryLine( $iscur, $file ) {
816 global $wgUser, $wgLang, $wgContLang, $wgTitle;
817
818 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
819 $img = $iscur ? $file->getName() : $file->getArchiveName();
820 $user = $file->getUser('id');
821 $usertext = $file->getUser('text');
822 $size = $file->getSize();
823 $description = $file->getDescription();
824 $dims = $file->getDimensionsString();
825 $sha1 = $file->getSha1();
826
827 $local = $this->current->isLocal();
828 $row = $css = $selected = '';
829
830 // Deletion link
831 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
832 $row .= '<td>';
833 # Link to remove from history
834 if( $wgUser->isAllowed( 'delete' ) ) {
835 $q = array();
836 $q[] = 'action=delete';
837 if( !$iscur )
838 $q[] = 'oldimage=' . urlencode( $img );
839 $row .= $this->skin->makeKnownLinkObj(
840 $this->title,
841 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
842 implode( '&', $q )
843 );
844 }
845 # Link to hide content
846 if( $wgUser->isAllowed( 'deleterevision' ) ) {
847 if( $wgUser->isAllowed('delete') ) {
848 $row .= '<br/>';
849 }
850 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
851 // If file is top revision or locked from this user, don't link
852 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
853 $del = wfMsgHtml( 'rev-delundel' );
854 } else {
855 // If the file was hidden, link to sha-1
856 list($ts,$name) = explode('!',$img,2);
857 $del = $this->skin->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
858 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
859 '&oldimage=' . urlencode( $ts ) );
860 // Bolden oversighted content
861 if( $file->isDeleted(File::DELETED_RESTRICTED) )
862 $del = "<strong>$del</strong>";
863 }
864 $row .= "<tt style='white-space: nowrap;'><small>$del</small></tt>";
865 }
866 $row .= '</td>';
867 }
868
869 // Reversion link/current indicator
870 $row .= '<td>';
871 if( $iscur ) {
872 $row .= wfMsgHtml( 'filehist-current' );
873 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
874 if( $file->isDeleted(File::DELETED_FILE) ) {
875 $row .= wfMsgHtml('filehist-revert');
876 } else {
877 $q = array();
878 $q[] = 'action=revert';
879 $q[] = 'oldimage=' . urlencode( $img );
880 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
881 $row .= $this->skin->makeKnownLinkObj( $this->title,
882 wfMsgHtml( 'filehist-revert' ),
883 implode( '&', $q ) );
884 }
885 }
886 $row .= '</td>';
887
888 // Date/time and image link
889 if( $file->getTimestamp() === $this->img->getTimestamp() ) {
890 $selected = "class='filehistory-selected'";
891 }
892 $row .= "<td $selected style='white-space: nowrap;'>";
893 if( !$file->userCan(File::DELETED_FILE) ) {
894 # Don't link to unviewable files
895 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
896 } else if( $file->isDeleted(File::DELETED_FILE) ) {
897 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
898 # Make a link to review the image
899 $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
900 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->current->getExtension() );
901 $row .= '<span class="history-deleted">'.$url.'</span>';
902 } else {
903 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
904 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
905 }
906
907 $row .= "</td><td>";
908
909 // Image dimensions
910 $row .= htmlspecialchars( $dims );
911
912 // File size
913 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $size ) . ')</span>';
914
915 // Uploading user
916 $row .= '</td><td>';
917 if( $local ) {
918 // Hide deleted usernames
919 if( $file->isDeleted(File::DELETED_USER) ) {
920 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
921 } else {
922 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
923 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
924 }
925 } else {
926 $row .= htmlspecialchars( $usertext );
927 }
928 $row .= '</td><td>';
929
930 // Don't show deleted descriptions
931 if ( $file->isDeleted(File::DELETED_COMMENT) ) {
932 $row .= '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
933 } else {
934 $row .= $this->skin->commentBlock( $description, $this->title );
935 }
936 $row .= '</td>';
937
938 wfRunHooks( 'ImagePageFileHistoryLine', array( &$file, &$row, &$css ) );
939 $trCSS = $css ? " class='$css'" : "";
940
941 return "<tr{$trCSS}>{$row}</tr>\n";
942 }
943 }