testswarm /inc/MaintenanceScript.php

Language PHP Lines 294
MD5 Hash 1c2e4375b63cefe973dd38dd5813f484
Repository https://github.com/richardudovich/testswarm.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
 * This is the central file for maintenance scripts.
 * It is not the entry point however.
 *
 * @author Timo Tijhof, 2012
 * @since 1.0.0
 * @package TestSwarm
 */
abstract class MaintenanceScript {
	private $context, $name, $description;
	private $flags = array();
	private $options = array();
	private $generalArgKeys = array();
	private $requiredKeys = array();
	private $parsed = array();

	abstract protected function init();

	abstract protected function execute();

	final protected function setDescription( $description ) {
		$this->description = $description;
	}

	/**
	 * Register a flag, usage any of these 4:
	 * - php script.php -a -b value -c "value" -d="value"
	 * @param string $key: one single character.
	 * @param string $type: one of "boolean", "value"
	 * @param string $description
	 */
	protected function registerFlag( $key, $type, $description ) {
		static $types = array( 'boolean', 'value' );
		if ( !is_string( $key ) || strlen ( $key ) !== 1 || !in_array( $type, $types ) ) {
			$this->error( 'Illegal flag registration' );
		}
		$this->flags[$key] = array(
			'type' => $type,
			'description' => $description,
		);
	}

	/**
	 * Register an option, usage any of these 4:
	 * - php script.php --foo --bar=value --quux="value"
	 * @param string $name: at least 2 characters
	 * @param string $type: one of "boolean", "value"
	 * @param string $description
	 * @param bool $required
	 */
	protected function registerOption( $name, $type, $description, $required = false ) {
		static $types = array( 'boolean', 'value' );
		if ( !is_string( $name ) || strlen ( $name ) < 2 || !in_array( $type, $types ) ) {
			$this->error( 'Illegal option registration' );
		}
		$this->options[$name] = array(
			'type' => $type,
			'description' => $description,
			'required' => (bool) $required
		);
	}

	protected function parseCliArguments() {
		// Prepare for getopt(). Note that it supports "require value" for cli args,
		// but we use our own format instead (see also php.net/getopt).
		$getoptShort = '';
		$getoptLong = array();
		$requiredKeys = array();
		foreach ( $this->flags as $flagKey => $flagInfo ) {
			switch ( $flagInfo['type'] ) {
			case 'value':
				$getoptShort .= $flagKey . '::';
				break;
			case 'boolean':
				$getoptShort .= $flagKey;
				break;
			}
		}
		foreach ( $this->options as $optionName => $optionInfo ) {
			if ( $optionInfo['required'] ) {
				$requiredKeys[] = $optionName;
			}
			switch ( $optionInfo['type'] ) {
			case 'value':
				$getoptLong[] = $optionName . '::';
				break;
			case 'boolean':
				$getoptLong[] = $optionName;
				break;
			}
		}
		$parsed = getopt( $getoptShort, $getoptLong );
		if ( !is_array( $parsed ) ) {
			$this->error( 'Parsing command line arguments failed.' );
		}
		$this->parsed = $parsed;

		if ( !$this->getOption( 'help' ) ) {
			foreach ( $requiredKeys as $requiredKey ) {
				if ( !isset( $parsed[$requiredKey] ) || !$parsed[$requiredKey] ) {
					$this->error( 'Option "' . $requiredKey  . '" is required.' );
				}
			}
		}
	}

	protected function getFlag( $key ) {
		if ( !isset( $this->flags[$key] ) || !isset( $this->parsed[$key] ) ) {
			return false;
		}
		return $this->flags[$key]['type'] === 'boolean' ? true : $this->parsed[$key];
	}

	protected function getOption( $name ) {
		if ( !isset( $this->options[$name] ) || !isset( $this->parsed[$name] ) ) {
			return false;
		}
		return $this->options[$name]['type'] === 'boolean' ? true :$this->parsed[$name];
	}

	public function run() {
		if ( !defined( 'SWARM_ENTRY' ) || SWARM_ENTRY !== 'SCRIPT' ) {
			$this->error( 'MaintenanceScript instances may only be run as part of a maintenace script.' );
		}
		if ( php_sapi_name() !== 'cli' ) {
			$this->error( 'Maintenance scripts may only be run from the command-line interface.' );
		}
		if ( !ini_get( 'register_argc_argv' ) || ini_get( 'register_argc_argv' ) == '0' ) {
			$this->error( 'Maintenance scripts require `register_argc_argv` to be enabled in php.ini.' );
		}

		// General options for all scripts
		$this->registerOption( 'help', 'boolean', 'Display this message' );
		// E.g. to allow puppet to run a script without it facing "(Y/N)" or something.
		$this->registerOption( 'quiet', 'boolean', 'Surpress standard output and requests for cli input' );

		$this->generalArgKeys = array_merge( array_keys( $this->options ), array_keys( $this->flags ) );

		$this->init();
		$name = get_class( $this );
		// "class FooBarScript extends .."
		if ( substr( $name, -6 ) === 'Script' ) {
			$name = substr( $name, 0, -6 );
		}
		$this->name = $name;
		if ( !isset( $this->description ) ) {
			$this->error( "{$this->name} is missing a description." );
		}
		$this->parseCliArguments();

		// Generate header
		$version = $this->getContext()->getVersionInfo( 'bypass-cache' );
		$versionText = $version['TestSwarm'];
		if ( $version['devInfo'] ) {
			$versionText .= ' (' . $version['devInfo']['branch'] . ' ' . substr( $version['devInfo']['SHA1'], 0, 7 ) . ')';
		}
		$description = wordwrap( $this->description, 72, "\n", true );
		$description = explode( "\n", $description );
		$description[] = str_repeat( '-', 72 );
		$label = "{$this->name}: ";
		$prefix = str_repeat( ' ', strlen( $label ) );
		$description = $label . implode( "\n" . $prefix, $description );
		$this->out("[TestSwarm $versionText] Maintenance script

$description
");

		// Help or continue
		if ( $this->getOption( 'help' ) ) {
			$this->displayHelp();
		} else {
			$this->execute();
		}
		$this->out( '' );
		exit;
	}

	protected function displayHelp() {
		$helpScript = '';
		$helpGeneral = '';
		$placeholder = array(
			'boolean' => '',
			'value' => ' <value>',
		);
		foreach ( $this->flags as $flagKey => $flagInfo ) {
			$help = "\n  -{$flagKey}" . $placeholder[$flagInfo['type']] . ": {$flagInfo['description']}";
			if ( in_array( $flagKey, $this->generalArgKeys ) ) {
				$helpGeneral .= $help;
			} else {
				$helpScript .= $help;
			}
		}
		foreach ( $this->options as $optionName => $optionInfo ) {
			$help = "\n  --{$optionName}" . $placeholder[$optionInfo['type']] . ": {$optionInfo['description']}";
			if ( in_array( $optionName, $this->generalArgKeys ) ) {
				$helpGeneral .= $help;
			} else {
				$helpScript .= $help;
			}
		}
		print ($helpScript ? "Parameters to this script:$helpScript" : '')
			. ($helpScript && $helpGeneral ? "\n\n" : '')
			. ($helpGeneral ? "General parameters:$helpGeneral" : '');
	}

	/** @param $seconds int */
	protected function wait( $seconds, $message = '' ) {
		print $message;
		$backspace = chr(8);
		for ( $i = $seconds; $i >= 0; $i-- ) {
			if ( $i != $seconds ) {
				$prevNrLen = strlen( $i + 1 );
				// We have to both print backspaces, spaces and then backspaces again. On
				// MacOSX, backspaces only move the cursor, it doesn't hide the characters.
				// So we overwrite with spaces and then back up (10|->9| instead of 10|->9|0)
				print str_repeat( $backspace, $prevNrLen ) . str_repeat( ' ', $prevNrLen )
					. str_repeat( $backspace, $prevNrLen );
			}
			print $i;
			flush();
			if ( $i > 0 ) {
				sleep( 1 );
			}
		}
		print "\n";
	}

	/**
	 * @param string $action: Correct grammar "This script will $action!"
	 */
	protected function timeWarningForScriptWill( $action, $seconds = 10 ) {
		$this->wait( 10, "WARNING: This script will $action! You can abort now with Control-C. Starting in " );
	}

	/**
	 * @param string $message: [optional] text before the input.
	 */
	protected function cliInput( $prefix = '> ' ) {
		static $isatty = null;
		if ( $this->getOption( 'quiet' ) ) {
			return '';
		}
		if ( $isatty === null ) {
			// Both `echo "foo" | php script.php` and `php script.php > foo.txt`
			// are being prevented.
			$isatty = posix_isatty( STDIN ) && posix_isatty( STDOUT );

			// No need to re-run this check each time, we abort within the if-null check
			if ( !$isatty ) {
				$this->error( 'This script requires an interactive terminal for output and input. Use --quiet to skip input requests.' );
			}
		}

		if ( function_exists( 'readline' ) ) {
			// Use readline if available, it's much nicer to work with for the user
			// (autocompletion of filenames and no weird characters when using arrow keys)
			return readline( $prefix );
		} else {
			// Readline is not available on Windows platforms (php.net/intro.readline)
			$this->outRaw( $prefix );
			return rtrim( fgets( STDIN ), "\n" );
		}
	}

	protected function out( $text ) {
		$this->outRaw( "$text\n" );
	}

	protected function outRaw( $text ) {
		if ( !$this->getOption( 'quiet' ) ) {
			print $text;
		}
	}

	protected function error( $msg ) {
		$msg = "Error: $msg\n";
		fwrite( STDERR, $msg );
		exit( E_ERROR );
	}

	public static function newFromContext( TestSwarmContext $context ) {
		$script = new static();
		$script->context = $context;
		return $script;
	}

	final protected function getContext() {
		return $this->context;
	}

	final private function __construct() {}
}
Back to Top