Seb35 has uploaded a new change for review.

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

Change subject: Added a script to execute a MediaWiki script in the context of 
a farm
......................................................................

Added a script to execute a MediaWiki script in the context of a farm
---
A scripts/mwscript.php
M src/MediaWikiFarm.php
M src/main.php
3 files changed, 168 insertions(+), 6 deletions(-)


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

diff --git a/scripts/mwscript.php b/scripts/mwscript.php
new file mode 100644
index 0000000..bf9cdb0
--- /dev/null
+++ b/scripts/mwscript.php
@@ -0,0 +1,162 @@
+<?php
+/**
+ * Entry point for CLI scripts in the context of a monoversion or multiversion 
MediaWiki farm.
+ * 
+ * @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
+ */
+
+# Protect against web entry
+if( PHP_SAPI != 'cli' ) exit;
+
+# Definition of a constant to protect dedicated entry points
+define( 'MEDIAWIKI_FARM', true );
+
+# Configuration of the MediaWiki Farm
+$wgMediaWikiFarmCodeDir = dirname( dirname( dirname( __FILE__ ) ) );
+$wgMediaWikiFarmConfigDir = '/etc/mediawiki';
+$wgMediaWikiFarmCacheDir = '/tmp/mw-cache';
+@include_once dirname( dirname( __FILE__ ) ) . 
'/config/MediaWikiFarmDirectories.php';
+
+# Include library
+// @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound
+require_once dirname( dirname( __FILE__ ) ) . '/src/MediaWikiFarm.php';
+// @codingStandardsIgnoreEnd
+
+# A small helper function
+/**
+ * Get a command line parameter.
+ * 
+ * The parameter can be removed from the list, except the first parameter 
(script name).
+ * 
+ * @param string|integer $name Parameter name or position (from 0).
+ * @param bool $shift Remove this parameter from the list?
+ * @return string|null Value of the parameter.
+ */
+function mwfGetParam( $name, $shift = true ) {
+       
+       global $argc, $argv;
+       
+       $posArg = 0;
+       $nbArgs = 0;
+       $value = null;
+       
+       # Search a named parameter
+       if( is_string( $name ) ) {
+               
+               for( $posArg = 1; $posArg < $argc; $posArg++ ) {
+                               
+                       if( substr( $argv[$posArg], 0, strlen($name)+3 ) == 
'--'.$name.'=' ) {
+                               $value = substr( $argv[$posArg], 
strlen($name)+3 );
+                               $nbArgs = 1;
+                               break;
+                       }
+                       elseif( $argv[$posArg] == '--'.$name && $posArg < $argc 
- 1 ) {
+                               $value = $argv[$posArg+1];
+                               $nbArgs = 2;
+                               break;
+                       }
+               }
+       }
+       
+       # Search a positional parameter
+       elseif( is_int( $name ) ) {
+               if( $name == 0 )
+                       $shift = false;
+               if( $name >= $argc )
+                       return null;
+               $value = $argv[$name];
+               $nbArgs = 1;
+       }
+       
+       # Remove the parameter from the list
+       if( $shift ) {
+               
+               $argc -= $nbArgs;
+               $argv = array_merge( array_slice( $argv, 0, $posArg ), 
array_slice( $argv, $posArg+$nbArgs ) );
+       }
+       
+       return $value;
+}
+
+# Usage help
+/**
+ * Display help and return success or error.
+ * 
+ * @param bool $error Return an error code?
+ * @return void
+ */
+function mwfUsage( $error = true ) {
+       
+       global $argv;
+       $fullPath = realpath( $argv[0] );
+       
+       # Minimal help, be it an error or not
+       echo <<<HELP
+
+    Usage: php {$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;
+       
+       # 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;
+       
+       exit( $error ? 1 : 0 );
+}
+if( $argc == 2 && ($argv[1] == '-h' || $argv[1] == '--help') ) mwfUsage( false 
);
+
+# Get wiki
+$mwfHost = mwfGetParam( 'wiki' );
+if( is_null( $mwfHost ) ) mwfUsage();
+
+# Get script
+$mwfScript = mwfGetParam( 1 );
+if( is_null( $mwfScript ) ) mwfUsage();
+if( preg_match( '/^[a-zA-Z-]+$/', $mwfScript ) )
+       $mwfScript = 'maintenance/' . $mwfScript . '.php';
+
+# Initialise the requested version
+MediaWikiFarm::getEntryPoint( $mwfScript, $mwfHost );
+
+# Display parameters
+echo <<<PARAMS
+
+Wiki:    $mwfHost (wikiID: {$wgMediaWikiFarm->params['wikiID']}; suffix: 
{$wgMediaWikiFarm->params['suffix']})
+Version: {$wgMediaWikiFarm->params['version']}: 
{$wgMediaWikiFarm->params['code']}
+Script:  $mwfScript
+
+
+PARAMS;
+
+# Clean this script
+if( !is_file( $mwfScript ) ) {
+       echo "Script not found.\n";
+       exit( 1 );
+}
+$argv[0] = $mwfScript;
+unset( $mwfScript );
+unset( $mwfHost );
+
+# Execute the script
+// Possibly it could be better to do a true system call with a child process 
(PHP function "system"), BUT
+// hostname must be passed as an environment variable and more importantly, in 
the current implementation of
+// MediaWikiFarm, the called version of the extension will be 
$version/extensions/MediaWikiFarm, and this
+// version is probably not configured as a standalone extension (directories 
set in LocalSettings.php); so
+// it will not work in current implementation.
+require $argv[0];
+
diff --git a/src/MediaWikiFarm.php b/src/MediaWikiFarm.php
index 38cab2b..63b7dd3 100644
--- a/src/MediaWikiFarm.php
+++ b/src/MediaWikiFarm.php
@@ -74,9 +74,9 @@
                if( !$wgMediaWikiFarm->checkExistence() ) {
                        
                        $version = $_SERVER['SERVER_PROTOCOL'] && 
$_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.0' ? '1.0' : '1.1';
-                       header( "HTTP/$version 404 Not Found" );
-                       echo 'Error: unknown wiki.';
-                       exit;
+                       if( PHP_SAPI != 'cli' ) header( "HTTP/$version 404 Not 
Found" );
+                       echo "Error: unknown wiki.\n";
+                       exit( 1 );
                }
                
                # Go to version directory
diff --git a/src/main.php b/src/main.php
index 9490f48..2d998c5 100644
--- a/src/main.php
+++ b/src/main.php
@@ -23,9 +23,9 @@
 if( !$wgMediaWikiFarm->checkExistence() ) {
        
        $version = $_SERVER['SERVER_PROTOCOL'] && $_SERVER['SERVER_PROTOCOL'] 
=== 'HTTP/1.0' ? '1.0' : '1.1';
-       header( "HTTP/$version 404 Not Found" );
-       echo 'Error: unknown wiki.';
-       exit;
+       if( PHP_SAPI != 'cli' ) header( "HTTP/$version 404 Not Found" );
+       echo "Error: unknown wiki.\n";
+       exit( 1 );
 }
 
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id6f5466be356bc9820d58365181c332031a37886
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