r92855 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92854‎ | r92855 | r92856 >
Date:10:45, 22 July 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
Reduce a bit the coupling between Skin and OutputPage:
* Removed Skin::setupUserCss() and merged it in OutputPage::buildCssLinks() and OutputPage::buildCssLinksArray(), also removed its call from ApiParse.php
* Moved Skin::userCanPreview() to OutputPage since two of the three calls to that function are made from there and the last one in SkinTemplate.php is in a somewhat deprecated function
* Removed the Skin parameter from OutputPage::buildCssLinks(), OutputPage::getHeadScripts(), OutputPage::getBottomScripts() and OutputPage::makeResourceLoaderLink() since we now have a context

Also made ApiParse.php call createContext() instead of create a new RequestContext manually and set its Title so so that it does not rely on $wgRequest and $wgTitle to get that member objects
Modified paths:
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/SkinTemplate.php (modified) (history)
  • /trunk/phase3/includes/api/ApiParse.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -2269,7 +2269,6 @@
22702270 if ( $sk->commonPrintStylesheet() ) {
22712271 $this->addModuleStyles( 'mediawiki.legacy.wikiprintable' );
22722272 }
2273 - $sk->setupUserCss( $this );
22742273
22752274 $ret = Html::htmlHeader( array( 'lang' => $wgLang->getCode(), 'dir' => $userdir ) );
22762275
@@ -2286,9 +2285,9 @@
22872286 $ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n";
22882287
22892288 $ret .= implode( "\n", array(
2290 - $this->getHeadLinks( $sk, true ),
2291 - $this->buildCssLinks( $sk ),
2292 - $this->getHeadScripts( $sk ),
 2289+ $this->getHeadLinks( null, true ),
 2290+ $this->buildCssLinks(),
 2291+ $this->getHeadScripts(),
22932292 $this->getHeadItems()
22942293 ) );
22952294
@@ -2397,13 +2396,12 @@
23982397
23992398 /**
24002399 * TODO: Document
2401 - * @param $skin Skin
24022400 * @param $modules Array/string with the module name
24032401 * @param $only String ResourceLoaderModule TYPE_ class constant
24042402 * @param $useESI boolean
24052403 * @return string html <script> and <style> tags
24062404 */
2407 - protected function makeResourceLoaderLink( Skin $skin, $modules, $only, $useESI = false ) {
 2405+ protected function makeResourceLoaderLink( $modules, $only, $useESI = false ) {
24082406 global $wgLoadScript, $wgResourceLoaderUseESI,
24092407 $wgResourceLoaderInlinePrivateModules;
24102408 // Lazy-load ResourceLoader
@@ -2411,7 +2409,7 @@
24122410 $baseQuery = array(
24132411 'lang' => $this->getContext()->getLang()->getCode(),
24142412 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
2415 - 'skin' => $skin->getSkinName(),
 2413+ 'skin' => $this->getSkin()->getSkinName(),
24162414 'only' => $only,
24172415 );
24182416 // Propagate printable and handheld parameters if present
@@ -2436,7 +2434,7 @@
24372435 // Recursively call us for every item
24382436 $links = '';
24392437 foreach ( $modules as $name ) {
2440 - $links .= $this->makeResourceLoaderLink( $skin, $name, $only, $useESI );
 2438+ $links .= $this->makeResourceLoaderLink( $name, $only, $useESI );
24412439 }
24422440 return $links;
24432441 }
@@ -2501,6 +2499,7 @@
25022500 )
25032501 );
25042502 }
 2503+ $links .= "\n";
25052504 continue;
25062505 }
25072506 // Special handling for the user group; because users might change their stuff
@@ -2553,12 +2552,11 @@
25542553 * JS stuff to put in the <head>. This is the startup module, config
25552554 * vars and modules marked with position 'top'
25562555 *
2557 - * @param $sk Skin object to use
25582556 * @return String: HTML fragment
25592557 */
2560 - function getHeadScripts( Skin $sk ) {
 2558+ function getHeadScripts() {
25612559 // Startup - this will immediately load jquery and mediawiki modules
2562 - $scripts = $this->makeResourceLoaderLink( $sk, 'startup', ResourceLoaderModule::TYPE_SCRIPTS, true );
 2560+ $scripts = $this->makeResourceLoaderLink( 'startup', ResourceLoaderModule::TYPE_SCRIPTS, true );
25632561
25642562 // Load config before anything else
25652563 $scripts .= Html::inlineScript(
@@ -2569,8 +2567,8 @@
25702568
25712569 // Script and Messages "only" requests marked for top inclusion
25722570 // Messages should go first
2573 - $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleMessages( true, 'top' ), ResourceLoaderModule::TYPE_MESSAGES );
2574 - $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts( true, 'top' ), ResourceLoaderModule::TYPE_SCRIPTS );
 2571+ $scripts .= $this->makeResourceLoaderLink( $this->getModuleMessages( true, 'top' ), ResourceLoaderModule::TYPE_MESSAGES );
 2572+ $scripts .= $this->makeResourceLoaderLink( $this->getModuleScripts( true, 'top' ), ResourceLoaderModule::TYPE_SCRIPTS );
25752573
25762574 // Modules requests - let the client calculate dependencies and batch requests as it likes
25772575 // Only load modules that have marked themselves for loading at the top
@@ -2590,17 +2588,15 @@
25912589 * JS stuff to put at the bottom of the <body>: modules marked with position 'bottom',
25922590 * legacy scripts ($this->mScripts), user preferences, site JS and user JS
25932591 *
2594 - * @param $sk Skin
2595 - *
25962592 * @return string
25972593 */
2598 - function getBottomScripts( Skin $sk ) {
 2594+ function getBottomScripts() {
25992595 global $wgUseSiteJs, $wgAllowUserJs;
26002596
26012597 // Script and Messages "only" requests marked for bottom inclusion
26022598 // Messages should go first
2603 - $scripts = $this->makeResourceLoaderLink( $sk, $this->getModuleMessages( true, 'bottom' ), ResourceLoaderModule::TYPE_MESSAGES );
2604 - $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts( true, 'bottom' ), ResourceLoaderModule::TYPE_SCRIPTS );
 2599+ $scripts = $this->makeResourceLoaderLink( $this->getModuleMessages( true, 'bottom' ), ResourceLoaderModule::TYPE_MESSAGES );
 2600+ $scripts .= $this->makeResourceLoaderLink( $this->getModuleScripts( true, 'bottom' ), ResourceLoaderModule::TYPE_SCRIPTS );
26052601
26062602 // Modules requests - let the client calculate dependencies and batch requests as it likes
26072603 // Only load modules that have marked themselves for loading at the bottom
@@ -2620,7 +2616,7 @@
26212617
26222618 // Add site JS if enabled
26232619 if ( $wgUseSiteJs ) {
2624 - $scripts .= $this->makeResourceLoaderLink( $sk, 'site', ResourceLoaderModule::TYPE_SCRIPTS );
 2620+ $scripts .= $this->makeResourceLoaderLink( 'site', ResourceLoaderModule::TYPE_SCRIPTS );
26252621 if( $this->getUser()->isLoggedIn() ){
26262622 $userScripts[] = 'user.groups';
26272623 }
@@ -2628,7 +2624,7 @@
26292625
26302626 // Add user JS if enabled
26312627 if ( $wgAllowUserJs && $this->getUser()->isLoggedIn() ) {
2632 - if( $this->getTitle() && $this->getTitle()->isJsSubpage() && $sk->userCanPreview() ) {
 2628+ if( $this->getTitle() && $this->getTitle()->isJsSubpage() && $this->userCanPreview() ) {
26332629 # XXX: additional security check/prompt?
26342630 $scripts .= Html::inlineScript( "\n" . $this->getRequest()->getText( 'wpTextbox1' ) . "\n" ) . "\n";
26352631 } else {
@@ -2637,7 +2633,7 @@
26382634 $userScripts[] = 'user';
26392635 }
26402636 }
2641 - $scripts .= $this->makeResourceLoaderLink( $sk, $userScripts, ResourceLoaderModule::TYPE_SCRIPTS );
 2637+ $scripts .= $this->makeResourceLoaderLink( $userScripts, ResourceLoaderModule::TYPE_SCRIPTS );
26422638
26432639 return $scripts;
26442640 }
@@ -2697,12 +2693,36 @@
26982694 }
26992695
27002696 /**
2701 - * @param $sk Skin
 2697+ * To make it harder for someone to slip a user a fake
 2698+ * user-JavaScript or user-CSS preview, a random token
 2699+ * is associated with the login session. If it's not
 2700+ * passed back with the preview request, we won't render
 2701+ * the code.
 2702+ *
 2703+ * @return bool
 2704+ */
 2705+ public function userCanPreview() {
 2706+ if ( $this->getRequest()->getVal( 'action' ) != 'submit'
 2707+ || !$this->getRequest()->wasPosted()
 2708+ || !$this->getUser()->matchEditToken(
 2709+ $this->getRequest()->getVal( 'wpEditToken' ) )
 2710+ ) {
 2711+ return false;
 2712+ }
 2713+ if ( !$this->getTitle()->isJsSubpage() && !$this->getTitle()->isCssSubpage() ) {
 2714+ return false;
 2715+ }
 2716+
 2717+ return !count( $this->getTitle()->getUserPermissionsErrors( 'edit', $this->getUser() ) );
 2718+ }
 2719+
 2720+ /**
 2721+ * @param $unused Unused
27022722 * @param $addContentType bool
27032723 *
27042724 * @return string HTML tag links to be put in the header.
27052725 */
2706 - public function getHeadLinks( Skin $sk, $addContentType = false ) {
 2726+ public function getHeadLinks( $unused = null, $addContentType = false ) {
27072727 global $wgUniversalEditButton, $wgFavicon, $wgAppleTouchIcon, $wgEnableAPI,
27082728 $wgSitename, $wgVersion, $wgHtml5, $wgMimeType,
27092729 $wgFeed, $wgOverrideSiteFeed, $wgAdvertisedFeedTypes,
@@ -2974,17 +2994,46 @@
29752995 /**
29762996 * Build a set of <link>s for the stylesheets specified in the $this->styles array.
29772997 * These will be applied to various media & IE conditionals.
2978 - * @param $sk Skin object
29792998 *
29802999 * @return string
29813000 */
2982 - public function buildCssLinks( $sk ) {
2983 - $ret = '';
 3001+ public function buildCssLinks() {
 3002+ global $wgUseSiteCss, $wgAllowUserCss, $wgAllowUserCssPrefs;
 3003+
 3004+ $this->getSkin()->setupSkinUserCss( $this );
 3005+
29843006 // Add ResourceLoader styles
29853007 // Split the styles into four groups
29863008 $styles = array( 'other' => array(), 'user' => array(), 'site' => array(), 'private' => array(), 'noscript' => array() );
29873009 $resourceLoader = $this->getResourceLoader();
2988 - foreach ( $this->getModuleStyles() as $name ) {
 3010+
 3011+ $moduleStyles = $this->getModuleStyles();
 3012+
 3013+ // Per-site custom styles
 3014+ if ( $wgUseSiteCss ) {
 3015+ $moduleStyles[] = 'site';
 3016+ $moduleStyles[] = 'noscript';
 3017+ if( $this->getUser()->isLoggedIn() ){
 3018+ $moduleStyles[] = 'user.groups';
 3019+ }
 3020+ }
 3021+
 3022+ // Per-user custom styles
 3023+ if ( $wgAllowUserCss ) {
 3024+ if ( $this->getTitle()->isCssSubpage() && $this->userCanPreview() ) {
 3025+ // @todo FIXME: Properly escape the cdata!
 3026+ $out->addInlineStyle( $this->getRequest()->getText( 'wpTextbox1' ) );
 3027+ } else {
 3028+ $moduleStyles[] = 'user';
 3029+ }
 3030+ }
 3031+
 3032+ // Per-user preference styles
 3033+ if ( $wgAllowUserCssPrefs ) {
 3034+ $moduleStyles[] = 'user.options';
 3035+ }
 3036+
 3037+ foreach ( $moduleStyles as $name ) {
29893038 $group = $resourceLoader->getModule( $name )->getGroup();
29903039 // Modules in groups named "other" or anything different than "user", "site" or "private"
29913040 // will be placed in the "other" group
@@ -2995,7 +3044,7 @@
29963045 // dynamically added styles to override statically added styles from other modules. So the order
29973046 // has to be other, dynamic, site, private, user
29983047 // Add statically added styles for other modules
2999 - $ret .= $this->makeResourceLoaderLink( $sk, $styles['other'], ResourceLoaderModule::TYPE_STYLES );
 3048+ $ret = $this->makeResourceLoaderLink( $styles['other'], ResourceLoaderModule::TYPE_STYLES );
30003049 // Add normal styles added through addStyle()/addInlineStyle() here
30013050 $ret .= implode( "\n", $this->buildCssLinksArray() ) . $this->mInlineStyles;
30023051 // Add marker tag to mark the place where the client-side loader should inject dynamic styles
@@ -3006,7 +3055,7 @@
30073056 // 'private' at present only contains user.options, so put that before 'user'
30083057 // Any future private modules will likely have a similar user-specific character
30093058 foreach ( array( 'site', 'noscript', 'private', 'user' ) as $group ) {
3010 - $ret .= $this->makeResourceLoaderLink( $sk, $styles[$group],
 3059+ $ret .= $this->makeResourceLoaderLink( $styles[$group],
30113060 ResourceLoaderModule::TYPE_STYLES
30123061 );
30133062 }
@@ -3015,6 +3064,13 @@
30163065
30173066 public function buildCssLinksArray() {
30183067 $links = array();
 3068+
 3069+ // Add any extension CSS
 3070+ foreach ( $this->mExtStyles as $url ) {
 3071+ $this->addStyle( $url );
 3072+ }
 3073+ $this->mExtStyles = array();
 3074+
30193075 foreach( $this->styles as $file => $options ) {
30203076 $link = $this->styleLink( $file, $options );
30213077 if( $link ) {
Index: trunk/phase3/includes/api/ApiParse.php
@@ -253,16 +253,16 @@
254254 }
255255
256256 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
257 - $context = new RequestContext;
 257+ $context = $this->createContext();
 258+ $context->setTitle( $titleObj );
258259 $context->getOutput()->addParserOutputNoText( $p_result );
259260
260261 if ( isset( $prop['headitems'] ) ) {
261262 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
262263
263 - $context->getSkin()->setupUserCss( $context->getOutput() );
264264 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
265265
266 - $scripts = array( $context->getOutput()->getHeadScripts( $context->getSkin() ) );
 266+ $scripts = array( $context->getOutput()->getHeadScripts() );
267267
268268 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
269269 }
Index: trunk/phase3/includes/SkinTemplate.php
@@ -208,8 +208,8 @@
209209 $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
210210 $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
211211 $tpl->set( 'html5version', $wgHtml5Version );
212 - $tpl->set( 'headlinks', $out->getHeadLinks( $this ) );
213 - $tpl->set( 'csslinks', $out->buildCssLinks( $this ) );
 212+ $tpl->set( 'headlinks', $out->getHeadLinks() );
 213+ $tpl->set( 'csslinks', $out->buildCssLinks() );
214214
215215 if( $wgUseTrackbacks && $out->isArticleRelated() ) {
216216 $tpl->set( 'trackbackhtml', $out->getTitle()->trackbackRDF() );
@@ -1278,7 +1278,7 @@
12791279 wfProfileIn( __METHOD__ );
12801280
12811281 if( $allowUserJs && $this->loggedin ) {
1282 - if( $this->getTitle()->isJsSubpage() and $this->userCanPreview() ) {
 1282+ if( $this->getTitle()->isJsSubpage() and $this->getOutput()->userCanPreview() ) {
12831283 # XXX: additional security check/prompt?
12841284 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText( 'wpTextbox1' ) . ' /*]]>*/';
12851285 } else {
Index: trunk/phase3/includes/Skin.php
@@ -308,30 +308,6 @@
309309 }
310310
311311 /**
312 - * To make it harder for someone to slip a user a fake
313 - * user-JavaScript or user-CSS preview, a random token
314 - * is associated with the login session. If it's not
315 - * passed back with the preview request, we won't render
316 - * the code.
317 - *
318 - * @return bool
319 - */
320 - public function userCanPreview() {
321 - if ( $this->getRequest()->getVal( 'action' ) != 'submit'
322 - || !$this->getRequest()->wasPosted()
323 - || !$this->getUser()->matchEditToken(
324 - $this->getRequest()->getVal( 'wpEditToken' ) )
325 - ) {
326 - return false;
327 - }
328 - if ( !$this->getTitle()->isJsSubpage() && !$this->getTitle()->isCssSubpage() ) {
329 - return false;
330 - }
331 -
332 - return !count( $this->getTitle()->getUserPermissionsErrors( 'edit', $this->getUser() ) );
333 - }
334 -
335 - /**
336312 * Generated JavaScript action=raw&gen=js
337313 * This used to load MediaWiki:Common.js and the skin-specific style
338314 * before the ResourceLoader.
@@ -357,48 +333,6 @@
358334 }
359335
360336 /**
361 - * @private
362 - * @todo document
363 - * @param $out OutputPage
364 - */
365 - function setupUserCss( OutputPage $out ) {
366 - global $wgUseSiteCss, $wgAllowUserCss, $wgAllowUserCssPrefs;
367 -
368 - wfProfileIn( __METHOD__ );
369 -
370 - $this->setupSkinUserCss( $out );
371 - // Add any extension CSS
372 - foreach ( $out->getExtStyle() as $url ) {
373 - $out->addStyle( $url );
374 - }
375 -
376 - // Per-site custom styles
377 - if ( $wgUseSiteCss ) {
378 - $out->addModuleStyles( array( 'site', 'noscript' ) );
379 - if( $this->getUser()->isLoggedIn() ){
380 - $out->addModuleStyles( 'user.groups' );
381 - }
382 - }
383 -
384 - // Per-user custom styles
385 - if ( $wgAllowUserCss ) {
386 - if ( $this->getTitle()->isCssSubpage() && $this->userCanPreview() ) {
387 - // @todo FIXME: Properly escape the cdata!
388 - $out->addInlineStyle( $this->getRequest()->getText( 'wpTextbox1' ) );
389 - } else {
390 - $out->addModuleStyles( 'user' );
391 - }
392 - }
393 -
394 - // Per-user preference styles
395 - if ( $wgAllowUserCssPrefs ) {
396 - $out->addModuleStyles( 'user.options' );
397 - }
398 -
399 - wfProfileOut( __METHOD__ );
400 - }
401 -
402 - /**
403337 * Get the query to generate a dynamic stylesheet
404338 *
405339 * @return array
@@ -695,7 +629,7 @@
696630 // TODO and the suckage continues. This function is really just a wrapper around
697631 // OutputPage::getBottomScripts() which takes a Skin param. This should be cleaned
698632 // up at some point
699 - $bottomScriptText = $out->getBottomScripts( $this );
 633+ $bottomScriptText = $out->getBottomScripts();
700634 wfRunHooks( 'SkinAfterBottomScripts', array( $this, &$bottomScriptText ) );
701635
702636 return $bottomScriptText;

Follow-up revisions

RevisionCommit summaryAuthorDate
r92856And I forgot to rename that variable in r92855ialex10:48, 22 July 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   18:46, 2 August 2011

Seems OK.

Status & tagging log