For now revert the rev_deleted branch merge; don't want trunk to get too far gone...
[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 != '-' ) {
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 global $wgUseImageResize, $wgGenerateThumbnailOnParse;
170
171 $full_url = $this->img->getURL();
172 $anchoropen = '';
173 $anchorclose = '';
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 $this->img->selectPage( $page );
195 } else {
196 $page = 1;
197 }
198 $width_orig = $this->img->getWidth();
199 $width = $width_orig;
200 $height_orig = $this->img->getHeight();
201 $height = $height_orig;
202 $mime = $this->img->getMimeType();
203 $showLink = false;
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 if( $wgUseImageResize ) {
228 $thumbnail = $this->img->getThumbnail( $width, -1, $wgGenerateThumbnailOnParse );
229 if ( $thumbnail == null ) {
230 $url = $this->img->getViewURL();
231 } else {
232 $url = $thumbnail->getURL();
233 }
234 } else {
235 # No resize ability? Show the full image, but scale
236 # it down in the browser so it fits on the page.
237 $url = $this->img->getViewURL();
238 }
239 $anchoropen = "<a href=\"{$full_url}\">";
240 $anchorclose = "</a><br />";
241 if( $this->img->mustRender() ) {
242 $showLink = true;
243 } else {
244 $anchorclose .= wfMsg('show-big-image-thumb', $width, $height ) .
245 '<br />' . "\n$anchoropen{$msgbig}</a> " . $msgsize;
246 }
247 } else {
248 $url = $this->img->getViewURL();
249 $showLink = true;
250 }
251
252 if ( $this->img->isMultipage() ) {
253 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
254 }
255
256 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
257 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
258 htmlspecialchars( $this->img->getTitle()->getPrefixedText() ).'" />' . $anchorclose . '</div>' );
259
260 if ( $this->img->isMultipage() ) {
261 $count = $this->img->pageCount();
262
263 if ( $page > 1 ) {
264 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
265 $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
266 $this->img->selectPage( $page - 1 );
267 $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
268 } else {
269 $thumb1 = '';
270 }
271
272 if ( $page < $count ) {
273 $label = wfMsg( 'imgmultipagenext' );
274 $this->img->selectPage( $page + 1 );
275 $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
276 $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
277 } else {
278 $thumb2 = '';
279 }
280
281 global $wgScript;
282 $select = '<form name="pageselector" action="' .
283 htmlspecialchars( $wgScript ) .
284 '" method="get" onchange="document.pageselector.submit();">' .
285 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
286 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
287 ' <select id="pageselector" name="page">';
288 for ( $i=1; $i <= $count; $i++ ) {
289 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
290 $i == $page );
291 }
292 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
293 '<input type="submit" value="' .
294 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
295
296 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
297 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
298 }
299 } else {
300 #if direct link is allowed but it's not a renderable image, show an icon.
301 if ($this->img->isSafeFile()) {
302 $icon= $this->img->iconThumb();
303
304 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
305 $icon->toHtml() .
306 '</a></div>' );
307 }
308
309 $showLink = true;
310 }
311
312
313 if ($showLink) {
314 // Hacky workaround: for some reason we use the incorrect MIME type
315 // image/svg for SVG. This should be fixed internally, but at least
316 // make the displayed type right.
317 if ($mime == 'image/svg') $mime = 'image/svg+xml';
318
319 $filename = wfEscapeWikiText( $this->img->getName() );
320 $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
321 $infores = '';
322
323 // Check for MIME type. Other types may have more information in the future.
324 if (substr($mime,0,9) == 'image/svg' ) {
325 $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
326 } elseif ( substr($mime,0,5) == 'image' ) {
327 $infores = wfMsg('file-nohires') . '<br />';
328 $info = wfMsg(
329 'file-info-size',
330 $width_orig, $height_orig,
331 $sk->formatSize( $this->img->getSize() ),
332 $mime );
333 }
334
335 global $wgContLang;
336 $dirmark = $wgContLang->getDirMark();
337 if (!$this->img->isSafeFile()) {
338 $warning = wfMsg( 'mediawarning' );
339 $wgOut->addWikiText( <<<END
340 <div class="fullMedia">$infores
341 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
342 <span class="fileInfo"> $info</span>
343 </div>
344
345 <div class="mediaWarning">$warning</div>
346 END
347 );
348 } else {
349 $wgOut->addWikiText( <<<END
350 <div class="fullMedia">$infores
351 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
352 </div>
353 END
354 );
355 }
356 }
357
358 if($this->img->fromSharedDirectory) {
359 $this->printSharedImageText();
360 }
361 } else {
362 # Image does not exist
363
364 $title = SpecialPage::getTitleFor( 'Upload' );
365 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
366 'wpDestFile=' . urlencode( $this->img->getName() ) );
367 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
368 }
369 }
370
371 function printSharedImageText() {
372 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
373
374 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
375 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
376 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
377
378 $sk = $wgUser->getSkin();
379 $title = SpecialPage::getTitleFor( 'Upload' );
380 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
381 array( 'wpDestFile' => urlencode( $this->img->getName() )));
382 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
383 }
384 $sharedtext .= "</div>";
385 $wgOut->addHTML($sharedtext);
386
387 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
388 $text = Http::get($url . '?action=render');
389 if ($text)
390 $this->mExtraDescription = $text;
391 }
392 }
393
394 function getUploadUrl() {
395 global $wgServer;
396 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
397 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
398 }
399
400 /**
401 * Print out the various links at the bottom of the image page, e.g. reupload,
402 * external editing (and instructions link) etc.
403 */
404 function uploadLinksBox() {
405 global $wgUser, $wgOut;
406
407 if( $this->img->fromSharedDirectory )
408 return;
409
410 $sk = $wgUser->getSkin();
411
412 $wgOut->addHtml( '<br /><ul>' );
413
414 # "Upload a new version of this file" link
415 if( $wgUser->isAllowed( 'reupload' ) ) {
416 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
417 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
418 }
419
420 # External editing link
421 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
422 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
423
424 $wgOut->addHtml( '</ul>' );
425 }
426
427 function closeShowImage()
428 {
429 # For overloading
430
431 }
432
433 /**
434 * If the page we've just displayed is in the "Image" namespace,
435 * we follow it with an upload history of the image and its usage.
436 */
437 function imageHistory()
438 {
439 global $wgUser, $wgOut, $wgUseExternalEditor;
440
441 $sk = $wgUser->getSkin();
442
443 $line = $this->img->nextHistoryLine();
444
445 if ( $line ) {
446 $list = new ImageHistoryList( $sk );
447 $s = $list->beginImageHistoryList() .
448 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
449 $this->mTitle->getDBkey(), $line->img_user,
450 $line->img_user_text, $line->img_size, $line->img_description,
451 $line->img_width, $line->img_height
452 );
453
454 while ( $line = $this->img->nextHistoryLine() ) {
455 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
456 $line->oi_archive_name, $line->img_user,
457 $line->img_user_text, $line->img_size, $line->img_description,
458 $line->img_width, $line->img_height
459 );
460 }
461 $s .= $list->endImageHistoryList();
462 } else { $s=''; }
463 $wgOut->addHTML( $s );
464
465 # Exist check because we don't want to show this on pages where an image
466 # doesn't exist along with the noimage message, that would suck. -ævar
467 if( $wgUseExternalEditor && $this->img->exists() ) {
468 $this->uploadLinksBox();
469 }
470
471 }
472
473 function imageLinks()
474 {
475 global $wgUser, $wgOut;
476
477 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
478
479 $dbr = wfGetDB( DB_SLAVE );
480 $page = $dbr->tableName( 'page' );
481 $imagelinks = $dbr->tableName( 'imagelinks' );
482
483 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
484 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
485 $sql = $dbr->limitResult($sql, 500, 0);
486 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
487
488 if ( 0 == $dbr->numRows( $res ) ) {
489 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
490 return;
491 }
492 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
493
494 $sk = $wgUser->getSkin();
495 while ( $s = $dbr->fetchObject( $res ) ) {
496 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
497 $link = $sk->makeKnownLinkObj( $name, "" );
498 $wgOut->addHTML( "<li>{$link}</li>\n" );
499 }
500 $wgOut->addHTML( "</ul>\n" );
501 }
502
503 function delete()
504 {
505 global $wgUser, $wgOut, $wgRequest;
506
507 $confirm = $wgRequest->wasPosted();
508 $reason = $wgRequest->getVal( 'wpReason' );
509 $image = $wgRequest->getVal( 'image' );
510 $oldimage = $wgRequest->getVal( 'oldimage' );
511
512 # Only sysops can delete images. Previously ordinary users could delete
513 # old revisions, but this is no longer the case.
514 if ( !$wgUser->isAllowed('delete') ) {
515 $wgOut->permissionRequired( 'delete' );
516 return;
517 }
518 if ( $wgUser->isBlocked() ) {
519 return $this->blockedIPpage();
520 }
521 if ( wfReadOnly() ) {
522 $wgOut->readOnlyPage();
523 return;
524 }
525
526 # Better double-check that it hasn't been deleted yet!
527 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
528 if ( ( !is_null( $image ) )
529 && ( '' == trim( $image ) ) ) {
530 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
531 return;
532 }
533
534 $this->img = new Image( $this->mTitle );
535
536 # Deleting old images doesn't require confirmation
537 if ( !is_null( $oldimage ) || $confirm ) {
538 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
539 $this->doDelete( $reason );
540 } else {
541 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
542 }
543 return;
544 }
545
546 if ( !is_null( $image ) ) {
547 $q = '&image=' . urlencode( $image );
548 } else if ( !is_null( $oldimage ) ) {
549 $q = '&oldimage=' . urlencode( $oldimage );
550 } else {
551 $q = '';
552 }
553 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
554 }
555
556 /*
557 * Delete an image.
558 * @param $reason User provided reason for deletion.
559 */
560 function doDelete( $reason ) {
561 global $wgOut, $wgRequest;
562
563 $oldimage = $wgRequest->getVal( 'oldimage' );
564
565 if ( !is_null( $oldimage ) ) {
566 if ( strlen( $oldimage ) < 16 ) {
567 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
568 return;
569 }
570 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
571 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
572 return;
573 }
574 if ( !$this->doDeleteOldImage( $oldimage ) ) {
575 return;
576 }
577 $deleted = $oldimage;
578 } else {
579 $ok = $this->img->delete( $reason );
580 if( !$ok ) {
581 # If the deletion operation actually failed, bug out:
582 $wgOut->showFileDeleteError( $this->img->getName() );
583 return;
584 }
585
586 # Image itself is now gone, and database is cleaned.
587 # Now we remove the image description page.
588
589 $article = new Article( $this->mTitle );
590 $article->doDeleteArticle( $reason ); # ignore errors
591
592 $deleted = $this->img->getName();
593 }
594
595 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
596 $wgOut->setRobotpolicy( 'noindex,nofollow' );
597
598 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
599 $text = wfMsg( 'deletedtext', $deleted, $loglink );
600
601 $wgOut->addWikiText( $text );
602
603 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
604 }
605
606 /**
607 * @return success
608 */
609 function doDeleteOldImage( $oldimage )
610 {
611 global $wgOut;
612
613 $ok = $this->img->deleteOld( $oldimage, '' );
614 if( !$ok ) {
615 # If we actually have a file and can't delete it, throw an error.
616 # Something went awry...
617 $wgOut->showFileDeleteError( "$oldimage" );
618 } else {
619 # Log the deletion
620 $log = new LogPage( 'delete' );
621 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
622 }
623 return $ok;
624 }
625
626 function revert() {
627 global $wgOut, $wgRequest, $wgUser;
628
629 $oldimage = $wgRequest->getText( 'oldimage' );
630 if ( strlen( $oldimage ) < 16 ) {
631 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
632 return;
633 }
634 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
635 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
636 return;
637 }
638
639 if ( wfReadOnly() ) {
640 $wgOut->readOnlyPage();
641 return;
642 }
643 if( $wgUser->isAnon() ) {
644 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
645 return;
646 }
647 if ( ! $this->mTitle->userCan( 'edit' ) ) {
648 $wgOut->readOnlyPage( $this->getContent(), true );
649 return;
650 }
651 if ( $wgUser->isBlocked() ) {
652 return $this->blockedIPpage();
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 function blockedIPpage() {
695 $edit = new EditPage( $this );
696 return $edit->blockedIPpage();
697 }
698
699 /**
700 * Override handling of action=purge
701 */
702 function doPurge() {
703 $this->img = new Image( $this->mTitle );
704 if( $this->img->exists() ) {
705 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
706 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
707 $update->doUpdate();
708 $this->img->purgeCache();
709 } else {
710 wfDebug( "ImagePage::doPurge no image\n" );
711 }
712 parent::doPurge();
713 }
714
715 }
716
717 /**
718 * @todo document
719 */
720 class ImageHistoryList {
721 function ImageHistoryList( &$skin ) {
722 $this->skin =& $skin;
723 }
724
725 function beginImageHistoryList() {
726 $s = "\n" .
727 Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'imghistory' ) ) .
728 "\n<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
729 return $s;
730 }
731
732 function endImageHistoryList() {
733 $s = "</ul>\n";
734 return $s;
735 }
736
737 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
738 global $wgUser, $wgLang, $wgTitle, $wgContLang;
739
740 $datetime = $wgLang->timeanddate( $timestamp, true );
741 $del = wfMsgHtml( 'deleteimg' );
742 $delall = wfMsgHtml( 'deleteimgcompletely' );
743 $cur = wfMsgHtml( 'cur' );
744
745 if ( $iscur ) {
746 $url = Image::imageUrl( $img );
747 $rlink = $cur;
748 if ( $wgUser->isAllowed('delete') ) {
749 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
750 '&action=delete' );
751 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
752
753 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
754 } else {
755 $dlink = $del;
756 }
757 } else {
758 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
759 if( $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
760 $token = urlencode( $wgUser->editToken( $img ) );
761 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
762 wfMsgHtml( 'revertimg' ), 'action=revert&oldimage=' .
763 urlencode( $img ) . "&wpEditToken=$token" );
764 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
765 $del, 'action=delete&oldimage=' . urlencode( $img ) .
766 "&wpEditToken=$token" );
767 } else {
768 # Having live active links for non-logged in users
769 # means that bots and spiders crawling our site can
770 # inadvertently change content. Baaaad idea.
771 $rlink = wfMsgHtml( 'revertimg' );
772 $dlink = $del;
773 }
774 }
775
776 $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
777 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
778 $wgLang->formatNum( $size ) );
779 $widthheight = wfMsgHtml( 'widthheight', $width, $height );
780 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
781
782 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
783
784 $s .= $this->skin->commentBlock( $description, $wgTitle );
785 $s .= "</li>\n";
786 return $s;
787 }
788
789 }
790
791
792 ?>