1 | <?php |
||
18 | class DatetimeOffsetFix extends JobHandler |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Key for the option used to track which models have been processed when doing the batches. |
||
23 | */ |
||
24 | const MODELS_TO_PROCESS_OPTION_KEY = 'ee_models_processed_for_datetime_offset_fix'; |
||
25 | |||
26 | |||
27 | const COUNT_OF_MODELS_PROCESSED = 'ee_count_of_ee_models_processed_for_datetime_offset_fixed'; |
||
28 | |||
29 | /** |
||
30 | * Key for the option used to track what the current offset is that will be applied when this tool is executed. |
||
31 | */ |
||
32 | const OFFSET_TO_APPLY_OPTION_KEY = 'ee_datetime_offset_fix_offset_to_apply'; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * String labelling the datetime offset fix type for change-log entries. |
||
37 | */ |
||
38 | const DATETIME_OFFSET_FIX_CHANGELOG_TYPE = 'datetime_offset_fix'; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * String labelling a datetime offset fix error for change-log entries. |
||
43 | */ |
||
44 | const DATETIME_OFFSET_FIX_CHANGELOG_ERROR_TYPE = 'datetime_offset_fix_error'; |
||
45 | |||
46 | /** |
||
47 | * @var EEM_Base[] |
||
48 | */ |
||
49 | protected $models_with_datetime_fields = array(); |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Performs any necessary setup for starting the job. This is also a good |
||
54 | * place to setup the $job_arguments which will be used for subsequent HTTP requests |
||
55 | * when continue_job will be called |
||
56 | * |
||
57 | * @param JobParameters $job_parameters |
||
58 | * @throws BatchRequestException |
||
59 | * @return JobStepResponse |
||
60 | */ |
||
61 | public function create_job(JobParameters $job_parameters) |
||
71 | |||
72 | /** |
||
73 | * Performs another step of the job |
||
74 | * |
||
75 | * @param JobParameters $job_parameters |
||
76 | * @param int $batch_size |
||
77 | * @return JobStepResponse |
||
78 | * @throws \EE_Error |
||
79 | */ |
||
80 | public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
||
103 | |||
104 | /** |
||
105 | * Performs any clean-up logic when we know the job is completed |
||
106 | * |
||
107 | * @param JobParameters $job_parameters |
||
108 | * @return JobStepResponse |
||
109 | * @throws BatchRequestException |
||
110 | */ |
||
111 | public function cleanup_job(JobParameters $job_parameters) |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Contains the logic for processing a model and applying the datetime offset to affected fields on that model. |
||
125 | * @param string $model_class_name |
||
126 | * @throws \EE_Error |
||
127 | */ |
||
128 | protected function processModel($model_class_name) |
||
171 | |||
172 | |||
173 | /** |
||
174 | * Records a changelog entry using the given information. |
||
175 | * |
||
176 | * @param EEM_Base $model |
||
177 | * @param float $offset |
||
178 | * @param EE_Table_Base $table |
||
179 | * @param EE_Model_Field_Base[] $model_fields_affected |
||
180 | * @param string $error_message If present then there was an error so let's record that instead. |
||
181 | * @throws \EE_Error |
||
182 | */ |
||
183 | private function recordChangeLog( |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Returns an array of models that have datetime fields. |
||
235 | * This array is added to a short lived transient cache to keep having to build this list to a minimum. |
||
236 | * @return array an array of model class names. |
||
237 | */ |
||
238 | private function getModelsWithDatetimeFields() |
||
257 | |||
258 | |||
259 | /** |
||
260 | * This simply records the models that have been processed with our tracking option. |
||
261 | * @param array $models_to_set array of model class names. |
||
262 | */ |
||
263 | private function setModelsToProcess($models_to_set) |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Used to keep track of how many models have been processed for the batch |
||
271 | * @param $count |
||
272 | */ |
||
273 | private function updateCountOfModelsProcessed($count = 1) |
||
278 | |||
279 | |||
280 | /** |
||
281 | * Retrieve the tracked number of models processed between requests. |
||
282 | * @return int |
||
283 | */ |
||
284 | private function getCountOfModelsProcessed() |
||
288 | |||
289 | |||
290 | /** |
||
291 | * Returns the models that are left to process. |
||
292 | * @return array an array of model class names. |
||
293 | */ |
||
294 | private function getModelsToProcess() |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Used to record the offset that will be applied to dates and times for EE_Datetime_Field columns. |
||
305 | * @param float $offset |
||
306 | */ |
||
307 | public static function updateOffset($offset) |
||
311 | |||
312 | |||
313 | /** |
||
314 | * Used to retrieve the saved offset that will be applied to dates and times for EE_Datetime_Field columns. |
||
315 | * |
||
316 | * @return float |
||
317 | */ |
||
318 | public static function getOffset() |
||
322 | } |
||
323 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.