Introduce a new hook that allows extensions to add to My Contributions
[mediawiki.git] / includes / actions / RawAction.php
blob174ca3f86cfeb3945b70ece6eb25c73afc54a425
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 * 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>
26 * @file
29 /**
30 * A simple method to retrieve the plain source of an article,
31 * using "action=raw" in the GET request string.
33 class RawAction extends FormlessAction {
34 private $mGen;
36 public function getName() {
37 return 'raw';
40 public function requiresWrite() {
41 return false;
44 public function requiresUnblock() {
45 return false;
48 function onView() {
49 global $wgGroupPermissions, $wgSquidMaxage, $wgForcedRawSMaxage, $wgJsMimeType;
51 $this->getOutput()->disable();
52 $request = $this->getRequest();
54 if ( !$request->checkUrlExtension() ) {
55 return;
58 if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
59 return; // Client cache fresh and headers sent, nothing more to do.
62 # special case for 'generated' raw things: user css/js
63 # This is deprecated and will only return empty content
64 $gen = $request->getVal( 'gen' );
65 $smaxage = $request->getIntOrNull( 'smaxage' );
67 if ( $gen == 'css' || $gen == 'js' ) {
68 $this->mGen = $gen;
69 if ( $smaxage === null ) {
70 $smaxage = $wgSquidMaxage;
72 } else {
73 $this->mGen = false;
76 $contentType = $this->getContentType();
78 # Force caching for CSS and JS raw content, default: 5 minutes
79 if ( $smaxage === null ) {
80 if ( $contentType == 'text/css' || $contentType == $wgJsMimeType ) {
81 $smaxage = intval( $wgForcedRawSMaxage );
82 } else {
83 $smaxage = 0;
87 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
89 $response = $request->response();
91 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
92 # Output may contain user-specific data;
93 # vary generated content for open sessions on private wikis
94 $privateCache = !$wgGroupPermissions['*']['read'] && ( $smaxage == 0 || session_id() != '' );
95 # allow the client to cache this for 24 hours
96 $mode = $privateCache ? 'private' : 'public';
97 $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
99 $text = $this->getRawText();
101 if ( $text === false && $contentType == 'text/x-wiki' ) {
102 # Don't return a 404 response for CSS or JavaScript;
103 # 404s aren't generally cached and it would create
104 # extra hits when user CSS/JS are on and the user doesn't
105 # have the pages.
106 $response->header( 'HTTP/1.x 404 Not Found' );
109 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
110 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
113 echo $text;
117 * Get the text that should be returned, or false if the page or revision
118 * was not found.
120 * @return String|Bool
122 public function getRawText() {
123 global $wgParser;
125 # No longer used
126 if( $this->mGen ) {
127 return '';
130 $text = false;
131 $title = $this->getTitle();
132 $request = $this->getRequest();
134 // If it's a MediaWiki message we can just hit the message cache
135 if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
136 // The first "true" is to use the database, the second is to use the content langue
137 // and the last one is to specify the message key already contains the language in it ("/de", etc.)
138 $text = MessageCache::singleton()->get( $title->getDBkey(), true, true, true );
139 // If the message doesn't exist, return a blank
140 if ( $text === false ) {
141 $text = '';
143 } else {
144 // Get it from the DB
145 $rev = Revision::newFromTitle( $title, $this->getOldId() );
146 if ( $rev ) {
147 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
148 $request->response()->header( "Last-modified: $lastmod" );
150 // Public-only due to cache headers
151 $text = $rev->getText();
152 $section = $request->getIntOrNull( 'section' );
153 if ( $section !== null ) {
154 $text = $wgParser->getSection( $text, $section );
159 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
160 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
163 return $text;
167 * Get the ID of the revision that should used to get the text.
169 * @return Integer
171 public function getOldId() {
172 $oldid = $this->getRequest()->getInt( 'oldid' );
173 switch ( $this->getRequest()->getText( 'direction' ) ) {
174 case 'next':
175 # output next revision, or nothing if there isn't one
176 if( $oldid ) {
177 $oldid = $this->getTitle()->getNextRevisionId( $oldid );
179 $oldid = $oldid ? $oldid : -1;
180 break;
181 case 'prev':
182 # output previous revision, or nothing if there isn't one
183 if( !$oldid ) {
184 # get the current revision so we can get the penultimate one
185 $oldid = $this->page->getLatest();
187 $prev = $this->getTitle()->getPreviousRevisionId( $oldid );
188 $oldid = $prev ? $prev : -1 ;
189 break;
190 case 'cur':
191 $oldid = 0;
192 break;
194 return $oldid;
198 * Get the content type to use for the response
200 * @return String
202 public function getContentType() {
203 global $wgJsMimeType;
205 $ctype = $this->getRequest()->getVal( 'ctype' );
207 if ( $ctype == '' ) {
208 $gen = $this->getRequest()->getVal( 'gen' );
209 if ( $gen == 'js' ) {
210 $ctype = $wgJsMimeType;
211 } elseif ( $gen == 'css' ) {
212 $ctype = 'text/css';
216 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
217 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
218 $ctype = 'text/x-wiki';
221 return $ctype;
226 * Backward compatibility for extensions
228 * @deprecated in 1.19
230 class RawPage extends RawAction {
231 public $mOldId;
233 function __construct( Page $page, $request = false ) {
234 wfDeprecated( __CLASS__, '1.19' );
235 parent::__construct( $page );
237 if ( $request !== false ) {
238 $context = new DerivativeContext( $this->getContext() );
239 $context->setRequest( $request );
240 $this->context = $context;
244 public function view() {
245 $this->onView();
248 public function getOldId() {
249 # Some extensions like to set $mOldId
250 if ( $this->mOldId !== null ) {
251 return $this->mOldId;
253 return parent::getOldId();