Merge pull request #15850 from civicrm/5.20
[civicrm-core.git] / api / v3 / Job.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
244bbdd8 13 * This api is used for working with scheduled "cron" jobs.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
6a488035 18/**
c1a920f1
EM
19 * Adjust metadata for "Create" action.
20 *
21 * The metadata is used for setting defaults, documentation & validation.
6a488035 22 *
cf470720 23 * @param array $params
b081365f 24 * Array of parameters determined by getfields.
6a488035
TO
25 */
26function _civicrm_api3_job_create_spec(&$params) {
27 $params['run_frequency']['api.required'] = 1;
28 $params['name']['api.required'] = 1;
29 $params['api_entity']['api.required'] = 1;
30 $params['api_action']['api.required'] = 1;
31
32 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
33 $params['is_active']['api.default'] = 1;
34}
35
36/**
c1a920f1 37 * Create scheduled job.
6a488035 38 *
cf470720
TO
39 * @param array $params
40 * Associative array of property name/value pairs to insert in new job.
6a488035 41 *
72b3a70c 42 * @return array
6a488035
TO
43 */
44function civicrm_api3_job_create($params) {
a25b46e9 45 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Job');
6a488035
TO
46}
47
7a6059c2
MW
48/**
49 * Adjust metadata for clone spec action.
50 *
51 * @param array $spec
52 */
53function _civicrm_api3_job_clone_spec(&$spec) {
54 $spec['id']['title'] = 'Job ID to clone';
55 $spec['id']['type'] = CRM_Utils_Type::T_INT;
56 $spec['id']['api.required'] = 1;
57 $spec['is_active']['title'] = 'Job is Active?';
58 $spec['is_active']['type'] = CRM_Utils_Type::T_BOOLEAN;
59 $spec['is_active']['api.required'] = 0;
60}
61
62/**
63 * Clone Job.
64 *
65 * @param array $params
66 *
67 * @return array
68 * @throws \API_Exception
69 * @throws \CiviCRM_API3_Exception
70 */
71function civicrm_api3_job_clone($params) {
72 if (empty($params['id'])) {
73 throw new API_Exception("Mandatory key(s) missing from params array: id field is required");
74 }
75 $id = $params['id'];
76 unset($params['id']);
77 $params['last_run'] = 'null';
78 $params['scheduled_run_date'] = 'null';
79 $newJobDAO = CRM_Core_BAO_Job::copy($id, $params);
cf8f0fff 80 return civicrm_api3('Job', 'get', ['id' => $newJobDAO->id]);
7a6059c2
MW
81}
82
6a488035 83/**
211e2fca 84 * Retrieve one or more job.
971d41b1
CW
85 *
86 * @param array $params
87 * input parameters
971d41b1 88 *
211e2fca 89 * @return array
6a488035
TO
90 */
91function civicrm_api3_job_get($params) {
92 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
93}
94
95/**
211e2fca 96 * Delete a job.
6a488035 97 *
9657ccf2 98 * @param array $params
6a488035
TO
99 */
100function civicrm_api3_job_delete($params) {
61ef23bd 101 _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
102}
103
104/**
211e2fca
EM
105 * Dumb wrapper to execute scheduled jobs.
106 *
107 * Always creates success - errors and results are handled in the job log.
6a488035 108 *
cf470720 109 * @param array $params
971d41b1 110 * input parameters (unused).
6a488035 111 *
a6c01b45 112 * @return array
72b3a70c 113 * API Result Array
6a488035 114 */
608e6658 115function civicrm_api3_job_execute($params) {
608e6658 116
6a488035
TO
117 $facility = new CRM_Core_JobManager();
118 $facility->execute(FALSE);
119
61fe4988 120 // Always creates success - results are handled elsewhere.
244bbdd8 121 return civicrm_api3_create_success(1, $params, 'Job');
6a488035
TO
122}
123
124/**
211e2fca 125 * Adjust Metadata for Execute action.
6a488035 126 *
cf470720 127 * @param array $params
b081365f 128 * Array of parameters determined by getfields.
6a488035
TO
129 */
130function _civicrm_api3_job_execute_spec(&$params) {
131}
132
133/**
211e2fca 134 * Geocode group of contacts based on given params.
6a488035 135 *
cf470720 136 * @param array $params
971d41b1 137 * input parameters.
6a488035 138 *
a6c01b45 139 * @return array
72b3a70c 140 * API Result Array
6a488035
TO
141 */
142function civicrm_api3_job_geocode($params) {
6a488035
TO
143 $gc = new CRM_Utils_Address_BatchUpdate($params);
144
6a488035
TO
145 $result = $gc->run();
146
147 if ($result['is_error'] == 0) {
148 return civicrm_api3_create_success($result['messages']);
149 }
150 else {
151 return civicrm_api3_create_error($result['messages']);
152 }
153}
9657ccf2 154
6a488035 155/**
c1a920f1 156 * First check on Code documentation.
9657ccf2
EM
157 *
158 * @param array $params
6a488035
TO
159 */
160function _civicrm_api3_job_geocode_spec(&$params) {
cf8f0fff 161 $params['start'] = [
8ebf8a02
DG
162 'title' => 'Starting Contact ID',
163 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
164 ];
165 $params['end'] = [
8ebf8a02
DG
166 'title' => 'Ending Contact ID',
167 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
168 ];
169 $params['geocoding'] = [
d142432b
EM
170 'title' => 'Geocode address?',
171 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff
CW
172 ];
173 $params['parse'] = [
d142432b
EM
174 'title' => 'Parse street address?',
175 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff
CW
176 ];
177 $params['throttle'] = [
b2ed8e73 178 'title' => 'Throttle?',
d142432b
EM
179 'description' => 'If enabled, geo-codes at a slow rate',
180 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff 181 ];
6a488035
TO
182}
183
184/**
c1a920f1 185 * Send the scheduled reminders for all contacts (either for activities or events).
6a488035 186 *
cf470720
TO
187 * @param array $params
188 * (reference ) input parameters.
6a488035
TO
189 * now - the time to use, in YmdHis format
190 * - makes testing a bit simpler since we can simulate past/future time
191 *
c23f45d3 192 * @return array
6a488035
TO
193 */
194function civicrm_api3_job_send_reminder($params) {
e27a3472
EM
195 //note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
196 //It's not clear whether than syntax can be passed in via the UI config - but this keeps the pre 4.4.4 behaviour
c23f45d3 197 // in that case (ie. makes it non-configurable via the UI). Another approach would be to set a default of 0
e27a3472
EM
198 // in the _spec function - but since that is a deprecated value it seems more contentious than this approach
199 $params['rowCount'] = 0;
83617886 200 $lock = Civi::lockManager()->acquire('worker.core.ActionSchedule');
39fefc30 201 if (!$lock->isAcquired()) {
33c5988b 202 return civicrm_api3_create_error('Could not acquire lock, another ActionSchedule process is running');
39fefc30
KJ
203 }
204
73f3e293 205 $result = CRM_Core_BAO_ActionSchedule::processQueue(CRM_Utils_Array::value('now', $params), $params);
39fefc30 206 $lock->release();
6a488035
TO
207
208 if ($result['is_error'] == 0) {
209 return civicrm_api3_create_success();
210 }
211 else {
212 return civicrm_api3_create_error($result['messages']);
213 }
214}
7c31ae57 215
73f3e293 216/**
c1a920f1
EM
217 * Adjust metadata for "send_reminder" action.
218 *
219 * The metadata is used for setting defaults, documentation & validation.
73f3e293 220 *
cf470720 221 * @param array $params
b081365f 222 * Array of parameters determined by getfields.
73f3e293
E
223 */
224function _civicrm_api3_job_send_reminder(&$params) {
225 //@todo this function will now take all fields in action_schedule as params
226 // as it is calling the api fn to set the filters - update getfields to reflect
cf8f0fff 227 $params['id'] = [
73f3e293 228 'type' => CRM_Utils_Type::T_INT,
21dfd5f5 229 'title' => 'Action Schedule ID',
cf8f0fff 230 ];
73f3e293 231}
7c31ae57 232
6a488035 233/**
c1a920f1 234 * Execute a specific report instance and send the output via email.
6a488035 235 *
cf470720
TO
236 * @param array $params
237 * (reference ) input parameters.
6a488035
TO
238 * sendmail - Boolean - should email be sent?, required
239 * instanceId - Integer - the report instance ID
240 * resetVal - Integer - should we reset form state (always true)?
241 *
c23f45d3 242 * @return array
6a488035
TO
243 */
244function civicrm_api3_job_mail_report($params) {
6a488035
TO
245 $result = CRM_Report_Utils_Report::processReport($params);
246
247 if ($result['is_error'] == 0) {
248 // this should be handling by throwing exceptions but can't remove until we can test that.
249 return civicrm_api3_create_success();
250 }
251 else {
252 return civicrm_api3_create_error($result['messages']);
253 }
254}
255
256/**
6a488035 257 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
211e2fca 258 *
6a488035
TO
259 * IMPORTANT: You must first create valid option value before using via admin interface.
260 * Check option lists for Email Greetings, Postal Greetings and Addressee
261 *
211e2fca 262 * @todo - is this here by mistake or should it be added to _spec function :id - Integer - greetings option group.
6a488035 263 *
9657ccf2 264 * @param array $params
77b97be7 265 *
3f83a242 266 * @return array
6a488035
TO
267 */
268function civicrm_api3_job_update_greeting($params) {
6a488035 269 if (isset($params['ct']) && isset($params['gt'])) {
6a488035
TO
270 $ct = explode(',', $params['ct']);
271 $gt = explode(',', $params['gt']);
272 foreach ($ct as $ctKey => $ctValue) {
273 foreach ($gt as $gtKey => $gtValue) {
274 $params['ct'] = trim($ctValue);
275 $params['gt'] = trim($gtValue);
3f83a242 276 CRM_Contact_BAO_Contact_Utils::updateGreeting($params);
6a488035
TO
277 }
278 }
279 }
280 else {
3f83a242 281 CRM_Contact_BAO_Contact_Utils::updateGreeting($params);
6a488035 282 }
3f83a242 283 return civicrm_api3_create_success();
6a488035
TO
284}
285
286/**
211e2fca 287 * Adjust Metadata for Get action.
72b3a70c 288 *
c1a920f1
EM
289 * The metadata is used for setting defaults, documentation & validation.
290 *
72b3a70c 291 * @param array $params
b081365f 292 * Array of parameters determined by getfields.
72b3a70c 293 */
6a488035 294function _civicrm_api3_job_update_greeting_spec(&$params) {
cf8f0fff 295 $params['ct'] = [
6a488035
TO
296 'api.required' => 1,
297 'title' => 'Contact Type',
298 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff
CW
299 ];
300 $params['gt'] = [
971d41b1
CW
301 'api.required' => 1,
302 'title' => 'Greeting Type',
303 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff 304 ];
6a488035
TO
305}
306
307/**
c1a920f1 308 * Mass update pledge statuses.
6a488035 309 *
cf470720 310 * @param array $params
c1a920f1 311 *
c23f45d3 312 * @return array
6a488035
TO
313 */
314function civicrm_api3_job_process_pledge($params) {
315 // *** Uncomment the next line if you want automated reminders to be sent
316 // $params['send_reminders'] = true;
317 $result = CRM_Pledge_BAO_Pledge::updatePledgeStatus($params);
318
319 if ($result['is_error'] == 0) {
320 // experiment: detailed execution log is a result here
321 return civicrm_api3_create_success($result['messages']);
322 }
323 else {
324 return civicrm_api3_create_error($result['error_message']);
325 }
326}
327
328/**
c1a920f1 329 * Process mail queue.
6a488035
TO
330 *
331 * @param array $params
332 *
333 * @return array
334 */
335function civicrm_api3_job_process_mailing($params) {
ec782609 336 $mailsProcessedOrig = CRM_Mailing_BAO_MailingJob::$mailsProcessed;
6a488035 337
03c5ceba 338 try {
339 CRM_Core_BAO_Setting::isAPIJobAllowedToRun($params);
340 }
341 catch (Exception $e) {
342 return civicrm_api3_create_error($e->getMessage());
343 }
344
6a488035
TO
345 if (!CRM_Mailing_BAO_Mailing::processQueue()) {
346 return civicrm_api3_create_error('Process Queue failed');
347 }
348 else {
cf8f0fff 349 $values = [
ec782609 350 'processed' => CRM_Mailing_BAO_MailingJob::$mailsProcessed - $mailsProcessedOrig,
cf8f0fff 351 ];
244bbdd8 352 return civicrm_api3_create_success($values, $params, 'Job', 'process_mailing');
6a488035
TO
353 }
354}
355
356/**
cd5823ae 357 * Process sms queue.
6a488035
TO
358 *
359 * @param array $params
360 *
361 * @return array
362 */
363function civicrm_api3_job_process_sms($params) {
ec782609
TO
364 $mailsProcessedOrig = CRM_Mailing_BAO_MailingJob::$mailsProcessed;
365
6a488035
TO
366 if (!CRM_Mailing_BAO_Mailing::processQueue('sms')) {
367 return civicrm_api3_create_error('Process Queue failed');
368 }
369 else {
cf8f0fff 370 $values = [
ec782609 371 'processed' => CRM_Mailing_BAO_MailingJob::$mailsProcessed - $mailsProcessedOrig,
cf8f0fff 372 ];
244bbdd8 373 return civicrm_api3_create_success($values, $params, 'Job', 'process_sms');
6a488035
TO
374 }
375}
39fefc30 376
6a488035 377/**
c1a920f1 378 * Job to get mail responses from civiMailing.
9657ccf2
EM
379 *
380 * @param array $params
381 *
382 * @return array
6a488035
TO
383 */
384function civicrm_api3_job_fetch_bounces($params) {
83617886 385 $lock = Civi::lockManager()->acquire('worker.mailing.EmailProcessor');
6a488035
TO
386 if (!$lock->isAcquired()) {
387 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
388 }
3ff238ae 389 CRM_Utils_Mail_EmailProcessor::processBounces($params['is_create_activities']);
6a488035 390 $lock->release();
39fefc30 391
3ff238ae 392 return civicrm_api3_create_success(1, $params, 'Job', 'fetch_bounces');
393}
394
395/**
396 * Metadata for bounce function.
397 *
398 * @param array $params
399 */
400function _civicrm_api3_job_fetch_bounces_spec(&$params) {
cf8f0fff 401 $params['is_create_activities'] = [
3ff238ae 402 'api.default' => 0,
403 'type' => CRM_Utils_Type::T_BOOLEAN,
404 'title' => ts('Create activities for replies?'),
cf8f0fff 405 ];
6a488035
TO
406}
407
408/**
35823763 409 * Job to get mail and create activities.
9657ccf2
EM
410 *
411 * @param array $params
412 *
413 * @return array
6a488035
TO
414 */
415function civicrm_api3_job_fetch_activities($params) {
83617886 416 $lock = Civi::lockManager()->acquire('worker.mailing.EmailProcessor');
6a488035
TO
417 if (!$lock->isAcquired()) {
418 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
419 }
39fefc30
KJ
420
421 try {
422 CRM_Utils_Mail_EmailProcessor::processActivities();
cf8f0fff 423 $values = [];
6a488035 424 $lock->release();
244bbdd8 425 return civicrm_api3_create_success($values, $params, 'Job', 'fetch_activities');
0db6c3e1
TO
426 }
427 catch (Exception $e) {
6a488035 428 $lock->release();
e7b952ac 429 return civicrm_api3_create_error($e->getMessage());
6a488035
TO
430 }
431}
432
433/**
35823763 434 * Process participant statuses.
6a488035 435 *
cf470720 436 * @param array $params
35823763 437 * Input parameters.
6a488035 438 *
a6c01b45 439 * @return array
72b3a70c 440 * array of properties, if error an array with an error id and error message
6a488035
TO
441 */
442function civicrm_api3_job_process_participant($params) {
6a488035
TO
443 $result = CRM_Event_BAO_ParticipantStatusType::process($params);
444
445 if (!$result['is_error']) {
446 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
447 }
448 else {
449 return civicrm_api3_create_error('Error while processing participant statuses');
450 }
451}
452
6a488035 453/**
61fe4988
EM
454 * This api checks and updates the status of all membership records for a given domain.
455 *
456 * The function uses the calc_membership_status and update_contact_membership APIs.
6a488035
TO
457 *
458 * IMPORTANT:
459 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
460 *
cf470720
TO
461 * @param array $params
462 * Input parameters NOT USED.
6a488035 463 *
971d41b1 464 * @return bool
72b3a70c 465 * true if success, else false
6a488035 466 */
608e6658 467function civicrm_api3_job_process_membership($params) {
83617886 468 $lock = Civi::lockManager()->acquire('worker.member.UpdateMembership');
39fefc30 469 if (!$lock->isAcquired()) {
017877e1 470 return civicrm_api3_create_error('Could not acquire lock, another Membership Processing process is running');
39fefc30
KJ
471 }
472
6a488035 473 $result = CRM_Member_BAO_Membership::updateAllMembershipStatus();
39fefc30 474 $lock->release();
6a488035
TO
475
476 if ($result['is_error'] == 0) {
244bbdd8 477 return civicrm_api3_create_success($result['messages'], $params, 'Job', 'process_membership');
6a488035
TO
478 }
479 else {
480 return civicrm_api3_create_error($result['messages']);
481 }
482}
483
484/**
3f83a242 485 * This api checks and updates the status of all survey respondents.
6a488035 486 *
cf470720
TO
487 * @param array $params
488 * (reference ) input parameters.
6a488035 489 *
971d41b1 490 * @return bool
72b3a70c 491 * true if success, else false
6a488035
TO
492 */
493function civicrm_api3_job_process_respondent($params) {
6a488035
TO
494 $result = CRM_Campaign_BAO_Survey::releaseRespondent($params);
495
496 if ($result['is_error'] == 0) {
497 return civicrm_api3_create_success();
498 }
499 else {
500 return civicrm_api3_create_error($result['messages']);
501 }
502}
503
504/**
505 * Merges given pair of duplicate contacts.
506 *
cf470720
TO
507 * @param array $params
508 * Input parameters.
6a488035 509 *
a6c01b45 510 * @return array
72b3a70c 511 * API Result Array
76ddbc8e 512 * @throws \CiviCRM_API3_Exception
6a488035
TO
513 */
514function civicrm_api3_job_process_batch_merge($params) {
6c866f0c 515 $rule_group_id = CRM_Utils_Array::value('rule_group_id', $params);
76ddbc8e 516 if (!$rule_group_id) {
cf8f0fff 517 $rule_group_id = civicrm_api3('RuleGroup', 'getvalue', [
76ddbc8e 518 'contact_type' => 'Individual',
519 'used' => 'Unsupervised',
520 'return' => 'id',
cf8f0fff
CW
521 'options' => ['limit' => 1],
522 ]);
76ddbc8e 523 }
f008885c 524 $rgid = CRM_Utils_Array::value('rgid', $params);
6a488035 525 $gid = CRM_Utils_Array::value('gid', $params);
6a488035 526 $mode = CRM_Utils_Array::value('mode', $params, 'safe');
6a488035 527
997a03fe 528 $result = CRM_Dedupe_Merger::batchMerge($rule_group_id, $gid, $mode, 1, 2, CRM_Utils_Array::value('criteria', $params, []), CRM_Utils_Array::value('check_permissions', $params), NULL, $params['search_limit']);
6a488035 529
76ddbc8e 530 return civicrm_api3_create_success($result, $params);
6a488035
TO
531}
532
3f83a242 533/**
61fe4988
EM
534 * Metadata for batch merge function.
535 *
3f83a242
EM
536 * @param $params
537 */
538function _civicrm_api3_job_process_batch_merge_spec(&$params) {
cf8f0fff 539 $params['rule_group_id'] = [
76ddbc8e 540 'title' => 'Dedupe rule group id, defaults to Contact Unsupervised rule',
3f83a242 541 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
542 'api.aliases' => ['rgid'],
543 ];
544 $params['gid'] = [
3f83a242
EM
545 'title' => 'group id',
546 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
547 ];
548 $params['mode'] = [
3f83a242 549 'title' => 'Mode',
608e6658 550 'description' => 'helps decide how to behave when there are conflicts. A \'safe\' value skips the merge if there are no conflicts. Does a force merge otherwise.',
3f83a242 551 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff
CW
552 ];
553 $params['auto_flip'] = [
3f83a242
EM
554 'title' => 'Auto Flip',
555 'description' => 'let the api decide which contact to retain and which to delete?',
556 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff 557 ];
997a03fe 558 $params['search_limit'] = [
559 'title' => ts('Number of contacts to look for matches for.'),
560 'type' => CRM_Utils_Type::T_INT,
561 'api.default' => (int) Civi::settings()->get('dedupe_default_limit'),
562 ];
563
3f83a242
EM
564}
565
6a488035 566/**
61fe4988 567 * Runs handlePaymentCron method in the specified payment processor.
6a488035 568 *
cf470720
TO
569 * @param array $params
570 * Input parameters.
6a488035 571 *
61fe4988
EM
572 * Expected @params array keys are: INCORRECTLY DOCUMENTED AND SHOULD BE IN THE _spec function
573 * for retrieval via getfields.
6a488035 574 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
6a488035
TO
575 */
576function civicrm_api3_job_run_payment_cron($params) {
577
6a488035
TO
578 // live mode
579 CRM_Core_Payment::handlePaymentMethod(
580 'PaymentCron',
581 array_merge(
582 $params,
cf8f0fff 583 [
6a488035 584 'caller' => 'api',
cf8f0fff 585 ]
6a488035
TO
586 )
587 );
588
589 // test mode
590 CRM_Core_Payment::handlePaymentMethod(
591 'PaymentCron',
592 array_merge(
593 $params,
cf8f0fff 594 [
6a488035 595 'mode' => 'test',
cf8f0fff 596 ]
6a488035
TO
597 )
598 );
599}
600
601/**
61fe4988
EM
602 * This api cleans up all the old session entries and temp tables.
603 *
604 * We recommend that sites run this on an hourly basis.
6a488035 605 *
cf470720 606 * @param array $params
971d41b1 607 * Sends in various config parameters to decide what needs to be cleaned.
6a488035 608 */
481a74f4 609function civicrm_api3_job_cleanup($params) {
971d41b1
CW
610 $session = CRM_Utils_Array::value('session', $params, TRUE);
611 $tempTable = CRM_Utils_Array::value('tempTables', $params, TRUE);
612 $jobLog = CRM_Utils_Array::value('jobLog', $params, TRUE);
fd33fead 613 $expired = CRM_Utils_Array::value('expiredDbCache', $params, TRUE);
971d41b1
CW
614 $prevNext = CRM_Utils_Array::value('prevNext', $params, TRUE);
615 $dbCache = CRM_Utils_Array::value('dbCache', $params, FALSE);
616 $memCache = CRM_Utils_Array::value('memCache', $params, FALSE);
9db8dfb7 617 $tplCache = CRM_Utils_Array::value('tplCache', $params, FALSE);
618 $wordRplc = CRM_Utils_Array::value('wordRplc', $params, FALSE);
6a488035 619
fd33fead
TO
620 if ($session || $tempTable || $prevNext || $expired) {
621 CRM_Core_BAO_Cache::cleanup($session, $tempTable, $prevNext, $expired);
6a488035
TO
622 }
623
481a74f4
TO
624 if ($jobLog) {
625 CRM_Core_BAO_Job::cleanup();
6a488035
TO
626 }
627
9db8dfb7 628 if ($tplCache) {
4dd6dceb 629 $config = CRM_Core_Config::singleton();
630 $config->cleanup(1, FALSE);
631 }
632
481a74f4
TO
633 if ($dbCache) {
634 CRM_Core_Config::clearDBCache();
6a488035
TO
635 }
636
481a74f4
TO
637 if ($memCache) {
638 CRM_Utils_System::flushCache();
6a488035 639 }
4dd6dceb 640
9db8dfb7 641 if ($wordRplc) {
4dd6dceb 642 CRM_Core_BAO_WordReplacement::rebuild();
643 }
6a488035
TO
644}
645
646/**
647 * Set expired relationships to disabled.
61fe4988
EM
648 *
649 * @param array $params
650 *
9657ccf2 651 * @return array
3f83a242 652 * @throws \API_Exception
6a488035 653 */
608e6658 654function civicrm_api3_job_disable_expired_relationships($params) {
6a488035 655 $result = CRM_Contact_BAO_Relationship::disableExpiredRelationships();
3f83a242
EM
656 if (!$result) {
657 throw new API_Exception('Failed to disable all expired relationships.');
6a488035 658 }
244bbdd8 659 return civicrm_api3_create_success(1, $params, 'Job', 'disable_expired_relationships');
6a488035
TO
660}
661
662/**
61fe4988
EM
663 * This api reloads all the smart groups.
664 *
665 * If the org has a large number of smart groups it is recommended that they use the limit clause
666 * to limit the number of smart groups evaluated on a per job basis.
667 *
668 * Might also help to increase the smartGroupCacheTimeout and use the cache.
9657ccf2
EM
669 *
670 * @param array $params
61fe4988 671 *
9657ccf2 672 * @return array
3f83a242 673 * @throws \API_Exception
6a488035 674 */
9657ccf2 675function civicrm_api3_job_group_rebuild($params) {
83617886 676 $lock = Civi::lockManager()->acquire('worker.core.GroupRebuild');
39fefc30 677 if (!$lock->isAcquired()) {
0feefbc7 678 throw new API_Exception('Could not acquire lock, another GroupRebuild process is running');
39fefc30
KJ
679 }
680
481a74f4 681 $limit = CRM_Utils_Array::value('limit', $params, 0);
6a488035 682
971d41b1 683 CRM_Contact_BAO_GroupContactCache::loadAll(NULL, $limit);
39fefc30
KJ
684 $lock->release();
685
f9e16e9a 686 return civicrm_api3_create_success();
6a488035 687}
999128a9 688
a357281f 689/**
690 * Flush smart groups caches.
691 *
692 * This job purges aged smart group cache data (based on the timeout value). Sites can decide whether they want this
693 * job and / or the group cache rebuild job to run. In some cases performance is better when old caches are cleared out
694 * prior to any attempt to rebuild them. Also, many sites are very happy to have caches built on demand, provided the
695 * user is not having to wait for deadlocks to clear when invalidating them.
696 *
697 * @param array $params
698 *
699 * @return array
700 * @throws \API_Exception
701 */
702function civicrm_api3_job_group_cache_flush($params) {
2b68a50c 703 CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush();
a357281f 704 return civicrm_api3_create_success();
705}
706
999128a9
CW
707/**
708 * Check for CiviCRM software updates.
709 *
710 * Anonymous site statistics are sent back to civicrm.org during this check.
711 */
712function civicrm_api3_job_version_check() {
713 $vc = new CRM_Utils_VersionCheck();
714 $vc->fetch();
e9e60a8c 715 return civicrm_api3_create_success();
999128a9 716}