MediaWiki  REL1_19
RawAction.php
Go to the documentation of this file.
00001 <?php
00020 class RawAction extends FormlessAction {
00021         private $mGen;
00022 
00023         public function getName() {
00024                 return 'raw';
00025         }
00026 
00027         public function requiresWrite() {
00028                 return false;
00029         }
00030 
00031         public function requiresUnblock() {
00032                 return false;
00033         }
00034 
00035         function onView() {
00036                 global $wgGroupPermissions, $wgSquidMaxage, $wgForcedRawSMaxage, $wgJsMimeType;
00037 
00038                 $this->getOutput()->disable();
00039                 $request = $this->getRequest();
00040 
00041                 if ( !$request->checkUrlExtension() ) {
00042                         return;
00043                 }
00044 
00045                 if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
00046                         return; // Client cache fresh and headers sent, nothing more to do.
00047                 }
00048 
00049                 # special case for 'generated' raw things: user css/js
00050                 # This is deprecated and will only return empty content
00051                 $gen = $request->getVal( 'gen' );
00052                 $smaxage = $request->getIntOrNull( 'smaxage' );
00053 
00054                 if ( $gen == 'css' || $gen == 'js' ) {
00055                         $this->mGen = $gen;
00056                         if ( $smaxage === null ) {
00057                                 $smaxage = $wgSquidMaxage;
00058                         }
00059                 } else {
00060                         $this->mGen = false;
00061                 }
00062 
00063                 $contentType = $this->getContentType();
00064 
00065                 # Force caching for CSS and JS raw content, default: 5 minutes
00066                 if ( $smaxage === null ) {
00067                         if ( $contentType == 'text/css' || $contentType == $wgJsMimeType ) {
00068                                 $smaxage = intval( $wgForcedRawSMaxage );
00069                         } else {
00070                                 $smaxage = 0;
00071                         }
00072                 }
00073 
00074                 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
00075 
00076                 $response = $request->response();
00077 
00078                 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
00079                 # Output may contain user-specific data;
00080                 # vary generated content for open sessions on private wikis
00081                 $privateCache = !$wgGroupPermissions['*']['read'] && ( $smaxage == 0 || session_id() != '' );
00082                 // Bug 53032 - make this private if user is logged in,
00083                 // so we don't accidentally cache cookies
00084                 if ( !$privateCache ) {
00085                         $privateCache = $this->getUser()->isLoggedIn();
00086                 }
00087                 # allow the client to cache this for 24 hours
00088                 $mode = $privateCache ? 'private' : 'public';
00089                 $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
00090 
00091                 $text = $this->getRawText();
00092 
00093                 if ( $text === false && $contentType == 'text/x-wiki' ) {
00094                         # Don't return a 404 response for CSS or JavaScript;
00095                         # 404s aren't generally cached and it would create
00096                         # extra hits when user CSS/JS are on and the user doesn't
00097                         # have the pages.
00098                         $response->header( 'HTTP/1.x 404 Not Found' );
00099                 }
00100 
00101                 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
00102                         wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
00103                 }
00104 
00105                 echo $text;
00106         }
00107 
00114         public function getRawText() {
00115                 global $wgParser;
00116 
00117                 # No longer used
00118                 if( $this->mGen ) {
00119                         return '';
00120                 }
00121 
00122                 $text = false;
00123                 $title = $this->getTitle();
00124                 $request = $this->getRequest();
00125 
00126                 // If it's a MediaWiki message we can just hit the message cache
00127                 if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
00128                         $key = $title->getDBkey();
00129                         $msg = wfMessage( $key )->inContentLanguage();
00130                         # If the message doesn't exist, return a blank
00131                         $text = !$msg->exists() ? '' : $msg->plain();
00132                 } else {
00133                         // Get it from the DB
00134                         $rev = Revision::newFromTitle( $title, $this->getOldId() );
00135                         if ( $rev ) {
00136                                 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
00137                                 $request->response()->header( "Last-modified: $lastmod" );
00138 
00139                                 // Public-only due to cache headers
00140                                 $text = $rev->getText();
00141                                 $section = $request->getIntOrNull( 'section' );
00142                                 if ( $section !== null ) {
00143                                         $text = $wgParser->getSection( $text, $section );
00144                                 }
00145                         }
00146                 }
00147 
00148                 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
00149                         $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
00150                 }
00151 
00152                 return $text;
00153         }
00154 
00160         public function getOldId() {
00161                 $oldid = $this->getRequest()->getInt( 'oldid' );
00162                 switch ( $this->getRequest()->getText( 'direction' ) ) {
00163                         case 'next':
00164                                 # output next revision, or nothing if there isn't one
00165                                 if( $oldid ) {
00166                                         $oldid = $this->getTitle()->getNextRevisionId( $oldid );
00167                                 }
00168                                 $oldid = $oldid ? $oldid : -1;
00169                                 break;
00170                         case 'prev':
00171                                 # output previous revision, or nothing if there isn't one
00172                                 if( !$oldid ) {
00173                                         # get the current revision so we can get the penultimate one
00174                                         $oldid = $this->page->getLatest();
00175                                 }
00176                                 $prev = $this->getTitle()->getPreviousRevisionId( $oldid );
00177                                 $oldid = $prev ? $prev : -1 ;
00178                                 break;
00179                         case 'cur':
00180                                 $oldid = 0;
00181                                 break;
00182                 }
00183                 return $oldid;
00184         }
00185 
00191         public function getContentType() {
00192                 global $wgJsMimeType;
00193 
00194                 $ctype = $this->getRequest()->getVal( 'ctype' );
00195 
00196                 if ( $ctype == '' ) {
00197                         $gen = $this->getRequest()->getVal( 'gen' );
00198                         if ( $gen == 'js' ) {
00199                                 $ctype = $wgJsMimeType;
00200                         } elseif ( $gen == 'css' ) {
00201                                 $ctype = 'text/css';
00202                         }
00203                 }
00204 
00205                 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
00206                 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
00207                         $ctype = 'text/x-wiki';
00208                 }
00209 
00210                 return $ctype;
00211         }
00212 }
00213 
00219 class RawPage extends RawAction {
00220         public $mOldId;
00221 
00222         function __construct( Page $page, $request = false ) {
00223                 wfDeprecated( __CLASS__, '1.19' );
00224                 parent::__construct( $page );
00225 
00226                 if ( $request !== false ) {
00227                         $context = new DerivativeContext( $this->getContext() );
00228                         $context->setRequest( $request );
00229                         $this->context = $context;
00230                 }
00231         }
00232 
00233         public function view() {
00234                 $this->onView();
00235         }
00236 
00237         public function getOldId() {
00238                 # Some extensions like to set $mOldId
00239                 if ( $this->mOldId !== null ) {
00240                         return $this->mOldId;
00241                 }
00242                 return parent::getOldId();
00243         }
00244 }