filerepo: fixed mem leak for history lines and exposed repository name
[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 function __construct( $title ) {
22 parent::__construct( $title );
23 $this->img = wfFindFile( $this->mTitle );
24 if ( !$this->img ) {
25 $this->img = wfLocalFile( $this->mTitle );
26 }
27 }
28
29 /**
30 * Handler for action=render
31 * Include body text only; none of the image extras
32 */
33 function render() {
34 global $wgOut;
35 $wgOut->setArticleBodyOnly( true );
36 $wgOut->addSecondaryWikitext( $this->getContent() );
37 }
38
39 function view() {
40 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
41
42 $diff = $wgRequest->getVal( 'diff' );
43 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
44
45 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
46 return Article::view();
47
48 if ($wgShowEXIF && $this->img->exists()) {
49 $exif = $this->img->getExifData();
50 $showmeta = count($exif) ? true : false;
51 } else {
52 $exif = false;
53 $showmeta = false;
54 }
55
56 if ($this->img->exists())
57 $wgOut->addHTML($this->showTOC($showmeta));
58
59 $this->openShowImage();
60
61 # No need to display noarticletext, we use our own message, output in openShowImage()
62 if ( $this->getID() ) {
63 Article::view();
64 } else {
65 # Just need to set the right headers
66 $wgOut->setArticleFlag( true );
67 $wgOut->setRobotpolicy( 'index,follow' );
68 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
69 $this->viewUpdates();
70 }
71
72 # Show shared description, if needed
73 if ( $this->mExtraDescription ) {
74 $fol = wfMsg( 'shareddescriptionfollows' );
75 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
76 $wgOut->addWikiText( $fol );
77 }
78 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
79 }
80
81 $this->closeShowImage();
82 $this->imageHistory();
83 $this->imageLinks();
84
85 if ( $exif ) {
86 global $wgStylePath, $wgStyleVersion;
87 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
88 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
89 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
90 $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
91 $wgOut->addHTML(
92 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
93 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
94 }
95 }
96
97 /**
98 * Create the TOC
99 *
100 * @access private
101 *
102 * @param bool $metadata Whether or not to show the metadata link
103 * @return string
104 */
105 function showTOC( $metadata ) {
106 global $wgLang;
107 $r = '<ul id="filetoc">
108 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
109 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
110 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
111 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
112 </ul>';
113 return $r;
114 }
115
116 /**
117 * Make a table with metadata to be shown in the output page.
118 *
119 * @access private
120 *
121 * @param array $exif The array containing the EXIF data
122 * @return string
123 */
124 function makeMetadataTable( $exif ) {
125 $r = wfMsg( 'metadata-help' ) . "\n\n";
126 $r .= "{| id=mw_metadata class=mw_metadata\n";
127 $visibleFields = $this->visibleMetadataFields();
128 foreach( $exif as $k => $v ) {
129 $tag = strtolower( $k );
130 $msg = wfMsg( "exif-$tag" );
131 $class = "exif-$tag";
132 if( !in_array( $tag, $visibleFields ) ) {
133 $class .= ' collapsable';
134 }
135 $r .= "|- class=\"$class\"\n";
136 $r .= "!| $msg\n";
137 $r .= "|| $v\n";
138 }
139 $r .= '|}';
140 return $r;
141 }
142
143 /**
144 * Get a list of EXIF metadata items which should be displayed when
145 * the metadata table is collapsed.
146 *
147 * @return array of strings
148 * @access private
149 */
150 function visibleMetadataFields() {
151 $fields = array();
152 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
153 foreach( $lines as $line ) {
154 $matches = array();
155 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
156 $fields[] = $matches[1];
157 }
158 }
159 return $fields;
160 }
161
162 /**
163 * Overloading Article's getContent method.
164 *
165 * Omit noarticletext if sharedupload; text will be fetched from the
166 * shared upload server if possible.
167 */
168 function getContent() {
169 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
170 return '';
171 }
172 return Article::getContent();
173 }
174
175 function openShowImage() {
176 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
177
178 $full_url = $this->img->getURL();
179 $linkAttribs = false;
180 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
181 if( !isset( $wgImageLimits[$sizeSel] ) ) {
182 $sizeSel = User::getDefaultOption( 'imagesize' );
183
184 // The user offset might still be incorrect, specially if
185 // $wgImageLimits got changed (see bug #8858).
186 if( !isset( $wgImageLimits[$sizeSel] ) ) {
187 // Default to the first offset in $wgImageLimits
188 $sizeSel = 0;
189 }
190 }
191 $max = $wgImageLimits[$sizeSel];
192 $maxWidth = $max[0];
193 $maxHeight = $max[1];
194 $sk = $wgUser->getSkin();
195
196 if ( $this->img->exists() ) {
197 # image
198 $page = $wgRequest->getIntOrNull( 'page' );
199 if ( is_null( $page ) ) {
200 $params = array();
201 $page = 1;
202 } else {
203 $params = array( 'page' => $page );
204 }
205 $width_orig = $this->img->getWidth();
206 $width = $width_orig;
207 $height_orig = $this->img->getHeight();
208 $height = $height_orig;
209 $mime = $this->img->getMimeType();
210 $showLink = false;
211 $linkAttribs = array( 'href' => $full_url );
212
213 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
214
215 if ( $this->img->allowInlineDisplay() and $width and $height) {
216 # image
217
218 # "Download high res version" link below the image
219 $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
220 # We'll show a thumbnail of this image
221 if ( $width > $maxWidth || $height > $maxHeight ) {
222 # Calculate the thumbnail size.
223 # First case, the limiting factor is the width, not the height.
224 if ( $width / $height >= $maxWidth / $maxHeight ) {
225 $height = round( $height * $maxWidth / $width);
226 $width = $maxWidth;
227 # Note that $height <= $maxHeight now.
228 } else {
229 $newwidth = floor( $width * $maxHeight / $height);
230 $height = round( $height * $newwidth / $width );
231 $width = $newwidth;
232 # Note that $height <= $maxHeight now, but might not be identical
233 # because of rounding.
234 }
235 $msgbig = wfMsgHtml( 'show-big-image' );
236 $msgsmall = wfMsgExt( 'show-big-image-thumb',
237 array( 'parseinline' ), $width, $height );
238 } else {
239 # Image is small enough to show full size on image page
240 $msgbig = htmlspecialchars( $this->img->getName() );
241 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
242 }
243
244 $params['width'] = $width;
245 $thumbnail = $this->img->transform( $params );
246
247 $anchorclose = "<br />";
248 if( $this->img->mustRender() ) {
249 $showLink = true;
250 } else {
251 $anchorclose .=
252 $msgsmall .
253 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . ' ' . $msgsize;
254 }
255
256 if ( $this->img->isMultipage() ) {
257 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
258 }
259
260 $imgAttribs = array(
261 'border' => 0,
262 'alt' => $this->img->getTitle()->getPrefixedText()
263 );
264
265 if ( $thumbnail ) {
266 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
267 $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
268 $anchorclose . '</div>' );
269 }
270
271 if ( $this->img->isMultipage() ) {
272 $count = $this->img->pageCount();
273
274 if ( $page > 1 ) {
275 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
276 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
277 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
278 array( 'page' => $page - 1 ) );
279 } else {
280 $thumb1 = '';
281 }
282
283 if ( $page < $count ) {
284 $label = wfMsg( 'imgmultipagenext' );
285 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
286 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
287 array( 'page' => $page + 1 ) );
288 } else {
289 $thumb2 = '';
290 }
291
292 global $wgScript;
293 $select = '<form name="pageselector" action="' .
294 htmlspecialchars( $wgScript ) .
295 '" method="get" onchange="document.pageselector.submit();">' .
296 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
297 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
298 ' <select id="pageselector" name="page">';
299 for ( $i=1; $i <= $count; $i++ ) {
300 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
301 $i == $page );
302 }
303 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
304 '<input type="submit" value="' .
305 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
306
307 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
308 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
309 }
310 } else {
311 #if direct link is allowed but it's not a renderable image, show an icon.
312 if ($this->img->isSafeFile()) {
313 $icon= $this->img->iconThumb();
314
315 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
316 $icon->toHtml() .
317 '</a></div>' );
318 }
319
320 $showLink = true;
321 }
322
323
324 if ($showLink) {
325 // Workaround for incorrect MIME type on SVGs uploaded in previous versions
326 if ($mime == 'image/svg') $mime = 'image/svg+xml';
327
328 $filename = wfEscapeWikiText( $this->img->getName() );
329 $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
330 $infores = '';
331
332 // Check for MIME type. Other types may have more information in the future.
333 if (substr($mime,0,9) == 'image/svg' ) {
334 $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
335 }
336
337 global $wgContLang;
338 $dirmark = $wgContLang->getDirMark();
339 if (!$this->img->isSafeFile()) {
340 $warning = wfMsg( 'mediawarning' );
341 $wgOut->addWikiText( <<<EOT
342 <div class="fullMedia">$infores
343 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
344 <span class="fileInfo"> $info</span>
345 </div>
346
347 <div class="mediaWarning">$warning</div>
348 EOT
349 );
350 } else {
351 $wgOut->addWikiText( <<<EOT
352 <div class="fullMedia">$infores
353 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
354 </div>
355 EOT
356 );
357 }
358 }
359
360 if(!$this->img->isLocal()) {
361 $this->printSharedImageText();
362 }
363 } else {
364 # Image does not exist
365
366 $title = SpecialPage::getTitleFor( 'Upload' );
367 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
368 'wpDestFile=' . urlencode( $this->img->getName() ) );
369 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
370 }
371 }
372
373 function printSharedImageText() {
374 global $wgOut, $wgUser;
375
376 $descUrl = $this->img->getDescriptionUrl();
377 $descText = $this->img->getDescriptionText();
378 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
379 if ( $descUrl && !$descText) {
380 $sk = $wgUser->getSkin();
381 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
382 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
383 }
384 $s .= "</div>";
385 $wgOut->addHTML($s);
386
387 if ( $descText ) {
388 $this->mExtraDescription = $descText;
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->isLocal() )
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, $this->img );
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 $this->img->resetHistory(); // free db resources
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 if ( !$this->img->exists() || !$this->img->isLocal() ) {
508 # Use standard article deletion
509 Article::delete();
510 return;
511 }
512
513 $confirm = $wgRequest->wasPosted();
514 $reason = $wgRequest->getVal( 'wpReason' );
515 $image = $wgRequest->getVal( 'image' );
516 $oldimage = $wgRequest->getVal( 'oldimage' );
517
518 # Only sysops can delete images. Previously ordinary users could delete
519 # old revisions, but this is no longer the case.
520 if ( !$wgUser->isAllowed('delete') ) {
521 $wgOut->permissionRequired( 'delete' );
522 return;
523 }
524 if ( $wgUser->isBlocked() ) {
525 $wgOut->blockedPage();
526 return;
527 }
528 if ( wfReadOnly() ) {
529 $wgOut->readOnlyPage();
530 return;
531 }
532
533 # Better double-check that it hasn't been deleted yet!
534 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
535 if ( ( !is_null( $image ) )
536 && ( '' == trim( $image ) ) ) {
537 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
538 return;
539 }
540
541 # Deleting old images doesn't require confirmation
542 if ( !is_null( $oldimage ) || $confirm ) {
543 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
544 $this->doDeleteImage( $reason );
545 } else {
546 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
547 }
548 return;
549 }
550
551 if ( !is_null( $image ) ) {
552 $q = '&image=' . urlencode( $image );
553 } else if ( !is_null( $oldimage ) ) {
554 $q = '&oldimage=' . urlencode( $oldimage );
555 } else {
556 $q = '';
557 }
558 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
559 }
560
561 /*
562 * Delete an image.
563 * Called doDeleteImage() not doDelete() so that Article::delete() doesn't
564 * call back to here.
565 *
566 * @param $reason User provided reason for deletion.
567 */
568 function doDeleteImage( $reason ) {
569 global $wgOut, $wgRequest;
570
571 $oldimage = $wgRequest->getVal( 'oldimage' );
572
573 if ( !is_null( $oldimage ) ) {
574 if ( strlen( $oldimage ) < 16 ) {
575 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
576 return;
577 }
578 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
579 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
580 return;
581 }
582 if ( !$this->doDeleteOldImage( $oldimage ) ) {
583 return;
584 }
585 $deleted = $oldimage;
586 } else {
587 $ok = $this->img->delete( $reason );
588 if( !$ok ) {
589 # If the deletion operation actually failed, bug out:
590 $wgOut->showFileDeleteError( $this->img->getName() );
591 return;
592 }
593
594 # Image itself is now gone, and database is cleaned.
595 # Now we remove the image description page.
596
597 $article = new Article( $this->mTitle );
598 $article->doDeleteArticle( $reason ); # ignore errors
599
600 $deleted = $this->img->getName();
601 }
602
603 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
604 $wgOut->setRobotpolicy( 'noindex,nofollow' );
605
606 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
607 $text = wfMsg( 'deletedtext', $deleted, $loglink );
608
609 $wgOut->addWikiText( $text );
610
611 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
612 }
613
614 /**
615 * @return success
616 */
617 function doDeleteOldImage( $oldimage )
618 {
619 global $wgOut;
620
621 $ok = $this->img->deleteOld( $oldimage, '' );
622 if( !$ok ) {
623 # If we actually have a file and can't delete it, throw an error.
624 # Something went awry...
625 $wgOut->showFileDeleteError( "$oldimage" );
626 } else {
627 # Log the deletion
628 $log = new LogPage( 'delete' );
629 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
630 }
631 return $ok;
632 }
633
634 function revert() {
635 global $wgOut, $wgRequest, $wgUser;
636
637 $oldimage = $wgRequest->getText( 'oldimage' );
638 if ( strlen( $oldimage ) < 16 ) {
639 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
640 return;
641 }
642 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
643 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
644 return;
645 }
646
647 if ( wfReadOnly() ) {
648 $wgOut->readOnlyPage();
649 return;
650 }
651 if( $wgUser->isAnon() ) {
652 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
653 return;
654 }
655 if ( ! $this->mTitle->userCan( 'edit' ) ) {
656 $wgOut->readOnlyPage( $this->getContent(), true );
657 return;
658 }
659 if ( $wgUser->isBlocked() ) {
660 $wgOut->blockedPage();
661 return;
662 }
663 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
664 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
665 return;
666 }
667
668 $sourcePath = $this->img->getArchiveVirtualUrl( $oldimage );
669 $comment = wfMsg( "reverted" );
670 $result = $this->img->upload( $sourcePath, $comment, $comment );
671
672 if ( WikiError::isError( $result ) ) {
673 $this->showError( $result );
674 return;
675 }
676
677 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
678 $wgOut->setRobotpolicy( 'noindex,nofollow' );
679 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
680
681 $descTitle = $this->img->getTitle();
682 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
683 }
684
685 /**
686 * Override handling of action=purge
687 */
688 function doPurge() {
689 if( $this->img->exists() ) {
690 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
691 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
692 $update->doUpdate();
693 $this->img->upgradeRow();
694 $this->img->purgeCache();
695 } else {
696 wfDebug( "ImagePage::doPurge no image\n" );
697 }
698 parent::doPurge();
699 }
700
701 /**
702 * Display an error from a wikitext-formatted WikiError object
703 */
704 function showError( WikiError $error ) {
705 global $wgOut;
706 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
707 $wgOut->setRobotpolicy( "noindex,nofollow" );
708 $wgOut->setArticleRelated( false );
709 $wgOut->enableClientCache( false );
710 $wgOut->addWikiText( $error->getMessage() );
711 }
712
713 }
714
715 /**
716 * @todo document
717 * @addtogroup Media
718 */
719 class ImageHistoryList {
720 var $img, $skin;
721 function ImageHistoryList( $skin, $img ) {
722 $this->skin = $skin;
723 $this->img = $img;
724 }
725
726 function beginImageHistoryList() {
727 $s = "\n" .
728 Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'imghistory' ) ) .
729 "\n<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
730 return $s;
731 }
732
733 function endImageHistoryList() {
734 $s = "</ul>\n";
735 return $s;
736 }
737
738 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
739 global $wgUser, $wgLang, $wgTitle, $wgContLang;
740
741 $datetime = $wgLang->timeanddate( $timestamp, true );
742 $del = wfMsgHtml( 'deleteimg' );
743 $delall = wfMsgHtml( 'deleteimgcompletely' );
744 $cur = wfMsgHtml( 'cur' );
745 $local = $this->img->isLocal();
746
747 if ( $iscur ) {
748 $url = htmlspecialchars( $this->img->getURL() );
749 $rlink = $cur;
750 if ( $local && $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( $this->img->getArchiveUrl( $img ) );
761 if( $local && $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 if ( $local ) {
779 $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
780 } else {
781 $userlink = htmlspecialchars( $usertext );
782 }
783 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
784 $wgLang->formatNum( $size ) );
785 $widthheight = wfMsgHtml( 'widthheight', $width, $height );
786 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
787
788 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
789
790 $s .= $this->skin->commentBlock( $description, $wgTitle );
791 $s .= "</li>\n";
792 return $s;
793 }
794 }
795
796
797