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