Add runImport function
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 4 Jun 2022 22:32:43 +0000 (10:32 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 4 Jun 2022 22:39:45 +0000 (10:39 +1200)
This is required both for the queue fix and for QA fixes on the import classes.

CRM/Import/Parser.php

index 93c5260ff197a1ec7390f16921fef46964858396..67878b087d7b08769005824b128f64528f5d9fe5 100644 (file)
@@ -1652,6 +1652,52 @@ abstract class CRM_Import_Parser {
     return $mappedFields;
   }
 
+  /**
+   * Run import.
+   *
+   * @param \CRM_Queue_TaskContext $taskContext
+   *
+   * @param int $userJobID
+   * @param int $limit
+   *
+   * @return bool
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  public static function runImport($taskContext, $userJobID, $limit) {
+    $userJob = UserJob::get()->addWhere('id', '=', $userJobID)->addSelect('type_id')->execute()->first();
+    $parserClass = NULL;
+    foreach (CRM_Core_BAO_UserJob::getTypes() as $userJobType) {
+      if ($userJob['type_id'] === $userJobType['id']) {
+        $parserClass = $userJobType['class'];
+      }
+    }
+    $parser = new $parserClass();
+    $parser->setUserJobID($userJobID);
+    // Not sure if we still need to init....
+    $parser->init();
+    $dataSource = $parser->getDataSourceObject();
+    $dataSource->setStatuses(['new']);
+    $dataSource->setLimit($limit);
+
+    while ($row = $dataSource->getRow()) {
+      $values = array_values($row);
+
+      try {
+        $parser->import($parser->getSubmittedValue('onDuplicate'), $values);
+      }
+      catch (CiviCRM_API3_Exception $e) {
+        // When we catch errors here we are not adding to the errors array - mostly
+        // because that will become obsolete once https://github.com/civicrm/civicrm-core/pull/23292
+        // is merged and this will replace it as the main way to handle errors (ie. update the table
+        // and move on).
+        $parser->setImportStatus((int) $values[count($values) - 1], 'ERROR', $e->getMessage());
+      }
+    }
+    $parser->doPostImportActions();
+    return TRUE;
+  }
+
   /**
    * Check if an error in custom data.
    *