Jqnatividad has uploaded a new change for review.
https://gerrit.wikimedia.org/r/77643
Change subject: Expanded MongoDB connect string comments; fully implemented
MongoDB aggregation framework support
......................................................................
Expanded MongoDB connect string comments; fully implemented MongoDB aggregation
framework support
Change-Id: I73203b6feb6b3b8df851fd3b83df81bbfebfe4d9
---
M ED_Utils.php
1 file changed, 33 insertions(+), 18 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ExternalData
refs/changes/43/77643/1
diff --git a/ED_Utils.php b/ED_Utils.php
index 3fba289..4758583 100644
--- a/ED_Utils.php
+++ b/ED_Utils.php
@@ -287,8 +287,10 @@
* MongoDB.
*/
static function getMongoDBData( $db_server, $db_username, $db_password,
$db_name, $from, $columns, $where, $sqlOptions, $otherParams ) {
-
- // construct connect string
+ // MongoDB login is done using a single string.
+ // When specifying extra connect string options (e.g.
replicasets,timeout, etc.),
+ // use $db_server to pass these values
+ // see
http://docs.mongodb.org/manual/reference/connection-string
$connect_string = "mongodb://";
if ( $db_username != '' ) {
$connect_string .= $db_username . ':' . $db_password .
'@';
@@ -299,19 +301,19 @@
$connect_string .= 'localhost:27017';
}
- // use try catch to suppress error message that shows MongoDB
connect string
- // that may have sensitive information
+ // Use try/catch to suppress error messages, which would show
+ // the MongoDB connect string, which may have sensitive
+ // information.
try {
- $m = new MongoClient($connect_string);
- } catch (Exception $e) {
+ $m = new MongoClient( $connect_string );
+ } catch ( Exception $e ) {
return wfMessage( "externaldata-db-could-not-connect"
)->text();
}
- // if working against a MongoDB replica set, it's OK to go to
secondary/slaves
- // should the primary go down
+ // If working against a MongoDB replica set, it's OK to go to
+ // secondary/slaves should the primary go down.
MongoCursor::$slaveOkay = true;
$db = $m->selectDB( $db_name );
-
// MongoDB doesn't seem to have a way to check whether either
// a database or a collection exists, so instead we'll use
@@ -329,12 +331,18 @@
$findArray = array();
$aggregateArray = array();
- // Was a direct MongoDB "find" query JSON string provided?
- // If so, use that.
- if ( array_key_exists( 'find query', $otherParams ) ) {
+ // Was an aggregation pipeline command issued?
+ if ( array_key_exists('aggregate', $otherParams ) ) {
+ // The 'aggregate' parameter should be an array of
+ // aggregation JSON pipeline commands.
// Note to users: be sure to use spaces between curly
- // brackets in the 'find' JSON so as not to trip up the
+ // brackets in the 'aggregate' JSON so as not to trip
up the
// MW parser.
+ $aggregateArray = json_decode
($otherParams['aggregate'], true);
+ } elseif ( array_key_exists( 'find query', $otherParams ) ) {
+ // Otherwise, was a direct MongoDB "find" query JSON
string provided?
+ // If so, use that. As with 'aggregate' JSON, use
spaces
+ // between curly brackets
$findArray = json_decode ($otherParams['find query'],
true);
} elseif ( $where != '' ) {
// If not, turn the SQL of the "where=" parameter into
@@ -370,7 +378,7 @@
}
}
- // Do the same for the "order=" parameter.
+ // Do the same for the "order=" parameter as the "where="
parameter
$sortArray = array();
if ( $sqlOptions['ORDER BY'] != '' ) {
$sortElements = explode( ',', $sqlOptions['ORDER BY'] );
@@ -389,11 +397,18 @@
// Get the data!
if ( array_key_exists( 'aggregate', $otherParams ) ) {
- $resultsCursor = $collection->aggregate(
$aggregateArray );
+ if ( $sqlOptions['ORDER BY'] != '') {
+ $aggregateArray[] = array( '$sort' =>
$sortArray );
+ }
+ if ( $sqlOptions['LIMIT'] != '' ) {
+ $aggregateArray[] = array( '$limit' => intval(
$sqlOptions['LIMIT'] ) );
+ }
+ $aggregateResult = $collection->aggregate(
$aggregateArray );
+ $resultsCursor = $aggregateResult['result'];
} else {
$resultsCursor = $collection->find( $findArray,
$columns )->sort( $sortArray )->limit( $sqlOptions['LIMIT'] );
}
-
+
$values = array();
foreach ( $resultsCursor as $doc ) {
foreach ( $columns as $column ) {
@@ -401,14 +416,14 @@
// If the exact path of the value was
// specified using dots (e.g., "a.b.c"),
// get the value that way.
- $values[$column][] =
self::getValueFromJSONArray( $doc, $column );
+ $values[$column][] =
self::getValueFromJSONArray( $doc, $column );
} elseif ( is_array( $doc[$column] ) ) {
// If MongoDB returns an array for a
column,
// but the exact location of the value
wasn't specified,
// 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
+ //
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];
--
To view, visit https://gerrit.wikimedia.org/r/77643
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I73203b6feb6b3b8df851fd3b83df81bbfebfe4d9
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