Yurik has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/327246 )

Change subject: Reindex tabular data array for easier lua access
......................................................................

Reindex tabular data array for easier lua access

change 0-based array indexes to 1-based,
otherwise lua users get very surprised of the
missing data

Bug: T152809
Change-Id: I5262edc279cd7737623e721faed968fa43b170b6
---
M includes/JCLuaLibrary.php
M tests/phpunit/JCTabularContentTest.php
2 files changed, 72 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/JsonConfig 
refs/changes/46/327246/1

diff --git a/includes/JCLuaLibrary.php b/includes/JCLuaLibrary.php
index 1bc5ee6..49a491e 100644
--- a/includes/JCLuaLibrary.php
+++ b/includes/JCLuaLibrary.php
@@ -3,11 +3,8 @@
 namespace JsonConfig;
 
 use Language;
-use MalformedTitleException;
 use Scribunto_LuaError;
 use Scribunto_LuaLibraryBase;
-use Title;
-use TitleValue;
 
 class JCLuaLibrary extends Scribunto_LuaLibraryBase {
 
@@ -72,8 +69,35 @@
                } else {
                        /** @var JCDataContent $content */
                        $result = $content->getLocalizedData( $language );
+
+                       if ( $content instanceof JCTabularContent ) {
+                               self::reindexTabularData( $result );
+                       }
                }
 
                return [ $result ];
        }
+
+       /**
+        * Reindex tabular data so it can be processed by Lua more easily
+        * @param object $data
+        */
+       public static function reindexTabularData( $data ) {
+               $columnCount = count( $data->schema->fields );
+               $rowCount = count( $data->data );
+               if ( $columnCount > 0 ) {
+                       $rowIndexes = range( 1, $columnCount );
+                       $data->schema->fields = array_combine( $rowIndexes, 
$data->schema->fields );
+                       if ( $rowCount > 0 ) {
+                               $data->data =
+                                       array_combine( range( 1, $rowCount ),
+                                               array_map( function ( $row ) 
use ( $rowIndexes ) {
+                                                       return array_combine( 
$rowIndexes, $row );
+                                               }, $data->data ) );
+                       }
+               } elseif ( $rowCount > 0 ) {
+                       // Weird, but legal
+                       $data->data = array_combine( range( 1, $rowCount ), 
$data->data );
+               }
+       }
 }
diff --git a/tests/phpunit/JCTabularContentTest.php 
b/tests/phpunit/JCTabularContentTest.php
index c8cf511..23e00b6 100644
--- a/tests/phpunit/JCTabularContentTest.php
+++ b/tests/phpunit/JCTabularContentTest.php
@@ -3,6 +3,7 @@
 namespace JsonConfig\Tests;
 
 use FormatJson;
+use JsonConfig\JCLuaLibrary;
 use JsonConfig\JCTabularContent;
 use Language;
 use MediaWikiTestCase;
@@ -97,4 +98,48 @@
 
                return $result;
        }
+
+
+       /**
+        * @dataProvider provideLuaReindexingTests
+        * @param int $fieldCount
+        * @param array $data
+        * @param array $expected
+        */
+       public function testLuaTabDataReindexing( $fieldCount, $data, $expected 
) {
+               if ( !class_exists( 'Scribunto_LuaLibraryBase' ) ) {
+                       $this->markTestSkipped( "Scribunto is required for this 
integration test" );
+               }
+
+               $value = (object)[ 'schema' => (object)[] ];
+               $value->data = $data;
+               $value->schema->fields = $fieldCount > 0 ? array_fill( 0, 
$fieldCount, (object) [ ] ) : [ ];
+               JCLuaLibrary::reindexTabularData( $value );
+               $this->assertEquals( $expected, $value->data );
+               $this->assertEquals( $fieldCount > 0 ? range( 1, $fieldCount ) 
: [ ],
+                       array_keys( $value->schema->fields ) );
+       }
+
+       public function provideLuaReindexingTests() {
+               return [
+                       // fieldCount, data, expected
+                       [ 0, [], [] ],
+                       [ 1, [], [] ],
+                       [
+                               1,
+                               [ [ "a" ] ],
+                               [ 1 => [ 1 => "a" ] ]
+                       ],
+                       [
+                               2,
+                               [ [ 0, "a" ] ],
+                               [ 1 => [ 1 => 0, 2 => "a" ] ]
+                       ],
+                       [
+                               2,
+                               [ [ 0, "a" ], [ - 1, "-" ] ],
+                               [ 1 => [ 1 => 0, 2 => "a" ], 2 => [ 1 => -1, 2 
=> "-" ] ]
+                       ],
+               ];
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5262edc279cd7737623e721faed968fa43b170b6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/JsonConfig
Gerrit-Branch: wmf/1.29.0-wmf.6
Gerrit-Owner: Yurik <yu...@wikimedia.org>

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

Reply via email to