*/
class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser {
+ /**
+ * Get information about the provided job.
+ * - name
+ * - id (generally the same as name)
+ * - label
+ *
+ * e.g. ['activity_import' => ['id' => 'activity_import', 'label' => ts('Activity Import'), 'name' => 'activity_import']]
+ *
+ * @return array
+ */
+ public static function getUserJobInfo(): array {
+ return [
+ 'activity_import' => [
+ 'id' => 'activity_import',
+ 'name' => 'activity_import',
+ 'label' => ts('Activity Import'),
+ ],
+ ];
+ }
+
/**
* The initializer code, called before the processing.
*/
}
}
+ /**
+ * Get information about the provided job.
+ *
+ * - name
+ * - id (generally the same as name)
+ * - label
+ *
+ * @return array
+ */
+ public static function getUserJobInfo(): array {
+ return [
+ 'contact_import' => [
+ 'id' => 'contact_import',
+ 'name' => 'contact_import',
+ 'label' => ts('Contact Import'),
+ ],
+ ];
+ }
+
/**
* Get the fields to track the import.
*
$this->_mapperKeys = $mapperKeys;
}
+ /**
+ * Get information about the provided job.
+ * - name
+ * - id (generally the same as name)
+ * - label
+ *
+ * e.g. ['activity_import' => ['id' => 'activity_import', 'label' => ts('Activity Import'), 'name' => 'activity_import']]
+ *
+ * @return array
+ */
+ public static function getUserJobInfo(): array {
+ return [
+ 'contribution_import' => [
+ 'id' => 'contribution_import',
+ 'name' => 'contribution_import',
+ 'label' => ts('Contribution Import'),
+ ],
+ ];
+ }
+
/**
* Contribution-specific result codes
* @see CRM_Import_Parser result code constants
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
+use Civi\Core\ClassScanner;
+use Civi\UserJob\UserJobInterface;
+
/**
* This class contains user jobs functionality.
*/
* @return array
*/
public static function getTypes(): array {
- return [
- [
- 'id' => 'contact_import',
- 'name' => 'contact_import',
- 'label' => ts('Contact Import'),
- 'class' => 'CRM_Contact_Import_Parser_Contact',
- ],
- [
- 'id' => 'contribution_import',
- 'name' => 'contribution_import',
- 'label' => ts('Contribution Import'),
- 'class' => 'CRM_Contribute_Import_Parser_Contribution',
- ],
- [
- 'id' => 'membership_import',
- 'name' => 'membership_import',
- 'label' => ts('Membership Import'),
- 'class' => 'CRM_Member_Import_Parser_Membership',
- ],
- [
- 'id' => 'activity_import',
- 'name' => 'activity_import',
- 'label' => ts('Activity Import'),
- 'class' => 'CRM_Activity_Import_Parser_Activity',
- ],
- [
- 'id' => 'participant_import',
- 'name' => 'participant_import',
- 'label' => ts('Participant Import'),
- 'class' => 'CRM_Event_Import_Parser_Participant',
- ],
- [
- 'id' => 'custom_field_import',
- 'name' => 'custom_field_import',
- 'label' => ts('Multiple Value Custom Field Import'),
- 'class' => 'CRM_Custom_Import_Parser_Api',
- ],
- ];
+ $types = Civi::cache()->get('UserJobTypes');
+ if ($types === NULL) {
+ $types = [];
+ $classes = ClassScanner::get(['interface' => UserJobInterface::class]);
+ foreach ($classes as $class) {
+ $declaredTypes = $class::getUserJobInfo();
+ foreach ($declaredTypes as $index => $declaredType) {
+ $declaredTypes[$index]['class'] = $class;
+ }
+ $types = array_merge($types, $declaredTypes);
+ }
+ Civi::cache()->set('UserJobTypes', $types);
+ }
+ return $types;
}
}
protected $_fields = [];
protected $_multipleCustomData = '';
+ /**
+ * Get information about the provided job.
+ *
+ * - name
+ * - id (generally the same as name)
+ * - label
+ *
+ * @return array
+ */
+ public static function getUserJobInfo(): array {
+ return [
+ 'custom_field_import' => [
+ 'id' => 'custom_field_import',
+ 'name' => 'custom_field_import',
+ 'label' => ts('Multiple Value Custom Field Import'),
+ ],
+ ];
+ }
+
/**
* The initializer code, called before the processing
*
$this->_mapperKeys = &$mapperKeys;
}
+ /**
+ * Get information about the provided job.
+ *
+ * - name
+ * - id (generally the same as name)
+ * - label
+ *
+ * @return array
+ */
+ public static function getUserJobInfo(): array {
+ return [
+ 'participant_import' => [
+ 'id' => 'participant_import',
+ 'name' => 'participant_import',
+ 'label' => ts('Participant Import'),
+ ],
+ ];
+ }
+
/**
* The initializer code, called before the processing.
*/
foreach (CRM_Core_BAO_UserJob::getTypes() as $type) {
if ($this->getUserJob()['job_type'] === $type['id']) {
$parserClass = $type['class'];
+ break;
}
}
/* @var \CRM_Import_Parser */
use Civi\Api4\CustomField;
use Civi\Api4\Event;
use Civi\Api4\UserJob;
+use Civi\UserJob\UserJobInterface;
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
-abstract class CRM_Import_Parser {
+abstract class CRM_Import_Parser implements UserJobInterface {
/**
* Settings
*/
$batchSize = $totalRows;
}
$task = new CRM_Queue_Task(
- [get_class($this), 'runImport'],
- ['userJobID' => $this->getUserJobID(), 'limit' => $batchSize],
+ [get_class($this), 'runJob'],
+ // Offset is unused by our import classes, but required by the interface.
+ ['userJobID' => $this->getUserJobID(), 'limit' => $batchSize, 'offset' => 0],
ts('Processed %1 rows out of %2', [1 => $offset + $batchSize, 2 => $totalRowCount])
);
$queue->createItem($task);
*
* @param int $userJobID
* @param int $limit
+ * @param int $offset
*
* @return bool
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
- public static function runImport($taskContext, $userJobID, $limit) {
+ public static function runJob(\CRM_Queue_TaskContext $taskContext, int $userJobID, int $limit, int $offset): bool {
$userJob = UserJob::get()->addWhere('id', '=', $userJobID)->addSelect('job_type')->execute()->first();
$parserClass = NULL;
foreach (CRM_Core_BAO_UserJob::getTypes() as $userJobType) {
*/
protected $_lineCount;
+ /**
+ * Get information about the provided job.
+ * - name
+ * - id (generally the same as name)
+ * - label
+ *
+ * e.g. ['activity_import' => ['id' => 'activity_import', 'label' => ts('Activity Import'), 'name' => 'activity_import']]
+ *
+ * @return array
+ */
+ public static function getUserJobInfo(): array {
+ return [
+ 'membership_import' => [
+ 'id' => 'membership_import',
+ 'name' => 'membership_import',
+ 'label' => ts('Membership Import'),
+ ],
+ ];
+ }
+
/**
* @param string $name
* @param $title
static::scanFolders($classes, $civicrmRoot, 'CRM/*/WorkflowMessage', '_');
static::scanFolders($classes, $civicrmRoot, 'Civi/*/WorkflowMessage', '\\');
static::scanFolders($classes, $civicrmRoot, 'Civi/WorkflowMessage', '\\');
+ static::scanFolders($classes, $civicrmRoot, 'CRM/*/Import', '_');
if (\CRM_Utils_Constant::value('CIVICRM_UF') === 'UnitTests') {
static::scanFolders($classes, $civicrmRoot . 'tests/phpunit', 'CRM/*/WorkflowMessage', '_');
static::scanFolders($classes, $civicrmRoot . 'tests/phpunit', 'Civi/*/WorkflowMessage', '\\');
<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved. |
+ | |
+ | This work is published under the GNU AGPLv3 license with some |
+ | permitted exceptions and without any warranty. For full license |
+ | and copyright information, see https://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\UserJob;
+
+interface UserJobInterface {
+
+ /**
+ * Get information about the provided job.
+ * - name
+ * - id (generally the same as name)
+ * - label
+ *
+ * e.g. ['activity_import' => ['id' => 'activity_import', 'label' => ts('Activity Import'), 'name' => 'activity_import']]
+ *
+ * @return array
+ */
+ public static function getUserJobInfo(): array;
+
+ /**
+ * Run import.
+ *
+ * @param \CRM_Queue_TaskContext $taskContext
+ * @param int $userJobID
+ * The id in the civicrm_user_job table.
+ * @param int $limit
+ * A value of zero means no limit
+ * @param int $offset
+ *
+ * @return bool
+ */
+ public static function runJob(\CRM_Queue_TaskContext $taskContext, int $userJobID, int $limit, int $offset): bool;
+
+}
$this->assertEquals(0, $queue->numberOfItems());
$userJob = \Civi\Api4\UserJob::create(FALSE)->setValues([
- 'job_type:label' => 'Contact Import',
+ 'job_type:name' => 'contact_import',
'status_id:name' => 'in_progress',
'queue_id.name' => $queue->getName(),
])->execute()->single();