diff --git a/includes/api/ApiChangeContentModel.php b/includes/api/ApiChangeContentModel.php index cd63f61beb6..8167a79939e 100644 --- a/includes/api/ApiChangeContentModel.php +++ b/includes/api/ApiChangeContentModel.php @@ -44,6 +44,7 @@ class ApiChangeContentModel extends ApiBase { $params = $this->extractRequestParams(); $wikiPage = $this->getTitleOrPageId( $params ); $title = $wikiPage->getTitle(); + $this->getErrorFormatter()->setContextTitle( $title ); if ( !$title->exists() ) { $this->dieWithError( 'apierror-changecontentmodel-missingtitle' ); diff --git a/includes/api/ApiDelete.php b/includes/api/ApiDelete.php index da5d0145bc3..dc702f32155 100644 --- a/includes/api/ApiDelete.php +++ b/includes/api/ApiDelete.php @@ -53,6 +53,7 @@ class ApiDelete extends ApiBase { $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); $titleObj = $pageObj->getTitle(); + $this->getErrorFormatter()->setContextTitle( $titleObj ); if ( !$pageObj->exists() && // @phan-suppress-next-line PhanUndeclaredMethod !( $titleObj->getNamespace() === NS_FILE && self::canDeleteFile( $pageObj->getFile() ) ) diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index 0cd3d1acaf6..d5e76ba5d74 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -67,6 +67,7 @@ class ApiEditPage extends ApiBase { $pageObj = $this->getTitleOrPageId( $params ); $titleObj = $pageObj->getTitle(); + $this->getErrorFormatter()->setContextTitle( $titleObj ); $apiResult = $this->getResult(); $revisionLookup = MediaWikiServices::getInstance()->getRevisionLookup(); @@ -120,6 +121,7 @@ class ApiEditPage extends ApiBase { // Since the page changed, update $pageObj $pageObj = WikiPage::factory( $titleObj ); + $this->getErrorFormatter()->setContextTitle( $titleObj ); } } diff --git a/includes/api/ApiErrorFormatter.php b/includes/api/ApiErrorFormatter.php index 2e935c4c291..e8634d9c732 100644 --- a/includes/api/ApiErrorFormatter.php +++ b/includes/api/ApiErrorFormatter.php @@ -37,6 +37,8 @@ class ApiErrorFormatter { /** @var Language */ protected $lang; + /** @var Title|null Title used for rendering error messages, or null to use the dummy title */ + protected $title = null; protected $useDB = false; protected $format = 'none'; @@ -119,6 +121,24 @@ class ApiErrorFormatter { return self::$dummyTitle; } + /** + * Get the title used for rendering error messages, e.g. for wikitext magic words like {{PAGENAME}} + * @since 1.37 + * @return Title + */ + public function getContextTitle() { + return $this->title ?: $this->getDummyTitle(); + } + + /** + * Set the title used for rendering error messages, e.g. for wikitext magic words like {{PAGENAME}} + * @since 1.37 + * @param Title $title + */ + public function setContextTitle( Title $title ) { + $this->title = $title; + } + /** * Add a warning to the result * @param string|null $modulePath @@ -129,7 +149,7 @@ class ApiErrorFormatter { public function addWarning( $modulePath, $msg, $code = null, $data = null ) { $msg = ApiMessage::create( $msg, $code, $data ) ->inLanguage( $this->lang ) - ->title( $this->getDummyTitle() ) + ->title( $this->getContextTitle() ) ->useDatabase( $this->useDB ); $this->addWarningOrError( 'warning', $modulePath, $msg ); } @@ -144,7 +164,7 @@ class ApiErrorFormatter { public function addError( $modulePath, $msg, $code = null, $data = null ) { $msg = ApiMessage::create( $msg, $code, $data ) ->inLanguage( $this->lang ) - ->title( $this->getDummyTitle() ) + ->title( $this->getContextTitle() ) ->useDatabase( $this->useDB ); $this->addWarningOrError( 'error', $modulePath, $msg ); } @@ -178,7 +198,7 @@ class ApiErrorFormatter { $msg = ApiMessage::create( $error ) ->inLanguage( $this->lang ) - ->title( $this->getDummyTitle() ) + ->title( $this->getContextTitle() ) ->useDatabase( $this->useDB ); if ( !in_array( $msg->getKey(), $filter, true ) ) { $this->addWarningOrError( $tag, $modulePath, $msg ); @@ -223,7 +243,7 @@ class ApiErrorFormatter { return ApiMessage::create( $msg, $options['code'], $options['data'] ) ->params( $params ) ->inLanguage( $this->lang ) - ->title( $this->getDummyTitle() ) + ->title( $this->getContextTitle() ) ->useDatabase( $this->useDB ); } @@ -252,7 +272,7 @@ class ApiErrorFormatter { public function formatMessage( $msg, $format = null ) { $msg = ApiMessage::create( $msg ) ->inLanguage( $this->lang ) - ->title( $this->getDummyTitle() ) + ->title( $this->getContextTitle() ) ->useDatabase( $this->useDB ); return $this->formatMessageInternal( $msg, $format ?: $this->format ); } diff --git a/includes/api/ApiProtect.php b/includes/api/ApiProtect.php index 0912dd4f00d..b7de5011294 100644 --- a/includes/api/ApiProtect.php +++ b/includes/api/ApiProtect.php @@ -39,6 +39,7 @@ class ApiProtect extends ApiBase { $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); $titleObj = $pageObj->getTitle(); + $this->getErrorFormatter()->setContextTitle( $titleObj ); $this->checkTitleUserPermissions( $titleObj, 'protect' ); diff --git a/includes/api/ApiSetPageLanguage.php b/includes/api/ApiSetPageLanguage.php index a62679a88f3..dd1c0d0a87a 100644 --- a/includes/api/ApiSetPageLanguage.php +++ b/includes/api/ApiSetPageLanguage.php @@ -59,11 +59,12 @@ class ApiSetPageLanguage extends ApiBase { $params = $this->extractRequestParams(); $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); + $titleObj = $pageObj->getTitle(); + $this->getErrorFormatter()->setContextTitle( $titleObj ); if ( !$pageObj->exists() ) { $this->dieWithError( 'apierror-missingtitle' ); } - $titleObj = $pageObj->getTitle(); $user = $this->getUser(); // Check that the user is allowed to edit the page diff --git a/includes/api/ApiStashEdit.php b/includes/api/ApiStashEdit.php index d5ca84b0d68..8c983b84853 100644 --- a/includes/api/ApiStashEdit.php +++ b/includes/api/ApiStashEdit.php @@ -85,6 +85,7 @@ class ApiStashEdit extends ApiBase { $page = $this->getTitleOrPageId( $params ); $title = $page->getTitle(); + $this->getErrorFormatter()->setContextTitle( $title ); if ( !$this->contentHandlerFactory ->getContentHandler( $params['contentmodel'] )