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, $wgJsMimeType;
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 == $wgJsMimeType ) {
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
::groupHasPermission( '*', '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 global $wgJsMimeType;
226 $ctype = $this->getRequest()->getVal( 'ctype' );
228 if ( $ctype == '' ) {
229 $gen = $this->getRequest()->getVal( 'gen' );
230 if ( $gen == 'js' ) {
231 $ctype = $wgJsMimeType;
232 } elseif ( $gen == 'css' ) {
237 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
238 if ( $ctype == '' ||
!in_array( $ctype, $allowedCTypes ) ) {
239 $ctype = 'text/x-wiki';
247 * Backward compatibility for extensions
249 * @deprecated in 1.19
251 class RawPage
extends RawAction
{
256 * @param WebRequest|bool $request The WebRequest (default: false).
258 function __construct( Page
$page, $request = false ) {
259 wfDeprecated( __CLASS__
, '1.19' );
260 parent
::__construct( $page );
262 if ( $request !== false ) {
263 $context = new DerivativeContext( $this->getContext() );
264 $context->setRequest( $request );
265 $this->context
= $context;
269 public function view() {
273 public function getOldId() {
274 # Some extensions like to set $mOldId
275 if ( $this->mOldId
!== null ) {
276 return $this->mOldId
;
278 return parent
::getOldId();