jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/351127 )

Change subject: Further removal of class_api3 call in favour of civicrm_api3
......................................................................


Further removal of class_api3 call in favour of civicrm_api3

class_api is causing test failures when the updated mysqli driver tries to free 
memory

This replacement promotes the use of wmf_civicrm_get_option() as a cached 
methodology
to retrieve an option for any field. There is actually caching at the api level
so the extra caching here is probably of marginal benefit, but I am replacing a 
statically cached call.

Change-Id: I2b78783f87a5f352b85b3adce2dbd8f203682136
---
M sites/all/modules/queue2civicrm/tests/phpunit/DonationQueueTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
2 files changed, 31 insertions(+), 40 deletions(-)

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



diff --git 
a/sites/all/modules/queue2civicrm/tests/phpunit/DonationQueueTest.php 
b/sites/all/modules/queue2civicrm/tests/phpunit/DonationQueueTest.php
index 0f84d75..c221efd 100644
--- a/sites/all/modules/queue2civicrm/tests/phpunit/DonationQueueTest.php
+++ b/sites/all/modules/queue2civicrm/tests/phpunit/DonationQueueTest.php
@@ -253,7 +253,7 @@
                                'is_active' => $is_active,
                        )
                );
-               civicrm_api_option_group( 
wmf_civicrm_get_direct_mail_field_option_name(), null, TRUE );
+               wmf_civicrm_flush_cached_options();
                return $appealField['id'];
        }
 
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 62de2ce..6aeeab7 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -4,40 +4,6 @@
 require_once 'tracking.inc';
 
 /**
- * return a map of option value (id) => option name
- *
- * FIXME: Break into separate functions rather than magically switching return 
type
- */
-function civicrm_api_option_group($group_name, $key = null, $force = FALSE)
-{
-       static $cached_group = array();
-       if (!array_key_exists($group_name, $cached_group) || $force)
-       {
-               $api = civicrm_api_classapi();
-               $api->OptionValue->Get(array(
-                       'option_group_name' => $group_name,
-                       'options' => array('limit' => 0),
-                       'is_active' => 1,
-               ));
-               $cached_group[$group_name] = array();
-               $option_values = $api->values();
-               foreach ($option_values as $row) {
-                       $cached_group[$group_name][$row->value] = $row->name;
-               }
-       }
-       if ($key)
-       {
-               // In this case, just lookup name and return the option value
-               $forwards = array_flip( $cached_group[$group_name] );
-               if ( !array_key_exists( $key, $forwards ) ) {
-                       throw new DomainException( "Option value not found: 
{$group_name} -> {$key}" );
-               }
-               return $forwards[$key];
-       }
-       return $cached_group[$group_name];
-}
-
-/**
  * Ensure the specified option value exists.
  *
  * @param string $group_name
@@ -66,8 +32,9 @@
   }
   if ($createRequired) {
     civicrm_api3('OptionValue', 'create', $params);
-    // Flush cache.
-    civicrm_api_option_group($group_name, $value, TRUE);
+    // It won't take much to rebuild this & we don't know the entity.
+    // This should be rare.
+    wmf_civicrm_flush_cached_options();
   }
 }
 
@@ -83,7 +50,7 @@
 
 function civicrm_api_contribution_status($key = null)
 {
-       return civicrm_api_option_group('contribution_status', $key);
+    return wmf_civicrm_get_option('Contribution', 'contribution_status_id', 
$key);
 }
 
 function civicrm_api_classapi()
@@ -3423,8 +3390,26 @@
  * @throws \CiviCRM_API3_Exception
  */
 function wmf_civicrm_get_options($entity, $field) {
-    $options = civicrm_api3($entity, 'getoptions', array('field' => $field));
-    return $options['values'];
+    if (!isset(\Civi::$statics['wmf_civicrm'][$entity][$field])) {
+      $options = civicrm_api3($entity, 'getoptions', array('field' => $field));
+      \Civi::$statics['wmf_civicrm'][$entity][$field] = $options['values'];
+    }
+    return \Civi::$statics['wmf_civicrm'][$entity][$field];
+}
+
+/**
+ * Get options for a field.
+ *
+ * @param string $entity
+ * @param string $field
+ * @param string $key
+ *
+ * @return array
+ * @throws \CiviCRM_API3_Exception
+ */
+function wmf_civicrm_get_option($entity, $field, $key) {
+  $options = wmf_civicrm_get_options($entity, $field);
+  return array_search($key, $options);
 }
 
 /**
@@ -3562,3 +3547,9 @@
     return $output;
   }
 
+/**
+ * Flush statically cached option values.
+ */
+  function wmf_civicrm_flush_cached_options() {
+    \Civi::$statics['wmf_civicrm'] = array();
+  }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I2b78783f87a5f352b85b3adce2dbd8f203682136
Gerrit-PatchSet: 6
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Eileen <[email protected]>
Gerrit-Reviewer: Eileen <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to