(bug 7958) Special:Cite of older version of an article should use old version id.
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 */
4
5 /**
6 *
7 */
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
10
11 /**
12 * Special handling for image description pages
13 *
14 * @addtogroup Media
15 */
16 class ImagePage extends Article {
17
18 /* private */ var $img; // Image object this page is shown for
19 var $mExtraDescription = false;
20
21 /**
22 * Handler for action=render
23 * Include body text only; none of the image extras
24 */
25 function render() {
26 global $wgOut;
27 $wgOut->setArticleBodyOnly( true );
28 $wgOut->addSecondaryWikitext( $this->getContent() );
29 }
30
31 function view() {
32 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
33
34 $this->img = new Image( $this->mTitle );
35
36 $diff = $wgRequest->getVal( 'diff' );
37 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
38
39 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
40 return Article::view();
41
42 if ($wgShowEXIF && $this->img->exists()) {
43 $exif = $this->img->getExifData();
44 $showmeta = count($exif) ? true : false;
45 } else {
46 $exif = false;
47 $showmeta = false;
48 }
49
50 if ($this->img->exists())
51 $wgOut->addHTML($this->showTOC($showmeta));
52
53 $this->openShowImage();
54
55 # No need to display noarticletext, we use our own message, output in openShowImage()
56 if ( $this->getID() ) {
57 Article::view();
58 } else {
59 # Just need to set the right headers
60 $wgOut->setArticleFlag( true );
61 $wgOut->setRobotpolicy( 'index,follow' );
62 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
63 $this->viewUpdates();
64 }
65
66 # Show shared description, if needed
67 if ( $this->mExtraDescription ) {
68 $fol = wfMsg( 'shareddescriptionfollows' );
69 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
70 $wgOut->addWikiText( $fol );
71 }
72 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
73 }
74
75 $this->closeShowImage();
76 $this->imageHistory();
77 $this->imageLinks();
78
79 if ( $exif ) {
80 global $wgStylePath, $wgStyleVersion;
81 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
82 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
83 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
84 $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
85 $wgOut->addHTML(
86 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
87 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
88 }
89 }
90
91 /**
92 * Create the TOC
93 *
94 * @access private
95 *
96 * @param bool $metadata Whether or not to show the metadata link
97 * @return string
98 */
99 function showTOC( $metadata ) {
100 global $wgLang;
101 $r = '<ul id="filetoc">
102 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
103 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
104 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
105 ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
106 </ul>';
107 return $r;
108 }
109
110 /**
111 * Make a table with metadata to be shown in the output page.
112 *
113 * @access private
114 *
115 * @param array $exif The array containing the EXIF data
116 * @return string
117 */
118 function makeMetadataTable( $exif ) {
119 $r = wfMsg( 'metadata-help' ) . "\n\n";
120 $r .= "{| id=mw_metadata class=mw_metadata\n";
121 $visibleFields = $this->visibleMetadataFields();
122 foreach( $exif as $k => $v ) {
123 $tag = strtolower( $k );
124 $msg = wfMsg( "exif-$tag" );
125 $class = "exif-$tag";
126 if( !in_array( $tag, $visibleFields ) ) {
127 $class .= ' collapsable';
128 }
129 $r .= "|- class=\"$class\"\n";
130 $r .= "!| $msg\n";
131 $r .= "|| $v\n";
132 }
133 $r .= '|}';
134 return $r;
135 }
136
137 /**
138 * Get a list of EXIF metadata items which should be displayed when
139 * the metadata table is collapsed.
140 *
141 * @return array of strings
142 * @access private
143 */
144 function visibleMetadataFields() {
145 $fields = array();
146 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
147 foreach( $lines as $line ) {
148 $matches = array();
149 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
150 $fields[] = $matches[1];
151 }
152 }
153 return $fields;
154 }
155
156 /**
157 * Overloading Article's getContent method.
158 *
159 * Omit noarticletext if sharedupload; text will be fetched from the
160 * shared upload server if possible.
161 */
162 function getContent() {
163 if( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
164 return '';
165 }
166 return Article::getContent();
167 }
168
169 function openShowImage() {
170 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
171
172 $full_url = $this->img->getURL();
173 $linkAttribs = false;
174 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
175 if( !isset( $wgImageLimits[$sizeSel] ) ) {
176 $sizeSel = User::getDefaultOption( 'imagesize' );
177
178 // The user offset might still be incorrect, specially if
179 // $wgImageLimits got changed (see bug #8858).
180 if( !isset( $wgImageLimits[$sizeSel] ) ) {
181 // Default to the first offset in $wgImageLimits
182 $sizeSel = 0;
183 }
184 }
185 $max = $wgImageLimits[$sizeSel];
186 $maxWidth = $max[0];
187 $maxHeight = $max[1];
188 $sk = $wgUser->getSkin();
189
190 if ( $this->img->exists() ) {
191 # image
192 $page = $wgRequest->getIntOrNull( 'page' );
193 if ( is_null( $page ) ) {
194 $params = array();
195 $page = 1;
196 } else {
197 $params = array( 'page' => $page );
198 }
199 $width_orig = $this->img->getWidth();
200 $width = $width_orig;
201 $height_orig = $this->img->getHeight();
202 $height = $height_orig;
203 $mime = $this->img->getMimeType();
204 $showLink = false;
205 $linkAttribs = array( 'href' => $full_url );
206
207 if ( $this->img->allowInlineDisplay() and $width and $height) {
208 # image
209
210 # "Download high res version" link below the image
211 $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
212 # We'll show a thumbnail of this image
213 if ( $width > $maxWidth || $height > $maxHeight ) {
214 # Calculate the thumbnail size.
215 # First case, the limiting factor is the width, not the height.
216 if ( $width / $height >= $maxWidth / $maxHeight ) {
217 $height = round( $height * $maxWidth / $width);
218 $width = $maxWidth;
219 # Note that $height <= $maxHeight now.
220 } else {
221 $newwidth = floor( $width * $maxHeight / $height);
222 $height = round( $height * $newwidth / $width );
223 $width = $newwidth;
224 # Note that $height <= $maxHeight now, but might not be identical
225 # because of rounding.
226 }
227 $msgbig = wfMsgHtml( 'show-big-image' );
228 $msgsmall = wfMsgExt( 'show-big-image-thumb',
229 array( 'parseinline' ), $width, $height );
230 } else {
231 # Image is small enough to show full size on image page
232 $msgbig = htmlspecialchars( $this->img->getName() );
233 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
234 }
235
236 $params['width'] = $width;
237 $thumbnail = $this->img->transform( $params );
238
239 $anchorclose = "<br />";
240 if( $this->img->mustRender() ) {
241 $showLink = true;
242 } else {
243 $anchorclose .=
244 $msgsmall .
245 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . ' ' . $msgsize;
246 }
247
248 if ( $this->img->isMultipage() ) {
249 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
250 }
251
252 $imgAttribs = array(
253 'border' => 0,
254 'alt' => $this->img->getTitle()->getPrefixedText()
255 );
256
257 if ( $thumbnail ) {
258 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
259 $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
260 $anchorclose . '</div>' );
261 }
262
263 if ( $this->img->isMultipage() ) {
264 $count = $this->img->pageCount();
265
266 if ( $page > 1 ) {
267 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
268 $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
269 $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none',
270 array( 'page' => $page - 1 ) );
271 } else {
272 $thumb1 = '';
273 }
274
275 if ( $page < $count ) {
276 $label = wfMsg( 'imgmultipagenext' );
277 $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
278 $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none',
279 array( 'page' => $page + 1 ) );
280 } else {
281 $thumb2 = '';
282 }
283
284 global $wgScript;
285 $select = '<form name="pageselector" action="' .
286 htmlspecialchars( $wgScript ) .
287 '" method="get" onchange="document.pageselector.submit();">' .
288 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
289 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
290 ' <select id="pageselector" name="page">';
291 for ( $i=1; $i <= $count; $i++ ) {
292 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
293 $i == $page );
294 }
295 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
296 '<input type="submit" value="' .
297 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
298
299 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
300 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
301 }
302 } else {
303 #if direct link is allowed but it's not a renderable image, show an icon.
304 if ($this->img->isSafeFile()) {
305 $icon= $this->img->iconThumb();
306
307 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
308 $icon->toHtml() .
309 '</a></div>' );
310 }
311
312 $showLink = true;
313 }
314
315
316 if ($showLink) {
317 // Workaround for incorrect MIME type on SVGs uploaded in previous versions
318 if ($mime == 'image/svg') $mime = 'image/svg+xml';
319
320 $filename = wfEscapeWikiText( $this->img->getName() );
321 $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
322 $infores = '';
323
324 // Check for MIME type. Other types may have more information in the future.
325 if (substr($mime,0,9) == 'image/svg' ) {
326 $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
327 }
328
329 global $wgContLang;
330 $dirmark = $wgContLang->getDirMark();
331 if (!$this->img->isSafeFile()) {
332 $warning = wfMsg( 'mediawarning' );
333 $wgOut->addWikiText( <<<END
334 <div class="fullMedia">$infores
335 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
336 <span class="fileInfo"> $info</span>
337 </div>
338
339 <div class="mediaWarning">$warning</div>
340 END
341 );
342 } else {
343 $wgOut->addWikiText( <<<END
344 <div class="fullMedia">$infores
345 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
346 </div>
347 END
348 );
349 }
350 }
351
352 if($this->img->fromSharedDirectory) {
353 $this->printSharedImageText();
354 }
355 } else {
356 # Image does not exist
357
358 $title = SpecialPage::getTitleFor( 'Upload' );
359 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
360 'wpDestFile=' . urlencode( $this->img->getName() ) );
361 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
362 }
363 }
364
365 function printSharedImageText() {
366 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
367
368 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
369 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
370 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
371
372 $sk = $wgUser->getSkin();
373 $title = SpecialPage::getTitleFor( 'Upload' );
374 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
375 array( 'wpDestFile' => urlencode( $this->img->getName() )));
376 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
377 }
378 $sharedtext .= "</div>";
379 $wgOut->addHTML($sharedtext);
380
381 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
382 $renderUrl = wfAppendQuery( $url, 'action=render' );
383 wfDebug( "Fetching shared description from $renderUrl\n" );
384 $text = Http::get( $renderUrl );
385 if ($text)
386 $this->mExtraDescription = $text;
387 }
388 }
389
390 function getUploadUrl() {
391 global $wgServer;
392 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
393 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
394 }
395
396 /**
397 * Print out the various links at the bottom of the image page, e.g. reupload,
398 * external editing (and instructions link) etc.
399 */
400 function uploadLinksBox() {
401 global $wgUser, $wgOut;
402
403 if( $this->img->fromSharedDirectory )
404 return;
405
406 $sk = $wgUser->getSkin();
407
408 $wgOut->addHtml( '<br /><ul>' );
409
410 # "Upload a new version of this file" link
411 if( $wgUser->isAllowed( 'reupload' ) ) {
412 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
413 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
414 }
415
416 # External editing link
417 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
418 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
419
420 $wgOut->addHtml( '</ul>' );
421 }
422
423 function closeShowImage()
424 {
425 # For overloading
426
427 }
428
429 /**
430 * If the page we've just displayed is in the "Image" namespace,
431 * we follow it with an upload history of the image and its usage.
432 */
433 function imageHistory()
434 {
435 global $wgUser, $wgOut, $wgUseExternalEditor;
436
437 $sk = $wgUser->getSkin();
438
439 $line = $this->img->nextHistoryLine();
440
441 if ( $line ) {
442 $list = new ImageHistoryList( $sk );
443 $s = $list->beginImageHistoryList() .
444 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
445 $this->mTitle->getDBkey(), $line->img_user,
446 $line->img_user_text, $line->img_size, $line->img_description,
447 $line->img_width, $line->img_height
448 );
449
450 while ( $line = $this->img->nextHistoryLine() ) {
451 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
452 $line->oi_archive_name, $line->img_user,
453 $line->img_user_text, $line->img_size, $line->img_description,
454 $line->img_width, $line->img_height
455 );
456 }
457 $s .= $list->endImageHistoryList();
458 } else { $s=''; }
459 $wgOut->addHTML( $s );
460
461 # Exist check because we don't want to show this on pages where an image
462 # doesn't exist along with the noimage message, that would suck. -ævar
463 if( $wgUseExternalEditor && $this->img->exists() ) {
464 $this->uploadLinksBox();
465 }
466
467 }
468
469 function imageLinks()
470 {
471 global $wgUser, $wgOut;
472
473 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
474
475 $dbr = wfGetDB( DB_SLAVE );
476 $page = $dbr->tableName( 'page' );
477 $imagelinks = $dbr->tableName( 'imagelinks' );
478
479 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
480 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
481 $sql = $dbr->limitResult($sql, 500, 0);
482 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
483
484 if ( 0 == $dbr->numRows( $res ) ) {
485 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
486 return;
487 }
488 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
489
490 $sk = $wgUser->getSkin();
491 while ( $s = $dbr->fetchObject( $res ) ) {
492 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
493 $link = $sk->makeKnownLinkObj( $name, "" );
494 $wgOut->addHTML( "<li>{$link}</li>\n" );
495 }
496 $wgOut->addHTML( "</ul>\n" );
497 }
498
499 function delete()
500 {
501 global $wgUser, $wgOut, $wgRequest;
502
503 $confirm = $wgRequest->wasPosted();
504 $reason = $wgRequest->getVal( 'wpReason' );
505 $image = $wgRequest->getVal( 'image' );
506 $oldimage = $wgRequest->getVal( 'oldimage' );
507
508 # Only sysops can delete images. Previously ordinary users could delete
509 # old revisions, but this is no longer the case.
510 if ( !$wgUser->isAllowed('delete') ) {
511 $wgOut->permissionRequired( 'delete' );
512 return;
513 }
514 if ( $wgUser->isBlocked() ) {
515 return $this->blockedIPpage();
516 }
517 if ( wfReadOnly() ) {
518 $wgOut->readOnlyPage();
519 return;
520 }
521
522 # Better double-check that it hasn't been deleted yet!
523 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
524 if ( ( !is_null( $image ) )
525 && ( '' == trim( $image ) ) ) {
526 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
527 return;
528 }
529
530 $this->img = new Image( $this->mTitle );
531
532 # Deleting old images doesn't require confirmation
533 if ( !is_null( $oldimage ) || $confirm ) {
534 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
535 $this->doDelete( $reason );
536 } else {
537 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
538 }
539 return;
540 }
541
542 if ( !is_null( $image ) ) {
543 $q = '&image=' . urlencode( $image );
544 } else if ( !is_null( $oldimage ) ) {
545 $q = '&oldimage=' . urlencode( $oldimage );
546 } else {
547 $q = '';
548 }
549 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
550 }
551
552 /*
553 * Delete an image.
554 * @param $reason User provided reason for deletion.
555 */
556 function doDelete( $reason ) {
557 global $wgOut, $wgRequest;
558
559 $oldimage = $wgRequest->getVal( 'oldimage' );
560
561 if ( !is_null( $oldimage ) ) {
562 if ( strlen( $oldimage ) < 16 ) {
563 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
564 return;
565 }
566 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
567 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
568 return;
569 }
570 if ( !$this->doDeleteOldImage( $oldimage ) ) {
571 return;
572 }
573 $deleted = $oldimage;
574 } else {
575 $ok = $this->img->delete( $reason );
576 if( !$ok ) {
577 # If the deletion operation actually failed, bug out:
578 $wgOut->showFileDeleteError( $this->img->getName() );
579 return;
580 }
581
582 # Image itself is now gone, and database is cleaned.
583 # Now we remove the image description page.
584
585 $article = new Article( $this->mTitle );
586 $article->doDeleteArticle( $reason ); # ignore errors
587
588 $deleted = $this->img->getName();
589 }
590
591 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
592 $wgOut->setRobotpolicy( 'noindex,nofollow' );
593
594 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
595 $text = wfMsg( 'deletedtext', $deleted, $loglink );
596
597 $wgOut->addWikiText( $text );
598
599 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
600 }
601
602 /**
603 * @return success
604 */
605 function doDeleteOldImage( $oldimage )
606 {
607 global $wgOut;
608
609 $ok = $this->img->deleteOld( $oldimage, '' );
610 if( !$ok ) {
611 # If we actually have a file and can't delete it, throw an error.
612 # Something went awry...
613 $wgOut->showFileDeleteError( "$oldimage" );
614 } else {
615 # Log the deletion
616 $log = new LogPage( 'delete' );
617 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
618 }
619 return $ok;
620 }
621
622 function revert() {
623 global $wgOut, $wgRequest, $wgUser;
624
625 $oldimage = $wgRequest->getText( 'oldimage' );
626 if ( strlen( $oldimage ) < 16 ) {
627 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
628 return;
629 }
630 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
631 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
632 return;
633 }
634
635 if ( wfReadOnly() ) {
636 $wgOut->readOnlyPage();
637 return;
638 }
639 if( $wgUser->isAnon() ) {
640 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
641 return;
642 }
643 if ( ! $this->mTitle->userCan( 'edit' ) ) {
644 $wgOut->readOnlyPage( $this->getContent(), true );
645 return;
646 }
647 if ( $wgUser->isBlocked() ) {
648 return $this->blockedIPpage();
649 }
650 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
651 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
652 return;
653 }
654 $name = substr( $oldimage, 15 );
655
656 $dest = wfImageDir( $name );
657 $archive = wfImageArchiveDir( $name );
658 $curfile = "{$dest}/{$name}";
659
660 if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
661 if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
662
663 if ( ! is_file( $curfile ) ) {
664 $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
665 return;
666 }
667 $oldver = wfTimestampNow() . "!{$name}";
668
669 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
670 $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
671 return;
672 }
673 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
674 $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
675 return;
676 }
677
678 # Record upload and update metadata cache
679 $img = Image::newFromName( $name );
680 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
681
682 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
683 $wgOut->setRobotpolicy( 'noindex,nofollow' );
684 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
685
686 $descTitle = $img->getTitle();
687 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
688 }
689
690 function blockedIPpage() {
691 $edit = new EditPage( $this );
692 return $edit->blockedIPpage();
693 }
694
695 /**
696 * Override handling of action=purge
697 */
698 function doPurge() {
699 $this->img = new Image( $this->mTitle );
700 if( $this->img->exists() ) {
701 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
702 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
703 $update->doUpdate();
704 $this->img->purgeCache();
705 } else {
706 wfDebug( "ImagePage::doPurge no image\n" );
707 }
708 parent::doPurge();
709 }
710
711 }
712
713 /**
714 * @todo document
715 * @addtogroup Media
716 */
717 class ImageHistoryList {
718 function ImageHistoryList( &$skin ) {
719 $this->skin =& $skin;
720 }
721
722 function beginImageHistoryList() {
723 $s = "\n" .
724 Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'imghistory' ) ) .
725 "\n<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
726 return $s;
727 }
728
729 function endImageHistoryList() {
730 $s = "</ul>\n";
731 return $s;
732 }
733
734 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
735 global $wgUser, $wgLang, $wgTitle, $wgContLang;
736
737 $datetime = $wgLang->timeanddate( $timestamp, true );
738 $del = wfMsgHtml( 'deleteimg' );
739 $delall = wfMsgHtml( 'deleteimgcompletely' );
740 $cur = wfMsgHtml( 'cur' );
741
742 if ( $iscur ) {
743 $url = Image::imageUrl( $img );
744 $rlink = $cur;
745 if ( $wgUser->isAllowed('delete') ) {
746 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
747 '&action=delete' );
748 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
749
750 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
751 } else {
752 $dlink = $del;
753 }
754 } else {
755 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
756 if( $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
757 $token = urlencode( $wgUser->editToken( $img ) );
758 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
759 wfMsgHtml( 'revertimg' ), 'action=revert&oldimage=' .
760 urlencode( $img ) . "&wpEditToken=$token" );
761 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
762 $del, 'action=delete&oldimage=' . urlencode( $img ) .
763 "&wpEditToken=$token" );
764 } else {
765 # Having live active links for non-logged in users
766 # means that bots and spiders crawling our site can
767 # inadvertently change content. Baaaad idea.
768 $rlink = wfMsgHtml( 'revertimg' );
769 $dlink = $del;
770 }
771 }
772
773 $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
774 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
775 $wgLang->formatNum( $size ) );
776 $widthheight = wfMsgHtml( 'widthheight', $width, $height );
777 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
778
779 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
780
781 $s .= $this->skin->commentBlock( $description, $wgTitle );
782 $s .= "</li>\n";
783 return $s;
784 }
785
786 }
787
788
789 ?>