Thiemo Mättig (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/328681 )

Change subject: Fix documentation and upredictable dynamic call in updateUnits
......................................................................

Fix documentation and upredictable dynamic call in updateUnits

This even gets reported as unused code. Not sure if this counts as a
possible injection vector, because this is actually executing user
input as code.

Change-Id: I223d03ca572dda0fb1849fbbb8eef093c3afb3d1
---
M repo/maintenance/updateUnits.php
1 file changed, 26 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/81/328681/1

diff --git a/repo/maintenance/updateUnits.php b/repo/maintenance/updateUnits.php
index fb90e84..2c4f3a8 100644
--- a/repo/maintenance/updateUnits.php
+++ b/repo/maintenance/updateUnits.php
@@ -27,17 +27,18 @@
  */
 class UpdateUnits extends Maintenance {
 
-
-       /** Base URI
+       /**
         * @var string
         */
        private $baseUri;
+
        /**
         * Length of the base URI.
         * Helper variable to speed up cutting it out.
         * @var int
         */
        private $baseLen;
+
        /**
         * @var SPARQLClient
         */
@@ -56,7 +57,7 @@
                $this->addOption( 'base-unit-types', 'Types of base units.', 
true, true );
                $this->addOption( 'base-uri', 'Base URI for the data.', false, 
true );
                $this->addOption( 'unit-class', 'Class for units.', false, true 
);
-               $this->addOption( 'format', 'Output format, default is json.', 
false, true );
+               $this->addOption( 'format', 'Output format "json" (default) or 
"csv".', false, true );
                $this->addOption( 'sparql', 'SPARQL endpoint URL.', false, true 
);
        }
 
@@ -66,9 +67,6 @@
                                1 );
                }
                $format = $this->getOption( 'format', 'json' );
-               if ( !is_callable( [ $this, 'format' . $format ] ) ) {
-                       $this->error( "Invalid format", 1 );
-               }
 
                $repo = WikibaseRepo::getDefaultInstance();
                $endPoint =
@@ -141,8 +139,16 @@
                        ];
                }
 
-               $formatter = 'format' . $format;
-               echo $this->$formatter( $convertUnits );
+               switch ( strtolower( $format ) ) {
+                       case 'csv':
+                               echo $this->formatCSV( $convertUnits );
+                               break;
+                       case 'json':
+                               echo $this->formatJSON( $convertUnits );
+                               break;
+                       default:
+                               $this->error( 'Invalid format', 1 );
+               }
        }
 
        /**
@@ -182,11 +188,11 @@
         * Create conversion data for a single unit.
         * @param string[] $unit Unit data
         * @param string[] $convertUnits Already converted data
-        * @param string[] $baseUnits Base unit list
+        * @param array[] $baseUnits Base unit list
         * @param string[] $unitUsage Unit usage data
         * @param string[] &$reconvert Array collecting units that require 
re-conversion later,
         *                 due to their target unit not being base.
-        * @return null|\string[] Produces conversion data for the unit or null 
if not possible.
+        * @return string[]|null Produces conversion data for the unit or null 
if not possible.
         */
        public function convertUnit( $unit, $convertUnits, $baseUnits, 
$unitUsage, &$reconvert ) {
                $unit['unit'] = substr( $unit['unit'], $this->baseLen );
@@ -238,10 +244,10 @@
 
        /**
         * Format units as JSON
-        * @param $convertUnits
+        * @param array[] $convertUnits
         * @return string
         */
-       private function formatJSON( $convertUnits ) {
+       private function formatJSON( array $convertUnits ) {
                return json_encode( $convertUnits, JSON_PRETTY_PRINT );
        }
 
@@ -273,7 +279,7 @@
        /**
         * Get base units
         * @param string $filter Unit filter
-        * @return array
+        * @return array[]
         */
        private function getBaseUnits( $filter ) {
                $types =
@@ -301,7 +307,7 @@
 
        /**
         * Retrieve the list of convertable units.
-        * @param $filter
+        * @param string $filter
         * @return array[]|false List of units that can be converted
         */
        private function getConvertableUnits( $filter ) {
@@ -328,10 +334,10 @@
 
        /**
         * Format units as CSV
-        * @param $convertUnits
+        * @param array[] $convertUnits
         * @return string
         */
-       private function formatCSV( $convertUnits ) {
+       private function formatCSV( array $convertUnits ) {
                $str = '';
                foreach ( $convertUnits as $name => $data ) {
                        $str .= "$name,$data[0],$data[1]\n";
@@ -339,6 +345,10 @@
                return $str;
        }
 
+       /**
+        * @param string $err
+        * @param int $die
+        */
        protected function error( $err, $die = 0 ) {
                if ( !$this->silent ) {
                        parent::error( $err, $die );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I223d03ca572dda0fb1849fbbb8eef093c3afb3d1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to