Use the edited page's title for magic words in action=edit error messages

Allow specifying the title used for rendering error messages in
ApiErrorFormatter.

Then, specify one in ApiEditPage (and a few similar modules that deal
with single pages) once we've figured out which page is being edited.

Bug: T247661
Change-Id: Ic3d70efc23744ef6e90abc445f3babebf45c4697
This commit is contained in:
Bartosz Dziewoński 2021-05-21 17:44:24 +02:00
parent bc46ba2de9
commit 2361889b78
7 changed files with 33 additions and 6 deletions

View file

@ -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' );

View file

@ -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() ) )

View file

@ -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 );
}
}

View file

@ -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 );
}

View file

@ -39,6 +39,7 @@ class ApiProtect extends ApiBase {
$pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
$titleObj = $pageObj->getTitle();
$this->getErrorFormatter()->setContextTitle( $titleObj );
$this->checkTitleUserPermissions( $titleObj, 'protect' );

View file

@ -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

View file

@ -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'] )