Jqnatividad has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/71255


Change subject: Added dot notation support for retrieving values from compound 
MongoDB result documents; added MongoDB aggregate mode
......................................................................

Added dot notation support for retrieving values from compound MongoDB result 
documents; added MongoDB aggregate mode

Change-Id: I9f2f01e381e74baa62faea0c65da8d7c1b9e23b4
---
M ED_ParserFunctions.php
M ED_Utils.php
2 files changed, 37 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ExternalData 
refs/changes/55/71255/1

diff --git a/ED_ParserFunctions.php b/ED_ParserFunctions.php
index 2bc8c92..752b4a7 100644
--- a/ED_ParserFunctions.php
+++ b/ED_ParserFunctions.php
@@ -253,7 +253,9 @@
                $groupBy = ( array_key_exists( 'group by', $args ) ) ? 
$args['group by'] : null;
                $sqlOptions = array( 'LIMIT' => $limit, 'ORDER BY' => $orderBy, 
'GROUP BY' => $groupBy );
                $otherParams = array();
-               if ( array_key_exists( 'find query', $args ) ) {
+               if ( array_key_exists('aggregate', $args ) ) {
+                       $otherParams['aggregate'] = $args['aggregate'];
+               } elseif ( array_key_exists( 'find query', $args ) ) {
                        $otherParams['find query'] = $args['find query'];
                }
                $mappings = EDUtils::paramToArray( $data ); // parse the data 
arg into mappings
diff --git a/ED_Utils.php b/ED_Utils.php
index 543d347..e7c8c02 100644
--- a/ED_Utils.php
+++ b/ED_Utils.php
@@ -266,6 +266,24 @@
                return $values;
        }
 
+
+       static function dotresolve(array $arrayName, $path, $default = null)
+       {
+         $current = $arrayName;
+         $token = strtok($path, '.');
+
+         while ($token !== false) {
+           if (!isset($current[$token])) {
+             return $default;
+           }
+           $current = $current[$token];
+           $token = strtok('.');
+         }
+
+         return $current;
+       }
+
+
        /**
         * Handles #get_db_data for the non-relational database system
         * MongoDB.
@@ -301,6 +319,7 @@
                $collection = new MongoCollection( $db, $from );
 
                $findArray = array();
+               $aggregateArray = array();
                // Was a direct MongoDB "find" query JSON string provided?
                // If so, use that.
                if ( array_key_exists( 'find query', $otherParams ) ) {
@@ -360,19 +379,26 @@
                }
 
                // Get the data!
-               $resultsCursor = $collection->find( $findArray, $columns 
)->sort( $sortArray )->limit( $sqlOptions['LIMIT'] );
+               if ( array_key_exists( 'aggregate', $otherParams ) ) {
+                       $resultsCursor = $collection->aggregate( 
$aggregateArray );
+               } else {
+                       $resultsCursor = $collection->find( $findArray, 
$columns )->sort( $sortArray )->limit( $sqlOptions['LIMIT'] );
+               }
 
                $values = array();
                foreach ( $resultsCursor as $doc ) {
                        foreach ( $columns as $column ) {
-                               // If MongoDB returns an array for a column,
-                               // do some extra processing.
-                               if ( is_array( $doc[$column] ) ) {
-                                       // Check if it's GeoJSON geometry:
-                                       // 
http://www.geojson.org/geojson-spec.html#geometry-objects 
-                                       // If so, return it in a format that
-                                       // the Maps extension can understand.
+                               if ( strstr($column, ".") ) {
+                                       // If the user specified dot notation 
to retrieve values from the MongoDB result array
+                                       $values[$column][] = 
self::dotresolve($doc, $column);
+                               } elseif ( is_array( $doc[$column] ) ) {
+                                       // If MongoDB returns an array for a 
column, but the user didnt specify dot notation
+                                       // do some extra processing.
                                        if ( $column == 'geometry' && 
array_key_exists( 'coordinates', $doc['geometry'] ) ) {
+                                               // Check if it's GeoJSON 
geometry:
+                                               // 
http://www.geojson.org/geojson-spec.html#geometry-objects 
+                                               // If so, return it in a format 
that
+                                               // the Maps extension can 
understand.
                                                $coordinates = 
$doc['geometry']['coordinates'][0];
                                                $coordinateStrings = array();
                                                foreach ( $coordinates as 
$coordinate ) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9f2f01e381e74baa62faea0c65da8d7c1b9e23b4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ExternalData
Gerrit-Branch: master
Gerrit-Owner: Jqnatividad <[email protected]>

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

Reply via email to