r100333 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100332‎ | r100333 | r100334 >
Date:08:10, 20 October 2011
Author:ialex
Status:resolved
Tags:
Comment:
* Changed action=raw to use an Action subclass
* Left RawPage with back compat code for extensions that use it
* Removed calls to Skin for gen=(css|js). Calling action=raw&gen=(css|js) will now unconditionally return empty content
* Removed Skin::generateUserJs() and Skin::generateUserStylesheet() since they were only used in RawPage
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/RawPage.php (deleted) (history)
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/Wiki.php (modified) (history)
  • /trunk/phase3/includes/actions/RawAction.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/RawPage.php
@@ -1,217 +0,0 @@
2 -<?php
3 -/**
4 - * Raw page text accessor
5 - *
6 - * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
7 - * http://wikidev.net/
8 - *
9 - * Based on HistoryPage and SpecialExport
10 - *
11 - * License: GPL (http://www.gnu.org/copyleft/gpl.html)
12 - *
13 - * @author Gabriel Wicke <wicke@wikidev.net>
14 - * @file
15 - */
16 -
17 -/**
18 - * A simple method to retrieve the plain source of an article,
19 - * using "action=raw" in the GET request string.
20 - */
21 -class RawPage {
22 - var $mArticle, $mTitle, $mRequest;
23 - var $mOldId, $mGen, $mCharset, $mSection;
24 - var $mSmaxage, $mMaxage;
25 - var $mContentType, $mExpandTemplates;
26 -
27 - function __construct( Page $article, $request = false ) {
28 - global $wgRequest, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
29 -
30 - $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
31 - $this->mArticle = $article;
32 - $this->mTitle = $article->getTitle();
33 -
34 - if( $request === false ) {
35 - $this->mRequest = $wgRequest;
36 - } else {
37 - $this->mRequest = $request;
38 - }
39 -
40 - $ctype = $this->mRequest->getVal( 'ctype' );
41 - $smaxage = $this->mRequest->getIntOrNull( 'smaxage' );
42 - $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
43 -
44 - $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
45 - $this->mUseMessageCache = $this->mRequest->getBool( 'usemsgcache' );
46 -
47 - $this->mSection = $this->mRequest->getIntOrNull( 'section' );
48 -
49 - $oldid = $this->mRequest->getInt( 'oldid' );
50 -
51 - switch( $wgRequest->getText( 'direction' ) ) {
52 - case 'next':
53 - # output next revision, or nothing if there isn't one
54 - if( $oldid ) {
55 - $oldid = $this->mTitle->getNextRevisionId( $oldid );
56 - }
57 - $oldid = $oldid ? $oldid : -1;
58 - break;
59 - case 'prev':
60 - # output previous revision, or nothing if there isn't one
61 - if( !$oldid ) {
62 - # get the current revision so we can get the penultimate one
63 - $this->mArticle->getTouched();
64 - $oldid = $this->mArticle->getLatest();
65 - }
66 - $prev = $this->mTitle->getPreviousRevisionId( $oldid );
67 - $oldid = $prev ? $prev : -1 ;
68 - break;
69 - case 'cur':
70 - $oldid = 0;
71 - break;
72 - }
73 - $this->mOldId = $oldid;
74 -
75 - # special case for 'generated' raw things: user css/js
76 - $gen = $this->mRequest->getVal( 'gen' );
77 -
78 - if( $gen == 'css' ) {
79 - $this->mGen = $gen;
80 - if( is_null( $smaxage ) ) {
81 - $smaxage = $wgSquidMaxage;
82 - }
83 - if( $ctype == '' ) {
84 - $ctype = 'text/css';
85 - }
86 - } elseif( $gen == 'js' ) {
87 - $this->mGen = $gen;
88 - if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
89 - if($ctype == '') $ctype = $wgJsMimeType;
90 - } else {
91 - $this->mGen = false;
92 - }
93 - $this->mCharset = 'UTF-8';
94 -
95 - # Force caching for CSS and JS raw content, default: 5 minutes
96 - if( is_null( $smaxage ) && ( $ctype == 'text/css' || $ctype == $wgJsMimeType ) ) {
97 - global $wgForcedRawSMaxage;
98 - $this->mSmaxage = intval( $wgForcedRawSMaxage );
99 - } else {
100 - $this->mSmaxage = intval( $smaxage );
101 - }
102 - $this->mMaxage = $maxage;
103 -
104 - # Output may contain user-specific data;
105 - # vary generated content for open sessions and private wikis
106 - if( $this->mGen || !$wgGroupPermissions['*']['read'] ) {
107 - $this->mPrivateCache = $this->mSmaxage == 0 || session_id() != '';
108 - } else {
109 - $this->mPrivateCache = false;
110 - }
111 -
112 - if( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
113 - $this->mContentType = 'text/x-wiki';
114 - } else {
115 - $this->mContentType = $ctype;
116 - }
117 - }
118 -
119 - function view() {
120 - global $wgOut, $wgRequest;
121 -
122 - if( !$wgRequest->checkUrlExtension() ) {
123 - $wgOut->disable();
124 - return;
125 - }
126 -
127 - header( 'Content-type: ' . $this->mContentType . '; charset=' . $this->mCharset );
128 - # allow the client to cache this for 24 hours
129 - $mode = $this->mPrivateCache ? 'private' : 'public';
130 - header( 'Cache-Control: ' . $mode . ', s-maxage=' . $this->mSmaxage . ', max-age=' . $this->mMaxage );
131 -
132 - $text = $this->getRawText();
133 -
134 - if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
135 - wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
136 - }
137 -
138 - echo $text;
139 - $wgOut->disable();
140 - }
141 -
142 - function getRawText() {
143 - global $wgOut, $wgUser;
144 - if( $this->mGen ) {
145 - $sk = $wgUser->getSkin();
146 - if( !StubObject::isRealObject( $wgOut ) ) {
147 - $wgOut->_unstub( 2 );
148 - }
149 - $sk->initPage( $wgOut );
150 - if( $this->mGen == 'css' ) {
151 - return $sk->generateUserStylesheet();
152 - } elseif( $this->mGen == 'js' ) {
153 - return $sk->generateUserJs();
154 - }
155 - } else {
156 - return $this->getArticleText();
157 - }
158 - return '';
159 - }
160 -
161 - function getArticleText() {
162 - $found = false;
163 - $text = '';
164 - if( $this->mTitle ) {
165 - // If it's a MediaWiki message we can just hit the message cache
166 - if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
167 - $key = $this->mTitle->getDBkey();
168 - $msg = wfMessage( $key )->inContentLanguage();
169 - # If the message doesn't exist, return a blank
170 - $text = !$msg->exists() ? '' : $msg->plain();
171 - $found = true;
172 - } else {
173 - // Get it from the DB
174 - $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
175 - if( $rev ) {
176 - $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
177 - header( "Last-modified: $lastmod" );
178 -
179 - if( !is_null( $this->mSection ) ) {
180 - global $wgParser;
181 - $text = $wgParser->getSection( $rev->getText(), $this->mSection );
182 - } else {
183 - $text = $rev->getText();
184 - }
185 - $found = true;
186 - }
187 - }
188 - }
189 -
190 - # Bad title or page does not exist
191 - if( !$found && $this->mContentType == 'text/x-wiki' ) {
192 - # Don't return a 404 response for CSS or JavaScript;
193 - # 404s aren't generally cached and it would create
194 - # extra hits when user CSS/JS are on and the user doesn't
195 - # have the pages.
196 - header( 'HTTP/1.0 404 Not Found' );
197 - }
198 -
199 - return $this->parseArticleText( $text );
200 - }
201 -
202 - /**
203 - * @param $text
204 - * @return string
205 - */
206 - function parseArticleText( $text ) {
207 - if( $text === '' ) {
208 - return '';
209 - } else {
210 - if( $this->mExpandTemplates ) {
211 - global $wgParser;
212 - return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
213 - } else {
214 - return $text;
215 - }
216 - }
217 - }
218 -}
Index: trunk/phase3/includes/actions/RawAction.php
@@ -0,0 +1,229 @@
 2+<?php
 3+/**
 4+ * Raw page text accessor
 5+ *
 6+ * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
 7+ * http://wikidev.net/
 8+ *
 9+ * Based on HistoryPage and SpecialExport
 10+ *
 11+ * License: GPL (http://www.gnu.org/copyleft/gpl.html)
 12+ *
 13+ * @author Gabriel Wicke <wicke@wikidev.net>
 14+ * @file
 15+ */
 16+
 17+/**
 18+ * A simple method to retrieve the plain source of an article,
 19+ * using "action=raw" in the GET request string.
 20+ */
 21+class RawAction extends FormlessAction {
 22+ private $mGen;
 23+
 24+ public function getName() {
 25+ return 'raw';
 26+ }
 27+
 28+ public function getRestriction() {
 29+ return 'read';
 30+ }
 31+
 32+ function onView() {
 33+ global $wgGroupPermissions, $wgSquidMaxage, $wgForcedRawSMaxage, $wgJsMimeType;
 34+
 35+ $this->getOutput()->disable();
 36+ $request = $this->getRequest();
 37+
 38+ if ( !$request->checkUrlExtension() ) {
 39+ return;
 40+ }
 41+
 42+ # special case for 'generated' raw things: user css/js
 43+ # This is deprecated and will only return empty content
 44+ $gen = $request->getVal( 'gen' );
 45+ $smaxage = $request->getIntOrNull( 'smaxage' );
 46+
 47+ if ( $gen == 'css' || $gen == 'js' ) {
 48+ $this->mGen = $gen;
 49+ if ( $smaxage === null ) {
 50+ $smaxage = $wgSquidMaxage;
 51+ }
 52+ } else {
 53+ $this->mGen = false;
 54+ }
 55+
 56+ $contentType = $this->getContentType();
 57+
 58+ # Force caching for CSS and JS raw content, default: 5 minutes
 59+ if ( $smaxage === null ) {
 60+ if ( $contentType == 'text/css' || $contentType == $wgJsMimeType ) {
 61+ $smaxage = intval( $wgForcedRawSMaxage );
 62+ } else {
 63+ $smaxage = 0;
 64+ }
 65+ }
 66+
 67+ $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
 68+
 69+ $response = $request->response();
 70+
 71+ $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
 72+ # Output may contain user-specific data;
 73+ # vary generated content for open sessions on private wikis
 74+ $privateCache = !$wgGroupPermissions['*']['read'] && ( $smaxage == 0 || session_id() != '' );
 75+ # allow the client to cache this for 24 hours
 76+ $mode = $privateCache ? 'private' : 'public';
 77+ $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
 78+
 79+ $text = $this->getRawText();
 80+
 81+ if ( $text === false && $contentType == 'text/x-wiki' ) {
 82+ # Don't return a 404 response for CSS or JavaScript;
 83+ # 404s aren't generally cached and it would create
 84+ # extra hits when user CSS/JS are on and the user doesn't
 85+ # have the pages.
 86+ $response->header( 'HTTP/1.x 404 Not Found' );
 87+ }
 88+
 89+ if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
 90+ wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
 91+ }
 92+
 93+ echo $text;
 94+ }
 95+
 96+ /**
 97+ * Get the text that should be returned, or false if the page or revision
 98+ * was not found.
 99+ *
 100+ * @return String|Bool
 101+ */
 102+ public function getRawText() {
 103+ global $wgParser;
 104+
 105+ # No longer used
 106+ if( $this->mGen ) {
 107+ return '';
 108+ }
 109+
 110+ $text = false;
 111+ $title = $this->getTitle();
 112+ $request = $this->getRequest();
 113+
 114+ // If it's a MediaWiki message we can just hit the message cache
 115+ if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
 116+ $key = $title->getDBkey();
 117+ $msg = wfMessage( $key )->inContentLanguage();
 118+ # If the message doesn't exist, return a blank
 119+ $text = !$msg->exists() ? '' : $msg->plain();
 120+ } else {
 121+ // Get it from the DB
 122+ $rev = Revision::newFromTitle( $title, $this->getOldId() );
 123+ if ( $rev ) {
 124+ $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
 125+ $request->response()->header( "Last-modified: $lastmod" );
 126+
 127+ $text = $rev->getText();
 128+ $section = $request->getIntOrNull( 'section' );
 129+ if ( $section !== null ) {
 130+ $text = $wgParser->getSection( $text, $section );
 131+ }
 132+ }
 133+ }
 134+
 135+ if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
 136+ $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
 137+ }
 138+
 139+ return $text;
 140+ }
 141+
 142+ /**
 143+ * Get the ID of the revision that should used to get the text.
 144+ *
 145+ * @return Integer
 146+ */
 147+ public function getOldId() {
 148+ $oldid = $this->getRequest()->getInt( 'oldid' );
 149+ switch ( $this->getRequest()->getText( 'direction' ) ) {
 150+ case 'next':
 151+ # output next revision, or nothing if there isn't one
 152+ if( $oldid ) {
 153+ $oldid = $this->getTitle()->getNextRevisionId( $oldid );
 154+ }
 155+ $oldid = $oldid ? $oldid : -1;
 156+ break;
 157+ case 'prev':
 158+ # output previous revision, or nothing if there isn't one
 159+ if( !$oldid ) {
 160+ # get the current revision so we can get the penultimate one
 161+ $oldid = $this->getTitle()->getLatestRevID();
 162+ }
 163+ $prev = $this->getTitle()->getPreviousRevisionId( $oldid );
 164+ $oldid = $prev ? $prev : -1 ;
 165+ break;
 166+ case 'cur':
 167+ $oldid = 0;
 168+ break;
 169+ }
 170+ return $oldid;
 171+ }
 172+
 173+ /**
 174+ * Get the content type to use for the response
 175+ *
 176+ * @return String
 177+ */
 178+ public function getContentType() {
 179+ global $wgJsMimeType;
 180+
 181+ $ctype = $this->getRequest()->getVal( 'ctype' );
 182+
 183+ if ( $ctype == '' ) {
 184+ $gen = $this->getRequest()->getVal( 'gen' );
 185+ if ( $gen == 'js' ) {
 186+ $ctype = $wgJsMimeType;
 187+ } elseif ( $gen == 'css' ) {
 188+ $ctype = 'text/css';
 189+ }
 190+ }
 191+
 192+ $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
 193+ if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
 194+ $ctype = 'text/x-wiki';
 195+ }
 196+
 197+ return $ctype;
 198+ }
 199+}
 200+
 201+/**
 202+ * Backward compatibility for extensions
 203+ *
 204+ * @deprecated in 1.19
 205+ */
 206+class RawPage extends RawAction {
 207+ public $mOldId;
 208+
 209+ function __construct( Page $page, $request = false ) {
 210+ parent::__construct( $page );
 211+
 212+ if ( $request !== false ) {
 213+ $context = new DerivativeContext( $this->getContext() );
 214+ $context->setRequest( $request );
 215+ $this->context = $context;
 216+ }
 217+ }
 218+
 219+ public function view() {
 220+ $this->onView();
 221+ }
 222+
 223+ public function getOldId() {
 224+ # Some extensions like to set $mOldId
 225+ if ( $this->mOldId !== null ) {
 226+ return $this->mOldId;
 227+ }
 228+ return parent::getOldId();
 229+ }
 230+}
Property changes on: trunk/phase3/includes/actions/RawAction.php
___________________________________________________________________
Added: svn:eol-style
1231 + native
Added: svn:keywords
2232 + Author Date Id Revision
Index: trunk/phase3/includes/AutoLoader.php
@@ -176,7 +176,6 @@
177177 'ProtectionForm' => 'includes/ProtectionForm.php',
178178 'QueryPage' => 'includes/QueryPage.php',
179179 'QuickTemplate' => 'includes/SkinTemplate.php',
180 - 'RawPage' => 'includes/RawPage.php',
181180 'RCCacheEntry' => 'includes/ChangesList.php',
182181 'RdfMetaData' => 'includes/Metadata.php',
183182 'ReadOnlyError' => 'includes/Exception.php',
@@ -261,6 +260,8 @@
262261 'InfoAction' => 'includes/actions/InfoAction.php',
263262 'MarkpatrolledAction' => 'includes/actions/MarkpatrolledAction.php',
264263 'PurgeAction' => 'includes/actions/PurgeAction.php',
 264+ 'RawAction' => 'includes/actions/RawAction.php',
 265+ 'RawPage' => 'includes/actions/RawAction.php',
265266 'RevertAction' => 'includes/actions/RevertAction.php',
266267 'RevertFileAction' => 'includes/actions/RevertAction.php',
267268 'RevisiondeleteAction' => 'includes/actions/RevisiondeleteAction.php',
Index: trunk/phase3/includes/Wiki.php
@@ -468,12 +468,6 @@
469469 $output->setSquidMaxage( $wgSquidMaxage );
470470 $article->view();
471471 break;
472 - case 'raw': // includes JS/CSS
473 - wfProfileIn( __METHOD__ . '-raw' );
474 - $raw = new RawPage( $article );
475 - $raw->view();
476 - wfProfileOut( __METHOD__ . '-raw' );
477 - break;
478472 case 'delete':
479473 case 'protect':
480474 case 'unprotect':
Index: trunk/phase3/includes/DefaultSettings.php
@@ -5244,6 +5244,7 @@
52455245 'info' => true,
52465246 'markpatrolled' => true,
52475247 'purge' => true,
 5248+ 'raw' => true,
52485249 'revert' => true,
52495250 'revisiondelete' => true,
52505251 'rollback' => true,
Index: trunk/phase3/includes/Skin.php
@@ -295,31 +295,6 @@
296296 }
297297
298298 /**
299 - * Generated JavaScript action=raw&gen=js
300 - * This used to load MediaWiki:Common.js and the skin-specific style
301 - * before the ResourceLoader.
302 - *
303 - * @deprecated since 1.18 Use the ResourceLoader instead. This may be removed at some
304 - * point.
305 - * @param $skinName String: If set, overrides the skin name
306 - * @return String
307 - */
308 - public function generateUserJs( $skinName = null ) {
309 - return '';
310 - }
311 -
312 - /**
313 - * Generate user stylesheet for action=raw&gen=css
314 - *
315 - * @deprecated since 1.18 Use the ResourceLoader instead. This may be removed at some
316 - * point.
317 - * @return String
318 - */
319 - public function generateUserStylesheet() {
320 - return '';
321 - }
322 -
323 - /**
324299 * Get the query to generate a dynamic stylesheet
325300 *
326301 * @return array

Follow-up revisions

RevisionCommit summaryAuthorDate
r100336Fix for r100333: forgot to override requiresWrite() and requiresUnblock()ialex10:29, 20 October 2011

Status & tagging log