diff --git a/classes/cli_helper.php b/classes/cli_helper.php index 7ba328f..ff42a9d 100644 --- a/classes/cli_helper.php +++ b/classes/cli_helper.php @@ -204,6 +204,10 @@ public function validate_and_clean_args(): void { } } if (isset($cliargs['ignorecat'])) { + // Allow escaping of forward slash at start in Windows. + if (strlen($cliargs['ignorecat']) > 1 && substr($cliargs['ignorecat'], 0, 2) === '//') { + $cliargs['ignorecat'] = substr($cliargs['ignorecat'], 1); + } if (@preg_match($cliargs['ignorecat'], 'zzzzzzzz') === false) { echo "\nThere is a problem with your regular expression for ignoring categories:\n"; echo error_get_last()["message"] . "\n"; diff --git a/cli/createrepo.php b/cli/createrepo.php index 6b8b4c8..7a9b9ad 100755 --- a/cli/createrepo.php +++ b/cli/createrepo.php @@ -142,7 +142,7 @@ [ 'longopt' => 'ignorecat', 'shortopt' => 'x', - 'description' => 'Regex of categories to ignore', + 'description' => 'Regex of categories to ignore - add an extra leading / for Windows.', 'default' => $ignorecat, 'variable' => 'ignorecat', 'valuerequired' => true, diff --git a/cli/deletefrommoodle.php b/cli/deletefrommoodle.php index e4a2ca0..77d50d3 100755 --- a/cli/deletefrommoodle.php +++ b/cli/deletefrommoodle.php @@ -140,7 +140,7 @@ [ 'longopt' => 'ignorecat', 'shortopt' => 'x', - 'description' => 'Regex of categories to ignore', + 'description' => 'Regex of categories to ignore - add an extra leading / for Windows.', 'default' => $ignorecat, 'variable' => 'ignorecat', 'valuerequired' => true, diff --git a/cli/exportrepofrommoodle.php b/cli/exportrepofrommoodle.php index 49c7a8f..a7491f0 100644 --- a/cli/exportrepofrommoodle.php +++ b/cli/exportrepofrommoodle.php @@ -100,7 +100,7 @@ [ 'longopt' => 'ignorecat', 'shortopt' => 'x', - 'description' => 'Regex of categories to ignore', + 'description' => 'Regex of categories to ignore - add an extra leading / for Windows.', 'default' => $ignorecat, 'variable' => 'ignorecat', 'valuerequired' => true, diff --git a/cli/importrepotomoodle.php b/cli/importrepotomoodle.php index c4075fb..0689909 100755 --- a/cli/importrepotomoodle.php +++ b/cli/importrepotomoodle.php @@ -140,7 +140,7 @@ [ 'longopt' => 'ignorecat', 'shortopt' => 'x', - 'description' => 'Regex of categories to ignore', + 'description' => 'Regex of categories to ignore - add an extra leading / for Windows.', 'default' => $ignorecat, 'variable' => 'ignorecat', 'valuerequired' => true, diff --git a/doc/createrepo.md b/doc/createrepo.md index 38dc8d0..d06ff9f 100644 --- a/doc/createrepo.md +++ b/doc/createrepo.md @@ -23,7 +23,7 @@ |n|instanceid|Numerical id of the course, module of course category. |t|token|Security token for webservice. |h|help| -|x|ignorecat|Regex of categories to ignore +|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows. ### Example 1: diff --git a/doc/deletefrommoodle.md b/doc/deletefrommoodle.md index f99a655..b4eda15 100644 --- a/doc/deletefrommoodle.md +++ b/doc/deletefrommoodle.md @@ -25,7 +25,7 @@ |n|instanceid|Numerical id of the course, module of course category. |t|token|Security token for webservice. |h|help| -|x|ignorecat|Regex of categories to ignore +|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows. Examples: diff --git a/doc/exportrepofrommoodle.md b/doc/exportrepofrommoodle.md index 2a84619..8336194 100644 --- a/doc/exportrepofrommoodle.md +++ b/doc/exportrepofrommoodle.md @@ -17,7 +17,7 @@ |q|questioncategoryid|Numerical id of subcategory to actually export. |t|token|Security token for webservice.| |h|help| -|x|ignorecat|Regex of categories to ignore +|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows. Examples: diff --git a/doc/importrepotomoodle.md b/doc/importrepotomoodle.md index 24850ec..202f02b 100644 --- a/doc/importrepotomoodle.md +++ b/doc/importrepotomoodle.md @@ -28,7 +28,7 @@ Commit this update. |n|instanceid|Numerical id of the course, module of course category. |t|token|Security token for webservice. |h|help| -|x|ignorecat|Regex of categories to ignore +|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows. Examples: diff --git a/doc/usinggit.md b/doc/usinggit.md index e00de62..22ac505 100644 --- a/doc/usinggit.md +++ b/doc/usinggit.md @@ -35,7 +35,7 @@ You can use context instance id and subcategory id instead: * `-n` the context instance id, which is the course id in this case. * `-q` the question category id -You can also ignore certain categories (and their descendants) using `-x` and a regex expression to match the category name(s) within Moodle. +You can also ignore certain categories (and their descendants) using `-x` and a regex expression to match the category name(s) within Moodle. (Add an extra leading / in Windows e.g. "//^.*DO_NOT_SHARE$/".) `php createrepo.php -l course -n 2 -d "master" -q 80 -x "/^.*DO_NOT_SHARE$/"` diff --git a/tests/cli_helper_test.php b/tests/cli_helper_test.php index 9ad4a48..e67ebe8 100644 --- a/tests/cli_helper_test.php +++ b/tests/cli_helper_test.php @@ -504,6 +504,7 @@ public function test_validation_ignorecat(): void { $helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath', 'ignorecat' => '/hello/']; $helper->validate_and_clean_args(); $this->expectOutputString(''); + $this->assertEquals('/hello/', $helper->processedoptions['ignorecat']); } /** @@ -516,4 +517,16 @@ public function test_validation_ignorecat_error(): void { @$helper->validate_and_clean_args(); $this->expectOutputRegex('/problem with your regular expression/'); } + + /** + * Validation + * @covers \gitsync\cli_helper\validate_and_clean_args() + */ + public function test_validation_ignorecat_replace(): void { + $helper = new fake_cli_helper([]); + $helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath', 'ignorecat' => '//hello\//']; + $helper->validate_and_clean_args(); + $this->expectOutputString(''); + $this->assertEquals('/hello\//', $helper->processedoptions['ignorecat']); + } }