Add temp flags to api calls

Temporary accounts are now distinct from users or anonymous.
Add a flag to reflect that to:
  - ApiQueryImageInfo
  - ApiQueryLogEvents
  - ApiQueryRecentChanges
  - ApiQueryRevisionsBase

Bug: T351636
Change-Id: I7986dea5ccd0dc942bf133040c4ac715487f29b9
This commit is contained in:
STran 2023-11-30 05:42:44 -08:00
parent 17c3c2ab8c
commit c668caa5ce
8 changed files with 127 additions and 3 deletions

View file

@ -199,6 +199,11 @@ because of Phabricator reports.
- ::runWithoutAbort()
- ::runner()
* Article::__get(), ::__set(), deprecated since 1.35, have been removed.
* Several API calls have been updated to return a 'temp' user flag:
- ApiQueryImageInfo
- ApiQueryLogEvents
- ApiQueryRecentChanges
- APIs extended from ApiQueryRevisionsBase
* …
=== Deprecations in 1.42 ===

View file

@ -383,6 +383,7 @@ class ApiQuery extends ApiBase {
'CommentStore',
'RowCommentFormatter',
'ChangeTagDefStore',
'UserNameUtils',
],
],
'pageswithprop' => [
@ -422,6 +423,7 @@ class ApiQuery extends ApiBase {
'ChangeTagDefStore',
'SlotRoleStore',
'SlotRoleRegistry',
'UserNameUtils',
],
],
'search' => [

View file

@ -476,6 +476,9 @@ class ApiQueryImageInfo extends ApiQueryBase {
if ( $userid ) {
$vals['userid'] = $uploader ? $uploader->getId() : 0;
}
if ( $uploader && $services->getUserNameUtils()->isTemp( $uploader->getName() ) ) {
$vals['temp'] = true;
}
if ( $uploader && !$uploader->isRegistered() ) {
$vals['anon'] = true;
}

View file

@ -29,6 +29,7 @@ use MediaWiki\ParamValidator\TypeDef\UserDef;
use MediaWiki\Storage\NameTableAccessException;
use MediaWiki\Storage\NameTableStore;
use MediaWiki\Title\Title;
use MediaWiki\User\UserNameUtils;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
use Wikimedia\Rdbms\IExpression;
@ -44,6 +45,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
private CommentStore $commentStore;
private CommentFormatter $commentFormatter;
private NameTableStore $changeTagDefStore;
private UserNameUtils $userNameUtils;
/** @var string[]|null */
private $formattedComments;
@ -54,18 +56,21 @@ class ApiQueryLogEvents extends ApiQueryBase {
* @param CommentStore $commentStore
* @param RowCommentFormatter $commentFormatter
* @param NameTableStore $changeTagDefStore
* @param UserNameUtils $userNameUtils
*/
public function __construct(
ApiQuery $query,
$moduleName,
CommentStore $commentStore,
RowCommentFormatter $commentFormatter,
NameTableStore $changeTagDefStore
NameTableStore $changeTagDefStore,
UserNameUtils $userNameUtils
) {
parent::__construct( $query, $moduleName, 'le' );
$this->commentStore = $commentStore;
$this->commentFormatter = $commentFormatter;
$this->changeTagDefStore = $changeTagDefStore;
$this->userNameUtils = $userNameUtils;
}
private $fld_ids = false, $fld_title = false, $fld_type = false,
@ -366,6 +371,10 @@ class ApiQueryLogEvents extends ApiQueryBase {
$vals['userid'] = (int)$row->actor_user;
}
if ( isset( $vals['user'] ) && $this->userNameUtils->isTemp( $vals['user'] ) ) {
$vals['temp'] = true;
}
if ( !$row->actor_user ) {
$vals['anon'] = true;
}

View file

@ -30,6 +30,7 @@ use MediaWiki\Revision\SlotRoleRegistry;
use MediaWiki\Storage\NameTableAccessException;
use MediaWiki\Storage\NameTableStore;
use MediaWiki\Title\Title;
use MediaWiki\User\UserNameUtils;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
@ -46,6 +47,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
private NameTableStore $changeTagDefStore;
private NameTableStore $slotRoleStore;
private SlotRoleRegistry $slotRoleRegistry;
private UserNameUtils $userNameUtils;
private $formattedComments = [];
@ -57,6 +59,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
* @param NameTableStore $changeTagDefStore
* @param NameTableStore $slotRoleStore
* @param SlotRoleRegistry $slotRoleRegistry
* @param UserNameUtils $userNameUtils
*/
public function __construct(
ApiQuery $query,
@ -65,7 +68,8 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
RowCommentFormatter $commentFormatter,
NameTableStore $changeTagDefStore,
NameTableStore $slotRoleStore,
SlotRoleRegistry $slotRoleRegistry
SlotRoleRegistry $slotRoleRegistry,
UserNameUtils $userNameUtils
) {
parent::__construct( $query, $moduleName, 'rc' );
$this->commentStore = $commentStore;
@ -73,6 +77,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
$this->changeTagDefStore = $changeTagDefStore;
$this->slotRoleStore = $slotRoleStore;
$this->slotRoleRegistry = $slotRoleRegistry;
$this->userNameUtils = $userNameUtils;
}
private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
@ -542,6 +547,10 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
$vals['userid'] = (int)$row->actor_user;
}
if ( isset( $row->actor_name ) && $this->userNameUtils->isTemp( $row->actor_name ) ) {
$vals['temp'] = true;
}
if ( !$row->actor_user ) {
$vals['anon'] = true;
}

View file

@ -35,6 +35,7 @@ use MediaWiki\Revision\SlotRoleRegistry;
use MediaWiki\Title\Title;
use MediaWiki\User\TempUser\TempUserCreator;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserNameUtils;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\EnumDef;
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
@ -85,6 +86,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
private CommentFormatter $commentFormatter;
private TempUserCreator $tempUserCreator;
private UserFactory $userFactory;
private UserNameUtils $userNameUtils;
/**
* @since 1.37 Support injection of services
@ -101,6 +103,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
* @param CommentFormatter|null $commentFormatter
* @param TempUserCreator|null $tempUserCreator
* @param UserFactory|null $userFactory
* @param UserNameUtils|null $userNameUtils
*/
public function __construct(
ApiQuery $queryModule,
@ -114,7 +117,8 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
ContentTransformer $contentTransformer = null,
CommentFormatter $commentFormatter = null,
TempUserCreator $tempUserCreator = null,
UserFactory $userFactory = null
UserFactory $userFactory = null,
UserNameUtils $userNameUtils = null
) {
parent::__construct( $queryModule, $moduleName, $paramPrefix );
// This class is part of the stable interface and
@ -129,6 +133,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
$this->commentFormatter = $commentFormatter ?? $services->getCommentFormatter();
$this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator();
$this->userFactory = $userFactory ?? $services->getUserFactory();
$this->userNameUtils = $userNameUtils ?? $services->getUserNameUtils();
}
public function execute() {
@ -338,6 +343,9 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
if ( $this->fld_user ) {
$vals['user'] = $u->getName();
}
if ( $this->userNameUtils->isTemp( $u->getName() ) ) {
$vals['temp'] = true;
}
if ( !$u->isRegistered() ) {
$vals['anon'] = true;
}

View file

@ -1,5 +1,6 @@
<?php
use MediaWiki\MainConfigNames;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWiki\User\UserIdentityValue;
use MediaWiki\Utils\MWTimestamp;
@ -25,7 +26,12 @@ class ApiQueryImageInfoTest extends ApiTestCase {
private const NO_COMMENT_TIMESTAMP = '20201105235239';
private const IMAGE_2_NAME = 'Random-2.png';
private const IMAGE_2_TIMESTAMP = '20230101000000';
private const IMAGE_2_SIZE = 12345;
private $testUser = null;
private $anonUser = null;
public function addDBData() {
parent::addDBData();
@ -98,6 +104,44 @@ class ApiQueryImageInfoTest extends ApiTestCase {
'oi_deleted' => 0,
]
);
// Set up temp user config
$this->overrideConfigValue(
MainConfigNames::AutoCreateTempUser,
[
'enabled' => true,
'expireAfterDays' => null,
'actions' => [ 'edit' ],
'genPattern' => '*Unregistered $1',
'matchPattern' => '*$1',
'serialProvider' => [ 'type' => 'local' ],
'serialMapping' => [ 'type' => 'plain-numeric' ],
]
);
$this->tempUser = new UserIdentityValue( 1236764321, '*Unregistered 1' );
$tempActorId = $this->getServiceContainer()
->getActorStore()
->acquireActorId( $this->tempUser, $this->db );
$this->db->insert(
'image',
[
'img_name' => self::IMAGE_2_NAME,
'img_size' => self::IMAGE_2_SIZE,
'img_width' => 1000,
'img_height' => 1800,
'img_metadata' => '',
'img_bits' => 16,
'img_media_type' => 'BITMAP',
'img_major_mime' => 'image',
'img_minor_mime' => 'png',
'img_description_id' => $this->getServiceContainer()
->getCommentStore()
->createComment( $this->db, "'''comment'''" )->id,
'img_actor' => $tempActorId,
'img_timestamp' => $this->db->timestamp( self::IMAGE_2_TIMESTAMP ),
'img_sha1' => 'aaaaasim0bgdh0jt4vdltuzoh7',
]
);
}
private function getImageInfoFromResult( array $result ) {
@ -132,6 +176,17 @@ class ApiQueryImageInfoTest extends ApiTestCase {
$this->assertSame( self::NEW_IMAGE_SIZE, $image['size'] );
}
public function testGetImageCreatedByTempUser() {
[ $result, ] = $this->doApiRequest( [
'action' => 'query',
'prop' => 'imageinfo',
'titles' => 'File:' . self::IMAGE_2_NAME
] );
$image = $result['query']['pages']['-1']['imageinfo'][0];
$this->assertArrayHasKey( 'temp', $image );
$this->assertTrue( $image['temp'] );
}
public function testGetImageEmptyComment() {
[ $result, ] = $this->doApiRequest( [
'action' => 'query',

View file

@ -1,9 +1,11 @@
<?php
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MainConfigNames;
use MediaWiki\Permissions\Authority;
use MediaWiki\Title\TitleValue;
use MediaWiki\User\User;
use MediaWiki\User\UserIdentityValue;
/**
* @group API
@ -59,6 +61,30 @@ class ApiQueryRecentChangesIntegrationTest extends ApiTestCase {
);
}
private function doTempPageEdit( LinkTarget $target, $summary ) {
// Set up temp user config
$this->overrideConfigValue(
MainConfigNames::AutoCreateTempUser,
[
'enabled' => true,
'expireAfterDays' => null,
'actions' => [ 'edit' ],
'genPattern' => '*Unregistered $1',
'matchPattern' => '*$1',
'serialProvider' => [ 'type' => 'local' ],
'serialMapping' => [ 'type' => 'plain-numeric' ],
]
);
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromLinkTarget( $target );
$page->doUserEditContent(
$page->getContentHandler()->unserializeContent( __CLASS__ ),
$this->getServiceContainer()
->getUserFactory()
->newFromUserIdentity( new UserIdentityValue( 123456, '*Unregistered 1' ) ),
$summary
);
}
/**
* Performs a batch of page edits as a specified user
* @param User $user
@ -277,13 +303,20 @@ class ApiQueryRecentChangesIntegrationTest extends ApiTestCase {
public function testUserPropParameter() {
$userEditTarget = new TitleValue( NS_MAIN, 'Foo' );
$anonEditTarget = new TitleValue( NS_MAIN, 'Bar' );
$tempEditTarget = new TitleValue( NS_MAIN, 'Baz' );
$this->doPageEdit( $this->getLoggedInTestUser(), $userEditTarget, 'Create the page' );
$this->doAnonPageEdit( $anonEditTarget, 'Create the page' );
$this->doTempPageEdit( $tempEditTarget, 'Create the page' );
$result = $this->doListRecentChangesRequest( [ 'rcprop' => 'user' ] );
$this->assertEquals(
[
[
'type' => 'new',
'temp' => true,
'user' => '*Unregistered 1',
],
[
'type' => 'new',
'anon' => true,