[MediaWiki-commits] [Gerrit] mediawiki...ReadingLists[master]: Add maintenance script for populating DB with test data

2017-12-05 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/394728 )

Change subject: Add maintenance script for populating DB with test data
..


Add maintenance script for populating DB with test data

Change-Id: Ia9e81b2021fd03e1dc78b8d1083773eb4c34bf51
---
A maintenance/populateWithTestData.php
1 file changed, 195 insertions(+), 0 deletions(-)

Approvals:
  jenkins-bot: Verified
  Mholloway: Looks good to me, approved



diff --git a/maintenance/populateWithTestData.php 
b/maintenance/populateWithTestData.php
new file mode 100644
index 000..8af4f32
--- /dev/null
+++ b/maintenance/populateWithTestData.php
@@ -0,0 +1,195 @@
+addDescription( 'Fill the database with test data, or 
remove it.' );
+   $this->addOption( 'users', 'Number of users', false, true );
+   $this->addOption( 'lists', 'Lists per user (number or stats 
distribution)', false, true );
+   $this->addOption( 'entries', 'Entries per list (number or stats 
distribution)', false, true );
+   $this->addOption( 'cleanup', 'Delete lists which look like test 
data' );
+   if ( !extension_loaded( 'stats' ) ) {
+   $this->error( 'Requires the stats PHP extension', 1 );
+   }
+   }
+
+   private function setupServices() {
+   // Can't do this in the constructor, initialization not done 
yet.
+   $services = MediaWikiServices::getInstance();
+   $this->loadBalancerFactory = 
$services->getDBLoadBalancerFactory();
+   $this->dbw = Utils::getDB( DB_MASTER, $services );
+   $this->dbr = Utils::getDB( DB_REPLICA, $services );
+   }
+
+   /**
+* @inheritDoc
+*/
+   public function execute() {
+   $this->setupServices();
+   $this->assertOptions();
+   if ( $this->getOption( 'cleanup' ) ) {
+   $this->cleanupTestData();
+   return;
+   }
+
+   $projects = $this->dbw->selectFieldValues( 
'reading_list_project', 'rlp_id' );
+   if ( !$projects ) {
+   $this->error( 'No projects! Please set up some', 1 );
+   }
+   $totalLists = $totalEntries = 0;
+   stats_rand_setall( mt_rand(), mt_rand() );
+   $users = $this->getOption( 'users' );
+   for ( $i = 0; $i < $users; $i++ ) {
+   // The test data is for performance testing so we don't 
care whether the user exists.
+   $centralId = 1000 + $i;
+   $repository = new ReadingListRepository( $centralId, 
$this->dbw, $this->dbr,
+   $this->loadBalancerFactory );
+   try {
+   $repository->setupForUser();
+   $i++;
+   // HACK mark default list so it will be deleted 
together with the rest
+   $this->dbw->update(
+   'reading_list',
+   [ 'rl_description' => __FILE__ ],
+   [
+   'rl_user_id' => $centralId,
+   'rl_is_default' => 1,
+   ]
+   );
+   } catch ( ReadingListRepositoryException $e ) {
+   // Instead of trying to find a user ID that's 
not used yet, we'll be lazy
+   // and just ignore "already set up" errors.
+   }
+   $lists = $this->getRandomValueFromDistribution( 
$this->getOption( 'lists' ) );
+   for ( $j = 0; $j < $lists; $j++, $totalLists++ ) {
+   $listId = $repository->addList( "test_$j", 
__FILE__ );
+   $entries = 
$this->getRandomValueFromDistribution( $this->getOption( 'entries' ) );
+   $rows = [];
+   for ( $k = 0; $k < $entries; $k++, 
$totalEntries++ ) {
+   $project = $projects[array_rand( 
$projects )];
+   // Calling addListEntry for each row 
separately would be a bit slow.
+   $rows[] = [
+   'rle_rl_id' => $listId,
+   'rle_user_id' => $centralId,
+   'rle_rlp_id' => $project,
+   'rle_title' => "Test_$k",
+   ];
+   }
+   $thi

[MediaWiki-commits] [Gerrit] mediawiki...ReadingLists[master]: Add maintenance script for populating DB with test data

2017-12-01 Thread Code Review
Gergő Tisza has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/394728 )

Change subject: Add maintenance script for populating DB with test data
..

Add maintenance script for populating DB with test data

Change-Id: Ia9e81b2021fd03e1dc78b8d1083773eb4c34bf51
---
A maintenance/populateWithTestData.php
1 file changed, 192 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ReadingLists 
refs/changes/28/394728/1

diff --git a/maintenance/populateWithTestData.php 
b/maintenance/populateWithTestData.php
new file mode 100644
index 000..70874ae
--- /dev/null
+++ b/maintenance/populateWithTestData.php
@@ -0,0 +1,192 @@
+addDescription( 'Fill the database with test data, or 
remove it.' );
+   $this->addOption( 'users', 'Number of users', false, true );
+   $this->addOption( 'lists', 'Lists per user (number or stats 
distribution)', false, true );
+   $this->addOption( 'entries', 'Entries per list (number or stats 
distribution)', false, true );
+   $this->addOption( 'cleanup', 'Delete lists which look like test 
data' );
+   if ( !extension_loaded( 'stats' ) ) {
+   $this->error( 'Requires the stats PHP extension', 1 );
+   }
+   }
+
+   private function setupServices() {
+   // Can't do this in the constructor, initialization not done 
yet.
+   $services = MediaWikiServices::getInstance();
+   $this->loadBalancerFactory = 
$services->getDBLoadBalancerFactory();
+   $this->dbw = Utils::getDB( DB_MASTER, $services );
+   $this->dbr = Utils::getDB( DB_REPLICA, $services );
+   }
+
+   /**
+* @inheritDoc
+*/
+   public function execute() {
+   $this->setupServices();
+   $this->assertOptions();
+   if ( $this->getOption( 'cleanup' ) ) {
+   $this->cleanupTestData();
+   return;
+   }
+
+   $projects = $this->dbw->selectFieldValues( 
'reading_list_project', 'rlp_id' );
+   if ( !$projects ) {
+   $this->error( 'No projects! Please set up some', 1 );
+   }
+   $totalLists = $totalEntries = 0;
+   stats_rand_setall( mt_rand(), mt_rand() );
+   $users = $this->getOption( 'users' );
+   for ( $i = 0; $i < $users; $i++ ) {
+   // The test data is for performance testing so we don't 
care whether the user exists.
+   $centralId = 1000 + $i;
+   $repository = new ReadingListRepository( $centralId, 
$this->dbw, $this->dbr,
+   $this->loadBalancerFactory );
+   try {
+   $repository->setupForUser();
+   $i++;
+   // HACK mark default list so it will be deleted 
together with the rest
+   $this->dbw->update(
+   'reading_list',
+   [ 'rl_description' => __FILE__ ],
+   [
+   'rl_user_id' => $centralId,
+   'rl_is_default' => 1,
+   ]
+   );
+   } catch ( ReadingListRepositoryException $e ) {}
+   $lists = $this->getRandomValueFromDistribution( 
$this->getOption( 'lists' ) );
+   for ( $j = 0; $j < $lists; $j++, $totalLists++ ) {
+   $listId = $repository->addList( "test_$j", 
__FILE__ );
+   $entries = 
$this->getRandomValueFromDistribution( $this->getOption( 'entries' ) );
+   $rows = [];
+   for ( $k = 0; $k < $entries; $k++, 
$totalEntries++ ) {
+   $project = $projects[array_rand( 
$projects )];
+   // Calling addListEntry for each row 
separately would be a bit slow.
+   $rows[] = [
+   'rle_rl_id' => $listId,
+   'rle_user_id' => $centralId,
+   'rle_rlp_id' => $project,
+   'rle_title' => "Test_$k",
+   ];
+   }
+   $this->dbw->insert(
+   'reading_list_entry',
+   $rows
+   );
+   }
+