Adamw has uploaded a new change for review.

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


Change subject: WIP (FR #917) Convert dangerous queries to use the Civi db 
connection
......................................................................

WIP (FR #917) Convert dangerous queries to use the Civi db connection

TODO:
write tests

Change-Id: Ie4f2271ca42dd5fe81fd553dd29140e320e3ce04
---
M sites/all/modules/queue2civicrm/recurring/recurring.module
M sites/all/modules/queue2civicrm/unsubscribe/wmf_unsubscribe_qc.module
M sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
3 files changed, 66 insertions(+), 79 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/96/100696/1

diff --git a/sites/all/modules/queue2civicrm/recurring/recurring.module 
b/sites/all/modules/queue2civicrm/recurring/recurring.module
index ec76a2a..c1f9378 100644
--- a/sites/all/modules/queue2civicrm/recurring/recurring.module
+++ b/sites/all/modules/queue2civicrm/recurring/recurring.module
@@ -469,7 +469,7 @@
   if ( !$api->ContributionRecur->Create( $update_params ) ) {
     throw new WmfException( 'INVALID_RECURRING', 'There was a problem updating 
the subscription record for subscription id ' . print_r( $msg['subscr_id'], 
true ) );
   }
-   
+
   // update the contact
   $contact = wmf_civicrm_message_contact_update( $msg, 
$recur_record->contact_id );
   
@@ -736,20 +736,26 @@
     return $msg[ 'custom' ];
   } elseif ( $msg[ 'txn_type' ] == 'subscr_payment' ) {
     $contribution_tracking_id = null;
-    $contrib_ids = array(); // for holding related contribtion ids
-    
-    $dbs = wmf_civicrm_get_dbs();
-    $dbs->push( 'civicrm' );
-    // determine if we already have a contrib tracking id for related 
contribs, working backwards from recur record
-    $query = "SELECT id FROM civicrm_contribution_recur WHERE trxn_id = :id";
-    $recur_id = db_query( $query, array( ':id' => $msg[ 'subscr_id' ] ) 
)->fetchField();
 
-    if ( $recur_id ) { // we potentially have related contributions
+    // determine if we already have a contrib tracking id for related 
contribs, working backwards from recur record
+    $api = civicrm_api_classapi();
+    $api->ContributionRecur->Get( array(
+        'trxn_id' => $msg['subscr_id'],
+
+        'version' => 3,
+    ) );
+
+    if ( $api->values() ) { // we potentially have related contributions
+      $row = array_pop( $api->values );
+      $recur_id = $row['id'];
       // find related contribution ids
-      $query = "SELECT id FROM civicrm_contribution WHERE 
contribution_recur_id = :id";
-      $result = db_query( $query, array( ':id' => $recur_id ) );
-      foreach ( $result as $row ) {
-        array_push( $contrib_ids, $row->id );
+      $api->Contribution->Get( array(
+        'contribution_recur_id' => $recur_id,
+
+        'version' => 3,
+      );
+      foreach ( $api->values() as $row ) {
+        array_push( $contrib_ids, $row['id'] );
       }
       
       if ( count( $contrib_ids )) { // if we've got any related contributions
diff --git 
a/sites/all/modules/queue2civicrm/unsubscribe/wmf_unsubscribe_qc.module 
b/sites/all/modules/queue2civicrm/unsubscribe/wmf_unsubscribe_qc.module
index bae0217..fc6e3c5 100644
--- a/sites/all/modules/queue2civicrm/unsubscribe/wmf_unsubscribe_qc.module
+++ b/sites/all/modules/queue2civicrm/unsubscribe/wmf_unsubscribe_qc.module
@@ -161,23 +161,23 @@
  * @return array
  */
 function unsubscribe_get_emails_from_contribution($ctid) {
-  $query = 'SELECT con.id, con.is_opt_out, e.email';
-  $query .= ' FROM civicrm_contribution ct, civicrm_contact con';
-  $query .= ' LEFT JOIN civicrm_email e';
-  $query .= '  ON con.id=e.contact_id';
-  $query .= ' WHERE ct.id = :id AND ct.contact_id=con.id';
+  $query = "
+SELECT con.id, con.is_opt_out, e.email
+FROM civicrm_contribution ct, civicrm_contact con
+LEFT JOIN civicrm_email e
+  ON con.id = e.contact_id
+WHERE ct.id = %1 AND ct.contact_id = con.id";
 
-  $dbs = wmf_civicrm_get_dbs();
-  $dbs->push( 'civicrm' );
-
-  $res = db_query( $query, array( ':id' => $ctid ) );
+  $dao = CRM_Core_DAO::executeQuery( $query, array(
+    1 => array( $ctid, 'Integer' ),
+  ) );
 
   $out = array();
-  foreach ( $res as $contact ) {
+  while ( $dao->fetch() ) {
     $out[] = array(
-      'contact_id' => (int)$contact->id,
-      'is_opt_out' => (bool)$contact->is_opt_out,
-      'email' => $contact->email,
+      'contact_id' => (int)$dao->id,
+      'is_opt_out' => (bool)$dao->is_opt_out,
+      'email' => $dao->email,
     );
   }
   return $out;
diff --git a/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
index cc4d983..f60cc07 100644
--- a/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
@@ -1099,22 +1099,13 @@
        $gateway = strtolower( $gateway );
        $query = "SELECT cx.*, cc.* FROM wmf_contribution_extra cx LEFT JOIN 
civicrm_contribution cc 
                ON cc.id = cx.entity_id 
-               WHERE gateway LIKE :gateway AND gateway_txn_id LIKE 
:gateway_txn_id";
-       //FIXME: review whether we still need LIKE
+               WHERE gateway = %1 AND gateway_txn_id = %2";
 
-       $row = false;
-       $dbs = wmf_civicrm_get_dbs();
-       $dbs->push( 'civicrm' );
-       $result = db_query( $query, array( ':gateway' => $gateway, 
':gateway_txn_id' => $gateway_txn_id ) );
-       $matching = array();
-       while ( $row = $result->fetchAssoc() ) {
-               $matching[] = $row;
-       }
-
-       if ( count( $matching ) == 0 ) {
-        return false;
-    }
-       return $matching;
+    $dao = CRM_Core_DAO::executeQuery( $query, array(
+        1 => array( $gateway, 'String' ),
+        2 => array( $gateway_txn_id, 'String' ),
+    ) );
+       return wmf_civicrm_dao_to_list( $dao );
 }
 
 function wmf_civicrm_get_child_contributions_from_gateway_id( $gateway, 
$gateway_txn_id ){
@@ -1122,21 +1113,13 @@
        $query = "SELECT cx.*, cc.* FROM wmf_contribution_extra cxp 
                INNER JOIN wmf_contribution_extra cx ON cxp.entity_id = 
cx.parent_contribution_id 
                LEFT JOIN civicrm_contribution cc ON cc.id = cx.entity_id 
-               WHERE cxp.gateway LIKE :gateway AND cxp.gateway_txn_id LIKE 
:gateway_txn_id";
+               WHERE cxp.gateway = %1 AND cxp.gateway_txn_id = %2";
 
-       $row = false;
-       $dbs = wmf_civicrm_get_dbs();
-       $dbs->push( 'civicrm' );
-       $result = db_query( $query, array( ':gateway' => $gateway, 
':gateway_txn_id' => $gateway_txn_id ) );
-       $matching = array();
-       while ( $row = $result->fetchAssoc() ) {
-               $matching[] = $row;
-       }
-
-       if ( count( $matching ) == 0 ) {
-        return false;
-    }
-       return $matching;
+    $dao = CRM_Core_DAO::executeQuery( $query, array(
+        1 => array( $gateway, 'String' ),
+        2 => array( $gateway_txn_id, 'String' ),
+    ) );
+       return wmf_civicrm_dao_to_list( $dao );
 }
 
 
@@ -1147,11 +1130,13 @@
 }
 
 function wmf_civicrm_set_thankyou_date_to_epoch( $contribution_id ){
-       $dbs = wmf_civicrm_get_dbs();
-       $dbs->push( 'civicrm' );
-       $query = "UPDATE civicrm_contribution SET thankyou_date = '1970-01-01 
00:00:00' WHERE id = :id";
+    $api = civicrm_api_classapi();
+    $result = $api->Contribution->Create( array(
+        'id' => $contribution_id,
+        'thankyou_date' => '1970-01-01 00:00:00',
+        'version' => 3,
+    ) );
 
-       $result = db_query( $query, array( ':id' => $contribution_id ) );
        if ( !$result ) {
                watchdog('thank_you', 'Updating TY date to epoch failed ', 
array(), WATCHDOG_ERROR);
                return false;
@@ -1197,17 +1182,17 @@
     $result = $api->values;
     $contribution = array_pop($result);
 
-    $dbs = wmf_civicrm_get_dbs();
-    $dbs->push( 'civicrm' );
-
     // Look for existing refunds
-    $query = <<<"EOS"
+    $query = "
 SELECT id FROM wmf_contribution_extra
 WHERE
-    parent_contribution_id = :id
-    OR (entity_id = :id AND COALESCE(parent_contribution_id, 0))
-EOS;
-    if ( db_query($query, array( ':id' => $contribution_id ))->rowCount() ) {
+    parent_contribution_id = %1
+    OR (entity_id = %1 AND COALESCE(parent_contribution_id, 0))";
+
+    $dao = CRM_Core_DAO::executeQuery( $query, array(
+        1 => array( $contribution_id, 'Integer' ),
+    ) );
+    if ( $dao->fetch() ) {
         throw new WmfException( 'INVALID_MESSAGE', "Contribution is already 
linked to a refund, or is itself a refund: $contribution_id" );
     }
 
@@ -1216,24 +1201,20 @@
             or $contribution->contribution_source === "RFD")
         and $original_currency and $original_amount > 0
     ) {
-        $dbs->pop();
-
-        //FIXME: call this from the contribution save hook. $round parameter. 
+        //FIXME!: call this from the contribution save hook. $round parameter. 
         $contribution->total_amount = round( exchange_rate_convert(
             $original_currency, $original_amount,
             strtotime( $contribution->receive_date )
         ), 2 );
 
-        $dbs->push( 'civicrm' );
+        $api->Contribution->Create( array(
+            'id' => $contribution_id,
 
-        $sql = <<<EOS
-UPDATE civicrm_contribution
-SET
-    source = :source,
-    total_amount = :total
-WHERE id = :id
-EOS;
-        db_query( $sql, array( ':source' => "$original_currency 
$original_amount", ':total' => $contribution->total_amount, ':id' => 
$contribution_id ) );
+            'source' => "{$original_currency} {$original_amount}",
+            'total_amount' => $contribution->total_amount,
+
+            'version' => 3,
+        ) );
     } else {
         list($actual_currency, $actual_amount) = explode(" ", 
$contribution->contribution_source);
         if ( ($original_currency && round($original_currency, 2) != 
round($actual_currency, 2))

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie4f2271ca42dd5fe81fd553dd29140e320e3ce04
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Adamw <[email protected]>

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

Reply via email to