diff --git a/classes/script_metadata.php b/classes/script_metadata.php index 129591b..dc68ee7 100644 --- a/classes/script_metadata.php +++ b/classes/script_metadata.php @@ -205,23 +205,40 @@ public static function get_redactable_param_names(): array { } /** - * Gets the name of the script. - * + * Gets the name of the script. Parameters share the names of globals as a hint to the caller. + * @param string|null $me + * @param string|null $script * @return string the request path for this profile. */ - public static function get_request(): string { - global $SCRIPT, $ME, $CFG; + public static function get_normalised_relative_script_path($me, $script): string { + if (isset($me)) { + $scriptpath = $me; + } else if (isset($script)) { + $scriptpath = $script; + } else { + return self::REQUEST_UNKNOWN; + } - if (!isset($ME)) { - // If set, it will trim off the leading '/' to normalise web & cli requests. - $request = isset($SCRIPT) ? ltrim($SCRIPT, '/') : self::REQUEST_UNKNOWN; - return $request; + // Remove consecutive slashes. + $scriptpath = preg_replace('/\/+/', '/', $scriptpath); + + // Strip pathinfo. + $scriptpath = preg_replace('/\.php.*$/', '.php', $scriptpath, 1); + + // Strip off the query string ($ME includes this). + $scriptpath = preg_replace('/\?.*$/', '', $scriptpath); + + // Remove 'index.php' from the end ($SCRIPT includes this). + $scriptpath = preg_replace('/\/index\.php$/', '', $scriptpath, 1); + + // Remove leading and trailing slashes. + $scriptpath = trim($scriptpath, '/'); + + if ($scriptpath === '') { + return '/'; } - $request = (new \moodle_url($ME))->out_omit_querystring(); - $request = str_replace($CFG->wwwroot, '', $request); - $request = ltrim($request, '/'); - return $request; + return $scriptpath; } /** @@ -401,7 +418,7 @@ public static function get_timer_interval(): float { public static function get_sampling_period(): float { $period = get_config('tool_excimer', 'sample_ms') / 1000; $insensiblerange = $period >= self::SAMPLING_PERIOD_MIN && $period <= self::SAMPLING_PERIOD_MAX; - if (! $insensiblerange) { + if (!$insensiblerange) { set_config('sample_ms', self::SAMPLING_PERIOD_DEFAULT * 1000, 'tool_excimer'); $period = self::SAMPLING_PERIOD_DEFAULT; } diff --git a/classes/web_processor.php b/classes/web_processor.php index a56b76f..502dc9a 100644 --- a/classes/web_processor.php +++ b/classes/web_processor.php @@ -59,7 +59,9 @@ public function init(manager $manager) { // Record and set initial memory usage at this point. $memoryusage = memory_get_usage(); - $request = script_metadata::get_request(); + global $ME, $SCRIPT; + + $request = script_metadata::get_normalised_relative_script_path($ME, $SCRIPT); $starttime = (int) $manager->get_starttime(); $this->sampleset = new sample_set($request, $starttime); diff --git a/tests/tool_excimer_script_metadata_test.php b/tests/tool_excimer_script_metadata_test.php index 3fce41e..6bb4385 100644 --- a/tests/tool_excimer_script_metadata_test.php +++ b/tests/tool_excimer_script_metadata_test.php @@ -188,28 +188,6 @@ public function sampling_limit_provider(): array { ]; } - /** - * Tests script_metadata::get_request(). - * @dataProvider relative_script_path_provider - * @covers \tool_excimer\script_metadata::get_request - * @param string $path - * @param string $expected - */ - public function test_get_request(string $path, string $expected) { - script_metadata::init(); - - global $SCRIPT, $ME; - $globalscript = $SCRIPT ?? null; - $globalme = $ME ?? null; - - $SCRIPT = $path; - $ME = $path; - $this->assertEquals($expected, script_metadata::get_request()); - - $SCRIPT = $globalscript; - $ME = $globalme; - } - /** * Tests script_metadata::get_normalised_relative_script_path(). * @dataProvider relative_script_path_provider @@ -229,28 +207,28 @@ public function test_get_normalised_relative_script_path(string $path, string $e */ public function relative_script_path_provider(): array { return [ - ['', ''], - ['/', ''], - ['//', ''], - ['///', ''], + ['', '/'], + ['/', '/'], + ['//', '/'], + ['///', '/'], ['home', 'home'], ['/home', 'home'], - ['home/', 'home/'], - ['/home/', 'home/'], + ['home/', 'home'], + ['/home/', 'home'], ['hello/world', 'hello/world'], ['/hello/world', 'hello/world'], - ['/hello/world/', 'hello/world/'], - ['/////hello////world///', 'hello////world///'], - ['/////hello////world/index.php', 'hello////world/index.php'], - ['/////hello////world///index.php', 'hello////world///index.php'], - ['/////hello////world/index.php///', 'hello////world/index.php'], - ['/index.php', 'index.php'], - ['/my//index.php////', 'my//index.php'], - ['/my//index.php////index.php', 'my//index.php'], - ['/my//index.php////index.php/', 'my//index.php'], - ['/my//index.php////index.php/hello/world?param=value¶m2=value', 'my//index.php'], - ['https://example.com//index.php', 'https://example.com//index.php'], - ['https://example.com/index.php/', 'https://example.com/index.php'], + ['/hello/world/', 'hello/world'], + ['/////hello////world///', 'hello/world'], + ['/////hello////world/index.php', 'hello/world'], + ['/////hello////world///index.php', 'hello/world'], + ['/////hello////world/index.php///', 'hello/world'], + ['/index.php', '/'], + ['/my//index.php////', 'my'], + ['/my//index.php////index.php', 'my'], + ['/my//index.php////index.php/', 'my'], + ['/my//index.php////index.php/hello/world?param=value¶m2=value', 'my'], + ['https://example.com//index.php', 'https:/example.com'], + ['https://example.com/index.php/', 'https:/example.com'], ]; }