diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2019-08-16 12:32:03 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2019-08-16 12:32:03 +0000 |
commit | cc8a88c3d081d26ab93a205510ca0ffff1f2e6a7 (patch) | |
tree | d64b6d490a682c2cc62141fa349ef5c8276cbe36 /includes | |
parent | 1235877b16bd4b23961ab53fa7fb1447bd31bb15 (diff) | |
parent | c503f129c3ed66f5d4865386bc0e26d11fa99a3c (diff) | |
download | mediawikicore-cc8a88c3d081d26ab93a205510ca0ffff1f2e6a7.tar.gz mediawikicore-cc8a88c3d081d26ab93a205510ca0ffff1f2e6a7.zip |
Merge "Introduce TitleParser::makeTitleValueSafe()"
Diffstat (limited to 'includes')
-rw-r--r-- | includes/title/MediaWikiTitleCodec.php | 34 | ||||
-rw-r--r-- | includes/title/TitleParser.php | 12 | ||||
-rw-r--r-- | includes/title/TitleValue.php | 8 |
3 files changed, 52 insertions, 2 deletions
diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index 5021a1c9fe9b..7e7d85a38e1c 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -186,6 +186,40 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { } /** + * Given a namespace and title, return a TitleValue if valid, or null if invalid. + * + * @param int $namespace + * @param string $text + * @param string $fragment + * @param string $interwiki + * + * @return TitleValue|null + */ + public function makeTitleValueSafe( $namespace, $text, $fragment = '', $interwiki = '' ) { + if ( !$this->nsInfo->exists( $namespace ) ) { + return null; + } + + $canonicalNs = $this->nsInfo->getCanonicalName( $namespace ); + $fullText = $canonicalNs == '' ? $text : "$canonicalNs:$text"; + if ( strval( $interwiki ) != '' ) { + $fullText = "$interwiki:$fullText"; + } + if ( strval( $fragment ) != '' ) { + $fullText .= '#' . $fragment; + } + + try { + $parts = $this->splitTitleString( $fullText ); + } catch ( MalformedTitleException $e ) { + return null; + } + + return new TitleValue( + $parts['namespace'], $parts['dbkey'], $parts['fragment'], $parts['interwiki'] ); + } + + /** * @see TitleFormatter::getText() * * @param LinkTarget $title diff --git a/includes/title/TitleParser.php b/includes/title/TitleParser.php index 8569735e82b2..0ce5ece015ac 100644 --- a/includes/title/TitleParser.php +++ b/includes/title/TitleParser.php @@ -43,4 +43,16 @@ interface TitleParser { * @return TitleValue */ public function parseTitle( $text, $defaultNamespace = NS_MAIN ); + + /** + * Given a namespace and title, return a TitleValue if valid, or null if invalid. + * + * @param int $namespace + * @param string $text + * @param string $fragment + * @param string $interwiki + * + * @return TitleValue|null + */ + public function makeTitleValueSafe( $namespace, $text, $fragment = '', $interwiki = '' ); } diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index b657f13ebd64..7abe21ba67a9 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -102,8 +102,12 @@ class TitleValue implements LinkTarget { // Sanity check, no full validation or normalization applied here! Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title', "invalid name '$title'" ); - Assert::parameter( $title !== '' || ( $fragment !== '' && $namespace === NS_MAIN ), - '$title', 'should not be empty unless namespace is main and fragment is non-empty' ); + Assert::parameter( + $title !== '' || + ( $namespace === NS_MAIN && ( $fragment !== '' || $interwiki !== '' ) ), + '$title', + 'should not be empty unless namespace is main and fragment or interwiki is non-empty' + ); $this->namespace = $namespace; $this->dbkey = strtr( $title, ' ', '_' ); |