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