MaxSem has uploaded a new change for review.
https://gerrit.wikimedia.org/r/74293
Change subject: Make ResourceLoader minifiers extendable
......................................................................
Make ResourceLoader minifiers extendable
This change allows extensions to override the default JS and CSS minifification,
add new filter types or disable minification even without ?debug=true.
Change-Id: I74e05b1408bc25823d12ecea2c63069d5e13f1ed
---
M includes/AutoLoader.php
M includes/DefaultSettings.php
A includes/resourceloader/ResourceFilters.php
M includes/resourceloader/ResourceLoader.php
4 files changed, 98 insertions(+), 19 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/93/74293/1
diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php
index 2f0ac23..ab181b5 100644
--- a/includes/AutoLoader.php
+++ b/includes/AutoLoader.php
@@ -824,6 +824,9 @@
'ProfileSection' => 'includes/profiler/Profiler.php',
# includes/resourceloader
+ 'IResourceFilter' => 'includes/resourceloader/ResourceFilters.php',
+ 'CssMinifierFilter' => 'includes/resourceloader/ResourceFilters.php',
+ 'JsMinifierFilter' => 'includes/resourceloader/ResourceFilters.php',
'ResourceLoader' => 'includes/resourceloader/ResourceLoader.php',
'ResourceLoaderContext' =>
'includes/resourceloader/ResourceLoaderContext.php',
'ResourceLoaderFileModule' =>
'includes/resourceloader/ResourceLoaderFileModule.php',
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 1f5b2f7..a7bf299 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -3048,6 +3048,15 @@
*/
$wgResourceLoaderExperimentalAsyncLoading = false;
+/**
+ * Filter classes configuration: 'filter type' => 'class name'.
+ * Used for overriding default minification.
+ */
+$wgResourceFilters = array(
+ 'minify-js' => 'JsMinifierFilter',
+ 'minify-css' => 'CssMinifierFilter',
+);
+
/** @} */ # End of resource loader settings }
/*************************************************************************//**
diff --git a/includes/resourceloader/ResourceFilters.php
b/includes/resourceloader/ResourceFilters.php
new file mode 100644
index 0000000..8610fca
--- /dev/null
+++ b/includes/resourceloader/ResourceFilters.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Default minification filters.
+ *
+ * @section LICENSE
+ * 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
+ *
+ * @file
+ */
+
+/**
+ * Base interface for all content-filtering classes
+ */
+interface IResourceFilter {
+ /**
+ * @param string $data: Resource to process
+ *
+ * @return string
+ */
+ function filter( $data );
+}
+
+class CssMinifierFilter implements IResourceFilter {
+ public function filter( $data ) {
+ wfProfileIn( __METHOD__ );
+ $result = CSSMin::minify( $data );
+ wfProfileOut( __METHOD__ );
+
+ return $result;
+ }
+}
+
+class JsMinifierFilter implements IResourceFilter {
+ public function filter( $data ) {
+ global $wgResourceLoaderMinifierStatementsOnOwnLine,
$wgResourceLoaderMinifierMaxLineLength;
+
+ wfProfileIn( __METHOD__ );
+ $result = JavaScriptMinifier::minify( $data,
+ $wgResourceLoaderMinifierStatementsOnOwnLine,
+ $wgResourceLoaderMinifierMaxLineLength
+ );
+ wfProfileOut( __METHOD__ );
+
+ return $result;
+ }
+}
diff --git a/includes/resourceloader/ResourceLoader.php
b/includes/resourceloader/ResourceLoader.php
index 1240abf..f948bd3 100644
--- a/includes/resourceloader/ResourceLoader.php
+++ b/includes/resourceloader/ResourceLoader.php
@@ -50,6 +50,9 @@
/** @var bool */
protected $hasErrors = false;
+ /** @var array: Associative array of content filters */
+ protected $filters = array();
+
/* Protected Methods */
/**
@@ -135,14 +138,12 @@
* @return String: Filtered data, or a comment containing an error
message
*/
protected function filter( $filter, $data ) {
- global $wgResourceLoaderMinifierStatementsOnOwnLine,
$wgResourceLoaderMinifierMaxLineLength;
wfProfileIn( __METHOD__ );
// For empty/whitespace-only data or for unknown filters, don't
perform
// any caching or processing
- if ( trim( $data ) === ''
- || !in_array( $filter, array( 'minify-js', 'minify-css'
) ) )
- {
+ $filterObj = $this->getFilter( $filter );
+ if ( trim( $data ) === '' || !$filterObj ) {
wfProfileOut( __METHOD__ );
return $data;
}
@@ -157,22 +158,10 @@
return $cacheEntry;
}
- $result = '';
- // Run the filter - we've already verified one of these will
work
+ // Run the filter
try {
- switch ( $filter ) {
- case 'minify-js':
- $result = JavaScriptMinifier::minify(
$data,
-
$wgResourceLoaderMinifierStatementsOnOwnLine,
-
$wgResourceLoaderMinifierMaxLineLength
- );
- $result .= "\n/* cache key: $key */";
- break;
- case 'minify-css':
- $result = CSSMin::minify( $data );
- $result .= "\n/* cache key: $key */";
- break;
- }
+ $result = $filterObj->filter( $data );
+ $result .= "\n/* cache key: $key */";
// Save filtered text to Memcached
$cache->set( $key, $result );
@@ -188,6 +177,25 @@
return $result;
}
+ /**
+ * Returns a filter object
+ *
+ * @param string $name: Filter name
+ * @return IResourceFilter
+ */
+ protected function getFilter( $name ) {
+ global $wgResourceFilters;
+
+ if ( isset( $this->filters[$name] ) ) {
+ return $this->filters[$name];
+ }
+ if ( isset( $wgResourceFilters[$name] ) ) {
+ $this->filters[$name] = new $wgResourceFilters[$name];
+ return $this->filters[$name];
+ }
+ return null;
+ }
+
/* Methods */
/**
--
To view, visit https://gerrit.wikimedia.org/r/74293
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I74e05b1408bc25823d12ecea2c63069d5e13f1ed
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits