public function validateTitle($title) { if (!$title) { // No form input yet return true; } try { $titleObj = Title::newFromTextThrow($title); } catch (MalformedTitleException $e) { $msg = $this->msg($e->getErrorMessage()); $params = $e->getErrorMessageParameters(); if ($params) { $msg->params($params); } return $msg->parse(); } if (!$titleObj->canExist()) { return $this->msg('changecontentmodel-title-cantexist', $titleObj->getPrefixedText())->escaped(); } $this->oldRevision = Revision::newFromTitle($titleObj) ?: false; if ($this->oldRevision) { $oldContent = $this->oldRevision->getContent(); if (!$oldContent->getContentHandler()->supportsDirectEditing()) { return $this->msg('changecontentmodel-nodirectediting')->params(ContentHandler::getLocalizedName($oldContent->getModel()))->escaped(); } } return true; }
/** * See also mediawiki.Title.test.js * @covers Title::secureAndSplit * @dataProvider provideInvalidSecureAndSplit * @note This mainly tests MediaWikiTitleCodec::parseTitle(). */ public function testSecureAndSplitInvalid($text, $expectedErrorMessage) { $this->secureAndSplitGlobals(); try { Title::newFromTextThrow($text); // should throw $this->assertTrue(false, "Invalid: {$text}"); } catch (MalformedTitleException $ex) { $this->assertEquals($expectedErrorMessage, $ex->getErrorMessage(), "Invalid: {$text}"); } }
public function validateTitle($title) { if (!$title) { // No form input yet return true; } // Already validated by HTMLForm, but if not, throw // and exception instead of a fatal $titleObj = Title::newFromTextThrow($title); $this->oldRevision = Revision::newFromTitle($titleObj) ?: false; if ($this->oldRevision) { $oldContent = $this->oldRevision->getContent(); if (!$oldContent->getContentHandler()->supportsDirectEditing()) { return $this->msg('changecontentmodel-nodirectediting')->params(ContentHandler::getLocalizedName($oldContent->getModel()))->escaped(); } } return true; }
public function validate($value, $alldata) { if ($this->mParent->getMethod() === 'get' && $value === '') { // If the form is a GET form and has no value, assume it hasn't been // submitted yet, and skip validation return parent::validate($value, $alldata); } try { if (!$this->mParams['relative']) { $title = Title::newFromTextThrow($value); } else { // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions global $wgContLang; $namespaceName = $wgContLang->getNsText($this->mParams['namespace']); $title = Title::newFromTextThrow($namespaceName . ':' . $value); } } catch (MalformedTitleException $e) { $msg = $this->msg($e->getErrorMessage()); $params = $e->getErrorMessageParameters(); if ($params) { $msg->params($params); } return $msg->parse(); } $text = $title->getPrefixedText(); if ($this->mParams['namespace'] !== false && !$title->inNamespace($this->mParams['namespace'])) { return $this->msg('htmlform-title-badnamespace', $this->mParams['namespace'], $text)->parse(); } if ($this->mParams['creatable'] && !$title->canExist()) { return $this->msg('htmlform-title-not-creatable', $text)->escaped(); } if ($this->mParams['exists'] && !$title->exists()) { return $this->msg('htmlform-title-not-exists', $text)->parse(); } return parent::validate($value, $alldata); }
public function validate($value, $alldata) { try { $title = Title::newFromTextThrow($value); } catch (MalformedTitleException $e) { $msg = $this->msg($e->getErrorMessage()); $params = $e->getErrorMessageParameters(); if ($params) { $msg->params($params); } return $msg->parse(); } $text = $title->getPrefixedText(); if ($this->mParams['namespace'] !== false && !$title->inNamespace($this->mParams['namespace'])) { return $this->msg('htmlform-title-badnamespace', $this->mParams['namespace'], $text)->parse(); } if ($this->mParams['creatable'] && !$title->canExist()) { return $this->msg('htmlform-title-not-creatable', $text)->escaped(); } if ($this->mParams['exists'] && !$title->exists()) { return $this->msg('htmlform-title-not-exists', $text)->parse(); } return parent::validate($value, $alldata); }
/** * Create a new Title from text, such as what one would find in a link. De- * codes any HTML entities in the text. * * @param string $text The link text; spaces, prefixes, and an * initial ':' indicating the main namespace are accepted. * @param int $defaultNamespace The namespace to use if none is specified * by a prefix. If you want to force a specific namespace even if * $text might begin with a namespace prefix, use makeTitle() or * makeTitleSafe(). * @throws InvalidArgumentException * @return Title|null Title or null on an error. */ public static function newFromText($text, $defaultNamespace = NS_MAIN) { 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; } }
/** * Create a new Title from text, such as what one would find in a link. De- * codes any HTML entities in the text. * * @param string|null $text The link text; spaces, prefixes, and an * initial ':' indicating the main namespace are accepted. * @param int $defaultNamespace The namespace to use if none is specified * by a prefix. If you want to force a specific namespace even if * $text might begin with a namespace prefix, use makeTitle() or * makeTitleSafe(). * @throws InvalidArgumentException * @return Title|null Title or null on an error. */ public static function newFromText($text, $defaultNamespace = NS_MAIN) { if (is_object($text)) { throw new InvalidArgumentException('$text must be a string.'); } if ($text !== null && !is_string($text)) { wfDebugLog('T76305', wfGetAllCallers(5)); return null; } if ($text === null) { return null; } try { return Title::newFromTextThrow($text, $defaultNamespace); } catch (MalformedTitleException $ex) { return null; } }
/** * Create a new Title from text, such as what one would find in a link. De- * codes any HTML entities in the text. * * @param string|int|null $text The link text; spaces, prefixes, and an * initial ':' indicating the main namespace are accepted. * @param int $defaultNamespace The namespace to use if none is specified * by a prefix. If you want to force a specific namespace even if * $text might begin with a namespace prefix, use makeTitle() or * makeTitleSafe(). * @throws InvalidArgumentException * @return Title|null Title or null on an error. */ public static function newFromText($text, $defaultNamespace = NS_MAIN) { // DWIM: Integers can be passed in here when page titles are used as array keys. if ($text !== null && !is_string($text) && !is_int($text)) { throw new InvalidArgumentException('$text must be a string.'); } if ($text === null) { return null; } try { return Title::newFromTextThrow(strval($text), $defaultNamespace); } catch (MalformedTitleException $ex) { return null; } }
/** * Parse the request to get the Title object * * @throws MalformedTitleException If a title has been provided by the user, but is invalid. * @return Title Title object to be $wgTitle */ private function parseTitle() { global $wgContLang; $request = $this->context->getRequest(); $curid = $request->getInt('curid'); $title = $request->getVal('title'); $action = $request->getVal('action'); if ($request->getCheck('search')) { // Compatibility with old search URLs which didn't use Special:Search // Just check for presence here, so blank requests still // show the search page when using ugly URLs (bug 8054). $ret = SpecialPage::getTitleFor('Search'); } elseif ($curid) { // URLs like this are generated by RC, because rc_title isn't always accurate $ret = Title::newFromID($curid); } else { $ret = Title::newFromURL($title); // Alias NS_MEDIA page URLs to NS_FILE...we only use NS_MEDIA // in wikitext links to tell Parser to make a direct file link if (!is_null($ret) && $ret->getNamespace() == NS_MEDIA) { $ret = Title::makeTitle(NS_FILE, $ret->getDBkey()); } // Check variant links so that interwiki links don't have to worry // about the possible different language variants if (count($wgContLang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0) { $wgContLang->findVariantLink($title, $ret); } } // If title is not provided, always allow oldid and diff to set the title. // If title is provided, allow oldid and diff to override the title, unless // we are talking about a special page which might use these parameters for // other purposes. if ($ret === null || !$ret->isSpecialPage()) { // We can have urls with just ?diff=,?oldid= or even just ?diff= $oldid = $request->getInt('oldid'); $oldid = $oldid ? $oldid : $request->getInt('diff'); // Allow oldid to override a changed or missing title if ($oldid) { $rev = Revision::newFromId($oldid); $ret = $rev ? $rev->getTitle() : $ret; } } // Use the main page as default title if nothing else has been provided if ($ret === null && strval($title) === '' && !$request->getCheck('curid') && $action !== 'delete') { $ret = Title::newMainPage(); } if ($ret === null || $ret->getDBkey() == '' && !$ret->isExternal()) { // If we get here, we definitely don't have a valid title; throw an exception. // Try to get detailed invalid title exception first, fall back to MalformedTitleException. Title::newFromTextThrow($title); throw new MalformedTitleException('badtitletext', $title); } return $ret; }
/** * Given an array of title strings, convert them into Title objects. * Alternatively, an array of Title objects may be given. * This method validates access rights for the title, * and appends normalization values to the output. * * @param array $titles Array of Title objects or strings * @return LinkBatch */ private function processTitlesArray($titles) { $usernames = array(); $linkBatch = new LinkBatch(); foreach ($titles as $title) { if (is_string($title)) { try { $titleObj = Title::newFromTextThrow($title, $this->mDefaultNamespace); } catch (MalformedTitleException $ex) { // Handle invalid titles gracefully $this->mAllPages[0][$title] = $this->mFakePageId; $this->mInvalidTitles[$this->mFakePageId] = array('title' => $title, 'invalidreason' => $ex->getMessage()); $this->mFakePageId--; continue; // There's nothing else we can do } } else { $titleObj = $title; } $unconvertedTitle = $titleObj->getPrefixedText(); $titleWasConverted = false; if ($titleObj->isExternal()) { // This title is an interwiki link. $this->mInterwikiTitles[$unconvertedTitle] = $titleObj->getInterwiki(); } else { // Variants checking global $wgContLang; if ($this->mConvertTitles && count($wgContLang->getVariants()) > 1 && !$titleObj->exists()) { // Language::findVariantLink will modify titleText and titleObj into // the canonical variant if possible $titleText = is_string($title) ? $title : $titleObj->getPrefixedText(); $wgContLang->findVariantLink($titleText, $titleObj); $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText(); } if ($titleObj->getNamespace() < 0) { // Handle Special and Media pages $titleObj = $titleObj->fixSpecialName(); $this->mSpecialTitles[$this->mFakePageId] = $titleObj; $this->mFakePageId--; } else { // Regular page $linkBatch->addObj($titleObj); } } // Make sure we remember the original title that was // given to us. This way the caller can correlate new // titles with the originally requested when e.g. the // namespace is localized or the capitalization is // different if ($titleWasConverted) { $this->mConvertedTitles[$unconvertedTitle] = $titleObj->getPrefixedText(); // In this case the page can't be Special. if (is_string($title) && $title !== $unconvertedTitle) { $this->mNormalizedTitles[$title] = $unconvertedTitle; } } elseif (is_string($title) && $title !== $titleObj->getPrefixedText()) { $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText(); } // Need gender information if (MWNamespace::hasGenderDistinction($titleObj->getNamespace())) { $usernames[] = $titleObj->getText(); } } // Get gender information $genderCache = GenderCache::singleton(); $genderCache->doQuery($usernames, __METHOD__); return $linkBatch; }