From a1789ddde42033f1b05cc4929491214ee6e79383 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Thu, 17 Dec 2015 09:15:42 +0100 Subject: Update to MediaWiki 1.26.0 --- includes/title/MalformedTitleException.php | 54 ++++++++++++++++++++++++++---- includes/title/MediaWikiTitleCodec.php | 34 +++++++++---------- includes/title/TitleValue.php | 24 ++++--------- 3 files changed, 70 insertions(+), 42 deletions(-) (limited to 'includes/title') diff --git a/includes/title/MalformedTitleException.php b/includes/title/MalformedTitleException.php index a9e58b3e..0892ce4e 100644 --- a/includes/title/MalformedTitleException.php +++ b/includes/title/MalformedTitleException.php @@ -1,7 +1,5 @@ errorMessage = $errorMessage; + $this->titleText = $titleText; + if ( $titleText !== null ) { + $errorMessageParameters[] = $titleText; + } + $this->errorMessageParameters = $errorMessageParameters; + + // Supply something useful for Exception::getMessage() to return. + $enMsg = wfMessage( $errorMessage, $errorMessageParameters ); + $enMsg->inLanguage( 'en' )->useDatabase( false ); + parent::__construct( $enMsg->text() ); + } + + /** + * @since 1.26 + * @return string|null + */ + public function getTitleText() { + return $this->titleText; + } + + /** + * @since 1.26 + * @return string|null + */ + public function getErrorMessage() { + return $this->errorMessage; + } + + /** + * @since 1.26 + * @return string[] + */ + public function getErrorMessageParameters() { + return $this->errorMessageParameters; + } } diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index 20034b74..01575ac0 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -137,12 +137,12 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { // Interwiki links are not supported by TitleValue if ( $parts['interwiki'] !== '' ) { - throw new MalformedTitleException( 'Title must not contain an interwiki prefix: ' . $text ); + throw new MalformedTitleException( 'title-invalid-interwiki', $text ); } // Relative fragment links are not supported by TitleValue if ( $parts['dbkey'] === '' ) { - throw new MalformedTitleException( 'Title must not be empty: ' . $text ); + throw new MalformedTitleException( 'title-invalid-empty', $text ); } return new TitleValue( $parts['namespace'], $parts['dbkey'], $parts['fragment'] ); @@ -232,7 +232,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) { # Contained illegal UTF-8 sequences or forbidden Unicode chars. - throw new MalformedTitleException( 'Bad UTF-8 sequences found in title: ' . $text ); + throw new MalformedTitleException( 'title-invalid-utf8', $text ); } $parts['dbkey'] = $dbkey; @@ -246,7 +246,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { } if ( $dbkey == '' ) { - throw new MalformedTitleException( 'Empty title: ' . $text ); + throw new MalformedTitleException( 'title-invalid-empty', $text ); } # Namespace or interwiki prefix @@ -263,11 +263,11 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { if ( $ns == NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) { if ( $this->language->getNsIndex( $x[1] ) ) { # Disallow Talk:File:x type titles... - throw new MalformedTitleException( 'Bad namespace prefix: ' . $text ); + throw new MalformedTitleException( 'title-invalid-talk-namespace', $text ); } elseif ( Interwiki::isValidInterwiki( $x[1] ) ) { //TODO: get rid of global state! # Disallow Talk:Interwiki:x type titles... - throw new MalformedTitleException( 'Interwiki prefix found in title: ' . $text ); + throw new MalformedTitleException( 'title-invalid-talk-namespace', $text ); } } } elseif ( Interwiki::isValidInterwiki( $p ) ) { @@ -324,8 +324,9 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { # Reject illegal characters. $rxTc = self::getTitleInvalidRegex(); - if ( preg_match( $rxTc, $dbkey ) ) { - throw new MalformedTitleException( 'Illegal characters found in title: ' . $text ); + $matches = array(); + if ( preg_match( $rxTc, $dbkey, $matches ) ) { + throw new MalformedTitleException( 'title-invalid-characters', $text, array( $matches[0] ) ); } # Pages with "/./" or "/../" appearing in the URLs will often be un- @@ -343,23 +344,22 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { substr( $dbkey, -3 ) == '/..' ) ) { - throw new MalformedTitleException( 'Bad title: ' . $text ); + throw new MalformedTitleException( 'title-invalid-relative', $text ); } # Magic tilde sequences? Nu-uh! if ( strpos( $dbkey, '~~~' ) !== false ) { - throw new MalformedTitleException( 'Bad title: ' . $text ); + throw new MalformedTitleException( 'title-invalid-magic-tilde', $text ); } # Limit the size of titles to 255 bytes. This is typically the size of the # underlying database field. We make an exception for special pages, which # don't need to be stored in the database, and may edge over 255 bytes due # to subpage syntax for long titles, e.g. [[Special:Block/Long name]] - if ( - ( $parts['namespace'] != NS_SPECIAL && strlen( $dbkey ) > 255 ) - || strlen( $dbkey ) > 512 - ) { - throw new MalformedTitleException( 'Title too long: ' . substr( $dbkey, 0, 255 ) . '...' ); + $maxLength = ( $parts['namespace'] != NS_SPECIAL ) ? 255 : 512; + if ( strlen( $dbkey ) > $maxLength ) { + throw new MalformedTitleException( 'title-invalid-too-long', $text, + array( Message::numParam( $maxLength ) ) ); } # Normally, all wiki links are forced to have an initial capital letter so [[foo]] @@ -374,7 +374,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { # self-links with a fragment identifier. if ( $dbkey == '' && $parts['interwiki'] === '' ) { if ( $parts['namespace'] != NS_MAIN ) { - throw new MalformedTitleException( 'Empty title: ' . $text ); + throw new MalformedTitleException( 'title-invalid-empty', $text ); } } @@ -390,7 +390,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { // Any remaining initial :s are illegal. if ( $dbkey !== '' && ':' == $dbkey[0] ) { - throw new MalformedTitleException( 'Title must not start with a colon: ' . $text ); + throw new MalformedTitleException( 'title-invalid-leading-colon', $text ); } # Fill fields diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 5cac3470..a0f3b6f9 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -21,6 +21,7 @@ * @license GPL 2+ * @author Daniel Kinzler */ +use Wikimedia\Assert\Assert; /** * Represents a page (or page fragment) title within %MediaWiki. @@ -67,26 +68,13 @@ class TitleValue { * @throws InvalidArgumentException */ public function __construct( $namespace, $dbkey, $fragment = '' ) { - if ( !is_int( $namespace ) ) { - throw new InvalidArgumentException( '$namespace must be an integer' ); - } - - if ( !is_string( $dbkey ) ) { - throw new InvalidArgumentException( '$dbkey must be a string' ); - } + Assert::parameterType( 'integer', $namespace, '$namespace' ); + Assert::parameterType( 'string', $dbkey, '$dbkey' ); + Assert::parameterType( 'string', $fragment, '$fragment' ); // Sanity check, no full validation or normalization applied here! - if ( preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ) ) { - throw new InvalidArgumentException( '$dbkey must be a valid DB key: ' . $dbkey ); - } - - if ( !is_string( $fragment ) ) { - throw new InvalidArgumentException( '$fragment must be a string' ); - } - - if ( $dbkey === '' ) { - throw new InvalidArgumentException( '$dbkey must not be empty' ); - } + Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', 'invalid DB key' ); + Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' ); $this->namespace = $namespace; $this->dbkey = $dbkey; -- cgit v1.2.2