MaxSem has uploaded a new change for review.
https://gerrit.wikimedia.org/r/86361
Change subject: Maintenance script to check LESS files for validity
......................................................................
Maintenance script to check LESS files for validity
A reworked version of script from I068686854ad79e2f63a08d81b1af02f373110613
Change-Id: I733b53171dca77f50a30e5bd0bd5f1b456e4c85d
---
M includes/resourceloader/ResourceLoader.php
M includes/resourceloader/ResourceLoaderFileModule.php
M includes/resourceloader/ResourceLoaderModule.php
A maintenance/checkLess.php
4 files changed, 106 insertions(+), 41 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/61/86361/1
diff --git a/includes/resourceloader/ResourceLoader.php
b/includes/resourceloader/ResourceLoader.php
index b943dd0..42e4a96 100644
--- a/includes/resourceloader/ResourceLoader.php
+++ b/includes/resourceloader/ResourceLoader.php
@@ -1213,4 +1213,43 @@
public static function isValidModuleName( $moduleName ) {
return !preg_match( '/[|,!]/', $moduleName ) && strlen(
$moduleName ) <= 255;
}
+
+ /**
+ * Returns LESS compiler set up for use with MediaWiki
+ *
+ * @return lessc
+ */
+ public static function getLessCompiler() {
+ global $wgResourceLoaderLESSFunctions,
$wgResourceLoaderLESSImportPaths;
+
+ $less = new lessc();
+ $less->setPreserveComments( true );
+ $less->setVariables( self::getLESSVars() );
+ $less->setImportDir( $wgResourceLoaderLESSImportPaths );
+ foreach ( $wgResourceLoaderLESSFunctions as $name => $func ) {
+ $less->registerFunction( $name, $func );
+ }
+ // To ensure embedded resources are refreshed when their source
files
+ // change, track the names and modification times of any files
that
+ // were embedded as data URIs in the generated CSS source.
+ $less->embeddedFiles = array();
+ return $less;
+ }
+
+ /**
+ * Get global LESS variables.
+ *
+ * @return array: Map of variable names to string CSS values.
+ */
+ public static function getLESSVars() {
+ global $wgResourceLoaderLESSVars;
+
+ static $lessVars = null;
+ if ( $lessVars === null ) {
+ $lessVars = $wgResourceLoaderLESSVars;
+ // Sort by key to ensure consistent hashing for cache
lookups.
+ ksort( $lessVars );
+ }
+ return $lessVars;
+ }
}
diff --git a/includes/resourceloader/ResourceLoaderFileModule.php
b/includes/resourceloader/ResourceLoaderFileModule.php
index 3b81b11..4967e9a 100644
--- a/includes/resourceloader/ResourceLoaderFileModule.php
+++ b/includes/resourceloader/ResourceLoaderFileModule.php
@@ -579,6 +579,25 @@
}
/**
+ * Returns all stlyle files used bt this module
+ * @return array
+ */
+ public function getAllStyleFiles() {
+ $files = array();
+ foreach( (array)$this->styles as $key => $value ) {
+ if ( is_array( $value ) ) {
+ $path = $key;
+ } else {
+ $path = $value;
+ }
+ if ( $this->getStyleSheetLang( $path ) === 'less' ) {
+ $files[] = $this->getLocalPath( $path );
+ }
+ }
+ return $files;
+ }
+
+ /**
* Gets the contents of a list of JavaScript files.
*
* @param array $scripts List of file paths to scripts to read, remap
and concetenate
@@ -714,7 +733,7 @@
protected static function getLESSCacheKey( $fileName ) {
global $wgShowExceptionDetails;
- $vars = json_encode( self::getLESSVars() );
+ $vars = json_encode( ResourceLoader::getLESSVars() );
$hash = md5( $fileName . $vars );
return wfMemcKey( 'resourceloader', 'less',
(string)$wgShowExceptionDetails, $hash );
}
@@ -745,7 +764,7 @@
$source = $fileName;
}
- $compiler = self::lessCompiler();
+ $compiler = ResourceLoader::getLessCompiler();
$expire = 0;
try {
$result = $compiler->cachedCompile( $source );
diff --git a/includes/resourceloader/ResourceLoaderModule.php
b/includes/resourceloader/ResourceLoaderModule.php
index 1119cdb..298f1fe 100644
--- a/includes/resourceloader/ResourceLoaderModule.php
+++ b/includes/resourceloader/ResourceLoaderModule.php
@@ -407,9 +407,6 @@
private static $jsParser;
private static $parseCacheVersion = 1;
- /** @var array Global LESS variables */
- private static $lessVars;
-
/**
* Validate a given script file; if valid returns the original source.
* If invalid, returns replacement JS source that throws an exception.
@@ -455,42 +452,6 @@
self::$jsParser = new JSParser();
}
return self::$jsParser;
- }
-
- /**
- * @return lessc
- */
- protected static function lessCompiler() {
- global $wgResourceLoaderLESSFunctions,
$wgResourceLoaderLESSImportPaths;
-
- $less = new lessc();
- $less->setPreserveComments( true );
- $less->setVariables( self::getLESSVars() );
- $less->setImportDir( $wgResourceLoaderLESSImportPaths );
- foreach ( $wgResourceLoaderLESSFunctions as $name => $func ) {
- $less->registerFunction( $name, $func );
- }
- // To ensure embedded resources are refreshed when their source
files
- // change, track the names and modification times of any files
that
- // were embedded as data URIs in the generated CSS source.
- $less->embeddedFiles = array();
- return $less;
- }
-
- /**
- * Get global LESS variables.
- *
- * @return array: Map of variable names to string CSS values.
- */
- protected static function getLESSVars() {
- global $wgResourceLoaderLESSVars;
-
- if ( self::$lessVars === null ) {
- self::$lessVars = $wgResourceLoaderLESSVars;
- // Sort by key to ensure consistent hashing for cache
lookups.
- ksort( self::$lessVars );
- }
- return self::$lessVars;
}
/**
diff --git a/maintenance/checkLess.php b/maintenance/checkLess.php
new file mode 100644
index 0000000..e00a363
--- /dev/null
+++ b/maintenance/checkLess.php
@@ -0,0 +1,46 @@
+<?php
+
+require_once __DIR__ . '/Maintenance.php';
+
+class CheckLess extends Maintenance {
+ public function __construct() {
+ parent::__construct();
+ $this->mDescription = 'Checks LESS files for errors';
+ }
+
+ public function execute() {
+ $result = 0;
+
+ $compiler = ResourceLoader::getLessCompiler();
+ $resourceLoader = new ResourceLoader();
+ foreach( $resourceLoader->getModuleNames() as $name ) {
+ /** @var ResourceLoaderFileModule $module */
+ $module = $resourceLoader->getModule( $name );
+ if ( !$module || !$module instanceof
ResourceLoaderFileModule ) {
+ continue;
+ }
+
+ $hadErrors = false;
+ foreach ( $module->getAllStyleFiles() as $file ) {
+ try {
+ $compiler->compileFile( $file );
+ } catch ( Exception $e ) {
+ if ( !$hadErrors ) {
+ $this->error( "Errors checking
module $name:\n" );
+ $hadErrors = true;
+ }
+ $this->error( $e->getMessage() . "\n" );
+ $result = 1;
+ }
+ }
+ }
+ if ( !$result ) {
+ $this->output( "No errors found\n" );
+ }
+
+ return $result;
+ }
+}
+
+$maintClass = 'CheckLess';
+require_once RUN_MAINTENANCE_IF_MAIN;
--
To view, visit https://gerrit.wikimedia.org/r/86361
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I733b53171dca77f50a30e5bd0bd5f1b456e4c85d
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