summaryrefslogtreecommitdiff
path: root/includes/title
diff options
context:
space:
mode:
Diffstat (limited to 'includes/title')
-rw-r--r--includes/title/MalformedTitleException.php54
-rw-r--r--includes/title/MediaWikiTitleCodec.php34
-rw-r--r--includes/title/TitleValue.php24
3 files changed, 70 insertions, 42 deletions
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 @@
<?php
/**
- * Representation of a page title within %MediaWiki.
- *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
@@ -18,16 +16,58 @@
* http://www.gnu.org/copyleft/gpl.html
*
* @file
- * @license GPL 2+
- * @author Daniel Kinzler
*/
/**
* MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
- *
- * @license GPL 2+
- * @author Daniel Kinzler
* @since 1.23
*/
class MalformedTitleException extends Exception {
+ private $titleText = null;
+ private $errorMessage = null;
+ private $errorMessageParameters = array();
+
+ /**
+ * @param string $errorMessage Localisation message describing the error (since MW 1.26)
+ * @param string $titleText The invalid title text (since MW 1.26)
+ * @param string[] $errorMessageParameters Additional parameters for the error message.
+ * $titleText will be appended if it's not null. (since MW 1.26)
+ */
+ public function __construct( $errorMessage = null, $titleText = null, $errorMessageParameters = array() ) {
+ $this->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;