From 028bbc5b17e682af70a162b7d7e799961a8b4f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 15 Sep 2015 23:01:09 +0200 Subject: [PATCH 1/2] MDL-51423 clilib: Introduce cli_write() and cli_writeln() functions These are supposed to provide unified API for outputting texts from the CLI scripts, without the need to mix other cli_* functions with echo() and manually concatenated end-of-line character. --- lib/clilib.php | 43 +++++++++++++++++++++++++++++++------------ lib/upgrade.txt | 2 ++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/clilib.php b/lib/clilib.php index 962a700f16e..4225c20eb6b 100644 --- a/lib/clilib.php +++ b/lib/clilib.php @@ -26,6 +26,26 @@ // NOTE: no MOODLE_INTERNAL test here, sometimes we use this before requiring Moodle libs! +/** + * Write a text to the given stream + * + * @param string $text text to be written + * @param resource $stream output stream to be written to, defaults to STDOUT + */ +function cli_write($text, $stream=STDOUT) { + fwrite($stream, $text); +} + +/** + * Write a text followed by an end of line symbol to the given stream + * + * @param string $text text to be written + * @param resource $stream output stream to be written to, defaults to STDOUT + */ +function cli_writeln($text, $stream=STDOUT) { + cli_write($text.PHP_EOL, $stream); +} + /** * Get input from user * @param string $prompt text prompt, should include possible options @@ -35,8 +55,8 @@ * @return string entered text */ function cli_input($prompt, $default='', array $options=null, $casesensitiveoptions=false) { - echo $prompt; - echo "\n: "; + cli_writeln($prompt); + cli_write(': '); $input = fread(STDIN, 2048); $input = trim($input); if ($input === '') { @@ -47,7 +67,7 @@ function cli_input($prompt, $default='', array $options=null, $casesensitiveopti $input = strtolower($input); } if (!in_array($input, $options)) { - echo "Incorrect value, please retry.\n"; // TODO: localize, mark as needed in install + cli_writeln('Incorrect value, please retry.'); // TODO: localize, mark as needed in install return cli_input($prompt, $default, $options, $casesensitiveoptions); } } @@ -131,11 +151,11 @@ function cli_get_params(array $longoptions, array $shortmapping=null) { * @return mixed void or string */ function cli_separator($return=false) { - $separator = str_repeat('-', 79)."\n"; + $separator = str_repeat('-', 79).PHP_EOL; if ($return) { return $separator; } else { - echo $separator; + cli_write($separator); } } @@ -146,11 +166,11 @@ function cli_separator($return=false) { * @return mixed void or string */ function cli_heading($string, $return=false) { - $string = "== $string ==\n"; + $string = "== $string ==".PHP_EOL; if ($return) { return $string; } else { - echo $string; + cli_write($string); } } @@ -160,19 +180,18 @@ function cli_heading($string, $return=false) { * @return void */ function cli_problem($text) { - fwrite(STDERR, $text."\n"); + cli_writeln($text, STDERR); } /** - * Write to standard out and error with exit in error. + * Write to standard error output and exit with the given code * * @param string $text * @param int $errorcode * @return void (does not return) */ function cli_error($text, $errorcode=1) { - fwrite(STDERR, $text); - fwrite(STDERR, "\n"); + cli_writeln($text.PHP_EOL, STDERR); die($errorcode); } @@ -205,6 +224,6 @@ function cli_logo($padding=2, $return=false) { if ($return) { return $logo; } else { - echo $logo; + cli_write($logo); } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 3ff40604582..0d0ab85de75 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -138,6 +138,8 @@ information provided here is intended especially for developers. * external_api::validate_context now is public, it can be called from other classes. * rss_error() now supports returning of correct HTTP status of error and will return '404 Not Found' unless other status is specified. +* The clilib.php provides two new functions cli_write() and cli_writeln() that should be used for outputting texts from the command + line interface scripts. === 2.9.1 === From c0e8a8a52627d57eeae5dc4104c2325bddb20890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 15 Sep 2015 23:11:01 +0200 Subject: [PATCH 2/2] MDL-51423 clilib: Fix the hard-coded English text with proper string The string already exists and is part of the install language packs. This was just forgotten TODO. --- lib/clilib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clilib.php b/lib/clilib.php index 4225c20eb6b..ba243f1029e 100644 --- a/lib/clilib.php +++ b/lib/clilib.php @@ -67,7 +67,7 @@ function cli_input($prompt, $default='', array $options=null, $casesensitiveopti $input = strtolower($input); } if (!in_array($input, $options)) { - cli_writeln('Incorrect value, please retry.'); // TODO: localize, mark as needed in install + cli_writeln(get_string('cliincorrectvalueretry', 'admin')); return cli_input($prompt, $default, $options, $casesensitiveoptions); } }