Fixed undefined defines warnings introduced in change 5131
[mediawiki.git] / includes / actions / RawAction.php
blob5615ad5221a38d6ca8b629ba3f885c6f23343c0d
1 <?php
2 /**
3 * Raw page text accessor
5 * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
6 * http://wikidev.net/
8 * Based on HistoryPage and SpecialExport
10 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
12 * @author Gabriel Wicke <wicke@wikidev.net>
13 * @file
16 /**
17 * A simple method to retrieve the plain source of an article,
18 * using "action=raw" in the GET request string.
20 class RawAction extends FormlessAction {
21 private $mGen;
23 public function getName() {
24 return 'raw';
27 public function requiresWrite() {
28 return false;
31 public function requiresUnblock() {
32 return false;
35 function onView() {
36 global $wgGroupPermissions, $wgSquidMaxage, $wgForcedRawSMaxage, $wgJsMimeType;
38 $this->getOutput()->disable();
39 $request = $this->getRequest();
41 if ( !$request->checkUrlExtension() ) {
42 return;
45 if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
46 return; // Client cache fresh and headers sent, nothing more to do.
49 # special case for 'generated' raw things: user css/js
50 # This is deprecated and will only return empty content
51 $gen = $request->getVal( 'gen' );
52 $smaxage = $request->getIntOrNull( 'smaxage' );
54 if ( $gen == 'css' || $gen == 'js' ) {
55 $this->mGen = $gen;
56 if ( $smaxage === null ) {
57 $smaxage = $wgSquidMaxage;
59 } else {
60 $this->mGen = false;
63 $contentType = $this->getContentType();
65 # Force caching for CSS and JS raw content, default: 5 minutes
66 if ( $smaxage === null ) {
67 if ( $contentType == 'text/css' || $contentType == $wgJsMimeType ) {
68 $smaxage = intval( $wgForcedRawSMaxage );
69 } else {
70 $smaxage = 0;
74 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
76 $response = $request->response();
78 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
79 # Output may contain user-specific data;
80 # vary generated content for open sessions on private wikis
81 $privateCache = !$wgGroupPermissions['*']['read'] && ( $smaxage == 0 || session_id() != '' );
82 # allow the client to cache this for 24 hours
83 $mode = $privateCache ? 'private' : 'public';
84 $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
86 $text = $this->getRawText();
88 if ( $text === false && $contentType == 'text/x-wiki' ) {
89 # Don't return a 404 response for CSS or JavaScript;
90 # 404s aren't generally cached and it would create
91 # extra hits when user CSS/JS are on and the user doesn't
92 # have the pages.
93 $response->header( 'HTTP/1.x 404 Not Found' );
96 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
97 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
100 echo $text;
104 * Get the text that should be returned, or false if the page or revision
105 * was not found.
107 * @return String|Bool
109 public function getRawText() {
110 global $wgParser;
112 # No longer used
113 if( $this->mGen ) {
114 return '';
117 $text = false;
118 $title = $this->getTitle();
119 $request = $this->getRequest();
121 // If it's a MediaWiki message we can just hit the message cache
122 if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
123 // The first "true" is to use the database, the second is to use the content langue
124 // and the last one is to specify the message key already contains the language in it ("/de", etc.)
125 $text = MessageCache::singleton()->get( $title->getDBkey(), true, true, true );
126 // If the message doesn't exist, return a blank
127 if ( $text === false ) {
128 $text = '';
130 } else {
131 // Get it from the DB
132 $rev = Revision::newFromTitle( $title, $this->getOldId() );
133 if ( $rev ) {
134 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
135 $request->response()->header( "Last-modified: $lastmod" );
137 // Public-only due to cache headers
138 $text = $rev->getText();
139 $section = $request->getIntOrNull( 'section' );
140 if ( $section !== null ) {
141 $text = $wgParser->getSection( $text, $section );
146 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
147 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
150 return $text;
154 * Get the ID of the revision that should used to get the text.
156 * @return Integer
158 public function getOldId() {
159 $oldid = $this->getRequest()->getInt( 'oldid' );
160 switch ( $this->getRequest()->getText( 'direction' ) ) {
161 case 'next':
162 # output next revision, or nothing if there isn't one
163 if( $oldid ) {
164 $oldid = $this->getTitle()->getNextRevisionId( $oldid );
166 $oldid = $oldid ? $oldid : -1;
167 break;
168 case 'prev':
169 # output previous revision, or nothing if there isn't one
170 if( !$oldid ) {
171 # get the current revision so we can get the penultimate one
172 $oldid = $this->page->getLatest();
174 $prev = $this->getTitle()->getPreviousRevisionId( $oldid );
175 $oldid = $prev ? $prev : -1 ;
176 break;
177 case 'cur':
178 $oldid = 0;
179 break;
181 return $oldid;
185 * Get the content type to use for the response
187 * @return String
189 public function getContentType() {
190 global $wgJsMimeType;
192 $ctype = $this->getRequest()->getVal( 'ctype' );
194 if ( $ctype == '' ) {
195 $gen = $this->getRequest()->getVal( 'gen' );
196 if ( $gen == 'js' ) {
197 $ctype = $wgJsMimeType;
198 } elseif ( $gen == 'css' ) {
199 $ctype = 'text/css';
203 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
204 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
205 $ctype = 'text/x-wiki';
208 return $ctype;
213 * Backward compatibility for extensions
215 * @deprecated in 1.19
217 class RawPage extends RawAction {
218 public $mOldId;
220 function __construct( Page $page, $request = false ) {
221 wfDeprecated( __CLASS__, '1.19' );
222 parent::__construct( $page );
224 if ( $request !== false ) {
225 $context = new DerivativeContext( $this->getContext() );
226 $context->setRequest( $request );
227 $this->context = $context;
231 public function view() {
232 $this->onView();
235 public function getOldId() {
236 # Some extensions like to set $mOldId
237 if ( $this->mOldId !== null ) {
238 return $this->mOldId;
240 return parent::getOldId();