MediaWiki  1.30.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  $gen = $request->getVal( 'gen' );
63  if ( $gen == 'css' || $gen == 'js' ) {
64  $this->gen = true;
65  }
66 
67  $contentType = $this->getContentType();
68 
69  $maxage = $request->getInt( 'maxage', $config->get( 'SquidMaxage' ) );
70  $smaxage = $request->getIntOrNull( 'smaxage' );
71  if ( $smaxage === null ) {
72  if ( $contentType == 'text/css' || $contentType == 'text/javascript' ) {
73  // CSS/JS raw content has its own CDN max age configuration.
74  // Note: Title::getCdnUrls() includes action=raw for css/js pages,
75  // so if using the canonical url, this will get HTCP purges.
76  $smaxage = intval( $config->get( 'ForcedRawSMaxage' ) );
77  } else {
78  // No CDN cache for anything else
79  $smaxage = 0;
80  }
81  }
82 
83  // Set standard Vary headers so cache varies on cookies and such (T125283)
84  $response->header( $this->getOutput()->getVaryHeader() );
85  if ( $config->get( 'UseKeyHeader' ) ) {
86  $response->header( $this->getOutput()->getKeyHeader() );
87  }
88 
89  $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
90  // Output may contain user-specific data;
91  // vary generated content for open sessions on private wikis
92  $privateCache = !User::isEveryoneAllowed( 'read' ) &&
93  ( $smaxage == 0 || MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() );
94  // Don't accidentally cache cookies if user is logged in (T55032)
95  $privateCache = $privateCache || $this->getUser()->isLoggedIn();
96  $mode = $privateCache ? 'private' : 'public';
97  $response->header(
98  'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage
99  );
100 
101  $text = $this->getRawText();
102 
103  // Don't return a 404 response for CSS or JavaScript;
104  // 404s aren't generally cached and it would create
105  // extra hits when user CSS/JS are on and the user doesn't
106  // have the pages.
107  if ( $text === false && $contentType == 'text/x-wiki' ) {
108  $response->statusHeader( 404 );
109  }
110 
111  // Avoid PHP 7.1 warning of passing $this by reference
112  $rawAction = $this;
113  if ( !Hooks::run( 'RawPageViewBeforeOutput', [ &$rawAction, &$text ] ) ) {
114  wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
115  }
116 
117  echo $text;
118  }
119 
126  public function getRawText() {
128 
129  $text = false;
130  $title = $this->getTitle();
131  $request = $this->getRequest();
132 
133  // If it's a MediaWiki message we can just hit the message cache
134  if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
135  // The first "true" is to use the database, the second is to use
136  // the content langue and the last one is to specify the message
137  // 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 = '';
142  }
143  } else {
144  // Get it from the DB
146  if ( $rev ) {
147  $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
148  $request->response()->header( "Last-modified: $lastmod" );
149 
150  // Public-only due to cache headers
151  $content = $rev->getContent();
152 
153  if ( $content === null ) {
154  // revision not found (or suppressed)
155  $text = false;
156  } elseif ( !$content instanceof TextContent ) {
157  // non-text content
158  wfHttpError( 415, "Unsupported Media Type", "The requested page uses the content model `"
159  . $content->getModel() . "` which is not supported via this interface." );
160  die();
161  } else {
162  // want a section?
163  $section = $request->getIntOrNull( 'section' );
164  if ( $section !== null ) {
165  $content = $content->getSection( $section );
166  }
167 
168  if ( $content === null || $content === false ) {
169  // section not found (or section not supported, e.g. for JS and CSS)
170  $text = false;
171  } else {
172  $text = $content->getNativeData();
173  }
174  }
175  }
176  }
177 
178  if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
179  $text = $wgParser->preprocess(
180  $text,
181  $title,
183  );
184  }
185 
186  return $text;
187  }
188 
194  public function getOldId() {
195  $oldid = $this->getRequest()->getInt( 'oldid' );
196  switch ( $this->getRequest()->getText( 'direction' ) ) {
197  case 'next':
198  # output next revision, or nothing if there isn't one
199  $nextid = 0;
200  if ( $oldid ) {
201  $nextid = $this->getTitle()->getNextRevisionID( $oldid );
202  }
203  $oldid = $nextid ?: -1;
204  break;
205  case 'prev':
206  # output previous revision, or nothing if there isn't one
207  if ( !$oldid ) {
208  # get the current revision so we can get the penultimate one
209  $oldid = $this->page->getLatest();
210  }
211  $previd = $this->getTitle()->getPreviousRevisionID( $oldid );
212  $oldid = $previd ?: -1;
213  break;
214  case 'cur':
215  $oldid = 0;
216  break;
217  }
218 
219  return $oldid;
220  }
221 
227  public function getContentType() {
228  $ctype = $this->getRequest()->getVal( 'ctype' );
229 
230  if ( $ctype == '' ) {
231  $gen = $this->getRequest()->getVal( 'gen' );
232  if ( $gen == 'js' ) {
233  $ctype = 'text/javascript';
234  } elseif ( $gen == 'css' ) {
235  $ctype = 'text/css';
236  }
237  }
238 
239  $allowedCTypes = [ 'text/x-wiki', 'text/javascript', 'text/css', 'application/x-zope-edit' ];
240  if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
241  $ctype = 'text/x-wiki';
242  }
243 
244  return $ctype;
245  }
246 }
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:194
RawAction\getName
getName()
Return the name of the action this object responds to.
Definition: RawAction.php:36
$wgParser
$wgParser
Definition: Setup.php:824
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:2040
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:126
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:134
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:227
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
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:1047
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:2581
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:106
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:781
User\isEveryoneAllowed
static isEveryoneAllowed( $right)
Check if all users may be assumed to have the given permission.
Definition: User.php:4811
wfHttpError
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1811
$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:2981
$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:1750
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:1965
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