jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/397556 )

Change subject: Convert form ID format in the data stored in the database
......................................................................


Convert form ID format in the data stored in the database

convertLexemeData "dev maintenance" script will update
the structure of the existing form data in the database.

Bug: T180470
Change-Id: I0436febd96b55c14e64fc5fe71b14e4e25ea6199
---
M devMaintenance/LexemeSerializationUpdater.php
M 
tests/phpunit/mediawiki/DevelopmentMaintenance/LexemeSerializationUpdaterTest.php
2 files changed, 50 insertions(+), 4 deletions(-)

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



diff --git a/devMaintenance/LexemeSerializationUpdater.php 
b/devMaintenance/LexemeSerializationUpdater.php
index 809300b..1eaf29a 100644
--- a/devMaintenance/LexemeSerializationUpdater.php
+++ b/devMaintenance/LexemeSerializationUpdater.php
@@ -102,6 +102,9 @@
 
                        $this->addFormsField( $blobData );
                        $this->addSensesField( $blobData );
+
+                       $this->prependFormIds( $blobData );
+
                        $this->addNextFormIdField( $blobData );
 
                        if ( $blobData !== $oldData ) {
@@ -166,11 +169,27 @@
        private function getMaxFormId( array $data ) {
                return (int)array_reduce(
                        $data['forms'],
-                       function ( $currentMaxId, $x ) {
-                               return max( $currentMaxId, (int)substr( 
$x['id'], 1 ) );
+                       function ( $currentMaxId, $x ) use ( $data ) {
+                               $formIdPart = substr( $x['id'], strlen( 
$data['id'] . '-' ) );
+                               return max( $currentMaxId, (int)substr( 
$formIdPart, 1 ) );
                        },
                        0
                );
        }
 
+       /**
+        * Prepends all form IDs with a lexeme ID.
+        * This change has been made to the data in the commit
+        * 6995fe11a7b637bf2301293c492b1fb3ff7a82a4.
+        */
+       private function prependFormIds( array &$data ) {
+               $lexemeId = $data['id'];
+
+               foreach ( $data['forms'] as &$formData ) {
+                       if ( strncmp( $formData['id'], $lexemeId . '-', strlen( 
$lexemeId ) + 1 ) !== 0 ) {
+                               $formData['id'] = $lexemeId . '-' . 
$formData['id'];
+                       }
+               }
+       }
+
 }
diff --git 
a/tests/phpunit/mediawiki/DevelopmentMaintenance/LexemeSerializationUpdaterTest.php
 
b/tests/phpunit/mediawiki/DevelopmentMaintenance/LexemeSerializationUpdaterTest.php
index 36ed2fa..421e402 100644
--- 
a/tests/phpunit/mediawiki/DevelopmentMaintenance/LexemeSerializationUpdaterTest.php
+++ 
b/tests/phpunit/mediawiki/DevelopmentMaintenance/LexemeSerializationUpdaterTest.php
@@ -63,6 +63,25 @@
                );
        }
 
+       public function 
testGivenLexemeDataWithFormIdsMissingLexemeId_formIdsArePrepended() {
+               $lexemeDataWithFormIdsPrependedWithLexemeId = 
$this->getLexemeDataWithNextFormId();
+
+               $db = $this->getDB( [ 
$this->getLexemeDataWithFormIdsMissingLexemeIdPart() ] );
+
+               $updater = $this->newUpdater( $db );
+
+               $updater->update();
+
+               $updatedData = $db->getUpdateData();
+
+               $this->assertCount( 1, $updatedData );
+               $this->assertEquals( [ 'old_id' => 1 ], 
$updatedData[0]['conds'] );
+               $this->assertEquals(
+                       $lexemeDataWithFormIdsPrependedWithLexemeId,
+                       json_decode( $updatedData[0]['new']['old_text'], true )
+               );
+       }
+
        public function testGivenLexemeDataStructureIsUpToDate_noUpdateDone() {
                $db = $this->getDB( [ $this->getLexemeDataWithNextFormId() ] );
 
@@ -104,7 +123,7 @@
                        'claims' => [],
                        'forms' => [
                                [
-                                       'id' => 'F2',
+                                       'id' => 'L1-F2',
                                        'representations' => [
                                                [ 'en' => [ 'language' => 'en', 
'value' => 'goat' ] ],
                                        ],
@@ -153,7 +172,7 @@
                        'nextFormId' => 3,
                        'forms' => [
                                [
-                                       'id' => 'F2',
+                                       'id' => 'L1-F2',
                                        'representations' => [
                                                [ 'en' => [ 'language' => 'en', 
'value' => 'goat' ] ],
                                        ],
@@ -174,6 +193,14 @@
                return $data;
        }
 
+       private function getLexemeDataWithFormIdsMissingLexemeIdPart() {
+               $data = $this->getLexemeDataWithNextFormId();
+
+               $data['forms'][0]['id'] = 'F2';
+
+               return $data;
+       }
+
        private function newUpdater( IDatabase $db ) {
                return new LexemeSerializationUpdater( $db, 666 );
        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I0436febd96b55c14e64fc5fe71b14e4e25ea6199
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: WMDE-leszek <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Thiemo Kreuz (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to