PHPDocumentor [http://en.wikipedia.org/wiki/PhpDocumentor] documentation tweaking...
[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 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 $renderUrl = wfAppendQuery( $url, 'action=render' );
389 wfDebug( "Fetching shared description from $renderUrl\n" );
390 $text = Http::get( $renderUrl );
391 if ($text)
392 $this->mExtraDescription = $text;
393 }
394 }
395
396 function getUploadUrl() {
397 global $wgServer;
398 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
399 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
400 }
401
402 /**
403 * Print out the various links at the bottom of the image page, e.g. reupload,
404 * external editing (and instructions link) etc.
405 */
406 function uploadLinksBox() {
407 global $wgUser, $wgOut;
408
409 if( $this->img->fromSharedDirectory )
410 return;
411
412 $sk = $wgUser->getSkin();
413
414 $wgOut->addHtml( '<br /><ul>' );
415
416 # "Upload a new version of this file" link
417 if( $wgUser->isAllowed( 'reupload' ) ) {
418 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
419 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
420 }
421
422 # External editing link
423 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
424 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
425
426 $wgOut->addHtml( '</ul>' );
427 }
428
429 function closeShowImage()
430 {
431 # For overloading
432
433 }
434
435 /**
436 * If the page we've just displayed is in the "Image" namespace,
437 * we follow it with an upload history of the image and its usage.
438 */
439 function imageHistory()
440 {
441 global $wgUser, $wgOut, $wgUseExternalEditor;
442
443 $sk = $wgUser->getSkin();
444
445 $line = $this->img->nextHistoryLine();
446
447 if ( $line ) {
448 $list = new ImageHistoryList( $sk );
449 $s = $list->beginImageHistoryList() .
450 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
451 $this->mTitle->getDBkey(), $line->img_user,
452 $line->img_user_text, $line->img_size, $line->img_description,
453 $line->img_width, $line->img_height
454 );
455
456 while ( $line = $this->img->nextHistoryLine() ) {
457 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
458 $line->oi_archive_name, $line->img_user,
459 $line->img_user_text, $line->img_size, $line->img_description,
460 $line->img_width, $line->img_height
461 );
462 }
463 $s .= $list->endImageHistoryList();
464 } else { $s=''; }
465 $wgOut->addHTML( $s );
466
467 # Exist check because we don't want to show this on pages where an image
468 # doesn't exist along with the noimage message, that would suck. -ævar
469 if( $wgUseExternalEditor && $this->img->exists() ) {
470 $this->uploadLinksBox();
471 }
472
473 }
474
475 function imageLinks()
476 {
477 global $wgUser, $wgOut;
478
479 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
480
481 $dbr = wfGetDB( DB_SLAVE );
482 $page = $dbr->tableName( 'page' );
483 $imagelinks = $dbr->tableName( 'imagelinks' );
484
485 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
486 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
487 $sql = $dbr->limitResult($sql, 500, 0);
488 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
489
490 if ( 0 == $dbr->numRows( $res ) ) {
491 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
492 return;
493 }
494 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
495
496 $sk = $wgUser->getSkin();
497 while ( $s = $dbr->fetchObject( $res ) ) {
498 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
499 $link = $sk->makeKnownLinkObj( $name, "" );
500 $wgOut->addHTML( "<li>{$link}</li>\n" );
501 }
502 $wgOut->addHTML( "</ul>\n" );
503 }
504
505 function delete()
506 {
507 global $wgUser, $wgOut, $wgRequest;
508
509 $confirm = $wgRequest->wasPosted();
510 $reason = $wgRequest->getVal( 'wpReason' );
511 $image = $wgRequest->getVal( 'image' );
512 $oldimage = $wgRequest->getVal( 'oldimage' );
513
514 # Only sysops can delete images. Previously ordinary users could delete
515 # old revisions, but this is no longer the case.
516 if ( !$wgUser->isAllowed('delete') ) {
517 $wgOut->permissionRequired( 'delete' );
518 return;
519 }
520 if ( $wgUser->isBlocked() ) {
521 return $this->blockedIPpage();
522 }
523 if ( wfReadOnly() ) {
524 $wgOut->readOnlyPage();
525 return;
526 }
527
528 # Better double-check that it hasn't been deleted yet!
529 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
530 if ( ( !is_null( $image ) )
531 && ( '' == trim( $image ) ) ) {
532 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
533 return;
534 }
535
536 $this->img = new Image( $this->mTitle );
537
538 # Deleting old images doesn't require confirmation
539 if ( !is_null( $oldimage ) || $confirm ) {
540 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
541 $this->doDelete( $reason );
542 } else {
543 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
544 }
545 return;
546 }
547
548 if ( !is_null( $image ) ) {
549 $q = '&image=' . urlencode( $image );
550 } else if ( !is_null( $oldimage ) ) {
551 $q = '&oldimage=' . urlencode( $oldimage );
552 } else {
553 $q = '';
554 }
555 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
556 }
557
558 /*
559 * Delete an image.
560 * @param $reason User provided reason for deletion.
561 */
562 function doDelete( $reason ) {
563 global $wgOut, $wgRequest;
564
565 $oldimage = $wgRequest->getVal( 'oldimage' );
566
567 if ( !is_null( $oldimage ) ) {
568 if ( strlen( $oldimage ) < 16 ) {
569 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
570 return;
571 }
572 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
573 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
574 return;
575 }
576 if ( !$this->doDeleteOldImage( $oldimage ) ) {
577 return;
578 }
579 $deleted = $oldimage;
580 } else {
581 $ok = $this->img->delete( $reason );
582 if( !$ok ) {
583 # If the deletion operation actually failed, bug out:
584 $wgOut->showFileDeleteError( $this->img->getName() );
585 return;
586 }
587
588 # Image itself is now gone, and database is cleaned.
589 # Now we remove the image description page.
590
591 $article = new Article( $this->mTitle );
592 $article->doDeleteArticle( $reason ); # ignore errors
593
594 $deleted = $this->img->getName();
595 }
596
597 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
598 $wgOut->setRobotpolicy( 'noindex,nofollow' );
599
600 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
601 $text = wfMsg( 'deletedtext', $deleted, $loglink );
602
603 $wgOut->addWikiText( $text );
604
605 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
606 }
607
608 /**
609 * @return success
610 */
611 function doDeleteOldImage( $oldimage )
612 {
613 global $wgOut;
614
615 $ok = $this->img->deleteOld( $oldimage, '' );
616 if( !$ok ) {
617 # If we actually have a file and can't delete it, throw an error.
618 # Something went awry...
619 $wgOut->showFileDeleteError( "$oldimage" );
620 } else {
621 # Log the deletion
622 $log = new LogPage( 'delete' );
623 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
624 }
625 return $ok;
626 }
627
628 function revert() {
629 global $wgOut, $wgRequest, $wgUser;
630
631 $oldimage = $wgRequest->getText( 'oldimage' );
632 if ( strlen( $oldimage ) < 16 ) {
633 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
634 return;
635 }
636 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
637 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
638 return;
639 }
640
641 if ( wfReadOnly() ) {
642 $wgOut->readOnlyPage();
643 return;
644 }
645 if( $wgUser->isAnon() ) {
646 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
647 return;
648 }
649 if ( ! $this->mTitle->userCan( 'edit' ) ) {
650 $wgOut->readOnlyPage( $this->getContent(), true );
651 return;
652 }
653 if ( $wgUser->isBlocked() ) {
654 return $this->blockedIPpage();
655 }
656 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
657 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
658 return;
659 }
660 $name = substr( $oldimage, 15 );
661
662 $dest = wfImageDir( $name );
663 $archive = wfImageArchiveDir( $name );
664 $curfile = "{$dest}/{$name}";
665
666 if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
667 if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
668
669 if ( ! is_file( $curfile ) ) {
670 $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
671 return;
672 }
673 $oldver = wfTimestampNow() . "!{$name}";
674
675 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
676 $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
677 return;
678 }
679 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
680 $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
681 return;
682 }
683
684 # Record upload and update metadata cache
685 $img = Image::newFromName( $name );
686 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
687
688 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
689 $wgOut->setRobotpolicy( 'noindex,nofollow' );
690 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
691
692 $descTitle = $img->getTitle();
693 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
694 }
695
696 function blockedIPpage() {
697 $edit = new EditPage( $this );
698 return $edit->blockedIPpage();
699 }
700
701 /**
702 * Override handling of action=purge
703 */
704 function doPurge() {
705 $this->img = new Image( $this->mTitle );
706 if( $this->img->exists() ) {
707 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
708 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
709 $update->doUpdate();
710 $this->img->purgeCache();
711 } else {
712 wfDebug( "ImagePage::doPurge no image\n" );
713 }
714 parent::doPurge();
715 }
716
717 }
718
719 /**
720 * @todo document
721 */
722 class ImageHistoryList {
723 function ImageHistoryList( &$skin ) {
724 $this->skin =& $skin;
725 }
726
727 function beginImageHistoryList() {
728 $s = "\n" .
729 Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'imghistory' ) ) .
730 "\n<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
731 return $s;
732 }
733
734 function endImageHistoryList() {
735 $s = "</ul>\n";
736 return $s;
737 }
738
739 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
740 global $wgUser, $wgLang, $wgTitle, $wgContLang;
741
742 $datetime = $wgLang->timeanddate( $timestamp, true );
743 $del = wfMsgHtml( 'deleteimg' );
744 $delall = wfMsgHtml( 'deleteimgcompletely' );
745 $cur = wfMsgHtml( 'cur' );
746
747 if ( $iscur ) {
748 $url = Image::imageUrl( $img );
749 $rlink = $cur;
750 if ( $wgUser->isAllowed('delete') ) {
751 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
752 '&action=delete' );
753 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
754
755 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
756 } else {
757 $dlink = $del;
758 }
759 } else {
760 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
761 if( $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
762 $token = urlencode( $wgUser->editToken( $img ) );
763 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
764 wfMsgHtml( 'revertimg' ), 'action=revert&oldimage=' .
765 urlencode( $img ) . "&wpEditToken=$token" );
766 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
767 $del, 'action=delete&oldimage=' . urlencode( $img ) .
768 "&wpEditToken=$token" );
769 } else {
770 # Having live active links for non-logged in users
771 # means that bots and spiders crawling our site can
772 # inadvertently change content. Baaaad idea.
773 $rlink = wfMsgHtml( 'revertimg' );
774 $dlink = $del;
775 }
776 }
777
778 $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
779 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
780 $wgLang->formatNum( $size ) );
781 $widthheight = wfMsgHtml( 'widthheight', $width, $height );
782 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
783
784 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
785
786 $s .= $this->skin->commentBlock( $description, $wgTitle );
787 $s .= "</li>\n";
788 return $s;
789 }
790
791 }
792
793
794 ?>