MediaWiki  1.31.0
RawAction.php
Go to the documentation of this file.
1 <?php
35 class RawAction extends FormlessAction {
36  public function getName() {
37  return 'raw';
38  }
39 
40  public function requiresWrite() {
41  return false;
42  }
43 
44  public function requiresUnblock() {
45  return false;
46  }
47 
48  function onView() {
49  $this->getOutput()->disable();
50  $request = $this->getRequest();
51  $response = $request->response();
52  $config = $this->context->getConfig();
53 
54  if ( !$request->checkUrlExtension() ) {
55  return;
56  }
57 
58  if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
59  return; // Client cache fresh and headers sent, nothing more to do.
60  }
61 
62  $contentType = $this->getContentType();
63 
64  $maxage = $request->getInt( 'maxage', $config->get( 'SquidMaxage' ) );
65  $smaxage = $request->getIntOrNull( 'smaxage' );
66  if ( $smaxage === null ) {
67  if (
68  $contentType == 'text/css' ||
69  $contentType == 'application/json' ||
70  $contentType == 'text/javascript'
71  ) {
72  // CSS/JSON/JS raw content has its own CDN max age configuration.
73  // Note: Title::getCdnUrls() includes action=raw for css/json/js
74  // pages, so if using the canonical url, this will get HTCP purges.
75  $smaxage = intval( $config->get( 'ForcedRawSMaxage' ) );
76  } else {
77  // No CDN cache for anything else
78  $smaxage = 0;
79  }
80  }
81 
82  // Set standard Vary headers so cache varies on cookies and such (T125283)
83  $response->header( $this->getOutput()->getVaryHeader() );
84  if ( $config->get( 'UseKeyHeader' ) ) {
85  $response->header( $this->getOutput()->getKeyHeader() );
86  }
87 
88  $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
89  // Output may contain user-specific data;
90  // vary generated content for open sessions on private wikis
91  $privateCache = !User::isEveryoneAllowed( 'read' ) &&
92  ( $smaxage == 0 || MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() );
93  // Don't accidentally cache cookies if user is logged in (T55032)
94  $privateCache = $privateCache || $this->getUser()->isLoggedIn();
95  $mode = $privateCache ? 'private' : 'public';
96  $response->header(
97  'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage
98  );
99 
100  $text = $this->getRawText();
101 
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  if ( $text === false && $contentType == 'text/x-wiki' ) {
107  $response->statusHeader( 404 );
108  }
109 
110  // Avoid PHP 7.1 warning of passing $this by reference
111  $rawAction = $this;
112  if ( !Hooks::run( 'RawPageViewBeforeOutput', [ &$rawAction, &$text ] ) ) {
113  wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
114  }
115 
116  echo $text;
117  }
118 
125  public function getRawText() {
127 
128  $text = false;
129  $title = $this->getTitle();
130  $request = $this->getRequest();
131 
132  // If it's a MediaWiki message we can just hit the message cache
133  if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
134  // The first "true" is to use the database, the second is to use
135  // the content langue and the last one is to specify the message
136  // key already contains the language in it ("/de", etc.).
137  $text = MessageCache::singleton()->get( $title->getDBkey(), true, true, true );
138  // If the message doesn't exist, return a blank
139  if ( $text === false ) {
140  $text = '';
141  }
142  } else {
143  // Get it from the DB
145  if ( $rev ) {
146  $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
147  $request->response()->header( "Last-modified: $lastmod" );
148 
149  // Public-only due to cache headers
150  $content = $rev->getContent();
151 
152  if ( $content === null ) {
153  // revision not found (or suppressed)
154  $text = false;
155  } elseif ( !$content instanceof TextContent ) {
156  // non-text content
157  wfHttpError( 415, "Unsupported Media Type", "The requested page uses the content model `"
158  . $content->getModel() . "` which is not supported via this interface." );
159  die();
160  } else {
161  // want a section?
162  $section = $request->getIntOrNull( 'section' );
163  if ( $section !== null ) {
164  $content = $content->getSection( $section );
165  }
166 
167  if ( $content === null || $content === false ) {
168  // section not found (or section not supported, e.g. for JS, JSON, and CSS)
169  $text = false;
170  } else {
171  $text = $content->getNativeData();
172  }
173  }
174  }
175  }
176 
177  if ( $text !== false && $text !== '' && $request->getRawVal( 'templates' ) === 'expand' ) {
178  $text = $wgParser->preprocess(
179  $text,
180  $title,
182  );
183  }
184 
185  return $text;
186  }
187 
193  public function getOldId() {
194  $oldid = $this->getRequest()->getInt( 'oldid' );
195  switch ( $this->getRequest()->getText( 'direction' ) ) {
196  case 'next':
197  # output next revision, or nothing if there isn't one
198  $nextid = 0;
199  if ( $oldid ) {
200  $nextid = $this->getTitle()->getNextRevisionID( $oldid );
201  }
202  $oldid = $nextid ?: -1;
203  break;
204  case 'prev':
205  # output previous revision, or nothing if there isn't one
206  if ( !$oldid ) {
207  # get the current revision so we can get the penultimate one
208  $oldid = $this->page->getLatest();
209  }
210  $previd = $this->getTitle()->getPreviousRevisionID( $oldid );
211  $oldid = $previd ?: -1;
212  break;
213  case 'cur':
214  $oldid = 0;
215  break;
216  }
217 
218  return $oldid;
219  }
220 
226  public function getContentType() {
227  // Use getRawVal instead of getVal because we only
228  // need to match against known strings, there is no
229  // storing of localised content or other user input.
230  $ctype = $this->getRequest()->getRawVal( 'ctype' );
231 
232  if ( $ctype == '' ) {
233  // Legacy compatibilty
234  $gen = $this->getRequest()->getRawVal( 'gen' );
235  if ( $gen == 'js' ) {
236  $ctype = 'text/javascript';
237  } elseif ( $gen == 'css' ) {
238  $ctype = 'text/css';
239  }
240  }
241 
242  $allowedCTypes = [
243  'text/x-wiki',
244  'text/javascript',
245  'text/css',
246  // FIXME: Should we still allow Zope editing? External editing feature was dropped
247  'application/x-zope-edit',
248  'application/json'
249  ];
250  if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
251  $ctype = 'text/x-wiki';
252  }
253 
254  return $ctype;
255  }
256 }
RawAction
A simple method to retrieve the plain source of an article, using "action=raw" in the GET request str...
Definition: RawAction.php:35
RawAction\getOldId
getOldId()
Get the ID of the revision that should used to get the text.
Definition: RawAction.php:193
RawAction\getName
getName()
Return the name of the action this object responds to.
Definition: RawAction.php:36
$wgParser
$wgParser
Definition: Setup.php:909
FormlessAction
An action which just does something, without showing a form first.
Definition: FormlessAction.php:28
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:197
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1968
page
target page
Definition: All_system_messages.txt:1267
RawAction\getRawText
getRawText()
Get the text that should be returned, or false if the page or revision was not found.
Definition: RawAction.php:125
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:133
Action\getContext
getContext()
Get the IContextSource in use here.
Definition: Action.php:178
RawAction\getContentType
getContentType()
Get the content type to use for the response.
Definition: RawAction.php:226
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
RawAction\requiresUnblock
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: RawAction.php:44
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:982
MessageCache\singleton
static singleton()
Get the signleton instance of this class.
Definition: MessageCache.php:113
RawAction\onView
onView()
Show something on GET request.
Definition: RawAction.php:48
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2604
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:217
MediaWiki\Session\SessionManager\getGlobalSession
static getGlobalSession()
Get the "global" session.
Definition: SessionManager.php:107
ParserOptions\newFromContext
static newFromContext(IContextSource $context)
Get a ParserOptions object from a IContextSource object.
Definition: ParserOptions.php:1005
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:246
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
$response
this hook is for auditing only $response
Definition: hooks.txt:783
User\isEveryoneAllowed
static isEveryoneAllowed( $right)
Check if all users may be assumed to have the given permission.
Definition: User.php:4947
wfHttpError
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1739
$section
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition: hooks.txt:3005
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1767
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1987
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:207
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:73
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
RawAction\requiresWrite
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: RawAction.php:40