Mailing List Archive

[MediaWiki-commits] [Gerrit] mediawiki/core[master]: Introduce and use CommentStoreFactory
Addshore has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/406131 )

Change subject: Introduce and use CommentStoreFactory
......................................................................

Introduce and use CommentStoreFactory

Change-Id: I3abb62a5cfb0dcd456da9f4eb35583476ae41cfb
---
M autoload.php
M includes/CommentStore.php
A includes/CommentStoreFactory.php
M includes/MediaWikiServices.php
M includes/ServiceWiring.php
M includes/Title.php
M includes/api/ApiQueryAllUsers.php
M includes/api/ApiQueryBlocks.php
M includes/api/ApiQueryDeletedrevs.php
M includes/api/ApiQueryFilearchive.php
M includes/api/ApiQueryLogEvents.php
M includes/api/ApiQueryProtectedTitles.php
M includes/api/ApiQueryRecentChanges.php
M includes/api/ApiQueryUserContributions.php
M includes/api/ApiQueryUsers.php
M includes/api/ApiQueryWatchlist.php
M includes/filerepo/file/LocalFile.php
M includes/page/WikiPage.php
M includes/watcheditem/WatchedItemQueryService.php
M maintenance/orphans.php
M maintenance/rebuildrecentchanges.php
A tests/phpunit/includes/CommentStoreFactoryTest.php
M tests/phpunit/includes/MediaWikiServicesTest.php
23 files changed, 102 insertions(+), 33 deletions(-)


git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/31/406131/1

diff --git a/autoload.php b/autoload.php
index 6fb2cc4..b7bf2f6 100644
--- a/autoload.php
+++ b/autoload.php
@@ -279,6 +279,7 @@
'CommandLineInc' => __DIR__ . '/maintenance/commandLine.inc',
'CommandLineInstaller' => __DIR__ . '/maintenance/install.php',
'CommentStore' => __DIR__ . '/includes/CommentStore.php',
+ 'CommentStoreFactory' => __DIR__ . '/includes/CommentStoreFactory.php',
'CommentStoreComment' => __DIR__ . '/includes/CommentStoreComment.php',
'CompareParserCache' => __DIR__ . '/maintenance/compareParserCache.php',
'CompareParsers' => __DIR__ . '/maintenance/compareParsers.php',
diff --git a/includes/CommentStore.php b/includes/CommentStore.php
index 0d679d3..102cb05 100644
--- a/includes/CommentStore.php
+++ b/includes/CommentStore.php
@@ -20,6 +20,7 @@
* @file
*/

+use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\IDatabase;

/**
@@ -97,12 +98,10 @@
* @param Language $lang Language to use for comment truncation. Defaults
* to $wgContLang.
*/
- public function __construct( $key, Language $lang = null ) {
- global $wgCommentTableSchemaMigrationStage, $wgContLang;
-
+ public function __construct( $key, Language $lang, $migrationStage ) {
$this->key = $key;
- $this->stage = $wgCommentTableSchemaMigrationStage;
- $this->lang = $lang ?: $wgContLang;
+ $this->stage = $migrationStage;
+ $this->lang = $lang;
}

/**
@@ -112,7 +111,9 @@
* @return CommentStore
*/
public static function newKey( $key ) {
- return new CommentStore( $key );
+ return MediaWikiServices::getInstance()
+ ->getCommentStoreFactory()
+ ->newForKey( $key );
}

/**
diff --git a/includes/CommentStoreFactory.php b/includes/CommentStoreFactory.php
new file mode 100644
index 0000000..9174f7d
--- /dev/null
+++ b/includes/CommentStoreFactory.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * @since 1.31
+ */
+class CommentStoreFactory {
+
+ /** @var Language Language to use for comment truncation */
+ private $lang;
+
+ /** @var int One of the MIGRATION_* constants */
+ private $migrationStage;
+
+ /**
+ * @param Language $lang to use for comment truncation
+ * @param int $migrationStage One of the MIGRATION_* constants
+ */
+ public function __construct( $lang, $migrationStage ) {
+ $this->lang = $lang;
+ $this->migrationStage = $migrationStage;
+ }
+
+ /**
+ * @since 1.31
+ *
+ * @param string $key A key such as "rev_comment" identifying the comment
+ * field being fetched.
+ * @return CommentStore
+ */
+ public function newForKey( $key ) {
+ return new CommentStore( $key, $this->lang, $this->migrationStage );
+ }
+
+}
diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php
index c283793..31b3c1c 100644
--- a/includes/MediaWikiServices.php
+++ b/includes/MediaWikiServices.php
@@ -1,6 +1,7 @@
<?php
namespace MediaWiki;

+use CommentStoreFactory;
use Config;
use ConfigFactory;
use CryptHKDF;
@@ -761,6 +762,14 @@
return $this->getService( 'HttpRequestFactory' );
}

+ /**
+ * @since 1.31
+ * @return CommentStoreFactory
+ */
+ public function getCommentStoreFactory() {
+ return $this->getService( 'CommentStoreFactory' );
+ }
+
///////////////////////////////////////////////////////////////////////////
// NOTE: When adding a service getter here, don't forget to add a test
// case for it in MediaWikiServicesTest::provideGetters() and in
diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php
index a89619f..5b5535e 100644
--- a/includes/ServiceWiring.php
+++ b/includes/ServiceWiring.php
@@ -520,6 +520,14 @@
return new \MediaWiki\Http\HttpRequestFactory();
},

+ 'CommentStoreFactory' => function ( MediaWikiServices $services ) {
+ global $wgContLang, $wgCommentTableSchemaMigrationStage;
+ return new CommentStoreFactory(
+ $wgContLang,
+ $wgCommentTableSchemaMigrationStage
+ );
+ }
+
///////////////////////////////////////////////////////////////////////////
// NOTE: When adding a service here, don't forget to add a getter function
// in the MediaWikiServices class. The convenience getter should just call
diff --git a/includes/Title.php b/includes/Title.php
index c4cf434..85fb675 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -2735,7 +2735,7 @@

if ( $this->mTitleProtection === null ) {
$dbr = wfGetDB( DB_REPLICA );
- $commentStore = new CommentStore( 'pt_reason' );
+ $commentStore = CommentStore::newKey( 'pt_reason' );
$commentQuery = $commentStore->getJoin();
$res = $dbr->select(
[ 'protected_titles' ] + $commentQuery['tables'],
diff --git a/includes/api/ApiQueryAllUsers.php b/includes/api/ApiQueryAllUsers.php
index d594ad4..55afd44 100644
--- a/includes/api/ApiQueryAllUsers.php
+++ b/includes/api/ApiQueryAllUsers.php
@@ -49,7 +49,7 @@
$activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );

$db = $this->getDB();
- $commentStore = new CommentStore( 'ipb_reason' );
+ $commentStore = CommentStore::newKey( 'ipb_reason' );

$prop = $params['prop'];
if ( !is_null( $prop ) ) {
diff --git a/includes/api/ApiQueryBlocks.php b/includes/api/ApiQueryBlocks.php
index 698c13c..b412b56 100644
--- a/includes/api/ApiQueryBlocks.php
+++ b/includes/api/ApiQueryBlocks.php
@@ -37,7 +37,7 @@

public function execute() {
$db = $this->getDB();
- $commentStore = new CommentStore( 'ipb_reason' );
+ $commentStore = CommentStore::newKey( 'ipb_reason' );
$params = $this->extractRequestParams();
$this->requireMaxOneParameter( $params, 'users', 'ip' );

diff --git a/includes/api/ApiQueryDeletedrevs.php b/includes/api/ApiQueryDeletedrevs.php
index 5dd007b..85fea16 100644
--- a/includes/api/ApiQueryDeletedrevs.php
+++ b/includes/api/ApiQueryDeletedrevs.php
@@ -44,7 +44,7 @@

$user = $this->getUser();
$db = $this->getDB();
- $commentStore = new CommentStore( 'ar_comment' );
+ $commentStore = CommentStore::newKey( 'ar_comment' );
$params = $this->extractRequestParams( false );
$prop = array_flip( $params['prop'] );
$fld_parentid = isset( $prop['parentid'] );
diff --git a/includes/api/ApiQueryFilearchive.php b/includes/api/ApiQueryFilearchive.php
index 838fc2b..9d318d4 100644
--- a/includes/api/ApiQueryFilearchive.php
+++ b/includes/api/ApiQueryFilearchive.php
@@ -43,7 +43,7 @@

$user = $this->getUser();
$db = $this->getDB();
- $commentStore = new CommentStore( 'fa_description' );
+ $commentStore = CommentStore::newKey( 'fa_description' );

$params = $this->extractRequestParams();

diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php
index 3066720..30246e9 100644
--- a/includes/api/ApiQueryLogEvents.php
+++ b/includes/api/ApiQueryLogEvents.php
@@ -45,7 +45,7 @@
public function execute() {
$params = $this->extractRequestParams();
$db = $this->getDB();
- $this->commentStore = new CommentStore( 'log_comment' );
+ $this->commentStore = CommentStore::newKey( 'log_comment' );
$this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );

$prop = array_flip( $params['prop'] );
diff --git a/includes/api/ApiQueryProtectedTitles.php b/includes/api/ApiQueryProtectedTitles.php
index b69a299..5f2e56b 100644
--- a/includes/api/ApiQueryProtectedTitles.php
+++ b/includes/api/ApiQueryProtectedTitles.php
@@ -59,7 +59,7 @@
$this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );

if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
- $commentStore = new CommentStore( 'pt_reason' );
+ $commentStore = CommentStore::newKey( 'pt_reason' );
$commentQuery = $commentStore->getJoin();
$this->addTables( $commentQuery['tables'] );
$this->addFields( $commentQuery['fields'] );
diff --git a/includes/api/ApiQueryRecentChanges.php b/includes/api/ApiQueryRecentChanges.php
index 63e0748..af2ae20 100644
--- a/includes/api/ApiQueryRecentChanges.php
+++ b/includes/api/ApiQueryRecentChanges.php
@@ -354,7 +354,7 @@
$this->token = $params['token'];

if ( $this->fld_comment || $this->fld_parsedcomment || $this->token ) {
- $this->commentStore = new CommentStore( 'rc_comment' );
+ $this->commentStore = CommentStore::newKey( 'rc_comment' );
$commentQuery = $this->commentStore->getJoin();
$this->addTables( $commentQuery['tables'] );
$this->addFields( $commentQuery['fields'] );
diff --git a/includes/api/ApiQueryUserContributions.php b/includes/api/ApiQueryUserContributions.php
index bb0f335..e5379fd 100644
--- a/includes/api/ApiQueryUserContributions.php
+++ b/includes/api/ApiQueryUserContributions.php
@@ -45,7 +45,7 @@
// Parse some parameters
$this->params = $this->extractRequestParams();

- $this->commentStore = new CommentStore( 'rev_comment' );
+ $this->commentStore = CommentStore::newKey( 'rev_comment' );

$prop = array_flip( $this->params['prop'] );
$this->fld_ids = isset( $prop['ids'] );
diff --git a/includes/api/ApiQueryUsers.php b/includes/api/ApiQueryUsers.php
index 8fc99bb..02ba6da 100644
--- a/includes/api/ApiQueryUsers.php
+++ b/includes/api/ApiQueryUsers.php
@@ -99,7 +99,7 @@

public function execute() {
$db = $this->getDB();
- $commentStore = new CommentStore( 'ipb_reason' );
+ $commentStore = CommentStore::newKey( 'ipb_reason' );

$params = $this->extractRequestParams();
$this->requireMaxOneParameter( $params, 'userids', 'users' );
diff --git a/includes/api/ApiQueryWatchlist.php b/includes/api/ApiQueryWatchlist.php
index 710550a..10d3ffc 100644
--- a/includes/api/ApiQueryWatchlist.php
+++ b/includes/api/ApiQueryWatchlist.php
@@ -91,7 +91,7 @@
}

if ( $this->fld_comment || $this->fld_parsedcomment ) {
- $this->commentStore = new CommentStore( 'rc_comment' );
+ $this->commentStore = CommentStore::newKey( 'rc_comment' );
}
}

diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php
index 4248f95..b7b56a3 100644
--- a/includes/filerepo/file/LocalFile.php
+++ b/includes/filerepo/file/LocalFile.php
@@ -1424,7 +1424,7 @@
# Test to see if the row exists using INSERT IGNORE
# This avoids race conditions by locking the row until the commit, and also
# doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
- $commentStore = new CommentStore( 'img_description' );
+ $commentStore = CommentStore::newKey( 'img_description' );
list( $commentFields, $commentCallback ) =
$commentStore->insertWithTempTable( $dbw, $comment );
$dbw->insert( 'image',
@@ -2403,10 +2403,10 @@
$now = time();
$dbw = $this->file->repo->getMasterDB();

- $commentStoreImgDesc = new CommentStore( 'img_description' );
- $commentStoreOiDesc = new CommentStore( 'oi_description' );
- $commentStoreFaDesc = new CommentStore( 'fa_description' );
- $commentStoreFaReason = new CommentStore( 'fa_deleted_reason' );
+ $commentStoreImgDesc = CommentStore::newKey( 'img_description' );
+ $commentStoreOiDesc = CommentStore::newKey( 'oi_description' );
+ $commentStoreFaDesc = CommentStore::newKey( 'fa_description' );
+ $commentStoreFaReason = CommentStore::newKey( 'fa_deleted_reason' );

$encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
$encUserId = $dbw->addQuotes( $this->user->getId() );
@@ -2734,9 +2734,9 @@

$dbw = $this->file->repo->getMasterDB();

- $commentStoreImgDesc = new CommentStore( 'img_description' );
- $commentStoreOiDesc = new CommentStore( 'oi_description' );
- $commentStoreFaDesc = new CommentStore( 'fa_description' );
+ $commentStoreImgDesc = CommentStore::newKey( 'img_description' );
+ $commentStoreOiDesc = CommentStore::newKey( 'oi_description' );
+ $commentStoreFaDesc = CommentStore::newKey( 'fa_description' );

$status = $this->file->repo->newGood();

diff --git a/includes/page/WikiPage.php b/includes/page/WikiPage.php
index d403ab5..2d1414b 100644
--- a/includes/page/WikiPage.php
+++ b/includes/page/WikiPage.php
@@ -2853,8 +2853,8 @@
$content = null;
}

- $revCommentStore = new CommentStore( 'rev_comment' );
- $arCommentStore = new CommentStore( 'ar_comment' );
+ $revCommentStore = CommentStore::newKey( 'rev_comment' );
+ $arCommentStore = CommentStore::newKey( 'ar_comment' );

$revQuery = Revision::getQueryInfo();
$bitfield = false;
diff --git a/includes/watcheditem/WatchedItemQueryService.php b/includes/watcheditem/WatchedItemQueryService.php
index 3478b08..39965d5 100644
--- a/includes/watcheditem/WatchedItemQueryService.php
+++ b/includes/watcheditem/WatchedItemQueryService.php
@@ -85,7 +85,7 @@

private function getCommentStore() {
if ( !$this->commentStore ) {
- $this->commentStore = new CommentStore( 'rc_comment' );
+ $this->commentStore = CommentStore::newKey( 'rc_comment' );
}
return $this->commentStore;
}
diff --git a/maintenance/orphans.php b/maintenance/orphans.php
index a1cf166..4e62a02 100644
--- a/maintenance/orphans.php
+++ b/maintenance/orphans.php
@@ -75,7 +75,7 @@
*/
private function checkOrphans( $fix ) {
$dbw = $this->getDB( DB_MASTER );
- $commentStore = new CommentStore( 'rev_comment' );
+ $commentStore = CommentStore::newKey( 'rev_comment' );

if ( $fix ) {
$this->lockTables( $dbw );
diff --git a/maintenance/rebuildrecentchanges.php b/maintenance/rebuildrecentchanges.php
index f26d8b3..9c5c1d1 100644
--- a/maintenance/rebuildrecentchanges.php
+++ b/maintenance/rebuildrecentchanges.php
@@ -80,8 +80,8 @@
*/
private function rebuildRecentChangesTablePass1() {
$dbw = $this->getDB( DB_MASTER );
- $revCommentStore = new CommentStore( 'rev_comment' );
- $rcCommentStore = new CommentStore( 'rc_comment' );
+ $revCommentStore = CommentStore::newKey( 'rev_comment' );
+ $rcCommentStore = CommentStore::newKey( 'rc_comment' );

if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
$this->cutoffFrom = wfTimestamp( TS_UNIX, $this->getOption( 'from' ) );
@@ -270,8 +270,8 @@
global $wgLogTypes, $wgLogRestrictions;

$dbw = $this->getDB( DB_MASTER );
- $logCommentStore = new CommentStore( 'log_comment' );
- $rcCommentStore = new CommentStore( 'rc_comment' );
+ $logCommentStore = CommentStore::newKey( 'log_comment' );
+ $rcCommentStore = CommentStore::newKey( 'rc_comment' );

$this->output( "Loading from user, page, and logging tables...\n" );

diff --git a/tests/phpunit/includes/CommentStoreFactoryTest.php b/tests/phpunit/includes/CommentStoreFactoryTest.php
new file mode 100644
index 0000000..59751c4
--- /dev/null
+++ b/tests/phpunit/includes/CommentStoreFactoryTest.php
@@ -0,0 +1,15 @@
+<?php
+
+class CommentStoreFactoryTest extends MediaWikiTestCase {
+
+ public function testNewForKey() {
+ $factory = new CommentStoreFactory( Language::factory( 'en' ), MIGRATION_NEW );
+ $store = $factory->newForKey( 'brocolkey' );
+ $fields = $store->getFields();
+ $this->assertSame(
+ [ 'brocolkey_id' => 'brocolkey_id' ],
+ $fields
+ );
+ }
+
+}
diff --git a/tests/phpunit/includes/MediaWikiServicesTest.php b/tests/phpunit/includes/MediaWikiServicesTest.php
index e3d5336..ee5ccb5 100644
--- a/tests/phpunit/includes/MediaWikiServicesTest.php
+++ b/tests/phpunit/includes/MediaWikiServicesTest.php
@@ -344,6 +344,7 @@
'RevisionStore' => [ 'RevisionStore', RevisionStore::class ],
'RevisionLookup' => [ 'RevisionLookup', RevisionLookup::class ],
'HttpRequestFactory' => [ 'HttpRequestFactory', HttpRequestFactory::class ],
+ 'CommentStoreFactory' => [ 'CommentStoreFactory', CommentStoreFactory::class ],
];
}


--
To view, visit https://gerrit.wikimedia.org/r/406131
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3abb62a5cfb0dcd456da9f4eb35583476ae41cfb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Addshore <addshorewiki@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits