Yaron Koren has submitted this change and it was merged.
Change subject: Incorporated Yaron's suggestion to use a separate parameter to
capture MongoDB Find Query. The new parm, is appropriately named "find query".
Also changed str_replace with str_ireplace for matching AND & LIKE in parsing
MongoDB where.
......................................................................
Incorporated Yaron's suggestion to use a separate parameter to capture MongoDB
Find Query. The new parm, is appropriately named "find query".
Also changed str_replace with str_ireplace for matching AND & LIKE in parsing
MongoDB where.
Change-Id: I9438fa2dc54d9c3d3f1de1eec984b998ee4f3093
---
M ED_ParserFunctions.php
M ED_Utils.php
2 files changed, 38 insertions(+), 41 deletions(-)
Approvals:
Yaron Koren: Verified; Looks good to me, approved
diff --git a/ED_ParserFunctions.php b/ED_ParserFunctions.php
index 93c0f05..44a3919 100644
--- a/ED_ParserFunctions.php
+++ b/ED_ParserFunctions.php
@@ -248,10 +248,11 @@
}
$table = ( array_key_exists( 'from', $args ) ) ? $args['from']
: null;
$conds = ( array_key_exists( 'where', $args ) ) ?
$args['where'] : null;
+ $findQuery = ( array_key_exists( 'find query', $args ) ) ?
$args['find query'] : null;
$limit = ( array_key_exists( 'limit', $args ) ) ?
$args['limit'] : null;
$orderBy = ( array_key_exists( 'order by', $args ) ) ?
$args['order by'] : null;
$groupBy = ( array_key_exists( 'group by', $args ) ) ?
$args['group by'] : null;
- $options = array( 'LIMIT' => $limit, 'ORDER BY' => $orderBy,
'GROUP BY' => $groupBy );
+ $options = array( 'LIMIT' => $limit, 'ORDER BY' => $orderBy,
'GROUP BY' => $groupBy, 'FIND QUERY' => $findQuery );
$mappings = EDUtils::paramToArray( $data ); // parse the data
arg into mappings
$external_values = EDUtils::getDBData( $dbID, $table,
array_values( $mappings ), $conds, $options );
diff --git a/ED_Utils.php b/ED_Utils.php
index ae90bb1..33845f5 100644
--- a/ED_Utils.php
+++ b/ED_Utils.php
@@ -291,47 +291,43 @@
$collection = new MongoCollection( $db, $from );
- // Turn the SQL of the "where=" parameter into the appropriate
- // array for MongoDB.
- $whereArray = array();
- if ( $where != '' ) {
-
- // if the string '<PASSTHRU>' is used, then just
passthrough the
- // JSON MongoDB find condition as a JSON string. This
is necessary so we don't try
+ $findArray = array();
+ // Was a direct MongoDB find query JSON string provided?
+ if ( $options['FIND QUERY'] != '' ) {
+ // if FIND QUERY is used, then just passthrough the
+ // JSON MongoDB FIND query as a JSON string. This is
necessary so we don't try
// to create a MongoDB find parser in ExternalData to
accomodate all the possible
// find scenarios since MongoDB is NotSQL :). Be sure
to use spaces between curly
- // brackets so as not to trip up the MW parser
- if ( substr($where, 0, 10) == '<PASSTHRU>' ) {
- $whereArray = json_decode (substr($where, 10),
true);
- } else {
- // Hopefully all-caps and all-lowercase are the
only
- // two variants that people will use -
otherwise,
- // preg_replace() should be used.
- $where = str_replace( ' and ', ' AND ', $where
);
- $where = str_replace( ' like ', ' LIKE ',
$where );
- $whereElements = explode( ' AND ', $where );
- foreach ( $whereElements as $whereElement ) {
- if ( strpos( $whereElement, '>=' ) ) {
- list( $fieldName, $value ) =
explode( '>=', $whereElement );
- $whereArray[trim( $fieldName )]
= array( '$gte' => trim( $value ) );
- } elseif ( strpos( $whereElement, '>' )
) {
- list( $fieldName, $value ) =
explode( '>', $whereElement );
- $whereArray[trim( $fieldName )]
= array( '$gt' => trim( $value ) );
- } elseif ( strpos( $whereElement, '<='
) ) {
- list( $fieldName, $value ) =
explode( '<=', $whereElement );
- $whereArray[trim( $fieldName )]
= array( '$lte' => trim( $value ) );
- } elseif ( strpos( $whereElement, '<' )
) {
- list( $fieldName, $value ) =
explode( '<', $whereElement );
- $whereArray[trim( $fieldName )]
= array( '$lt' => trim( $value ) );
- } elseif ( strpos( $whereElement, '
LIKE ' ) ) {
- list( $fieldName, $value ) =
explode( ' LIKE ', $whereElement );
- $value = trim( $value );
- $regex = new MongoRegex(
"/$value/i" );
- $whereArray[trim( $fieldName )]
= $regex;
- } else {
- list( $fieldName, $value ) =
explode( '=', $whereElement );
- $whereArray[trim( $fieldName )]
= trim( $value );
- }
+ // brackets in the FIND JSON so as not to trip up the
MW parser
+ $findArray = json_decode ($options['FIND QUERY'], true);
+ } elseif ( $where != '' ) {
+ // If not, turn the SQL of the "where=" parameter into
the appropriate find
+ // array for MongoDB. Note that this approach is only
appropriate for simple
+ // find queries - the ff operators: OR, AND, >=, >, <=,
< and LIKE and NO NUMBER LITERALS
+ $where = str_ireplace( ' and ', ' AND ', $where );
+ $where = str_ireplace( ' like ', ' LIKE ', $where );
+ $whereElements = explode( ' AND ', $where );
+ foreach ( $whereElements as $whereElement ) {
+ if ( strpos( $whereElement, '>=' ) ) {
+ list( $fieldName, $value ) = explode(
'>=', $whereElement );
+ $findArray[trim( $fieldName )] = array(
'$gte' => trim( $value ) );
+ } elseif ( strpos( $whereElement, '>' ) ) {
+ list( $fieldName, $value ) = explode(
'>', $whereElement );
+ $findArray[trim( $fieldName )] = array(
'$gt' => trim( $value ) );
+ } elseif ( strpos( $whereElement, '<=' ) ) {
+ list( $fieldName, $value ) = explode(
'<=', $whereElement );
+ $findArray[trim( $fieldName )] = array(
'$lte' => trim( $value ) );
+ } elseif ( strpos( $whereElement, '<' ) ) {
+ list( $fieldName, $value ) = explode(
'<', $whereElement );
+ $findArray[trim( $fieldName )] = array(
'$lt' => trim( $value ) );
+ } elseif ( strpos( $whereElement, ' LIKE ' ) ) {
+ list( $fieldName, $value ) = explode( '
LIKE ', $whereElement );
+ $value = trim( $value );
+ $regex = new MongoRegex( "/$value/i" );
+ $findArray[trim( $fieldName )] = $regex;
+ } else {
+ list( $fieldName, $value ) = explode(
'=', $whereElement );
+ $findArray[trim( $fieldName )] = trim(
$value );
}
}
}
@@ -354,7 +350,7 @@
}
// Get the data!
- $resultsCursor = $collection->find( $whereArray, $columns
)->sort( $sortArray )->limit( $options['LIMIT'] );
+ $resultsCursor = $collection->find( $findArray, $columns
)->sort( $sortArray )->limit( $options['LIMIT'] );
$values = array();
foreach ( $resultsCursor as $doc ) {
--
To view, visit https://gerrit.wikimedia.org/r/62819
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9438fa2dc54d9c3d3f1de1eec984b998ee4f3093
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ExternalData
Gerrit-Branch: master
Gerrit-Owner: Jqnatividad <[email protected]>
Gerrit-Reviewer: Demon <[email protected]>
Gerrit-Reviewer: Yaron Koren <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits