[MediaWiki-commits] [Gerrit] mediawiki...Wikispeech[master]: Enable extension specific variables

2016-09-26 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Enable extension specific variables
..


Enable extension specific variables

Variables can be accessed in PHP and JavaScript. Two cases of PHP were added
(tag removal criteria and namspaces) and one case of Javascript (TTS server
URL). Default variables are set in extension.json and can be overwritten in
LocalSettings.php.

Bug: T140221
Change-Id: If040e7c8cac5897712582d0759865f5ad4717479
---
M Hooks.php
M extension.json
M includes/Cleaner.php
M modules/ext.wikispeech.js
M tests/phpunit/CleanerTest.php
5 files changed, 112 insertions(+), 36 deletions(-)

Approvals:
  Lokal Profil: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Hooks.php b/Hooks.php
index 7d98a87..c683e93 100644
--- a/Hooks.php
+++ b/Hooks.php
@@ -51,7 +51,9 @@
 */
 
public static function onParserAfterTidy( &$parser, &$text ) {
-   if ( $parser->getTitle()->getNamespace() == NS_MAIN && $text != 
"" ) {
+   if ( self::isValidNamespace( 
$parser->getTitle()->getNamespace() ) &&
+$text != ""
+   ) {
wfDebugLog(
'Wikispeech',
'HTML from onParserAfterTidy(): ' . $text
@@ -74,6 +76,25 @@
}
 
/**
+* Test if a namespace is valid for wikispeech.
+*
+* @param int $namespace The namespace id to test.
+* @return bool true if the namespace id matches one defined in
+*  $wgWikispeechNamespaces, else false.
+*/
+
+   private static function isValidNamespace( $namespace ) {
+   global $wgWikispeechNamespaces;
+   foreach ( $wgWikispeechNamespaces as $namespaceId ) {
+   if ( defined( $namespaceId ) &&
+   $namespace == constant( $namespaceId )
+   ) {
+   return true;
+   }
+   }
+   return false;
+   }
+   /**
 * Hook for BeforePageDisplay.
 *
 * Enables JavaScript.
@@ -89,4 +110,10 @@
) {
$out->addModules( [ 'ext.wikispeech' ] );
}
+
+   public static function onResourceLoaderGetConfigVars( &$vars ) {
+   global $wgWikispeechServerUrl;
+   $vars['wgWikispeechServerUrl'] = $wgWikispeechServerUrl;
+   return true;
+   }
 }
diff --git a/extension.json b/extension.json
index 2d3a525..bcc1df3 100644
--- a/extension.json
+++ b/extension.json
@@ -54,6 +54,22 @@
],
"BeforePageDisplay": [
"WikispeechHooks::onBeforePageDisplay"
+   ],
+   "ResourceLoaderGetConfigVars": [
+   "WikispeechHooks::onResourceLoaderGetConfigVars"
]
+   },
+   "config": {
+   "WikispeechServerUrl": "https://morf.se/wikispeech/";,
+   "WikispeechRemoveTags": {
+   "editsection": true,
+   "toc": true,
+   "table": true,
+   "sup": { "class": "reference" },
+   "div": { "class": "thumb" },
+   "ul": true,
+   "ol": true
+   },
+   "WikispeechNamespaces": [ "NS_MAIN" ]
}
 }
diff --git a/includes/Cleaner.php b/includes/Cleaner.php
index 10e318a..9dcc7e6 100644
--- a/includes/Cleaner.php
+++ b/includes/Cleaner.php
@@ -7,18 +7,6 @@
  */
 
 class Cleaner {
-   // HTML tags that should be removes altogether. If value is null, there
-   // are no further criteria. If the value contains 'class', the tag
-   // also needs to have that class to be removed.
-   private static $REMOVE_TAGS = [
-   'editsection' => null,
-   'toc' => null,
-   'table' => null,
-   'sup' => [ 'class' => 'reference' ],
-   'div' => [ 'class' => 'thumb' ],
-   'ul' => null,
-   'ol' => null
-   ];
 
/**
 * Clean HTML tags by removing some altogether and keeping content
@@ -69,33 +57,63 @@
}
 
/**
-* Check if a node matches criteria for removal.
+* Check if a tag matches criteria for removal.
+*
+* The criteria are defined by $wgWikispeechRemoveTags, which is a map
+* where the keys are tag names. If the value is true, the tag will be
+* removed. If the value is an array, it defines further criteria,
+* currently only class name, which needs to match for the tag to be
+* removed.
+*
+* The value may be false, which means the tag won't be removed. This 
is to
+* allow overriding default values in LocalSettings.php, but is 
otherwise
+  

[MediaWiki-commits] [Gerrit] mediawiki...Wikispeech[master]: Enable extension specific variables

2016-08-19 Thread Sebastian Berlin (WMSE) (Code Review)
Sebastian Berlin (WMSE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/305640

Change subject: Enable extension specific variables
..

Enable extension specific variables

Variables can be accessed in PHP and JavaScript, one case of each was
added (tag removal criteria and TTS server URL, respectively). Default
variables are set in extension.json and can be overwritten in
LocalSettings.php.

Bug: T140221
Change-Id: If040e7c8cac5897712582d0759865f5ad4717479
---
M Hooks.php
M extension.json
M includes/Cleaner.php
M modules/ext.wikispeech.js
M tests/phpunit/CleanerTest.php
5 files changed, 117 insertions(+), 46 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikispeech 
refs/changes/40/305640/1

diff --git a/Hooks.php b/Hooks.php
index 1c7bc3a..360f803 100644
--- a/Hooks.php
+++ b/Hooks.php
@@ -63,4 +63,10 @@
) {
$out->addModules( [ 'ext.wikispeech' ] );
}
+
+   public static function onResourceLoaderGetConfigVars( &$vars ) {
+   global $wgWikispeechServerUrl;
+   $vars['wgWikispeechServerUrl'] = $wgWikispeechServerUrl;
+   return true;
+   }
 }
diff --git a/extension.json b/extension.json
index 2d3a525..cbb3394 100644
--- a/extension.json
+++ b/extension.json
@@ -54,6 +54,21 @@
],
"BeforePageDisplay": [
"WikispeechHooks::onBeforePageDisplay"
+   ],
+   "ResourceLoaderGetConfigVars": [
+   "WikispeechHooks::onResourceLoaderGetConfigVars"
]
+   },
+   "config": {
+   "WikispeechServerUrl": "https://morf.se/wikispeech/";,
+   "WikispeechRemoveTags": {
+   "editsection": true,
+   "toc": true,
+   "table": true,
+   "sup": { "class": "reference" },
+   "div": { "class": "thumb" },
+   "ul": true,
+   "ol": true
+   }
}
 }
diff --git a/includes/Cleaner.php b/includes/Cleaner.php
index 8f8361a..af7348f 100644
--- a/includes/Cleaner.php
+++ b/includes/Cleaner.php
@@ -7,18 +7,6 @@
  */
 
 class Cleaner {
-   // HTML tags that should be removes altogether. If value is null, there
-   // are no further criteria. If the value contains 'class', the tag
-   // also needs to have that class to be removed.
-   private static $REMOVE_TAGS = [
-   'editsection' => null,
-   'toc' => null,
-   'table' => null,
-   'sup' => [ 'class' => 'reference' ],
-   'div' => [ 'class' => 'thumb' ],
-   'ul' => null,
-   'ol' => null
-   ];
 
/**
 * Clean HTML tags by removing some altogether and keeping content
@@ -67,33 +55,63 @@
}
 
/**
-* Check if a node matches criteria for removal.
+* Check if a tag matches criteria for removal.
+*
+* The criteria are defined by $wgWikispeechRemoveTags, which is a map
+* where the keys are tag names. If the value is true, the tag will be
+* removed. If the value is an array, it defines further criteria,
+* currently only class name, which needs to match for the tag to be
+* removed.
+*
+* The value may be false, which means the tag won't be removed. This 
is to
+* allow overriding default values in LocalSettings.php, but is 
otherwise
+* not required.
 *
 * @since 0.0.1
-* @param DOMNode $node The node to check.
-* @return bool true if the node match removal criteria, otherwise 
false.
+* @param DOMNode $node The node for the tag to check.
+* @return bool true if the tag match removal criteria, otherwise false.
 */
 
private static function matchesRemove( $node ) {
-   if ( !array_key_exists( $node->nodeName, self::$REMOVE_TAGS ) ) 
{
+   global $wgWikispeechRemoveTags;
+   if ( !array_key_exists( $node->nodeName, 
$wgWikispeechRemoveTags ) ) {
// The node name isn't found in the removal list.
return false;
}
-   $removeCriteria = self::$REMOVE_TAGS[ $node->nodeName ];
-   if ( $removeCriteria == null ) {
-   // Node name matches and there are no extra criteria.
+   $removeCriteria = $wgWikispeechRemoveTags[ $node->nodeName ];
+   if ( $removeCriteria === true ) {
+   // Node name is found and there are no extra criteria.
return true;
}
+   if ( self::nodeHasClass( $node, $removeCriteria[ 'class' ] ) ) {
+   // Node name and class name match.
+