API bug 10046: incorrect action produces invalid response format
[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 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
208
209 if ( $this->img->allowInlineDisplay() and $width and $height) {
210 # image
211
212 # "Download high res version" link below the image
213 $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
214 # We'll show a thumbnail of this image
215 if ( $width > $maxWidth || $height > $maxHeight ) {
216 # Calculate the thumbnail size.
217 # First case, the limiting factor is the width, not the height.
218 if ( $width / $height >= $maxWidth / $maxHeight ) {
219 $height = round( $height * $maxWidth / $width);
220 $width = $maxWidth;
221 # Note that $height <= $maxHeight now.
222 } else {
223 $newwidth = floor( $width * $maxHeight / $height);
224 $height = round( $height * $newwidth / $width );
225 $width = $newwidth;
226 # Note that $height <= $maxHeight now, but might not be identical
227 # because of rounding.
228 }
229 $msgbig = wfMsgHtml( 'show-big-image' );
230 $msgsmall = wfMsgExt( 'show-big-image-thumb',
231 array( 'parseinline' ), $width, $height );
232 } else {
233 # Image is small enough to show full size on image page
234 $msgbig = htmlspecialchars( $this->img->getName() );
235 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
236 }
237
238 $params['width'] = $width;
239 $thumbnail = $this->img->transform( $params );
240
241 $anchorclose = "<br />";
242 if( $this->img->mustRender() ) {
243 $showLink = true;
244 } else {
245 $anchorclose .=
246 $msgsmall .
247 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . ' ' . $msgsize;
248 }
249
250 if ( $this->img->isMultipage() ) {
251 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
252 }
253
254 $imgAttribs = array(
255 'border' => 0,
256 'alt' => $this->img->getTitle()->getPrefixedText()
257 );
258
259 if ( $thumbnail ) {
260 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
261 $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
262 $anchorclose . '</div>' );
263 }
264
265 if ( $this->img->isMultipage() ) {
266 $count = $this->img->pageCount();
267
268 if ( $page > 1 ) {
269 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
270 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
271 $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none',
272 array( 'page' => $page - 1 ) );
273 } else {
274 $thumb1 = '';
275 }
276
277 if ( $page < $count ) {
278 $label = wfMsg( 'imgmultipagenext' );
279 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
280 $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none',
281 array( 'page' => $page + 1 ) );
282 } else {
283 $thumb2 = '';
284 }
285
286 global $wgScript;
287 $select = '<form name="pageselector" action="' .
288 htmlspecialchars( $wgScript ) .
289 '" method="get" onchange="document.pageselector.submit();">' .
290 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
291 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
292 ' <select id="pageselector" name="page">';
293 for ( $i=1; $i <= $count; $i++ ) {
294 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
295 $i == $page );
296 }
297 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
298 '<input type="submit" value="' .
299 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
300
301 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
302 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
303 }
304 } else {
305 #if direct link is allowed but it's not a renderable image, show an icon.
306 if ($this->img->isSafeFile()) {
307 $icon= $this->img->iconThumb();
308
309 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
310 $icon->toHtml() .
311 '</a></div>' );
312 }
313
314 $showLink = true;
315 }
316
317
318 if ($showLink) {
319 // Workaround for incorrect MIME type on SVGs uploaded in previous versions
320 if ($mime == 'image/svg') $mime = 'image/svg+xml';
321
322 $filename = wfEscapeWikiText( $this->img->getName() );
323 $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
324 $infores = '';
325
326 // Check for MIME type. Other types may have more information in the future.
327 if (substr($mime,0,9) == 'image/svg' ) {
328 $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
329 }
330
331 global $wgContLang;
332 $dirmark = $wgContLang->getDirMark();
333 if (!$this->img->isSafeFile()) {
334 $warning = wfMsg( 'mediawarning' );
335 $wgOut->addWikiText( <<<END
336 <div class="fullMedia">$infores
337 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
338 <span class="fileInfo"> $info</span>
339 </div>
340
341 <div class="mediaWarning">$warning</div>
342 END
343 );
344 } else {
345 $wgOut->addWikiText( <<<END
346 <div class="fullMedia">$infores
347 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
348 </div>
349 END
350 );
351 }
352 }
353
354 if($this->img->fromSharedDirectory) {
355 $this->printSharedImageText();
356 }
357 } else {
358 # Image does not exist
359
360 $title = SpecialPage::getTitleFor( 'Upload' );
361 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
362 'wpDestFile=' . urlencode( $this->img->getName() ) );
363 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
364 }
365 }
366
367 function printSharedImageText() {
368 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
369
370 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
371 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
372 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
373
374 $sk = $wgUser->getSkin();
375 $title = SpecialPage::getTitleFor( 'Upload' );
376 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
377 array( 'wpDestFile' => urlencode( $this->img->getName() )));
378 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
379 }
380 $sharedtext .= "</div>";
381 $wgOut->addHTML($sharedtext);
382
383 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
384 $renderUrl = wfAppendQuery( $url, 'action=render' );
385 wfDebug( "Fetching shared description from $renderUrl\n" );
386 $text = Http::get( $renderUrl );
387 if ($text)
388 $this->mExtraDescription = $text;
389 }
390 }
391
392 function getUploadUrl() {
393 global $wgServer;
394 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
395 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
396 }
397
398 /**
399 * Print out the various links at the bottom of the image page, e.g. reupload,
400 * external editing (and instructions link) etc.
401 */
402 function uploadLinksBox() {
403 global $wgUser, $wgOut;
404
405 if( $this->img->fromSharedDirectory )
406 return;
407
408 $sk = $wgUser->getSkin();
409
410 $wgOut->addHtml( '<br /><ul>' );
411
412 # "Upload a new version of this file" link
413 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
414 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
415 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
416 }
417
418 # External editing link
419 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
420 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
421
422 $wgOut->addHtml( '</ul>' );
423 }
424
425 function closeShowImage()
426 {
427 # For overloading
428
429 }
430
431 /**
432 * If the page we've just displayed is in the "Image" namespace,
433 * we follow it with an upload history of the image and its usage.
434 */
435 function imageHistory()
436 {
437 global $wgUser, $wgOut, $wgUseExternalEditor;
438
439 $sk = $wgUser->getSkin();
440
441 $line = $this->img->nextHistoryLine();
442
443 if ( $line ) {
444 $list = new ImageHistoryList( $sk );
445 $s = $list->beginImageHistoryList() .
446 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
447 $this->mTitle->getDBkey(), $line->img_user,
448 $line->img_user_text, $line->img_size, $line->img_description,
449 $line->img_width, $line->img_height
450 );
451
452 while ( $line = $this->img->nextHistoryLine() ) {
453 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
454 $line->oi_archive_name, $line->img_user,
455 $line->img_user_text, $line->img_size, $line->img_description,
456 $line->img_width, $line->img_height
457 );
458 }
459 $s .= $list->endImageHistoryList();
460 } else { $s=''; }
461 $wgOut->addHTML( $s );
462
463 # Exist check because we don't want to show this on pages where an image
464 # doesn't exist along with the noimage message, that would suck. -ævar
465 if( $wgUseExternalEditor && $this->img->exists() ) {
466 $this->uploadLinksBox();
467 }
468
469 }
470
471 function imageLinks()
472 {
473 global $wgUser, $wgOut;
474
475 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
476
477 $dbr = wfGetDB( DB_SLAVE );
478 $page = $dbr->tableName( 'page' );
479 $imagelinks = $dbr->tableName( 'imagelinks' );
480
481 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
482 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
483 $sql = $dbr->limitResult($sql, 500, 0);
484 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
485
486 if ( 0 == $dbr->numRows( $res ) ) {
487 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
488 return;
489 }
490 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
491
492 $sk = $wgUser->getSkin();
493 while ( $s = $dbr->fetchObject( $res ) ) {
494 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
495 $link = $sk->makeKnownLinkObj( $name, "" );
496 $wgOut->addHTML( "<li>{$link}</li>\n" );
497 }
498 $wgOut->addHTML( "</ul>\n" );
499 }
500
501 function delete()
502 {
503 global $wgUser, $wgOut, $wgRequest;
504
505 $confirm = $wgRequest->wasPosted();
506 $reason = $wgRequest->getVal( 'wpReason' );
507 $image = $wgRequest->getVal( 'image' );
508 $oldimage = $wgRequest->getVal( 'oldimage' );
509
510 # Only sysops can delete images. Previously ordinary users could delete
511 # old revisions, but this is no longer the case.
512 if ( !$wgUser->isAllowed('delete') ) {
513 $wgOut->permissionRequired( 'delete' );
514 return;
515 }
516 if ( $wgUser->isBlocked() ) {
517 $wgOut->blockedPage();
518 return;
519 }
520 if ( wfReadOnly() ) {
521 $wgOut->readOnlyPage();
522 return;
523 }
524
525 # Better double-check that it hasn't been deleted yet!
526 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
527 if ( ( !is_null( $image ) )
528 && ( '' == trim( $image ) ) ) {
529 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
530 return;
531 }
532
533 $this->img = new Image( $this->mTitle );
534
535 # Deleting old images doesn't require confirmation
536 if ( !is_null( $oldimage ) || $confirm ) {
537 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
538 $this->doDelete( $reason );
539 } else {
540 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
541 }
542 return;
543 }
544
545 if ( !is_null( $image ) ) {
546 $q = '&image=' . urlencode( $image );
547 } else if ( !is_null( $oldimage ) ) {
548 $q = '&oldimage=' . urlencode( $oldimage );
549 } else {
550 $q = '';
551 }
552 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
553 }
554
555 /*
556 * Delete an image.
557 * @param $reason User provided reason for deletion.
558 */
559 function doDelete( $reason ) {
560 global $wgOut, $wgRequest;
561
562 $oldimage = $wgRequest->getVal( 'oldimage' );
563
564 if ( !is_null( $oldimage ) ) {
565 if ( strlen( $oldimage ) < 16 ) {
566 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
567 return;
568 }
569 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
570 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
571 return;
572 }
573 if ( !$this->doDeleteOldImage( $oldimage ) ) {
574 return;
575 }
576 $deleted = $oldimage;
577 } else {
578 $ok = $this->img->delete( $reason );
579 if( !$ok ) {
580 # If the deletion operation actually failed, bug out:
581 $wgOut->showFileDeleteError( $this->img->getName() );
582 return;
583 }
584
585 # Image itself is now gone, and database is cleaned.
586 # Now we remove the image description page.
587
588 $article = new Article( $this->mTitle );
589 $article->doDeleteArticle( $reason ); # ignore errors
590
591 $deleted = $this->img->getName();
592 }
593
594 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
595 $wgOut->setRobotpolicy( 'noindex,nofollow' );
596
597 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
598 $text = wfMsg( 'deletedtext', $deleted, $loglink );
599
600 $wgOut->addWikiText( $text );
601
602 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
603 }
604
605 /**
606 * @return success
607 */
608 function doDeleteOldImage( $oldimage )
609 {
610 global $wgOut;
611
612 $ok = $this->img->deleteOld( $oldimage, '' );
613 if( !$ok ) {
614 # If we actually have a file and can't delete it, throw an error.
615 # Something went awry...
616 $wgOut->showFileDeleteError( "$oldimage" );
617 } else {
618 # Log the deletion
619 $log = new LogPage( 'delete' );
620 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
621 }
622 return $ok;
623 }
624
625 function revert() {
626 global $wgOut, $wgRequest, $wgUser;
627
628 $oldimage = $wgRequest->getText( 'oldimage' );
629 if ( strlen( $oldimage ) < 16 ) {
630 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
631 return;
632 }
633 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
634 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
635 return;
636 }
637
638 if ( wfReadOnly() ) {
639 $wgOut->readOnlyPage();
640 return;
641 }
642 if( $wgUser->isAnon() ) {
643 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
644 return;
645 }
646 if ( ! $this->mTitle->userCan( 'edit' ) ) {
647 $wgOut->readOnlyPage( $this->getContent(), true );
648 return;
649 }
650 if ( $wgUser->isBlocked() ) {
651 $wgOut->blockedPage();
652 return;
653 }
654 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
655 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
656 return;
657 }
658 $name = substr( $oldimage, 15 );
659
660 $dest = wfImageDir( $name );
661 $archive = wfImageArchiveDir( $name );
662 $curfile = "{$dest}/{$name}";
663
664 if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
665 if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
666
667 if ( ! is_file( $curfile ) ) {
668 $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
669 return;
670 }
671 $oldver = wfTimestampNow() . "!{$name}";
672
673 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
674 $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
675 return;
676 }
677 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
678 $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
679 return;
680 }
681
682 # Record upload and update metadata cache
683 $img = Image::newFromName( $name );
684 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
685
686 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
687 $wgOut->setRobotpolicy( 'noindex,nofollow' );
688 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
689
690 $descTitle = $img->getTitle();
691 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
692 }
693
694 /**
695 * Override handling of action=purge
696 */
697 function doPurge() {
698 $this->img = new Image( $this->mTitle );
699 if( $this->img->exists() ) {
700 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
701 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
702 $update->doUpdate();
703 $this->img->upgradeRow();
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 ?>