Seb35 has uploaded a new change for review.

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

Change subject: Minor refactoring of CLI part
......................................................................

Minor refactoring of CLI part

Code:
* Create an abstract class AbstractMediaWikiFarmScript containing 
general-purpose
  methods and a real class MediaWikiFarmScript directly used by 
'bin/mwscript.php';
  the abstract class will be useful for another script in development about 
handling
  Composer-installed extensions

Bugs:
* Scripts in composer.json was in wrong (old) directory 'scripts'

Change-Id: I50e475b249a74b976b226215e652ab6552742244
---
M bin/mwscript.php
M composer.json
M phpcs.xml
R src/AbstractMediaWikiFarmScript.php
A src/MediaWikiFarmScript.php
R tests/phpunit/MediaWikiFarmScriptTest.php
6 files changed, 240 insertions(+), 117 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MediaWikiFarm 
refs/changes/96/311196/1

diff --git a/bin/mwscript.php b/bin/mwscript.php
index c1952f3..0cc21ee 100644
--- a/bin/mwscript.php
+++ b/bin/mwscript.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * Entry point for CLI scripts in the context of a monoversion or multiversion 
MediaWiki farm.
+ * Entry point for CLI scripts in the context of a MediaWiki farm.
  *
  * @author Sébastien Beyou ~ Seb35 <se...@seb35.fr>
  * @license GPL-3.0+ GNU General Public License v3.0 ou version ultérieure
@@ -14,7 +14,7 @@
 }
 
 # Load classes
-require_once dirname( dirname( __FILE__ ) ) . '/src/Script.php';
+require_once dirname( dirname( __FILE__ ) ) . '/src/MediaWikiFarmScript.php';
 
 # Prepare environment
 $wgMediaWikiFarmScript = new MediaWikiFarmScript( $argc, $argv );
diff --git a/composer.json b/composer.json
index 8feeb8e..7eb1384 100644
--- a/composer.json
+++ b/composer.json
@@ -22,9 +22,14 @@
                "mediawiki/mediawiki-codesniffer": "*"
        },
        "scripts": {
-               "validate-schema": "php ./scripts/validate-schema.php",
-               "phpdoc": "./vendor/bin/phpdoc -d src,scripts -e php,php.txt -t 
./docs/code",
-               "parallel-lint": "./vendor/bin/parallel-lint --exclude vendor 
.",
-               "phpcs": "phpcs -p -s"
+               "validate-schema": "php ./bin/validate-schema.php",
+               "lint": "parallel-lint --exclude vendor .",
+               "phpcs": "phpcs -p -s",
+               "phpdoc": "phpdoc -d bin,src -t ./docs/code",
+               "test": [
+                       "composer lint",
+                       "composer phpcs",
+                       "composer phpdoc"
+               ]
        }
 }
diff --git a/phpcs.xml b/phpcs.xml
index c52652f..6224889 100644
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -23,6 +23,11 @@
                        <property name="lineLimit" value="160" />
                </properties>
        </rule>
+       <rule ref="MediaWiki.NamingConventions.ValidGlobalName">
+               <properties>
+                       <property name="ignoreList" type="array" value="$IP" />
+               </properties>
+       </rule>
        <arg name="encoding" value="utf8" />
        <arg name="extensions" value="php" />
        <exclude-pattern>tests/perfs/results</exclude-pattern>
diff --git a/src/Script.php b/src/AbstractMediaWikiFarmScript.php
similarity index 61%
rename from src/Script.php
rename to src/AbstractMediaWikiFarmScript.php
index 17774c5..1c0e972 100644
--- a/src/Script.php
+++ b/src/AbstractMediaWikiFarmScript.php
@@ -7,13 +7,17 @@
  * @license AGPL-3.0+ GNU Affero General Public License v3.0 ou version 
ultérieure
  */
 
+// @codeCoverageIgnoreStart
+require_once dirname( __FILE__ ) . '/MediaWikiFarm.php';
+// @codeCoverageIgnoreEnd
+
 /**
  * This class contains the major part of the script utility, mainly in the 
main() method.
  * Using a class instead of a raw script it better for testability purposes 
and to use
  * less global variables (in fact none; the only global variable written are 
for
  * compatibility purposes, e.g. PHPUnit expects $_SERVER['argv']).
  */
-class MediaWikiFarmScript {
+abstract class AbstractMediaWikiFarmScript {
 
        /** @var int Number of input arguments. */
        public $argc = 0;
@@ -21,11 +25,14 @@
        /** @var string[] Input arguments. */
        public $argv = array();
 
+       /** @var string Short usage, displayed on request or error. */
+       public $shortUsage = '';
+
+       /** @var string Long usage, displayed on request. */
+       public $longUsage = '';
+
        /** @var string Host name. */
        public $host = '';
-
-       /** @var string Script name. */
-       public $script = '';
 
        /** @var int Status. */
        public $status = 0;
@@ -35,7 +42,7 @@
         *
         * @param int $argc Number of input arguments.
         * @param string[] $argv Input arguments.
-        * @return MediaWikiFarmScript
+        * @return AbstractMediaWikiFarmScript
         */
        function __construct( $argc, $argv ) {
 
@@ -97,47 +104,26 @@
        }
 
        /**
-        * Display help and return success or error.
+        * Display help.
         *
-        * @mediawikifarm-const
-        * @mediawikifarm-idempotent
-        *
-        * @param bool $error Return an error code?
-        * @return void
+        * @param bool $long Show extended usage.
+        * @return void.
         */
-       function usage( $error = true ) {
-
-               $fullPath = realpath( $this->argv[0] );
+       function usage( $long = false ) {
 
                # Minimal help, be it an error or not
-               echo <<<HELP
-
-    Usage: php {$this->argv[0]} MediaWikiScript --wiki=hostname …
-
-    Parameters:
-
-      - MediaWikiScript: name of the script, e.g. "maintenance/runJobs.php"
-      - hostname: hostname of the wiki, e.g. "mywiki.example.org"
-
-
-HELP;
+               if( $this->shortUsage ) {
+                       echo $this->shortUsage . "\n";
+               }
 
                # Regular help
-               if( !$error ) {
-                       echo <<<HELP
-    | Note simple names as "runJobs" will be converted to 
"maintenance/runJobs.php".
-    |
-    | For easier use, you can alias it in your shell:
-    |
-    |     alias mwscript='php $fullPath'
-
-
-HELP;
+               if( $long && $this->longUsage ) {
+                       echo $this->longUsage . "\n";
                }
        }
 
        /**
-        * Load related global parameters and symbols
+        * Load related global parameters and symbols.
         *
         * Note that, these files being loaded in a restricted scope, only the 
three global variables explicitely
         * declared as global will be affected (but obviously the variables 
using $GLOBALS in these files); this
@@ -145,7 +131,7 @@
         *
         * @codeCoverageIgnore
         *
-        * @return void
+        * @return void.
         */
        function load() {
 
@@ -180,7 +166,7 @@
         * Perhaps in the future some other globals will be changed, like in 
$_SERVER: PWD, PHP_SELF,
         * SCRIPT_NAME, SCRIPT_FILENAME, PATH_TRANSLATED, if it is needed.
         *
-        * @return void
+        * @return void.
         */
        function exportArguments() {
 
@@ -194,74 +180,38 @@
        }
 
        /**
-        * Main program for the script.
+        * Main program for the script, preliminary part.
         *
-        * @return int HTTP-like return code, in [200, 400, 404, 500].
+        * Although it returns void, the 'status' property can say if there was 
an error or not,
+        * and if it becomes different than 0, the main program will (should) 
return.
+        *
+        * @return void.
         */
-       function main() {
+       function premain() {
 
                # Return usage
                if( $this->argc == 2 && ( $this->argv[1] == '-h' || 
$this->argv[1] == '--help' ) ) {
-                       $this->usage( false );
+                       $this->usage( true );
                        $this->status = 204;
                        return;
                }
+       }
 
-               # Get wiki
-               $this->host = $this->getParam( 'wiki' );
-               if( is_null( $this->host ) ) {
-                       $this->usage();
-                       $this->status = 400;
-                       return;
-               }
+       /**
+        * Main program for the script, preliminary part.
+        *
+        * Although it returns void, the 'status' property says if there was an 
error or not.
+        *
+        * @return void.
+        */
+       abstract function main();
 
-               # Get script
-               $this->script = $this->getParam( 1, false );
-               if( preg_match( '/^[a-zA-Z-]+$/', $this->script ) ) {
-                       $this->script = 'maintenance/' . $this->script . '.php';
-               }
-
-               if( is_null( $this->script ) ) {
-                       $this->usage();
-                       $this->status = 400;
-                       return;
-               }
-
-               # Replace the caller script by the MediaWiki script
-               $this->getParam( 0 );
-               $this->argv[0] = $this->script;
-
-
-               # Initialise the requested version
-               $code = MediaWikiFarm::load( $this->script, $this->host );
-               if( $code != 200 ) {
-                       $this->status = $code;
-                       return;
-               }
-               if( !is_file( $this->script ) ) {
-                       echo "Script not found.\n";
-                       $this->status = 400;
-                       return;
-               }
-
-
-               # Display parameters
-               # NB: avoid to use `global $wgMediaWikiFarm;` because it would 
create the global variable
-               # and set it to null if it does not exist
-               $wgMediaWikiFarm = $GLOBALS['wgMediaWikiFarm'];
-               $wikiID = $wgMediaWikiFarm->getVariable( '$WIKIID' );
-               $suffix = $wgMediaWikiFarm->getVariable( '$SUFFIX' );
-               $version = $wgMediaWikiFarm->getVariable( '$VERSION' ) ? 
$wgMediaWikiFarm->getVariable( '$VERSION' ) : 'current';
-               $code = $wgMediaWikiFarm->getVariable( '$CODE' );
-               echo <<<PARAMS
-
-Wiki:    {$this->host} (wikiID: $wikiID; suffix: $suffix)
-Version: $version: $code
-Script:  {$this->script}
-
-
-PARAMS;
-
+       /**
+        * Main program for the script, postliminary part.
+        *
+        * @return void.
+        */
+       function postmain() {
 
                # Export symbols
                $this->exportArguments();
@@ -272,15 +222,7 @@
        /**
         * Post-execution of the main script, only needed in the case 
'maintenance/update.php' is run (see main documentation).
         *
-        * @return void
+        * @return void.
         */
-       function restInPeace() {
-
-               if( !array_key_exists( 'wgMediaWikiFarm', $GLOBALS ) || 
!$GLOBALS['wgMediaWikiFarm'] instanceof MediaWikiFarm ) {
-                       return;
-               }
-
-               # Update version after maintenance/update.php (the only case 
where another version is given before execution)
-               $GLOBALS['wgMediaWikiFarm']->updateVersionAfterMaintenance();
-       }
+       function restInPeace() {}
 }
diff --git a/src/MediaWikiFarmScript.php b/src/MediaWikiFarmScript.php
new file mode 100644
index 0000000..05df818
--- /dev/null
+++ b/src/MediaWikiFarmScript.php
@@ -0,0 +1,145 @@
+<?php
+/**
+ * Wrapper around MediaWiki scripts.
+ *
+ * @author Sébastien Beyou ~ Seb35 <se...@seb35.fr>
+ * @license GPL-3.0+ GNU General Public License v3.0 ou version ultérieure
+ * @license AGPL-3.0+ GNU Affero General Public License v3.0 ou version 
ultérieure
+ */
+
+// @codeCoverageIgnoreStart
+require_once dirname( __FILE__ ) . '/AbstractMediaWikiFarmScript.php';
+// @codeCoverageIgnoreEnd
+
+/**
+ * This class contains the major part of the script utility, mainly in the 
main() method.
+ * Using a class instead of a raw script it better for testability purposes 
and to use
+ * less global variables (in fact none; the only global variable written are 
for
+ * compatibility purposes, e.g. PHPUnit expects $_SERVER['argv']).
+ */
+class MediaWikiFarmScript extends AbstractMediaWikiFarmScript {
+
+       /** @var string Script name. */
+       public $script = '';
+
+       /**
+        * Create the object with a copy of $argc and $argv.
+        *
+        * @param int $argc Number of input arguments.
+        * @param string[] $argv Input arguments.
+        * @return MediaWikiFarmScript
+        */
+       function __construct( $argc, $argv ) {
+
+               parent::__construct( $argc, $argv );
+
+               $this->shortUsage = <<<HELP
+
+    Usage: php {$this->argv[0]} MediaWikiScript --wiki=hostname …
+
+    Parameters:
+
+      - MediaWikiScript: name of the script, e.g. "maintenance/runJobs.php"
+      - hostname: hostname of the wiki, e.g. "mywiki.example.org"
+
+HELP;
+
+               $fullPath = realpath( $this->argv[0] );
+               $this->longUsage = <<<HELP
+    | Note simple names as "runJobs" will be converted to 
"maintenance/runJobs.php".
+    |
+    | For easier use, you can alias it in your shell:
+    |
+    |     alias mwscript='php $fullPath'
+
+HELP;
+       }
+
+       /**
+        * Main program for the script.
+        *
+        * Although it returns void, the 'status' property says if there was an 
error or not.
+        *
+        * @return void.
+        */
+       function main() {
+
+               # Manage mandatory arguments.
+               $this->premain();
+               if( $this->status ) {
+                       return;
+               }
+
+               # Get wiki
+               $this->host = $this->getParam( 'wiki' );
+               if( is_null( $this->host ) ) {
+                       $this->usage();
+                       $this->status = 400;
+                       return;
+               }
+
+               # Get script
+               $this->script = $this->getParam( 1, false );
+               if( preg_match( '/^[a-zA-Z-]+$/', $this->script ) ) {
+                       $this->script = 'maintenance/' . $this->script . '.php';
+               }
+
+               if( is_null( $this->script ) ) {
+                       $this->usage();
+                       $this->status = 400;
+                       return;
+               }
+
+               # Replace the caller script by the MediaWiki script
+               $this->getParam( 0 );
+               $this->argv[0] = $this->script;
+
+
+               # Initialise the requested version
+               $code = MediaWikiFarm::load( $this->script, $this->host );
+               if( $code != 200 ) {
+                       $this->status = $code;
+                       return;
+               }
+               if( !is_file( $this->script ) ) {
+                       echo "Script not found.\n";
+                       $this->status = 400;
+                       return;
+               }
+
+
+               # Display parameters
+               # NB: avoid to use `global $wgMediaWikiFarm;` because it would 
create the global variable
+               # and set it to null if it does not exist
+               $wgMediaWikiFarm = $GLOBALS['wgMediaWikiFarm'];
+               $wikiID = $wgMediaWikiFarm->getVariable( '$WIKIID' );
+               $suffix = $wgMediaWikiFarm->getVariable( '$SUFFIX' );
+               $version = $wgMediaWikiFarm->getVariable( '$VERSION' ) ? 
$wgMediaWikiFarm->getVariable( '$VERSION' ) : 'current';
+               $code = $wgMediaWikiFarm->getVariable( '$CODE' );
+               echo <<<PARAMS
+
+Wiki:    {$this->host} (wikiID: $wikiID; suffix: $suffix)
+Version: $version: $code
+Script:  {$this->script}
+
+
+PARAMS;
+
+               $this->postmain();
+       }
+
+       /**
+        * Post-execution of the main script, only needed in the case 
'maintenance/update.php' is run (see main documentation).
+        *
+        * @return void.
+        */
+       function restInPeace() {
+
+               if( !array_key_exists( 'wgMediaWikiFarm', $GLOBALS ) || 
!$GLOBALS['wgMediaWikiFarm'] instanceof MediaWikiFarm ) {
+                       return;
+               }
+
+               # Update version after maintenance/update.php (the only case 
where another version is given before execution)
+               $GLOBALS['wgMediaWikiFarm']->updateVersionAfterMaintenance();
+       }
+}
diff --git a/tests/phpunit/ScriptTest.php 
b/tests/phpunit/MediaWikiFarmScriptTest.php
similarity index 90%
rename from tests/phpunit/ScriptTest.php
rename to tests/phpunit/MediaWikiFarmScriptTest.php
index 8dd432a..92415e9 100644
--- a/tests/phpunit/ScriptTest.php
+++ b/tests/phpunit/MediaWikiFarmScriptTest.php
@@ -63,6 +63,7 @@
         * Test construction.
         *
         * @covers MediaWikiFarmScript::__construct
+        * @covers AbstractMediaWikiFarmScript::__construct
         */
        function testConstruction() {
 
@@ -76,7 +77,9 @@
         * Test usage method.
         *
         * @covers MediaWikiFarmScript::usage
+        * @covers AbstractMediaWikiFarmScript::usage
         * @uses MediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::__construct
         */
        function testUsage1() {
 
@@ -91,7 +94,9 @@
         * Test usage method.
         *
         * @covers MediaWikiFarmScript::usage
+        * @covers AbstractMediaWikiFarmScript::usage
         * @uses MediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::__construct
         */
        function testUsage2() {
 
@@ -99,15 +104,17 @@
 
                $wgMediaWikiFarmScript = new MediaWikiFarmScript( 1, array( 
self::$mwscriptPath ) );
 
-               $wgMediaWikiFarmScript->usage( false );
+               $wgMediaWikiFarmScript->usage( true );
        }
 
        /**
         * Test usage method.
         *
         * @covers MediaWikiFarmScript::main
+        * @covers AbstractMediaWikiFarmScript::premain
         * @uses MediaWikiFarmScript::__construct
-        * @uses MediaWikiFarmScript::usage
+        * @uses AbstractMediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::usage
         */
        function testUsage3() {
 
@@ -124,8 +131,10 @@
         * Test usage method.
         *
         * @covers MediaWikiFarmScript::main
+        * @covers AbstractMediaWikiFarmScript::premain
         * @uses MediaWikiFarmScript::__construct
-        * @uses MediaWikiFarmScript::usage
+        * @uses AbstractMediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::usage
         */
        function testUsage4() {
 
@@ -143,6 +152,7 @@
         *
         * @covers MediaWikiFarmScript::exportArguments
         * @uses MediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::__construct
         * @backupGlobals enabled
         */
        function testExport() {
@@ -168,6 +178,7 @@
         *
         * @covers MediaWikiFarmScript::getParam
         * @uses MediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::__construct
         */
        function testGetParam() {
 
@@ -207,6 +218,7 @@
         * @ backupGlobals enabled
         * @ covers MediaWikiFarmScript::load
         * @ uses MediaWikiFarmScript::__construct
+        * @ uses AbstractMediaWikiFarmScript::__construct
         *
        function testLoad() {
 
@@ -226,7 +238,9 @@
         * @covers MediaWikiFarmScript::main
         * @uses MediaWikiFarmScript::__construct
         * @uses MediaWikiFarmScript::getParam
-        * @uses MediaWikiFarmScript::usage
+        * @uses AbstractMediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::usage
+        * @uses AbstractMediaWikiFarmScript::premain
         */
        function testMissingArgumentWiki() {
 
@@ -245,7 +259,9 @@
         * @covers MediaWikiFarmScript::main
         * @uses MediaWikiFarmScript::__construct
         * @uses MediaWikiFarmScript::getParam
-        * @uses MediaWikiFarmScript::usage
+        * @uses AbstractMediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::usage
+        * @uses AbstractMediaWikiFarmScript::premain
         */
        function testMissingArgumentScript() {
 
@@ -268,6 +284,8 @@
         * @covers MediaWikiFarmScript::main
         * @uses MediaWikiFarmScript::__construct
         * @uses MediaWikiFarmScript::getParam
+        * @uses AbstractMediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::premain
         * @uses MediaWikiFarm::load
         * @uses MediaWikiFarm::__construct
         * @uses MediaWikiFarm::selectFarm
@@ -304,6 +322,8 @@
         * @covers MediaWikiFarmScript::main
         * @uses MediaWikiFarmScript::__construct
         * @uses MediaWikiFarmScript::getParam
+        * @uses AbstractMediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::premain
         * @uses MediaWikiFarm::load
         * @uses MediaWikiFarm::__construct
         * @uses MediaWikiFarm::selectFarm
@@ -340,9 +360,12 @@
         *
         * @backupGlobals enabled
         * @covers MediaWikiFarmScript::main
+        * @covers AbstractMediaWikiFarmScript::premain
+        * @covers AbstractMediaWikiFarmScript::postmain
         * @uses MediaWikiFarmScript::__construct
         * @uses MediaWikiFarmScript::getParam
         * @uses MediaWikiFarmScript::exportArguments
+        * @uses AbstractMediaWikiFarmScript::__construct
         * @uses MediaWikiFarm::load
         * @uses MediaWikiFarm::__construct
         * @uses MediaWikiFarm::selectFarm
@@ -398,6 +421,9 @@
         * @uses MediaWikiFarmScript::__construct
         * @uses MediaWikiFarmScript::getParam
         * @uses MediaWikiFarmScript::exportArguments
+        * @uses AbstractMediaWikiFarmScript::__construct
+        * @uses AbstractMediaWikiFarmScript::premain
+        * @uses AbstractMediaWikiFarmScript::postmain
         * @uses MediaWikiFarm::load
         * @uses MediaWikiFarm::__construct
         * @uses MediaWikiFarm::selectFarm

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I50e475b249a74b976b226215e652ab6552742244
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MediaWikiFarm
Gerrit-Branch: master
Gerrit-Owner: Seb35 <seb35wikipe...@gmail.com>

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

Reply via email to