* (Bug 16121) Add a note that a page move was without creating a redirect in the...
[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 public function setFile( $file ) {
27 $this->displayImg = $file;
28 $this->img = $file;
29 $this->fileLoaded = true;
30 }
31
32 protected function loadFile() {
33 if( $this->fileLoaded ) {
34 return true;
35 }
36 $this->fileLoaded = true;
37
38 $this->displayImg = $this->img = false;
39 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img, &$this->displayImg ) );
40 if( !$this->img ) {
41 $this->img = wfFindFile( $this->mTitle );
42 if( !$this->img ) {
43 $this->img = wfLocalFile( $this->mTitle );
44 }
45 }
46 if( !$this->displayImg ) {
47 $this->displayImg = $this->img;
48 }
49 $this->repo = $this->img->getRepo();
50 }
51
52 /**
53 * Handler for action=render
54 * Include body text only; none of the image extras
55 */
56 public function render() {
57 global $wgOut;
58 $wgOut->setArticleBodyOnly( true );
59 parent::view();
60 }
61
62 public function view() {
63 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
64 $this->loadFile();
65
66 if( $this->mTitle->getNamespace() == NS_FILE && $this->img->getRedirected() ) {
67 if( $this->mTitle->getDBkey() == $this->img->getName() ) {
68 // mTitle is the same as the redirect target so ask Article
69 // to perform the redirect for us.
70 return Article::view();
71 } else {
72 // mTitle is not the same as the redirect target so it is
73 // probably the redirect page itself. Fake the redirect symbol
74 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
75 $wgOut->addHTML( $this->viewRedirect( Title::makeTitle( NS_FILE, $this->img->getName() ),
76 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
77 $this->viewUpdates();
78 return;
79 }
80 }
81
82 $diff = $wgRequest->getVal( 'diff' );
83 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
84
85 if( $this->mTitle->getNamespace() != NS_FILE || ( isset( $diff ) && $diffOnly ) )
86 return Article::view();
87
88 if( $wgShowEXIF && $this->displayImg->exists() ) {
89 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
90 $formattedMetadata = $this->displayImg->formatMetadata();
91 $showmeta = $formattedMetadata !== false;
92 } else {
93 $showmeta = false;
94 }
95
96 if( $this->displayImg->exists() )
97 $wgOut->addHTML( $this->showTOC($showmeta) );
98
99 $this->openShowImage();
100
101 # No need to display noarticletext, we use our own message, output in openShowImage()
102 if( $this->getID() ) {
103 Article::view();
104 } else {
105 # Just need to set the right headers
106 $wgOut->setArticleFlag( true );
107 $wgOut->setRobotPolicy( 'noindex,nofollow' );
108 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
109 $this->viewUpdates();
110
111 # And also show "redirected from" message
112 $sk = $wgUser->getSkin();
113 $redir = $sk->makeKnownLinkObj( $this->mRedirectedFrom, '', 'redirect=no' );
114 $wgOut->setSubtitle( wfMsgExt( 'redirectedfrom', array( 'parseinline', 'replaceafter' ), $redir ) );
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( Xml::escapeJsString( wfMsg( 'metadata-expand' ) ) );
146 $collapse = htmlspecialchars( Xml::escapeJsString( 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_FILE, $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_FILE, $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 * @param bool $metadata Whether or not to show the metadata link
231 * @return string
232 */
233 protected function showTOC( $metadata ) {
234 global $wgLang;
235 $r = '<ul id="filetoc">
236 <li><a href="#file">' . $wgLang->getNsText( NS_FILE ) . '</a></li>
237 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
238 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
239 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
240 </ul>';
241 return $r;
242 }
243
244 /**
245 * Make a table with metadata to be shown in the output page.
246 *
247 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
248 *
249 * @param array $exif The array containing the EXIF data
250 * @return string
251 */
252 protected function makeMetadataTable( $metadata ) {
253 $r = wfMsg( 'metadata-help' ) . "\n\n";
254 $r .= "{| id=mw_metadata class=mw_metadata\n";
255 foreach ( $metadata as $type => $stuff ) {
256 foreach ( $stuff as $v ) {
257 # FIXME, why is this using escapeId for a class?!
258 $class = Sanitizer::escapeId( $v['id'] );
259 if( $type == 'collapsed' ) {
260 $class .= ' collapsable';
261 }
262 $r .= "|- class=\"$class\"\n";
263 $r .= "!| {$v['name']}\n";
264 $r .= "|| {$v['value']}\n";
265 }
266 }
267 $r .= '|}';
268 return $r;
269 }
270
271 /**
272 * Overloading Article's getContent method.
273 *
274 * Omit noarticletext if sharedupload; text will be fetched from the
275 * shared upload server if possible.
276 */
277 public function getContent() {
278 $this->loadFile();
279 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
280 return '';
281 }
282 return Article::getContent();
283 }
284
285 protected function openShowImage() {
286 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
287
288 $this->loadFile();
289
290 $full_url = $this->displayImg->getURL();
291 $linkAttribs = false;
292 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
293 if( !isset( $wgImageLimits[$sizeSel] ) ) {
294 $sizeSel = User::getDefaultOption( 'imagesize' );
295
296 // The user offset might still be incorrect, specially if
297 // $wgImageLimits got changed (see bug #8858).
298 if( !isset( $wgImageLimits[$sizeSel] ) ) {
299 // Default to the first offset in $wgImageLimits
300 $sizeSel = 0;
301 }
302 }
303 $max = $wgImageLimits[$sizeSel];
304 $maxWidth = $max[0];
305 $maxHeight = $max[1];
306 $sk = $wgUser->getSkin();
307 $dirmark = $wgContLang->getDirMark();
308
309 if( $this->displayImg->exists() ) {
310 # image
311 $page = $wgRequest->getIntOrNull( 'page' );
312 if( is_null( $page ) ) {
313 $params = array();
314 $page = 1;
315 } else {
316 $params = array( 'page' => $page );
317 }
318 $width_orig = $this->displayImg->getWidth();
319 $width = $width_orig;
320 $height_orig = $this->displayImg->getHeight();
321 $height = $height_orig;
322 $mime = $this->displayImg->getMimeType();
323 $showLink = false;
324 $linkAttribs = array( 'href' => $full_url );
325 $longDesc = $this->displayImg->getLongDesc();
326
327 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
328
329 if( $this->displayImg->allowInlineDisplay() ) {
330 # image
331
332 # "Download high res version" link below the image
333 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
334 # We'll show a thumbnail of this image
335 if( $width > $maxWidth || $height > $maxHeight ) {
336 # Calculate the thumbnail size.
337 # First case, the limiting factor is the width, not the height.
338 if( $width / $height >= $maxWidth / $maxHeight ) {
339 $height = round( $height * $maxWidth / $width);
340 $width = $maxWidth;
341 # Note that $height <= $maxHeight now.
342 } else {
343 $newwidth = floor( $width * $maxHeight / $height);
344 $height = round( $height * $newwidth / $width );
345 $width = $newwidth;
346 # Note that $height <= $maxHeight now, but might not be identical
347 # because of rounding.
348 }
349 $msgbig = wfMsgHtml( 'show-big-image' );
350 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
351 $wgLang->formatNum( $width ),
352 $wgLang->formatNum( $height )
353 );
354 } else {
355 # Image is small enough to show full size on image page
356 $msgbig = htmlspecialchars( $this->displayImg->getName() );
357 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
358 }
359
360 $params['width'] = $width;
361 $thumbnail = $this->displayImg->transform( $params );
362
363 $anchorclose = "<br />";
364 if( $this->displayImg->mustRender() ) {
365 $showLink = true;
366 } else {
367 $anchorclose .=
368 $msgsmall .
369 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
370 }
371
372 if( $this->displayImg->isMultipage() ) {
373 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
374 }
375
376 if( $thumbnail ) {
377 $options = array(
378 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
379 'file-link' => true,
380 );
381 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
382 $thumbnail->toHtml( $options ) .
383 $anchorclose . '</div>' );
384 }
385
386 if( $this->displayImg->isMultipage() ) {
387 $count = $this->displayImg->pageCount();
388
389 if( $page > 1 ) {
390 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
391 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
392 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
393 array( 'page' => $page - 1 ) );
394 } else {
395 $thumb1 = '';
396 }
397
398 if( $page < $count ) {
399 $label = wfMsg( 'imgmultipagenext' );
400 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
401 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
402 array( 'page' => $page + 1 ) );
403 } else {
404 $thumb2 = '';
405 }
406
407 global $wgScript;
408
409 $formParams = array(
410 'name' => 'pageselector',
411 'action' => $wgScript,
412 'onchange' => 'document.pageselector.submit();',
413 );
414
415 $option = array();
416 for ( $i=1; $i <= $count; $i++ ) {
417 $options[] = Xml::option( $wgLang->formatNum($i), $i, $i == $page );
418 }
419 $select = Xml::tags( 'select',
420 array( 'id' => 'pageselector', 'name' => 'page' ),
421 implode( "\n", $options ) );
422
423 $wgOut->addHTML(
424 '</td><td><div class="multipageimagenavbox">' .
425 Xml::openElement( 'form', $formParams ) .
426 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
427 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
428 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
429 Xml::closeElement( 'form' ) .
430 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
431 );
432 }
433 } else {
434 #if direct link is allowed but it's not a renderable image, show an icon.
435 if( $this->displayImg->isSafeFile() ) {
436 $icon= $this->displayImg->iconThumb();
437
438 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
439 $icon->toHtml( array( 'desc-link' => true ) ) .
440 '</div>' );
441 }
442
443 $showLink = true;
444 }
445
446
447 if($showLink) {
448 $filename = wfEscapeWikiText( $this->displayImg->getName() );
449
450 if( !$this->displayImg->isSafeFile() ) {
451 $warning = wfMsgNoTrans( 'mediawarning' );
452 $wgOut->addWikiText( <<<EOT
453 <div class="fullMedia">
454 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
455 <span class="fileInfo"> $longDesc</span>
456 </div>
457
458 <div class="mediaWarning">$warning</div>
459 EOT
460 );
461 } else {
462 $wgOut->addWikiText( <<<EOT
463 <div class="fullMedia">
464 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
465 </div>
466 EOT
467 );
468 }
469 }
470
471 if( !$this->displayImg->isLocal() ) {
472 $this->printSharedImageText();
473 }
474 } else {
475 # Image does not exist
476
477 $title = SpecialPage::getTitleFor( 'Upload' );
478 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
479 'wpDestFile=' . urlencode( $this->displayImg->getName() ) );
480 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
481 }
482 }
483
484 /**
485 * Show a notice that the file is from a shared repository
486 */
487 protected function printSharedImageText() {
488 global $wgOut, $wgUser;
489
490 $this->loadFile();
491
492 $descUrl = $this->img->getDescriptionUrl();
493 $descText = $this->img->getDescriptionText();
494 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
495 if( $descUrl ) {
496 $sk = $wgUser->getSkin();
497 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
498 $msg = ( $descText ) ? 'shareduploadwiki-desc' : 'shareduploadwiki';
499 $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
500 if( $msg != '-' ) {
501 # Show message only if not voided by local sysops
502 $s .= $msg;
503 }
504 }
505 $s .= "</div>";
506 $wgOut->addHTML( $s );
507
508 if( $descText ) {
509 $this->mExtraDescription = $descText;
510 }
511 }
512
513 /*
514 * Check for files with the same name on the foreign repos.
515 */
516 protected function checkSharedConflict() {
517 global $wgOut, $wgUser;
518
519 $repoGroup = RepoGroup::singleton();
520 if( !$repoGroup->hasForeignRepos() ) {
521 return;
522 }
523
524 $this->loadFile();
525 if( !$this->img->isLocal() ) {
526 return;
527 }
528
529 $this->dupFile = null;
530 $repoGroup->forEachForeignRepo( array( $this, 'checkSharedConflictCallback' ) );
531
532 if( !$this->dupFile )
533 return;
534 $dupfile = $this->dupFile;
535 $same = (
536 ($this->img->getSha1() == $dupfile->getSha1()) &&
537 ($this->img->getSize() == $dupfile->getSize())
538 );
539
540 $sk = $wgUser->getSkin();
541 $descUrl = $dupfile->getDescriptionUrl();
542 if( $same ) {
543 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadduplicate-linktext' ) );
544 $wgOut->addHTML( '<div id="shared-image-dup">' . wfMsgWikiHtml( 'shareduploadduplicate', $link ) . '</div>' );
545 } else {
546 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadconflict-linktext' ) );
547 $wgOut->addHTML( '<div id="shared-image-conflict">' . wfMsgWikiHtml( 'shareduploadconflict', $link ) . '</div>' );
548 }
549 }
550
551 public function checkSharedConflictCallback( $repo ) {
552 $this->loadFile();
553 $dupfile = $repo->newFile( $this->img->getTitle() );
554 if( $dupfile && $dupfile->exists() ) {
555 $this->dupFile = $dupfile;
556 return $dupfile->exists();
557 }
558 return false;
559 }
560
561 public 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 protected 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 . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . '</small></li>' );
595
596 $wgOut->addHTML( '</ul>' );
597 }
598
599 protected function closeShowImage() {} # For overloading
600
601 /**
602 * If the page we've just displayed is in the "Image" namespace,
603 * we follow it with an upload history of the image and its usage.
604 */
605 protected function imageHistory() {
606 global $wgOut, $wgUseExternalEditor;
607
608 $this->loadFile();
609 $pager = new ImageHistoryPseudoPager( $this );
610 $wgOut->addHTML( $pager->getBody() );
611
612 $this->img->resetHistory(); // free db resources
613
614 # Exist check because we don't want to show this on pages where an image
615 # doesn't exist along with the noimage message, that would suck. -ævar
616 if( $wgUseExternalEditor && $this->img->exists() ) {
617 $this->uploadLinksBox();
618 }
619 }
620
621 protected function imageLinks() {
622 global $wgUser, $wgOut, $wgLang;
623
624 $limit = 100;
625
626 $dbr = wfGetDB( DB_SLAVE );
627
628 $res = $dbr->select(
629 array( 'imagelinks', 'page' ),
630 array( 'page_namespace', 'page_title' ),
631 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
632 __METHOD__,
633 array( 'LIMIT' => $limit + 1)
634 );
635 $count = $dbr->numRows( $res );
636 if( $count == 0 ) {
637 $wgOut->addHTML( "<div id='mw-imagepage-nolinkstoimage'>\n" );
638 $wgOut->addWikiMsg( 'nolinkstoimage' );
639 $wgOut->addHTML( "</div>\n" );
640 return;
641 }
642
643 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
644 if( $count <= $limit - 1 ) {
645 $wgOut->addWikiMsg( 'linkstoimage', $count );
646 } else {
647 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
648 $wgOut->addWikiMsg( 'linkstoimage-more',
649 $wgLang->formatNum( $limit ),
650 $this->mTitle->getPrefixedDBkey()
651 );
652 }
653
654 $wgOut->addHTML( "<ul class='mw-imagepage-linkstoimage'>\n" );
655 $sk = $wgUser->getSkin();
656 $count = 0;
657 while ( $s = $res->fetchObject() ) {
658 $count++;
659 if( $count <= $limit ) {
660 // We have not yet reached the extra one that tells us there is more to fetch
661 $name = Title::makeTitle( $s->page_namespace, $s->page_title );
662 $link = $sk->makeKnownLinkObj( $name, "" );
663 $wgOut->addHTML( "<li>{$link}</li>\n" );
664 }
665 }
666 $wgOut->addHTML( "</ul></div>\n" );
667 $res->free();
668
669 // Add a links to [[Special:Whatlinkshere]]
670 if( $count > $limit )
671 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
672 }
673
674 protected function imageRedirects() {
675 global $wgUser, $wgOut, $wgLang;
676
677 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
678 if( count( $redirects ) == 0 ) return;
679
680 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
681 $wgOut->addWikiMsg( 'redirectstofile',
682 $wgLang->formatNum( count( $redirects ) )
683 );
684 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
685
686 $sk = $wgUser->getSkin();
687 foreach ( $redirects as $title ) {
688 $link = $sk->makeKnownLinkObj( $title, "", "redirect=no" );
689 $wgOut->addHTML( "<li>{$link}</li>\n" );
690 }
691 $wgOut->addHTML( "</ul></div>\n" );
692
693 }
694
695 protected function imageDupes() {
696 global $wgOut, $wgUser, $wgLang;
697
698 $this->loadFile();
699
700 $dupes = $this->getDuplicates();
701 if( count( $dupes ) == 0 ) return;
702
703 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
704 $wgOut->addWikiMsg( 'duplicatesoffile',
705 $wgLang->formatNum( count( $dupes ) )
706 );
707 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
708
709 $sk = $wgUser->getSkin();
710 foreach ( $dupes as $file ) {
711 if( $file->isLocal() )
712 $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
713 else {
714 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
715 $file->getTitle()->getPrefixedText() );
716 }
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 public 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 public function getImagePage() {
795 return $this->imagePage;
796 }
797
798 public function getSkin() {
799 return $this->skin;
800 }
801
802 public function getFile() {
803 return $this->img;
804 }
805
806 public function beginImageHistoryList( $navLinks = '' ) {
807 global $wgOut, $wgUser;
808 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
809 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
810 . $navLinks
811 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
812 . '<tr><td></td>'
813 . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
814 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
815 . '<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>'
816 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
817 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
818 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
819 . "</tr>\n";
820 }
821
822 public function endImageHistoryList( $navLinks = '' ) {
823 return "</table>\n$navLinks\n";
824 }
825
826 public function imageHistoryLine( $iscur, $file ) {
827 global $wgUser, $wgLang, $wgContLang, $wgTitle;
828
829 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
830 $img = $iscur ? $file->getName() : $file->getArchiveName();
831 $user = $file->getUser('id');
832 $usertext = $file->getUser('text');
833 $size = $file->getSize();
834 $description = $file->getDescription();
835 $dims = $file->getDimensionsString();
836 $sha1 = $file->getSha1();
837
838 $local = $this->current->isLocal();
839 $row = $css = $selected = '';
840
841 // Deletion link
842 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
843 $row .= '<td>';
844 # Link to remove from history
845 if( $wgUser->isAllowed( 'delete' ) ) {
846 $q = array();
847 $q[] = 'action=delete';
848 if( !$iscur )
849 $q[] = 'oldimage=' . urlencode( $img );
850 $row .= $this->skin->makeKnownLinkObj(
851 $this->title,
852 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
853 implode( '&', $q )
854 );
855 }
856 # Link to hide content
857 if( $wgUser->isAllowed( 'deleterevision' ) ) {
858 if( $wgUser->isAllowed('delete') ) {
859 $row .= '<br/>';
860 }
861 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
862 // If file is top revision or locked from this user, don't link
863 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
864 $del = wfMsgHtml( 'rev-delundel' );
865 } else {
866 // If the file was hidden, link to sha-1
867 list($ts,$name) = explode('!',$img,2);
868 $del = $this->skin->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
869 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
870 '&oldimage=' . urlencode( $ts ) );
871 // Bolden oversighted content
872 if( $file->isDeleted(File::DELETED_RESTRICTED) )
873 $del = "<strong>$del</strong>";
874 }
875 $row .= "<tt style='white-space: nowrap;'><small>$del</small></tt>";
876 }
877 $row .= '</td>';
878 }
879
880 // Reversion link/current indicator
881 $row .= '<td>';
882 if( $iscur ) {
883 $row .= wfMsgHtml( 'filehist-current' );
884 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
885 if( $file->isDeleted(File::DELETED_FILE) ) {
886 $row .= wfMsgHtml('filehist-revert');
887 } else {
888 $q = array();
889 $q[] = 'action=revert';
890 $q[] = 'oldimage=' . urlencode( $img );
891 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
892 $row .= $this->skin->makeKnownLinkObj( $this->title,
893 wfMsgHtml( 'filehist-revert' ),
894 implode( '&', $q ) );
895 }
896 }
897 $row .= '</td>';
898
899 // Date/time and image link
900 if( $file->getTimestamp() === $this->img->getTimestamp() ) {
901 $selected = "class='filehistory-selected'";
902 }
903 $row .= "<td $selected style='white-space: nowrap;'>";
904 if( !$file->userCan(File::DELETED_FILE) ) {
905 # Don't link to unviewable files
906 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
907 } else if( $file->isDeleted(File::DELETED_FILE) ) {
908 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
909 # Make a link to review the image
910 $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
911 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->current->getExtension() );
912 $row .= '<span class="history-deleted">'.$url.'</span>';
913 } else {
914 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
915 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
916 }
917
918 // Thumbnail
919 if( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
920 $params = array(
921 'width' => '120',
922 'height' => '120',
923 );
924 $thumbnail = $file->transform( $params );
925 $options = array(
926 'alt' => wfMsg( 'filehist-thumbtext', $wgLang->timeAndDate( $timestamp, true ) ),
927 'file-link' => true,
928 );
929 $row .= '</td><td>' . $thumbnail->toHtml( $options );
930 } else {
931 $row .= '</td><td>' . wfMsgHtml( 'filehist-nothumb' );
932 }
933 $row .= "</td><td>";
934
935 // Image dimensions
936 $row .= htmlspecialchars( $dims );
937
938 // File size
939 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $size ) . ')</span>';
940
941 // Uploading user
942 $row .= '</td><td>';
943 if( $local ) {
944 // Hide deleted usernames
945 if( $file->isDeleted(File::DELETED_USER) ) {
946 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
947 } else {
948 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
949 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
950 }
951 } else {
952 $row .= htmlspecialchars( $usertext );
953 }
954 $row .= '</td><td>';
955
956 // Don't show deleted descriptions
957 if( $file->isDeleted(File::DELETED_COMMENT) ) {
958 $row .= '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
959 } else {
960 $row .= $this->skin->commentBlock( $description, $this->title );
961 }
962 $row .= '</td>';
963
964 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
965 $classAttr = $rowClass ? " class='$rowClass'" : "";
966
967 return "<tr{$classAttr}>{$row}</tr>\n";
968 }
969 }
970
971 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
972 function __construct( $imagePage ) {
973 parent::__construct();
974 $this->mImagePage = $imagePage;
975 $this->mTitle = clone( $imagePage->getTitle() );
976 $this->mTitle->setFragment( '#filehistory' );
977 $this->mImg = NULL;
978 $this->mHist = array();
979 $this->mRange = array( 0, 0 ); // display range
980 }
981
982 function getTitle() {
983 return $this->mTitle;
984 }
985
986 function getQueryInfo() {
987 return false;
988 }
989
990 function getIndexField() {
991 return '';
992 }
993
994 function formatRow( $row ) {
995 return '';
996 }
997
998 function getBody() {
999 $s = '';
1000 $this->doQuery();
1001 if( count($this->mHist) ) {
1002 $list = new ImageHistoryList( $this->mImagePage );
1003 # Generate prev/next links
1004 $navLink = $this->getNavigationBar();
1005 $s = $list->beginImageHistoryList($navLink);
1006 // Skip rows there just for paging links
1007 for( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
1008 $file = $this->mHist[$i];
1009 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1010 }
1011 $s .= $list->endImageHistoryList($navLink);
1012 }
1013 return $s;
1014 }
1015
1016 function doQuery() {
1017 if( $this->mQueryDone ) return;
1018 $this->mImg = $this->mImagePage->getFile(); // ensure loading
1019 if( !$this->mImg->exists() ) {
1020 return;
1021 }
1022 $queryLimit = $this->mLimit + 1; // limit plus extra row
1023 if( $this->mIsBackwards ) {
1024 // Fetch the file history
1025 $this->mHist = $this->mImg->getHistory($queryLimit,null,$this->mOffset,false);
1026 // The current rev may not meet the offset/limit
1027 $numRows = count($this->mHist);
1028 if( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
1029 $this->mHist = array_merge( array($this->mImg), $this->mHist );
1030 }
1031 } else {
1032 // The current rev may not meet the offset
1033 if( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
1034 $this->mHist[] = $this->mImg;
1035 }
1036 // Old image versions (fetch extra row for nav links)
1037 $oiLimit = count($this->mHist) ? $this->mLimit : $this->mLimit+1;
1038 // Fetch the file history
1039 $this->mHist = array_merge( $this->mHist,
1040 $this->mImg->getHistory($oiLimit,$this->mOffset,null,false) );
1041 }
1042 $numRows = count($this->mHist); // Total number of query results
1043 if( $numRows ) {
1044 # Index value of top item in the list
1045 $firstIndex = $this->mIsBackwards ?
1046 $this->mHist[$numRows-1]->getTimestamp() : $this->mHist[0]->getTimestamp();
1047 # Discard the extra result row if there is one
1048 if( $numRows > $this->mLimit && $numRows > 1 ) {
1049 if( $this->mIsBackwards ) {
1050 # Index value of item past the index
1051 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
1052 # Index value of bottom item in the list
1053 $lastIndex = $this->mHist[1]->getTimestamp();
1054 # Display range
1055 $this->mRange = array( 1, $numRows-1 );
1056 } else {
1057 # Index value of item past the index
1058 $this->mPastTheEndIndex = $this->mHist[$numRows-1]->getTimestamp();
1059 # Index value of bottom item in the list
1060 $lastIndex = $this->mHist[$numRows-2]->getTimestamp();
1061 # Display range
1062 $this->mRange = array( 0, $numRows-2 );
1063 }
1064 } else {
1065 # Setting indexes to an empty string means that they will be
1066 # omitted if they would otherwise appear in URLs. It just so
1067 # happens that this is the right thing to do in the standard
1068 # UI, in all the relevant cases.
1069 $this->mPastTheEndIndex = '';
1070 # Index value of bottom item in the list
1071 $lastIndex = $this->mIsBackwards ?
1072 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows-1]->getTimestamp();
1073 # Display range
1074 $this->mRange = array( 0, $numRows-1 );
1075 }
1076 } else {
1077 $firstIndex = '';
1078 $lastIndex = '';
1079 $this->mPastTheEndIndex = '';
1080 }
1081 if( $this->mIsBackwards ) {
1082 $this->mIsFirst = ( $numRows < $queryLimit );
1083 $this->mIsLast = ( $this->mOffset == '' );
1084 $this->mLastShown = $firstIndex;
1085 $this->mFirstShown = $lastIndex;
1086 } else {
1087 $this->mIsFirst = ( $this->mOffset == '' );
1088 $this->mIsLast = ( $numRows < $queryLimit );
1089 $this->mLastShown = $lastIndex;
1090 $this->mFirstShown = $firstIndex;
1091 }
1092 $this->mQueryDone = true;
1093 }
1094 }