[MediaWiki-commits] [Gerrit] mediawiki...RandomSelection[master]: Version 2.2.2: RationalWiki changes

2017-08-09 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/367414 )

Change subject: Version 2.2.2: RationalWiki changes
..


Version 2.2.2: RationalWiki changes

From https://github.com/RationalWiki/mediawiki-extensions-RandomSelection

* Adds {{#choose:}} in addition to the old  tag
** Example usage: http://rationalwiki.org/wiki/Fun:Trump_tweet_generator
* BREAKING CHANGE: Caching in the  tag is disabled only if the user 
specifies ; previously caching was disabled by default

Change-Id: I857824472237af2352ba5f1bdd01272e000ad964
---
M RandomSelection.class.php
A RandomSelection.i18n.magic.php
M extension.json
3 files changed, 100 insertions(+), 10 deletions(-)

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



diff --git a/RandomSelection.class.php b/RandomSelection.class.php
index 5abeb18..7c92a2f 100644
--- a/RandomSelection.class.php
+++ b/RandomSelection.class.php
@@ -6,20 +6,33 @@
  *
  * @file
  * @ingroup Extensions
- * @version 2.2
- * @date 23 June 2015
+ * @version 2.2.2
+ * @date 24 July 2017
  * @author Ross McClure 
  * @link https://www.mediawiki.org/wiki/Extension:RandomSelection Documentation
  */
 class RandomSelection {
/**
-* Register the  tag with the Parser.
+* Register the  tag and {{#choose:option 1|...|option N}} 
function
+* with the Parser.
 *
 * @param Parser $parser
 * @return bool
 */
public static function register( &$parser ) {
$parser->setHook( 'choose', array( __CLASS__, 'render' ) );
+   $parser->setFunctionHook( 'choose', array( __CLASS__, 
'renderParserFunction' ), Parser::SFH_OBJECT_ARGS );
+   return true;
+   }
+
+   /**
+* Register the magic word ID.
+*
+* @param array $variableIds
+* @return bool
+*/
+   public static function variableIds( &$variableIds ) {
+   $variableIds[] = 'choose';
return true;
}
 
@@ -27,12 +40,14 @@
 * Callback for register() which actually does all the processing.
 *
 * @param string $input User-supplied input
-* @param array $argv [unused]
+* @param array $argv User-supplied arguments to the tag, e.g. ...
 * @param Parser $parser
 */
public static function render( $input, $argv, $parser ) {
-   # Prevent caching
-   $parser->disableCache();
+   # Prevent caching if specified so by the user
+   if ( isset( $argv['uncached'] ) ) {
+   $parser->disableCache();
+   }
 
# Parse the options and calculate total weight
$len = preg_match_all(
@@ -88,4 +103,58 @@
 
return $parser->recursiveTagParse( $input );
}
+
+   /**
+* Callback for the {{#choose:}} magic word magic (see register() in 
this file)
+*
+* @param Parser $parser
+* @param PPFrame $frame
+* @param array $args User-supplied arguments
+*/
+   public static function renderParserFunction( &$parser, $frame, $args ) {
+   $options = array();
+   $r = 0;
+
+   // First one is not an object
+   $arg = array_shift( $args );
+   $parts = explode( '=', $arg, 2 );
+   if ( count( $parts ) == 2 ) {
+   $options[] = array( intval( trim( $parts[0] ) ), 
$parts[1] );
+   $r += intval( trim( $parts[0] ) );
+   } elseif ( count( $parts ) == 1 ) {
+   $options[] = array( 1, $parts[0] );
+   $r += 1;
+   }
+
+   foreach ( $args as $arg ) {
+   $bits = $arg->splitArg();
+   $nameNode = $bits['name'];
+   $index = $bits['index'];
+   $valueNode = $bits['value'];
+   if ( $index === '' ) {
+   $name = trim( $frame->expand( $nameNode ) );
+   $options[] = array( intval( $name ), $valueNode 
);
+   $r += intval( $name );
+   } else {
+   $options[] = array( 1, $valueNode );
+   $r += 1;
+   }
+   }
+
+   # Choose an option at random
+   if ( $r <= 0 ) {
+   return '';
+   }
+   $r = mt_rand( 1, $r );
+   for ( $i = 0; $i < count( $options ); $i++ ) {
+   $r -= $options[$i][0];
+   if ( $r <= 0 ) {
+   $output = $options[$i][1];
+   break;
+ 

[MediaWiki-commits] [Gerrit] mediawiki...RandomSelection[master]: Version 2.2.2: RationalWiki changes

2017-07-24 Thread Jack Phoenix (Code Review)
Jack Phoenix has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/367414 )

Change subject: Version 2.2.2: RationalWiki changes
..

Version 2.2.2: RationalWiki changes

From https://github.com/RationalWiki/mediawiki-extensions-RandomSelection

* Adds {{#choose:}} in addition to the old  tag
** Example usage: http://rationalwiki.org/wiki/Fun:Trump_tweet_generator
* BREAKING CHANGE: Caching in the  tag is disabled only if the user 
specifies ; previously caching was disabled by default

Change-Id: I857824472237af2352ba5f1bdd01272e000ad964
---
M RandomSelection.class.php
A RandomSelection.i18n.magic.php
M extension.json
3 files changed, 102 insertions(+), 11 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/RandomSelection 
refs/changes/14/367414/1

diff --git a/RandomSelection.class.php b/RandomSelection.class.php
index 5abeb18..7c92a2f 100644
--- a/RandomSelection.class.php
+++ b/RandomSelection.class.php
@@ -6,20 +6,33 @@
  *
  * @file
  * @ingroup Extensions
- * @version 2.2
- * @date 23 June 2015
+ * @version 2.2.2
+ * @date 24 July 2017
  * @author Ross McClure 
  * @link https://www.mediawiki.org/wiki/Extension:RandomSelection Documentation
  */
 class RandomSelection {
/**
-* Register the  tag with the Parser.
+* Register the  tag and {{#choose:option 1|...|option N}} 
function
+* with the Parser.
 *
 * @param Parser $parser
 * @return bool
 */
public static function register( &$parser ) {
$parser->setHook( 'choose', array( __CLASS__, 'render' ) );
+   $parser->setFunctionHook( 'choose', array( __CLASS__, 
'renderParserFunction' ), Parser::SFH_OBJECT_ARGS );
+   return true;
+   }
+
+   /**
+* Register the magic word ID.
+*
+* @param array $variableIds
+* @return bool
+*/
+   public static function variableIds( &$variableIds ) {
+   $variableIds[] = 'choose';
return true;
}
 
@@ -27,12 +40,14 @@
 * Callback for register() which actually does all the processing.
 *
 * @param string $input User-supplied input
-* @param array $argv [unused]
+* @param array $argv User-supplied arguments to the tag, e.g. ...
 * @param Parser $parser
 */
public static function render( $input, $argv, $parser ) {
-   # Prevent caching
-   $parser->disableCache();
+   # Prevent caching if specified so by the user
+   if ( isset( $argv['uncached'] ) ) {
+   $parser->disableCache();
+   }
 
# Parse the options and calculate total weight
$len = preg_match_all(
@@ -88,4 +103,58 @@
 
return $parser->recursiveTagParse( $input );
}
+
+   /**
+* Callback for the {{#choose:}} magic word magic (see register() in 
this file)
+*
+* @param Parser $parser
+* @param PPFrame $frame
+* @param array $args User-supplied arguments
+*/
+   public static function renderParserFunction( &$parser, $frame, $args ) {
+   $options = array();
+   $r = 0;
+
+   // First one is not an object
+   $arg = array_shift( $args );
+   $parts = explode( '=', $arg, 2 );
+   if ( count( $parts ) == 2 ) {
+   $options[] = array( intval( trim( $parts[0] ) ), 
$parts[1] );
+   $r += intval( trim( $parts[0] ) );
+   } elseif ( count( $parts ) == 1 ) {
+   $options[] = array( 1, $parts[0] );
+   $r += 1;
+   }
+
+   foreach ( $args as $arg ) {
+   $bits = $arg->splitArg();
+   $nameNode = $bits['name'];
+   $index = $bits['index'];
+   $valueNode = $bits['value'];
+   if ( $index === '' ) {
+   $name = trim( $frame->expand( $nameNode ) );
+   $options[] = array( intval( $name ), $valueNode 
);
+   $r += intval( $name );
+   } else {
+   $options[] = array( 1, $valueNode );
+   $r += 1;
+   }
+   }
+
+   # Choose an option at random
+   if ( $r <= 0 ) {
+   return '';
+   }
+   $r = mt_rand( 1, $r );
+   for ( $i = 0; $i < count( $options ); $i++ ) {
+   $r -= $options[$i][0];
+   if ( $r <= 0 ) {
+   $output = $options[$i][1];
+