jenkins-bot has submitted this change and it was merged.

Change subject: SMW\DISerializer export formatting relevant info
......................................................................


SMW\DISerializer export formatting relevant info

Make getOutputFormat() available and enable printRequest related
formatting to be exported.

* Support printrequests that can carry an additional notion such as 
Modification date#ISO (see [1] ).

* Fixed [{\"value\":188,\"unit\":\"km\\u00b2\"},777.00077700078] turns into
[{\"value\":188,\"unit\":\"km\\u00b2\"},{\"value\":777.00077700078,\"unit\":\"km\\u00b2\"}],
when another quantity was part of the same $resultArray array_map would
invoke an empty $printRequest array (which normally contains the _qty type
indentification) showing a number instead of a quantity

[1] 
http://www.semantic-mediawiki.org/wiki/Help:Displaying_information#Formats_for_specific_printout_types

Change-Id: I44a563d28f5f61b36ef384ba3a377e5442fd7e3c
---
M includes/dataitems/DISerializer.php
M tests/phpunit/includes/dataitems/DISerializerTest.php
2 files changed, 117 insertions(+), 47 deletions(-)

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



diff --git a/includes/dataitems/DISerializer.php 
b/includes/dataitems/DISerializer.php
index ec84b64..d91a8b3 100644
--- a/includes/dataitems/DISerializer.php
+++ b/includes/dataitems/DISerializer.php
@@ -34,6 +34,15 @@
  * @licence GNU GPL v2 or later
  * @author Jeroen De Dauw < [email protected] >
  */
+
+/**
+ * Class for serializing SMWDataItem and SMWQueryResult objects to a context
+ * independent object consisting of arrays and associative arrays, which can
+ * be fed directly to json_encode, the MediaWiki API, and similar serializers.
+ *
+ * @ingroup SMW
+ * @ingroup DataItem
+ */
 class DISerializer {
 
        /**
@@ -107,6 +116,7 @@
                                'label' => $printRequest->getLabel(),
                                'typeid' => $printRequest->getTypeID(),
                                'mode' => $printRequest->getMode(),
+                               'format' => $printRequest->getOutputFormat()
                        );
                }
 
@@ -120,10 +130,11 @@
                                        $dataItems = $resultArray->getContent();
                                        $result += self::getSerialization( 
array_shift( $dataItems ), $printRequest );
                                } else if ( $resultArray->getContent() !== 
array() ) {
-                                       
$result['printouts'][$printRequest->getLabel()] = array_map(
-                                               array( __CLASS__, 
'getSerialization' ),
-                                               $resultArray->getContent(), 
array ( $printRequest )
-                                       );
+                                       $values = array();
+                                       foreach ( $resultArray->getContent() as 
$dataItem ) {
+                                               $values[] = 
self::getSerialization( $dataItem, $printRequest );
+                                       }
+                                       
$result['printouts'][$printRequest->getLabel()] = $values;
                                } else {
                                        // For those objects that are empty 
return an empty array
                                        // to keep the output consistent
@@ -143,4 +154,4 @@
  *
  * @deprecated since SMW 1.9
  */
-class_alias( 'SMW\DISerializer', 'SMWDISerializer' );
\ No newline at end of file
+class_alias( 'SMW\DISerializer', 'SMWDISerializer' );
diff --git a/tests/phpunit/includes/dataitems/DISerializerTest.php 
b/tests/phpunit/includes/dataitems/DISerializerTest.php
index 26ef0be..290c8f0 100644
--- a/tests/phpunit/includes/dataitems/DISerializerTest.php
+++ b/tests/phpunit/includes/dataitems/DISerializerTest.php
@@ -1,6 +1,7 @@
 <?php
 
 namespace SMW\Test;
+
 use SMW\DISerializer;
 use SMWQueryProcessor;
 
@@ -22,9 +23,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
- * @file
  * @since 1.9
  *
+ * @file
  * @ingroup SMW
  * @ingroup Test
  *
@@ -34,81 +35,139 @@
  * @licence GNU GPL v2+
  * @author mwjames
  */
+
+/**
+ * DISerializer test is to verify the exported array structure for content
+ * consumers such as the API etc.
+ */
 class DISerializerTest extends \MediaWikiTestCase {
 
        /**
-        * Helper function to build a query
+        * DataProvider
         *
+        * @return array
         */
-       protected function getQuery( $queryString, $parameters, array 
$printouts ) {
-               SMWQueryProcessor::addThisPrintout( $printouts, $parameters );
-               $parameters = SMWQueryProcessor::getProcessedParams( 
$parameters, $printouts );
+       public function getDataProvider() {
+               return array(
+                       array(
 
-               return SMWQueryProcessor::createQuery(
-                       $queryString,
-                       $parameters,
-                       SMWQueryProcessor::SPECIAL_PAGE,
-                       '',
-                       $printouts
+                               // #1 Standard query
+                               array( 'query' => array(
+                                       '[[Modification date::+]]',
+                                       '?Modification date',
+                                       'limit=10'
+                                       )
+                               ),
+                               array(
+                                       array(
+                                               'label'=> '',
+                                               'typeid' => '_wpg',
+                                               'mode' => 2,
+                                               'format' => false
+                                       ),
+                                       array(
+                                               'label'=> 'Modification date',
+                                               'typeid' => '_dat',
+                                               'mode' => 1,
+                                               'format' => ''
+                                       )
+                               )
+                       ),
+
+                       // #2 Query containing a printrequest formatting
+                       array(
+                               array( 'query' => array(
+                                       '[[Modification date::+]]',
+                                       '?Modification date#ISO',
+                                       'limit=10'
+                                       )
+                               ),
+                               array(
+                                       array(
+                                               'label'=> '',
+                                               'typeid' => '_wpg',
+                                               'mode' => 2,
+                                               'format' => false
+                                       ),
+                                       array(
+                                               'label'=> 'Modification date',
+                                               'typeid' => '_dat',
+                                               'mode' => 1,
+                                               'format' => 'ISO'
+                                       )
+                               )
+                       ),
                );
        }
 
        /**
-        * Helper function to fetch the query results
+        * Helper function to return a query result object from a query string
         *
         */
-       protected function getQueryResult( $queryString ) {
-               $rawParams = preg_split( "/(?<=[^\|])\|(?=[^\|])/", 
$queryString );
-               list( $queryString, $parameters, $printouts ) = 
SMWQueryProcessor::getComponentsFromFunctionParams( $rawParams, false );
+       private function getQueryResult( $queryString ) {
+               list( $query, $formattedParams ) = 
SMWQueryProcessor::getQueryAndParamsFromFunctionParams(
+                       $queryString,
+                       SMW_OUTPUT_WIKI,
+                       SMWQueryProcessor::INLINE_QUERY,
+                       false
+               );
 
-               return smwfGetStore()->getQueryResult( $this->getQuery( 
$queryString, $parameters, $printouts ) );
+               return smwfGetStore()->getQueryResult( $query );
        }
 
        /**
-        * @covers DISerializer::getSerializedQueryResult
-        * @covers SMWQueryResult::toArray
+        * Test DISerializer::getSerializedQueryResult
         *
         * @since  1.9
+        *
+        * @dataProvider getDataProvider
+        * @param array $test
+        * @param array $expected
         */
-       public function testSerializedQueryResult( ) {
+       public function testDISerializerQueryResult( array $test, array 
$expected ) {
 
-               $query = '[[Modification date::+]]|?Modification date|limit=10';
-
-               $queryResult = $this->getQueryResult( $query );
+               $queryResult = $this->getQueryResult( $test['query'] );
                $this->assertInstanceOf( '\SMWQueryResult', $queryResult );
 
                $results = DISerializer::getSerializedQueryResult( $queryResult 
);
-               $this->assertTrue( is_array( $results ) );
+               $this->assertInternalType( 'array' , $results );
 
-               $printrequests[0] = array( 'label'=> '', 'typeid' => '_wpg', 
'mode' => 2 );
-               $printrequests[1] = array( 'label'=> 'Modification date', 
'typeid' => '_dat', 'mode' => 1 );
-
-               $this->assertEquals( $results['printrequests'][0], 
$printrequests[0] );
-               $this->assertEquals( $results['printrequests'][1], 
$printrequests[1] );
-
-               $queryResultToArray = $queryResult->toArray();
-
-               $this->assertEquals( $queryResultToArray['printrequests'][0], 
$printrequests[0] );
-               $this->assertEquals( $queryResultToArray['printrequests'][1], 
$printrequests[1] );
-
+               //  Compare array structure
+               $this->assertEquals( $expected[0], $results['printrequests'][0] 
);
+               $this->assertEquals( $expected[1], $results['printrequests'][1] 
);
        }
 
        /**
-        * @covers DISerializer::getSerialization
+        * Test SMWQueryResult::toArray
+        *
+        * @since  1.9
+        *
+        * @dataProvider getDataProvider
+        * @param array $test
+        * @param array $expected
+        */
+       public function testQueryResulttoArray( array $test, array $expected ) {
+
+               $queryResult = $this->getQueryResult( $test['query'] );
+               $this->assertInstanceOf( '\SMWQueryResult', $queryResult );
+
+               $results = $queryResult->toArray();
+
+               //  Compare array structure
+               $this->assertEquals( $expected[0], $results['printrequests'][0] 
);
+               $this->assertEquals( $expected[1], $results['printrequests'][1] 
);
+       }
+
+       /**
+        * Test DISerializer::getSerialization
         *
         * @since  1.9
         */
-       public function testGetSerialization( ) {
+       public function testNumberSerialization() {
 
                // Number
                $dataItem = new \SMWDINumber( 1001 );
                $results = DISerializer::getSerialization( $dataItem );
                $this->assertEquals( $results, 1001 );
-
-               // Quantity
-               // Test the quantity here but after spending hours to figure out
-               // how to mock the $printRequest object which come to no 
fruitful
-               // success we miss out a test
        }
-
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I44a563d28f5f61b36ef384ba3a377e5442fd7e3c
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Mwjames <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to