71fedb330d2956983a234b20eb46ac5ff27aba0f
[yaffs-website] / vendor / drush / drush / includes / batch.inc
1 <?php
2 /**
3  * @file
4  *    Drush batch API.
5  *
6  * This file contains a fork of the Drupal Batch API that has been drastically
7  * simplified and tailored to Drush's unique use case.
8  *
9  * The existing API is very targeted towards environments that are web accessible,
10  * and would frequently attempt to redirect the user which would result in the
11  * drush process being completely destroyed with no hope of recovery.
12  *
13  * While the original API does offer a 'non progressive' mode which simply
14  * calls each operation in sequence within the current process, in most
15  * implementations (D6), it would still attempt to redirect
16  * unless very specific conditions were met.
17  *
18  * When operating in 'non progressive' mode, Drush would experience the problems
19  * that the API was written to solve in the first place, specifically that processes
20  * would exceed the available memory and exit with an error.
21  *
22  * Each major release of Drupal has also had slightly different implementations
23  * of the batch API, and this provides a uniform interface to all of these
24  * implementations.
25  */
26
27 use Drush\Log\LogLevel;
28
29 /**
30  * Class extending ArrayObject to allow the batch API to perform logging when
31  * some keys of the array change.
32  *
33  * It is used to wrap batch's $context array and set log messages when values
34  * are assigned to keys 'message' or 'error_message'.
35  *
36  * @see _drush_batch_worker().
37  */
38 class DrushBatchContext extends ArrayObject {
39   function offsetSet($name, $value) {
40     if ($name == 'message') {
41       drush_log(strip_tags($value), LogLevel::OK);
42     }
43     elseif ($name == 'error_message') {
44       drush_set_error('DRUSH_BATCH_ERROR', strip_tags($value));
45     }
46     parent::offsetSet($name, $value);
47   }
48 }
49
50 /**
51  * Process a Drupal batch by spawning multiple Drush processes.
52  *
53  * This function will include the correct batch engine for the current
54  * major version of Drupal, and will make use of the drush_backend_invoke
55  * system to spawn multiple worker threads to handle the processing of
56  * the current batch, while keeping track of available memory.
57  *
58  * The batch system will process as many batch sets as possible until
59  * the entire batch has been completed or half of the available memory
60  * has been used.
61  *
62  * This function is a drop in replacement for the existing batch_process()
63  * function of Drupal.
64  *
65  * @param string $command
66  *   (optional) The command to call for the back end process. By default this will be
67  *   the 'batch-process' command, but some commands such as updatedb will
68  *   have special initialization requirements, and will need to define and
69  *   use their own command.
70  * @param array $args
71  *   (optional)
72  * @param array $options
73  *   (optional)
74  */
75 function drush_backend_batch_process($command = 'batch-process', $args = array(), $options = array()) {
76   // Command line options to pass to the command.
77   $options['u'] = drush_user_get_class()->getCurrentUserAsSingle()->id();
78
79   drush_include_engine('drupal', 'batch');
80   _drush_backend_batch_process($command, $args, $options);
81 }
82
83 /**
84  * Process sets from the specified batch.
85  *
86  * This function is called by the worker process that is spawned by the
87  * drush_backend_batch_process function.
88  *
89  * The command called needs to call this function after it's special bootstrap
90  * requirements have been taken care of.
91  *
92  * @param int $id
93  *   The batch ID of the batch being processed.
94  */
95 function drush_batch_command($id) {
96   include_once(DRUSH_DRUPAL_CORE . '/includes/batch.inc');
97   drush_include_engine('drupal', 'batch');
98   _drush_batch_command($id);
99 }