Jdlrobson has uploaded a new change for review.
https://gerrit.wikimedia.org/r/68123
Change subject: Proof of concept: Allow templates to have css associated with
them
......................................................................
Proof of concept: Allow templates to have css associated with them
Many pages use the style attribute to style content.
Use of the style attribute makes optimisation for mobile very difficult
as it is impossible to special case things for mobile using media queries.
(see bug 35704)
A better approach would be to allow stylesheets to be associated with
templates.
In future we could turn off use of the style attribute and use template
stylesheets in a similar way to MediaWiki:Common.css
If a style is being used outside a template this is a sign that the style
should be 'common' and formalised by the community to encourage consistency
across the wiki.
Note this existing patchset may have security implications as well
as performance implications.
Change-Id: I557f5abd54d73c288c23c00ecc9f568400355d7c
---
M includes/DefaultSettings.php
M includes/OutputPage.php
A includes/resourceloader/ResourceLoaderWikiTemplateModule.php
3 files changed, 90 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/23/68123/1
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index b560baf..107a052 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -2703,6 +2703,9 @@
/** Use the site's Cascading Style Sheets (CSS)? */
$wgUseSiteCss = true;
+/** Allow Template:Foo to have an associated stylesheet Template:Foo.css */
+$wgUseSiteTemplateCss = true;
+
/**
* Break out of framesets. This can be used to prevent clickjacking attacks,
* or to prevent external sites from framing your site with ads.
diff --git a/includes/OutputPage.php b/includes/OutputPage.php
index 3657ac3..0c811e9 100644
--- a/includes/OutputPage.php
+++ b/includes/OutputPage.php
@@ -1549,6 +1549,32 @@
}
/**
+ * Adds an inline style derived from all templates which have
stylesheets associated with them
+ *
+ * @param $parserOutput ParserOutput object
+ */
+ public function addTemplateStyleTag( $parserOutput ) {
+ global $wgScriptPath, $wgUseSiteTemplateCss;
+ $templates = $parserOutput->getTemplates()[NS_TEMPLATE];
+ $style = '';
+ foreach( $templates as $name => $id ) {
+ $title = Title::newFromText( 'Template:' . $name .
'.css' );
+ $revision = Revision::newFromTitle( $title, false,
Revision::READ_NORMAL );
+ if ( $revision ) {
+ $content = $revision->getContent( Revision::RAW
);
+ // is this safe?
+ $style .= $content->getNativeData();
+ }
+ }
+ $style = CSSMin::remap( $style, false, $wgScriptPath, true );
+ $style = CSSMin::minify( $style );
+ // FIXME: Move to link tag and external stylesheet
+ if ( $wgUseSiteTemplateCss ) {
+ $this->addInlineStyle( $style );
+ }
+ }
+
+ /**
* Add a ParserOutput object, but without Html
*
* @param $parserOutput ParserOutput object
@@ -1569,6 +1595,7 @@
$this->addModuleScripts( $parserOutput->getModuleScripts() );
$this->addModuleStyles( $parserOutput->getModuleStyles() );
$this->addModuleMessages( $parserOutput->getModuleMessages() );
+ $this->addTemplateStyleTag( $parserOutput );
// Template versioning...
foreach ( (array)$parserOutput->getTemplateIds() as $ns =>
$dbks ) {
diff --git a/includes/resourceloader/ResourceLoaderWikiTemplateModule.php
b/includes/resourceloader/ResourceLoaderWikiTemplateModule.php
new file mode 100644
index 0000000..5440f45
--- /dev/null
+++ b/includes/resourceloader/ResourceLoaderWikiTemplateModule.php
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Resource loader module for assets associated with Wiki Templates
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+/**
+ * Module for site customizations
+ */
+class ResourceLoaderWikiTemplateModule extends ResourceLoaderWikiModule {
+ protected $pages;
+
+ /* Protected Methods */
+
+ /**
+ * Gets list of pages used by this module
+ *
+ * @param $context ResourceLoaderContext
+ *
+ * @return Array: List of pages
+ */
+ protected function getPages( ResourceLoaderContext $context ) {
+ return $this->pages;
+ }
+
+ /* Methods */
+
+ /**
+ * Gets group name
+ *
+ * @return String: Name of group
+ */
+ public function getGroup() {
+ return 'wikitemplate';
+ }
+
+ public function __construct( $templates = array() ) {
+ global $wgUseSiteCss;
+ $pages = array();
+ foreach ( $templates as $key => $val ) {
+ $pages[ 'Template:' . $key . '.css' ] = array( 'type'
=> 'style' );
+ }
+ $this->pages = $pages;
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/68123
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I557f5abd54d73c288c23c00ecc9f568400355d7c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits