diff options
author | Umherirrender <umherirrender_de.wp@web.de> | 2023-04-17 22:36:48 +0200 |
---|---|---|
committer | Umherirrender <umherirrender_de.wp@web.de> | 2023-05-04 20:52:40 +0200 |
commit | 65f04c763aa062d90a4c72cab222bb068a433aa0 (patch) | |
tree | b9ec64102e390ae8e004f814a6d2d5a0dbfa92d0 /includes/api | |
parent | 2b5d25ba71dd47e698f2ea83e2044a1e2c956db4 (diff) | |
download | mediawikicore-65f04c763aa062d90a4c72cab222bb068a433aa0.tar.gz mediawikicore-65f04c763aa062d90a4c72cab222bb068a433aa0.zip |
api: Use a temp placeholder for signature on preview/pst
For pst on parse/compare/editstash/(all)revisions/(all)deletedrevisions
Do not show the IP when IP masking is enabled,
instead show a previous aquired temp name or a placeholder on preview.
MediaWiki itself used this for the ajax preview on GUI's action=edit
Cannot acquire a new unsaved temp user as api parse does not persist
the global session (each request results in a new id)
and it would require a db write on a read request.
Bug: T331397
Change-Id: I74bb4d655f371bd99e3b618d1a0ac45d730c746c
Diffstat (limited to 'includes/api')
-rw-r--r-- | includes/api/ApiComparePages.php | 31 | ||||
-rw-r--r-- | includes/api/ApiMain.php | 6 | ||||
-rw-r--r-- | includes/api/ApiParse.php | 31 | ||||
-rw-r--r-- | includes/api/ApiQuery.php | 8 | ||||
-rw-r--r-- | includes/api/ApiQueryAllDeletedRevisions.php | 12 | ||||
-rw-r--r-- | includes/api/ApiQueryAllRevisions.php | 12 | ||||
-rw-r--r-- | includes/api/ApiQueryDeletedRevisions.php | 12 | ||||
-rw-r--r-- | includes/api/ApiQueryRevisions.php | 12 | ||||
-rw-r--r-- | includes/api/ApiQueryRevisionsBase.php | 31 | ||||
-rw-r--r-- | includes/api/ApiStashEdit.php | 30 |
10 files changed, 170 insertions, 15 deletions
diff --git a/includes/api/ApiComparePages.php b/includes/api/ApiComparePages.php index 4c82ec47aa5a..b2759230d6ae 100644 --- a/includes/api/ApiComparePages.php +++ b/includes/api/ApiComparePages.php @@ -28,6 +28,8 @@ use MediaWiki\Revision\RevisionStore; use MediaWiki\Revision\SlotRecord; use MediaWiki\Revision\SlotRoleRegistry; use MediaWiki\Title\Title; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\RequestTimeout\TimeoutException; @@ -55,6 +57,12 @@ class ApiComparePages extends ApiBase { /** @var CommentFormatter */ private $commentFormatter; + /** @var TempUserCreator */ + private $tempUserCreator; + + /** @var UserFactory */ + private $userFactory; + private bool $inlineSupported; /** @@ -65,6 +73,8 @@ class ApiComparePages extends ApiBase { * @param IContentHandlerFactory $contentHandlerFactory * @param ContentTransformer $contentTransformer * @param CommentFormatter $commentFormatter + * @param TempUserCreator $tempUserCreator + * @param UserFactory $userFactory */ public function __construct( ApiMain $mainModule, @@ -73,7 +83,9 @@ class ApiComparePages extends ApiBase { SlotRoleRegistry $slotRoleRegistry, IContentHandlerFactory $contentHandlerFactory, ContentTransformer $contentTransformer, - CommentFormatter $commentFormatter + CommentFormatter $commentFormatter, + TempUserCreator $tempUserCreator, + UserFactory $userFactory ) { parent::__construct( $mainModule, $moduleName ); $this->revisionStore = $revisionStore; @@ -81,6 +93,8 @@ class ApiComparePages extends ApiBase { $this->contentHandlerFactory = $contentHandlerFactory; $this->contentTransformer = $contentTransformer; $this->commentFormatter = $commentFormatter; + $this->tempUserCreator = $tempUserCreator; + $this->userFactory = $userFactory; $this->inlineSupported = function_exists( 'wikidiff2_inline_diff' ) && DifferenceEngine::getEngine() === 'wikidiff2'; } @@ -562,7 +576,7 @@ class ApiComparePages extends ApiBase { $content, // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141 $title, - $this->getUser(), + $this->getUserForPreview(), $popts ); } @@ -689,6 +703,19 @@ class ApiComparePages extends ApiBase { } } + private function getUserForPreview() { + $user = $this->getUser(); + if ( !$user->isRegistered() + && $this->tempUserCreator->isAutoCreateAction( 'edit' ) + && $user->isAllowed( 'createaccount' ) + ) { + return $this->userFactory->newUnsavedTempUser( + $this->tempUserCreator->getStashedNameOrPlaceholder( $this->getRequest()->getSession() ) + ); + } + return $user; + } + public function getAllowedParams() { $slotRoles = $this->slotRoleRegistry->getKnownRoles(); sort( $slotRoles, SORT_STRING ); diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index fbe9b98a385a..18c6a7f42c71 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -151,6 +151,8 @@ class ApiMain extends ApiBase { 'ContentRenderer', 'ContentTransformer', 'CommentFormatter', + 'TempUserCreator', + 'UserFactory', ] ], 'stashedit' => [ @@ -161,6 +163,8 @@ class ApiMain extends ApiBase { 'RevisionLookup', 'StatsdDataFactory', 'WikiPageFactory', + 'TempUserCreator', + 'UserFactory', ] ], 'opensearch' => [ @@ -221,6 +225,8 @@ class ApiMain extends ApiBase { 'ContentHandlerFactory', 'ContentTransformer', 'CommentFormatter', + 'TempUserCreator', + 'UserFactory', ] ], 'checktoken' => [ diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index 316e2a42127b..880b5e84f61a 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -36,6 +36,8 @@ use MediaWiki\Revision\RevisionLookup; use MediaWiki\Revision\RevisionRecord; use MediaWiki\Revision\SlotRecord; use MediaWiki\Title\Title; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use MediaWiki\WikiMap\WikiMap; use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\TypeDef\EnumDef; @@ -90,6 +92,12 @@ class ApiParse extends ApiBase { /** @var ContentRenderer */ private $contentRenderer; + /** @var TempUserCreator */ + private $tempUserCreator; + + /** @var UserFactory */ + private $userFactory; + /** * @param ApiMain $main * @param string $action @@ -104,6 +112,8 @@ class ApiParse extends ApiBase { * @param ContentRenderer $contentRenderer * @param ContentTransformer $contentTransformer * @param CommentFormatter $commentFormatter + * @param TempUserCreator $tempUserCreator + * @param UserFactory $userFactory */ public function __construct( ApiMain $main, @@ -118,7 +128,9 @@ class ApiParse extends ApiBase { WikiPageFactory $wikiPageFactory, ContentRenderer $contentRenderer, ContentTransformer $contentTransformer, - CommentFormatter $commentFormatter + CommentFormatter $commentFormatter, + TempUserCreator $tempUserCreator, + UserFactory $userFactory ) { parent::__construct( $main, $action ); $this->revisionLookup = $revisionLookup; @@ -132,6 +144,8 @@ class ApiParse extends ApiBase { $this->contentRenderer = $contentRenderer; $this->contentTransformer = $contentTransformer; $this->commentFormatter = $commentFormatter; + $this->tempUserCreator = $tempUserCreator; + $this->userFactory = $userFactory; } private function getPoolKey(): string { @@ -163,6 +177,19 @@ class ApiParse extends ApiBase { return $worker->execute(); } + private function getUserForPreview() { + $user = $this->getUser(); + if ( !$user->isRegistered() + && $this->tempUserCreator->isAutoCreateAction( 'edit' ) + && $user->isAllowed( 'createaccount' ) + ) { + return $this->userFactory->newUnsavedTempUser( + $this->tempUserCreator->getStashedNameOrPlaceholder( $this->getRequest()->getSession() ) + ); + } + return $user; + } + private function getPageParserOutput( WikiPage $page, $revId, @@ -397,7 +424,7 @@ class ApiParse extends ApiBase { $this->pstContent = $this->contentTransformer->preSaveTransform( $this->content, $titleObj, - $this->getUser(), + $this->getUserForPreview(), $popts ); } diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 55b10627557e..9ec84bb167b0 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -72,6 +72,8 @@ class ApiQuery extends ApiBase { 'ContentRenderer', 'ContentTransformer', 'CommentFormatter', + 'TempUserCreator', + 'UserFactory', ] ], 'duplicatefiles' => [ @@ -166,6 +168,8 @@ class ApiQuery extends ApiBase { 'ContentRenderer', 'ContentTransformer', 'CommentFormatter', + 'TempUserCreator', + 'UserFactory', ] ], 'stashimageinfo' => [ @@ -213,6 +217,8 @@ class ApiQuery extends ApiBase { 'ContentRenderer', 'ContentTransformer', 'CommentFormatter', + 'TempUserCreator', + 'UserFactory', ] ], 'allfileusages' => [ @@ -269,6 +275,8 @@ class ApiQuery extends ApiBase { 'ContentRenderer', 'ContentTransformer', 'CommentFormatter', + 'TempUserCreator', + 'UserFactory', ] ], 'mystashedfiles' => [ diff --git a/includes/api/ApiQueryAllDeletedRevisions.php b/includes/api/ApiQueryAllDeletedRevisions.php index ea07a6c5c7cc..43e185aec04a 100644 --- a/includes/api/ApiQueryAllDeletedRevisions.php +++ b/includes/api/ApiQueryAllDeletedRevisions.php @@ -35,6 +35,8 @@ use MediaWiki\Revision\SlotRoleRegistry; use MediaWiki\Storage\NameTableAccessException; use MediaWiki\Storage\NameTableStore; use MediaWiki\Title\Title; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use Wikimedia\ParamValidator\ParamValidator; /** @@ -65,6 +67,8 @@ class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase { * @param ContentRenderer $contentRenderer * @param ContentTransformer $contentTransformer * @param CommentFormatter $commentFormatter + * @param TempUserCreator $tempUserCreator + * @param UserFactory $userFactory */ public function __construct( ApiQuery $query, @@ -77,7 +81,9 @@ class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase { NamespaceInfo $namespaceInfo, ContentRenderer $contentRenderer, ContentTransformer $contentTransformer, - CommentFormatter $commentFormatter + CommentFormatter $commentFormatter, + TempUserCreator $tempUserCreator, + UserFactory $userFactory ) { parent::__construct( $query, @@ -89,7 +95,9 @@ class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase { $slotRoleRegistry, $contentRenderer, $contentTransformer, - $commentFormatter + $commentFormatter, + $tempUserCreator, + $userFactory ); $this->revisionStore = $revisionStore; $this->changeTagDefStore = $changeTagDefStore; diff --git a/includes/api/ApiQueryAllRevisions.php b/includes/api/ApiQueryAllRevisions.php index 22e0f911642d..74c0e98e18da 100644 --- a/includes/api/ApiQueryAllRevisions.php +++ b/includes/api/ApiQueryAllRevisions.php @@ -31,6 +31,8 @@ use MediaWiki\Revision\RevisionStore; use MediaWiki\Revision\SlotRoleRegistry; use MediaWiki\Title\Title; use MediaWiki\User\ActorMigration; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use Wikimedia\ParamValidator\ParamValidator; /** @@ -62,6 +64,8 @@ class ApiQueryAllRevisions extends ApiQueryRevisionsBase { * @param ContentRenderer $contentRenderer * @param ContentTransformer $contentTransformer * @param CommentFormatter $commentFormatter + * @param TempUserCreator $tempUserCreator + * @param UserFactory $userFactory */ public function __construct( ApiQuery $query, @@ -74,7 +78,9 @@ class ApiQueryAllRevisions extends ApiQueryRevisionsBase { NamespaceInfo $namespaceInfo, ContentRenderer $contentRenderer, ContentTransformer $contentTransformer, - CommentFormatter $commentFormatter + CommentFormatter $commentFormatter, + TempUserCreator $tempUserCreator, + UserFactory $userFactory ) { parent::__construct( $query, @@ -86,7 +92,9 @@ class ApiQueryAllRevisions extends ApiQueryRevisionsBase { $slotRoleRegistry, $contentRenderer, $contentTransformer, - $commentFormatter + $commentFormatter, + $tempUserCreator, + $userFactory ); $this->revisionStore = $revisionStore; $this->actorMigration = $actorMigration; diff --git a/includes/api/ApiQueryDeletedRevisions.php b/includes/api/ApiQueryDeletedRevisions.php index 15ebd5768c1e..40387401fc4e 100644 --- a/includes/api/ApiQueryDeletedRevisions.php +++ b/includes/api/ApiQueryDeletedRevisions.php @@ -35,6 +35,8 @@ use MediaWiki\Revision\SlotRoleRegistry; use MediaWiki\Storage\NameTableAccessException; use MediaWiki\Storage\NameTableStore; use MediaWiki\Title\Title; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use Wikimedia\ParamValidator\ParamValidator; /** @@ -65,6 +67,8 @@ class ApiQueryDeletedRevisions extends ApiQueryRevisionsBase { * @param ContentRenderer $contentRenderer * @param ContentTransformer $contentTransformer * @param CommentFormatter $commentFormatter + * @param TempUserCreator $tempUserCreator + * @param UserFactory $userFactory */ public function __construct( ApiQuery $query, @@ -77,7 +81,9 @@ class ApiQueryDeletedRevisions extends ApiQueryRevisionsBase { LinkBatchFactory $linkBatchFactory, ContentRenderer $contentRenderer, ContentTransformer $contentTransformer, - CommentFormatter $commentFormatter + CommentFormatter $commentFormatter, + TempUserCreator $tempUserCreator, + UserFactory $userFactory ) { parent::__construct( $query, @@ -89,7 +95,9 @@ class ApiQueryDeletedRevisions extends ApiQueryRevisionsBase { $slotRoleRegistry, $contentRenderer, $contentTransformer, - $commentFormatter + $commentFormatter, + $tempUserCreator, + $userFactory ); $this->revisionStore = $revisionStore; $this->changeTagDefStore = $changeTagDefStore; diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index 2bd5813ec0cf..548aafd395e5 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -32,6 +32,8 @@ use MediaWiki\Storage\NameTableAccessException; use MediaWiki\Storage\NameTableStore; use MediaWiki\Title\Title; use MediaWiki\User\ActorMigration; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use Wikimedia\ParamValidator\ParamValidator; /** @@ -65,6 +67,8 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase { * @param ContentRenderer $contentRenderer * @param ContentTransformer $contentTransformer * @param CommentFormatter $commentFormatter + * @param TempUserCreator $tempUserCreator + * @param UserFactory $userFactory */ public function __construct( ApiQuery $query, @@ -77,7 +81,9 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase { ActorMigration $actorMigration, ContentRenderer $contentRenderer, ContentTransformer $contentTransformer, - CommentFormatter $commentFormatter + CommentFormatter $commentFormatter, + TempUserCreator $tempUserCreator, + UserFactory $userFactory ) { parent::__construct( $query, @@ -89,7 +95,9 @@ class ApiQueryRevisions extends ApiQueryRevisionsBase { $slotRoleRegistry, $contentRenderer, $contentTransformer, - $commentFormatter + $commentFormatter, + $tempUserCreator, + $userFactory ); $this->revisionStore = $revisionStore; $this->changeTagDefStore = $changeTagDefStore; diff --git a/includes/api/ApiQueryRevisionsBase.php b/includes/api/ApiQueryRevisionsBase.php index b1d583de736b..1634f42abc66 100644 --- a/includes/api/ApiQueryRevisionsBase.php +++ b/includes/api/ApiQueryRevisionsBase.php @@ -33,6 +33,8 @@ use MediaWiki\Revision\RevisionStore; use MediaWiki\Revision\SlotRecord; use MediaWiki\Revision\SlotRoleRegistry; use MediaWiki\Title\Title; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\TypeDef\EnumDef; use Wikimedia\ParamValidator\TypeDef\IntegerDef; @@ -93,6 +95,12 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { /** @var CommentFormatter */ private $commentFormatter; + /** @var TempUserCreator */ + private $tempUserCreator; + + /** @var UserFactory */ + private $userFactory; + /** * @since 1.37 Support injection of services * @stable to call @@ -106,6 +114,8 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { * @param ContentRenderer|null $contentRenderer * @param ContentTransformer|null $contentTransformer * @param CommentFormatter|null $commentFormatter + * @param TempUserCreator|null $tempUserCreator + * @param UserFactory|null $userFactory */ public function __construct( ApiQuery $queryModule, @@ -117,7 +127,9 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { SlotRoleRegistry $slotRoleRegistry = null, ContentRenderer $contentRenderer = null, ContentTransformer $contentTransformer = null, - CommentFormatter $commentFormatter = null + CommentFormatter $commentFormatter = null, + TempUserCreator $tempUserCreator = null, + UserFactory $userFactory = null ) { parent::__construct( $queryModule, $moduleName, $paramPrefix ); // This class is part of the stable interface and @@ -130,6 +142,8 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { $this->contentRenderer = $contentRenderer ?? $services->getContentRenderer(); $this->contentTransformer = $contentTransformer ?? $services->getContentTransformer(); $this->commentFormatter = $commentFormatter ?? $services->getCommentFormatter(); + $this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator(); + $this->userFactory = $userFactory ?? $services->getUserFactory(); } public function execute() { @@ -698,7 +712,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { $difftocontent = $this->contentTransformer->preSaveTransform( $difftocontent, $title, - $this->getUser(), + $this->getUserForPreview(), $popts ); } @@ -729,6 +743,19 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { return $vals; } + private function getUserForPreview() { + $user = $this->getUser(); + if ( !$user->isRegistered() + && $this->tempUserCreator->isAutoCreateAction( 'edit' ) + && $user->isAllowed( 'createaccount' ) + ) { + return $this->userFactory->newUnsavedTempUser( + $this->tempUserCreator->getStashedNameOrPlaceholder( $this->getRequest()->getSession() ) + ); + } + return $user; + } + /** * @stable to override * @param array $params diff --git a/includes/api/ApiStashEdit.php b/includes/api/ApiStashEdit.php index f212435cb147..1c40325a5d87 100644 --- a/includes/api/ApiStashEdit.php +++ b/includes/api/ApiStashEdit.php @@ -23,6 +23,8 @@ use MediaWiki\Page\WikiPageFactory; use MediaWiki\Revision\RevisionLookup; use MediaWiki\Revision\SlotRecord; use MediaWiki\Storage\PageEditStash; +use MediaWiki\User\TempUser\TempUserCreator; +use MediaWiki\User\UserFactory; use Wikimedia\ParamValidator\ParamValidator; /** @@ -55,6 +57,12 @@ class ApiStashEdit extends ApiBase { /** @var WikiPageFactory */ private $wikiPageFactory; + /** @var TempUserCreator */ + private $tempUserCreator; + + /** @var UserFactory */ + private $userFactory; + /** * @param ApiMain $main * @param string $action @@ -63,6 +71,8 @@ class ApiStashEdit extends ApiBase { * @param RevisionLookup $revisionLookup * @param IBufferingStatsdDataFactory $statsdDataFactory * @param WikiPageFactory $wikiPageFactory + * @param TempUserCreator $tempUserCreator + * @param UserFactory $userFactory */ public function __construct( ApiMain $main, @@ -71,7 +81,9 @@ class ApiStashEdit extends ApiBase { PageEditStash $pageEditStash, RevisionLookup $revisionLookup, IBufferingStatsdDataFactory $statsdDataFactory, - WikiPageFactory $wikiPageFactory + WikiPageFactory $wikiPageFactory, + TempUserCreator $tempUserCreator, + UserFactory $userFactory ) { parent::__construct( $main, $action ); @@ -80,6 +92,8 @@ class ApiStashEdit extends ApiBase { $this->revisionLookup = $revisionLookup; $this->statsdDataFactory = $statsdDataFactory; $this->wikiPageFactory = $wikiPageFactory; + $this->tempUserCreator = $tempUserCreator; + $this->userFactory = $userFactory; } public function execute() { @@ -194,6 +208,7 @@ class ApiStashEdit extends ApiBase { if ( $user->pingLimiter( 'stashedit' ) ) { $status = 'ratelimited'; } else { + $user = $this->getUserForPreview(); $updater = $page->newPageUpdater( $user ); $status = $this->pageEditStash->parseAndCache( $updater, $content, $user, $params['summary'] ); $this->pageEditStash->stashInputText( $text, $textHash ); @@ -210,6 +225,19 @@ class ApiStashEdit extends ApiBase { $this->getResult()->addValue( null, $this->getModuleName(), $ret ); } + private function getUserForPreview() { + $user = $this->getUser(); + if ( !$user->isRegistered() + && $this->tempUserCreator->isAutoCreateAction( 'edit' ) + && $user->isAllowed( 'createaccount' ) + ) { + return $this->userFactory->newUnsavedTempUser( + $this->tempUserCreator->getStashedNameOrPlaceholder( $this->getRequest()->getSession() ) + ); + } + return $user; + } + public function getAllowedParams() { return [ 'title' => [ |