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 <choose> tag
** Example usage: http://rationalwiki.org/wiki/Fun:Trump_tweet_generator
* BREAKING CHANGE: Caching in the <choose> tag is disabled only if the user 
specifies <choose uncached>; 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 <http://www.mediawiki.org/wiki/User:Algorithm>
  * @link https://www.mediawiki.org/wiki/Extension:RandomSelection Documentation
  */
 class RandomSelection {
        /**
-        * Register the <choose> tag with the Parser.
+        * Register the <choose> 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. <choose 
uncached>...</choose>
         * @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;
+                       }
+               }
+
+               return trim( $frame->expand( $output ) );
+       }
+
 }
\ No newline at end of file
diff --git a/RandomSelection.i18n.magic.php b/RandomSelection.i18n.magic.php
new file mode 100644
index 0000000..3a1636f
--- /dev/null
+++ b/RandomSelection.i18n.magic.php
@@ -0,0 +1,19 @@
+<?php
+/**
+ * Internationalization file for the RandomSelection extension.
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+$magicWords = array();
+
+/** English */
+$magicWords['en'] = array(
+       'choose' => array( 0, 'choose' ),
+);
+
+/** Finnish (suomi) */
+$magicWords['fi'] = array(
+       'choose' => array( 0, 'valitse' ),
+);
diff --git a/extension.json b/extension.json
index 3c2dc2d..a564e9b 100644
--- a/extension.json
+++ b/extension.json
@@ -1,6 +1,6 @@
 {
        "name": "RandomSelection",
-       "version": "2.2.1",
+       "version": "2.2.2",
        "author": [
                "Ross McClure",
                "Jack Phoenix",
@@ -18,10 +18,12 @@
        "AutoloadClasses": {
                "RandomSelection": "RandomSelection.class.php"
        },
+       "ExtensionMessagesFiles": {
+               "RandomSelectionMagic": "RandomSelection.i18n.magic.php"
+       },
        "Hooks": {
-               "ParserFirstCallInit": [
-                       "RandomSelection::register"
-               ]
+               "ParserFirstCallInit": "RandomSelection::register",
+               "MagicWordwgVariableIDs": "RandomSelection::variableIds"
        },
        "manifest_version": 1
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/367414
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I857824472237af2352ba5f1bdd01272e000ad964
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/RandomSelection
Gerrit-Branch: master
Gerrit-Owner: Jack Phoenix <j...@countervandalism.net>
Gerrit-Reviewer: Kghbln <mediaw...@kghoffmeyer.de>
Gerrit-Reviewer: SamanthaNguyen <samanthanguyen1...@gmail.com>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to