aboutsummaryrefslogtreecommitdiffstats
path: root/includes/title/Title.php
diff options
context:
space:
mode:
authordaniel <dkinzler@wikimedia.org>2024-02-23 19:16:52 +0100
committerdaniel <dkinzler@wikimedia.org>2024-02-23 19:16:52 +0100
commit21f35aef24544bc516eb8d9de328d043af279356 (patch)
treea48e424175262778fedbe9d952aff6fcc1192857 /includes/title/Title.php
parent260fc1b50f7da1d1d395bffc60a9e784112ddcd1 (diff)
downloadmediawikicore-21f35aef24544bc516eb8d9de328d043af279356.tar.gz
mediawikicore-21f35aef24544bc516eb8d9de328d043af279356.zip
Title: be more robust when handling invalid titles
The postcondition in getRootTitle and getBaseTitle was failing when dealing with an invalid title. This patch makes these methods just return the title unchanged when it is invalid, since we cannot expect to parse invalid titles successfully. Bug: T290194 Change-Id: I14d644c42130504055e04cc59d8d7558c93e445d
Diffstat (limited to 'includes/title/Title.php')
-rw-r--r--includes/title/Title.php38
1 files changed, 30 insertions, 8 deletions
diff --git a/includes/title/Title.php b/includes/title/Title.php
index 4a40a7bb1e9f..80165a8b52e6 100644
--- a/includes/title/Title.php
+++ b/includes/title/Title.php
@@ -1971,10 +1971,21 @@ class Title implements LinkTarget, PageIdentity {
*/
public function getRootTitle() {
$title = self::makeTitleSafe( $this->mNamespace, $this->getRootText() );
- Assert::postcondition(
- $title !== null,
- 'makeTitleSafe() should always return a Title for the text returned by getRootText().'
- );
+
+ if ( !$title ) {
+ if ( !$this->isValid() ) {
+ // If the title wasn't valid in the first place, we can't expect
+ // to successfully parse it. T290194
+ return $this;
+ }
+
+ Assert::postcondition(
+ $title !== null,
+ 'makeTitleSafe() should always return a Title for the text ' .
+ 'returned by getRootText().'
+ );
+ }
+
return $title;
}
@@ -2021,10 +2032,21 @@ class Title implements LinkTarget, PageIdentity {
*/
public function getBaseTitle() {
$title = self::makeTitleSafe( $this->mNamespace, $this->getBaseText() );
- Assert::postcondition(
- $title !== null,
- 'makeTitleSafe() should always return a Title for the text returned by getBaseText().'
- );
+
+ if ( !$title ) {
+ if ( !$this->isValid() ) {
+ // If the title wasn't valid in the first place, we can't expect
+ // to successfully parse it. T290194
+ return $this;
+ }
+
+ Assert::postcondition(
+ $title !== null,
+ 'makeTitleSafe() should always return a Title for the text ' .
+ 'returned by getBaseText().'
+ );
+ }
+
return $title;
}