X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FTitle.php;h=601211d1b11cc40e4d3ae9d432088e8e8100f976;hb=448c7ea03aef4194bc7cff8987c2b9d4cd379d8b;hp=b0df15f044f5494373032ba928aef2b0bc13eadd;hpb=0d1487dd630b74f5c21146f9c14a97189eb7dab3;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Title.php b/includes/Title.php index b0df15f044..601211d1b1 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -225,9 +225,11 @@ class Title { public static function newFromDBkey( $key ) { $t = new Title(); $t->mDbkeyform = $key; - if ( $t->secureAndSplit() ) { + + try { + $t->secureAndSplit(); return $t; - } else { + } catch ( MalformedTitleException $ex ) { return null; } } @@ -263,9 +265,36 @@ class Title { if ( is_object( $text ) ) { throw new InvalidArgumentException( '$text must be a string.' ); } elseif ( !is_string( $text ) ) { + wfDebugLog( 'T76305', wfGetAllCallers( 5 ) ); wfWarn( __METHOD__ . ': $text must be a string. This will throw an InvalidArgumentException in future.', 2 ); } + try { + return Title::newFromTextThrow( $text, $defaultNamespace ); + } catch ( MalformedTitleException $ex ) { + return null; + } + } + + /** + * Like Title::newFromText(), but throws MalformedTitleException when the title is invalid, + * rather than returning null. + * + * The exception subclasses encode detailed information about why the title is invalid. + * + * @see Title::newFromText + * + * @since 1.25 + * @param string $text Title text to check + * @param int $defaultNamespace + * @throws MalformedTitleException If the title is invalid + * @return Title + */ + public static function newFromTextThrow( $text, $defaultNamespace = NS_MAIN ) { + if ( is_object( $text ) ) { + throw new MWException( 'Title::newFromTextThrow given an object' ); + } + $cache = self::getTitleCache(); /** @@ -287,14 +316,11 @@ class Title { $t->mDbkeyform = str_replace( ' ', '_', $filteredText ); $t->mDefaultNamespace = intval( $defaultNamespace ); - if ( $t->secureAndSplit() ) { - if ( $defaultNamespace == NS_MAIN ) { - $cache->set( $text, $t ); - } - return $t; - } else { - return null; + $t->secureAndSplit(); + if ( $defaultNamespace == NS_MAIN ) { + $cache->set( $text, $t ); } + return $t; } /** @@ -323,9 +349,11 @@ class Title { } $t->mDbkeyform = str_replace( ' ', '_', $url ); - if ( $t->secureAndSplit() ) { + + try { + $t->secureAndSplit(); return $t; - } else { + } catch ( MalformedTitleException $ex ) { return null; } } @@ -507,9 +535,11 @@ class Title { $t = new Title(); $t->mDbkeyform = Title::makeName( $ns, $title, $fragment, $interwiki, true ); - if ( $t->secureAndSplit() ) { + + try { + $t->secureAndSplit(); return $t; - } else { + } catch ( MalformedTitleException $ex ) { return null; } } @@ -940,7 +970,6 @@ class Title { /** * Get the page's content model id, see the CONTENT_MODEL_XXX constants. * - * @throws MWException * @param int $flags A bit field; may be Title::GAID_FOR_UPDATE to select for update * @return string Content model id */ @@ -955,10 +984,6 @@ class Title { $this->mContentModel = ContentHandler::getDefaultModelFor( $this ); } - if ( !$this->mContentModel ) { - throw new MWException( 'Failed to determine content model!' ); - } - return $this->mContentModel; } @@ -3310,6 +3335,7 @@ class Title { * namespace prefixes, sets the other forms, and canonicalizes * everything. * + * @throws MalformedTitleException On invalid titles * @return bool True on success */ private function secureAndSplit() { @@ -3320,15 +3346,12 @@ class Title { $dbkey = $this->mDbkeyform; - try { - // @note: splitTitleString() is a temporary hack to allow MediaWikiTitleCodec to share - // the parsing code with Title, while avoiding massive refactoring. - // @todo: get rid of secureAndSplit, refactor parsing code. - $titleParser = self::getTitleParser(); - $parts = $titleParser->splitTitleString( $dbkey, $this->getDefaultNamespace() ); - } catch ( MalformedTitleException $ex ) { - return false; - } + // @note: splitTitleString() is a temporary hack to allow MediaWikiTitleCodec to share + // the parsing code with Title, while avoiding massive refactoring. + // @todo: get rid of secureAndSplit, refactor parsing code. + $titleParser = self::getTitleParser(); + // MalformedTitleException can be thrown here + $parts = $titleParser->splitTitleString( $dbkey, $this->getDefaultNamespace() ); # Fill fields $this->setFragment( '#' . $parts['fragment'] ); @@ -4213,10 +4236,12 @@ class Title { * If you want to know if a title can be meaningfully viewed, you should * probably call the isKnown() method instead. * + * @param int $flags An optional bit field; may be Title::GAID_FOR_UPDATE to check + * from master/for update * @return bool */ - public function exists() { - $exists = $this->getArticleID() != 0; + public function exists( $flags = 0 ) { + $exists = $this->getArticleID( $flags ) != 0; Hooks::run( 'TitleExists', array( $this, &$exists ) ); return $exists; }