3 * Raw page text accessor
5 * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
8 * Based on HistoryPage and SpecialExport
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @author Gabriel Wicke <wicke@wikidev.net>
30 * A simple method to retrieve the plain source of an article,
31 * using "action=raw" in the GET request string.
35 class RawAction
extends FormlessAction
{
38 public function getName() {
42 public function requiresWrite() {
46 public function requiresUnblock() {
51 global $wgSquidMaxage, $wgForcedRawSMaxage;
53 $this->getOutput()->disable();
54 $request = $this->getRequest();
56 if ( !$request->checkUrlExtension() ) {
60 if ( $this->getOutput()->checkLastModified( $this->page
->getTouched() ) ) {
61 return; // Client cache fresh and headers sent, nothing more to do.
64 # special case for 'generated' raw things: user css/js
65 # This is deprecated and will only return empty content
66 $gen = $request->getVal( 'gen' );
67 $smaxage = $request->getIntOrNull( 'smaxage' );
69 if ( $gen == 'css' ||
$gen == 'js' ) {
71 if ( $smaxage === null ) {
72 $smaxage = $wgSquidMaxage;
78 $contentType = $this->getContentType();
80 # Force caching for CSS and JS raw content, default: 5 minutes
81 if ( $smaxage === null ) {
82 if ( $contentType == 'text/css' ||
$contentType == 'text/javascript' ) {
83 $smaxage = intval( $wgForcedRawSMaxage );
89 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
91 $response = $request->response();
93 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
94 # Output may contain user-specific data;
95 # vary generated content for open sessions on private wikis
96 $privateCache = !User
::isEveryoneAllowed( 'read' ) && ( $smaxage == 0 ||
session_id() != '' );
97 # allow the client to cache this for 24 hours
98 $mode = $privateCache ?
'private' : 'public';
99 $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
101 $text = $this->getRawText();
103 if ( $text === false && $contentType == 'text/x-wiki' ) {
104 # Don't return a 404 response for CSS or JavaScript;
105 # 404s aren't generally cached and it would create
106 # extra hits when user CSS/JS are on and the user doesn't
108 $response->header( 'HTTP/1.x 404 Not Found' );
111 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
112 wfDebug( __METHOD__
. ": RawPageViewBeforeOutput hook broke raw page output.\n" );
119 * Get the text that should be returned, or false if the page or revision
122 * @return String|Bool
124 public function getRawText() {
133 $title = $this->getTitle();
134 $request = $this->getRequest();
136 // If it's a MediaWiki message we can just hit the message cache
137 if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI
) {
138 // The first "true" is to use the database, the second is to use the content langue
139 // and the last one is to specify the message key already contains the language in it ("/de", etc.)
140 $text = MessageCache
::singleton()->get( $title->getDBkey(), true, true, true );
141 // If the message doesn't exist, return a blank
142 if ( $text === false ) {
146 // Get it from the DB
147 $rev = Revision
::newFromTitle( $title, $this->getOldId() );
149 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
150 $request->response()->header( "Last-modified: $lastmod" );
152 // Public-only due to cache headers
153 $content = $rev->getContent();
155 if ( $content === null ) {
156 // revision not found (or suppressed)
158 } elseif ( !$content instanceof TextContent
) {
160 wfHttpError( 415, "Unsupported Media Type", "The requested page uses the content model `"
161 . $content->getModel() . "` which is not supported via this interface." );
165 $section = $request->getIntOrNull( 'section' );
166 if ( $section !== null ) {
167 $content = $content->getSection( $section );
170 if ( $content === null ||
$content === false ) {
171 // section not found (or section not supported, e.g. for JS and CSS)
174 $text = $content->getNativeData();
180 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
181 $text = $wgParser->preprocess( $text, $title, ParserOptions
::newFromContext( $this->getContext() ) );
188 * Get the ID of the revision that should used to get the text.
192 public function getOldId() {
193 $oldid = $this->getRequest()->getInt( 'oldid' );
194 switch ( $this->getRequest()->getText( 'direction' ) ) {
196 # output next revision, or nothing if there isn't one
198 $oldid = $this->getTitle()->getNextRevisionID( $oldid );
200 $oldid = $oldid ?
$oldid : -1;
203 # output previous revision, or nothing if there isn't one
205 # get the current revision so we can get the penultimate one
206 $oldid = $this->page
->getLatest();
208 $prev = $this->getTitle()->getPreviousRevisionID( $oldid );
209 $oldid = $prev ?
$prev : -1;
219 * Get the content type to use for the response
223 public function getContentType() {
224 $ctype = $this->getRequest()->getVal( 'ctype' );
226 if ( $ctype == '' ) {
227 $gen = $this->getRequest()->getVal( 'gen' );
228 if ( $gen == 'js' ) {
229 $ctype = 'text/javascript';
230 } elseif ( $gen == 'css' ) {
235 $allowedCTypes = array( 'text/x-wiki', 'text/javascript', 'text/css', 'application/x-zope-edit' );
236 if ( $ctype == '' ||
!in_array( $ctype, $allowedCTypes ) ) {
237 $ctype = 'text/x-wiki';
245 * Backward compatibility for extensions
247 * @deprecated in 1.19
249 class RawPage
extends RawAction
{
254 * @param WebRequest|bool $request The WebRequest (default: false).
256 function __construct( Page
$page, $request = false ) {
257 wfDeprecated( __CLASS__
, '1.19' );
258 parent
::__construct( $page );
260 if ( $request !== false ) {
261 $context = new DerivativeContext( $this->getContext() );
262 $context->setRequest( $request );
263 $this->context
= $context;
267 public function view() {
271 public function getOldId() {
272 # Some extensions like to set $mOldId
273 if ( $this->mOldId
!== null ) {
274 return $this->mOldId
;
276 return parent
::getOldId();