MDL-29091 weblib - Introduce out_as_local_url() method to moodle_url

This can be used for passing around PARAM_LOCALURL's where necessary
stripping the wwwroot
This commit is contained in:
Dan Poltawski 2011-08-24 15:20:46 +01:00 committed by kordan
parent a519d915bf
commit 34c0d46e94
2 changed files with 31 additions and 0 deletions

View File

@ -127,6 +127,15 @@ class web_test extends UnitTestCase {
$this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
}
function test_out_as_local_url() {
$url1 = new moodle_url('/lib/simpletest/testweblib.php');
$this->assertEqual('/lib/simpletest/testweblib.php', $url1->out_as_local_url());
$url2 = new moodle_url('http://www.google.com/lib/simpletest/testweblib.php');
$this->expectException('coding_exception');
$url2->out_as_local_url();
}
public function test_html_to_text_simple() {
$this->assertEqual("\n\n_Hello_ WORLD!", html_to_text('<p><i>Hello</i> <b>world</b>!</p>'));
}

View File

@ -717,6 +717,28 @@ class moodle_url {
$urlbase = "$CFG->wwwroot/file.php";
return self::make_file_url($urlbase, '/'.$courseid.'/'.$filepath, $forcedownload);
}
/**
* Returns URL a relative path from $CFG->wwwroot
*
* Can be used for passing around urls with the wwwroot stripped
*
* @param boolean $escaped Use &amp; as params separator instead of plain &
* @param array $overrideparams params to add to the output url, these override existing ones with the same name.
* @return string Resulting URL
* @throws coding_exception if called on a non-local url
*/
public function out_as_local_url($escaped = true, array $overrideparams = null) {
global $CFG;
$url = $this->out($escaped, $overrideparams);
if (strpos($url, $CFG->wwwroot) !== 0) {
throw new coding_exception('out_as_local_url called on a non-local URL');
}
return str_replace($CFG->wwwroot, '', $url);
}
}
/**