Tsugi

CCFileBase
in package

Expand and canonicalize Common Cartridge course-local URLs in HTML.

At rest in the database, course-local URLs use the historical token concatenated onto a relative path with no slash after the token:

$IMS-CC-FILEBASE$pages/week2
$IMS-CC-FILEBASE$files/images/example.png

That is the Common Cartridge spelling, not LTI. The implementation guide's examples are $TOKEN$images/icon.gif (no slash between the closing $ and the path). The token is a flag for importers; the path after it is relative to the resource base directory.

Tags
link

CC 1.4 Implementation Guide §5.3.3 (example: $1EdTech-CC-FILEBASE$images/icon.gif)

CC 1.4 Implementation Guide §5.5 (example: $1EdTech-CC-FILEBASE$../images/icon.gif)

CC 1.2 Implementation Guide §3.4.3.3 (same construction; older docs used $IMS-CC-FILEBASE$)

Interactive use (browser render, rich-text editor) expands the token to the current course base URL. Saving converts current-course URLs back to the token. Extra slashes after the token or in the path are collapsed so join never produces //. Export policy for $1EdTech-CC-FILEBASE$ vs $IMS-CC-FILEBASE$ is out of scope here; storage stays on the historical $IMS-CC-FILEBASE$ form.

Callers pass any first-path-segment allowlist (e.g. pages, files). This class does not know about Tsugi controllers.

Table of Contents

Constants

TOKEN  : mixed = '$IMS-CC-FILEBASE$'
Historical Common Cartridge file-base token.
TOKEN_ALIASES  : mixed = array('$IMS-CC-FILEBASE$', '$1EdTech-CC-FILEBAS...
Token spellings recognized on input. The first entry is the storage form.
URL_ATTRIBUTES  : mixed = array('href', 'src', 'poster', 'action', 'forma...
HTML attributes that carry a single URL.

Methods

canonicalize()  : string
Convert current-course URLs (and token aliases) to $IMS-CC-FILEBASE$.
courseBaseUrl()  : string
Build a course file-base URL from a request path prefix and site home.
courseRemainder()  : string|null
Remainder after the course prefix, or null if the URL is not course-local.
expand()  : string
Convert FILEBASE tokens and historical course-local URLs to the current course URL.
rewriteUrl()  : string
collapseSlashRuns()  : string
Collapse duplicate slashes in a path. Does not touch a URL scheme.
collapseUrlPathSlashes()  : string
Collapse duplicate slashes in the path of an absolute or path-absolute URL.
containsFileBaseToken()  : bool
fragmentFromUrl()  : string|null
joinCourseUrl()  : string
normalizeRemainder()  : string
Remainder with no leading slash and no duplicate slashes in the path.
pathHasDotDot()  : bool
portsCompatible()  : bool
prefixCandidates()  : array<int, array{scheme: ?string, host: ?string, port: ?int, path: string}>
Prefixes to try when matching a URL to the current course, longest first.
queryFromUrl()  : string|null
remainderAfterPrefix()  : string|null
remainderFromToken()  : string|null
remainderStartsWithLocalPrefix()  : bool
rewriteHtml()  : string
rewriteSrcset()  : string
shouldSkipUrl()  : bool
splitUrl()  : array{scheme: ?string, host: ?string, port: ?int, path: string, query: ?string, fragment: ?string, original: string}|null
stripPathPrefix()  : string|null

Constants

TOKEN_ALIASES

Token spellings recognized on input. The first entry is the storage form.

public mixed TOKEN_ALIASES = array('$IMS-CC-FILEBASE$', '$1EdTech-CC-FILEBASE$', '$IMS_CC_FILEBASE$')

URL_ATTRIBUTES

HTML attributes that carry a single URL.

public mixed URL_ATTRIBUTES = array('href', 'src', 'poster', 'action', 'formaction', 'cite', 'data', 'data-oembed-url', 'longdesc', 'background')

Methods

canonicalize()

Convert current-course URLs (and token aliases) to $IMS-CC-FILEBASE$.

public static canonicalize(string $html, string $courseBaseUrl[, array<string|int, string> $courseLocalPrefixes = array() ]) : string

..

Parameters
$html : string
$courseBaseUrl : string
$courseLocalPrefixes : array<string|int, string> = array()
Return values
string

courseBaseUrl()

Build a course file-base URL from a request path prefix and site home.

public static courseBaseUrl(string $pathPrefix[, string|null $absoluteHome = null ]) : string
Parameters
$pathPrefix : string

toolParent() value, e.g. '/courses/2' or '/py4e' or ''

$absoluteHome : string|null = null

$CFG->apphome or $CFG->wwwroot

Return values
string —

Course base with no trailing slash

courseRemainder()

Remainder after the course prefix, or null if the URL is not course-local.

public static courseRemainder(string $url, string $courseBaseUrl[, array<string|int, string> $courseLocalPrefixes = array() ]) : string|null

Query string and fragment are kept on the remainder.

Parameters
$url : string
$courseBaseUrl : string
$courseLocalPrefixes : array<string|int, string> = array()
Return values
string|null

expand()

Convert FILEBASE tokens and historical course-local URLs to the current course URL.

public static expand(string $html, string $courseBaseUrl[, array<string|int, string> $courseLocalPrefixes = array() ]) : string
Parameters
$html : string
$courseBaseUrl : string
$courseLocalPrefixes : array<string|int, string> = array()

First path segments the caller treats as course-local (e.g. pages, files). Empty means: rewrite any path under a non-empty course base, but not origin-only URLs.

Return values
string

rewriteUrl()

public static rewriteUrl(string $url, string $courseBaseUrl, string $mode[, array<string|int, string> $courseLocalPrefixes = array() ]) : string
Parameters
$url : string
$courseBaseUrl : string
$mode : string
$courseLocalPrefixes : array<string|int, string> = array()
Return values
string

collapseSlashRuns()

Collapse duplicate slashes in a path. Does not touch a URL scheme.

private static collapseSlashRuns(string $path) : string
Parameters
$path : string
Return values
string

collapseUrlPathSlashes()

Collapse duplicate slashes in the path of an absolute or path-absolute URL.

private static collapseUrlPathSlashes(string $url) : string
Parameters
$url : string
Return values
string

containsFileBaseToken()

private static containsFileBaseToken(string $html) : bool
Parameters
$html : string
Return values
bool

fragmentFromUrl()

private static fragmentFromUrl(string $url, array<string|int, mixed> $parts) : string|null
Parameters
$url : string
$parts : array<string|int, mixed>
Return values
string|null

joinCourseUrl()

private static joinCourseUrl(string $courseBaseUrl, string $remainder) : string
Parameters
$courseBaseUrl : string
$remainder : string
Return values
string

normalizeRemainder()

Remainder with no leading slash and no duplicate slashes in the path.

private static normalizeRemainder(string $remainder) : string

Query string and fragment are left intact (so http:// in a query stays).

Parameters
$remainder : string
Return values
string

pathHasDotDot()

private static pathHasDotDot(string $remainder) : bool
Parameters
$remainder : string
Return values
bool

portsCompatible()

private static portsCompatible(int|null $a, int|null $b, string|null $schemeA, string|null $schemeB) : bool
Parameters
$a : int|null
$b : int|null
$schemeA : string|null
$schemeB : string|null
Return values
bool

prefixCandidates()

Prefixes to try when matching a URL to the current course, longest first.

private static prefixCandidates(array<string|int, mixed> $baseParts) : array<int, array{scheme: ?string, host: ?string, port: ?int, path: string}>
Parameters
$baseParts : array<string|int, mixed>
Return values
array<int, array{scheme: ?string, host: ?string, port: ?int, path: string}>

queryFromUrl()

private static queryFromUrl(string $url, array<string|int, mixed> $parts) : string|null
Parameters
$url : string
$parts : array<string|int, mixed>
Return values
string|null

remainderAfterPrefix()

private static remainderAfterPrefix(string $url, array<string|int, mixed> $prefix) : string|null
Parameters
$url : string
$prefix : array<string|int, mixed>
Return values
string|null

remainderFromToken()

private static remainderFromToken(string $url) : string|null
Parameters
$url : string
Return values
string|null —

Remainder after a FILEBASE token, or null

remainderStartsWithLocalPrefix()

private static remainderStartsWithLocalPrefix(string $remainder, array<string|int, string> $courseLocalPrefixes) : bool
Parameters
$remainder : string
$courseLocalPrefixes : array<string|int, string>
Return values
bool

rewriteHtml()

private static rewriteHtml(string $html, string $courseBaseUrl, string $mode[, array<string|int, string> $courseLocalPrefixes = array() ]) : string
Parameters
$html : string
$courseBaseUrl : string
$mode : string

expand|canonicalize

$courseLocalPrefixes : array<string|int, string> = array()
Return values
string

rewriteSrcset()

private static rewriteSrcset(string $value, string $courseBaseUrl, string $mode[, array<string|int, string> $courseLocalPrefixes = array() ]) : string
Parameters
$value : string
$courseBaseUrl : string
$mode : string
$courseLocalPrefixes : array<string|int, string> = array()
Return values
string

shouldSkipUrl()

private static shouldSkipUrl(string $url) : bool
Parameters
$url : string
Return values
bool

splitUrl()

private static splitUrl(string $url) : array{scheme: ?string, host: ?string, port: ?int, path: string, query: ?string, fragment: ?string, original: string}|null
Parameters
$url : string
Return values
array{scheme: ?string, host: ?string, port: ?int, path: string, query: ?string, fragment: ?string, original: string}|null

stripPathPrefix()

private static stripPathPrefix(string $path, string $prefix) : string|null
Parameters
$path : string
$prefix : string
Return values
string|null —

Path after prefix, no leading slash; null if not under prefix


        
On this page

Search results