| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Util; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\ImporterManager; |
| 6 |
use ImportWP\Common\Importer\State\ImporterState; |
| 7 |
use ImportWP\Container; |
| 8 |
|
| 9 |
class Util |
| 10 |
{ |
| 11 |
function set_time_limit() |
| 12 |
{ |
| 13 |
if (function_exists('set_time_limit')) { |
| 14 |
@\set_time_limit(0); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
function set_time_limit_available() |
| 19 |
{ |
| 20 |
if (!function_exists('set_time_limit') || !function_exists('ini_get')) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
$tmp_max_execution_time = (ini_get('max_execution_time') == 30) ? 31 : 30; |
| 25 |
@set_time_limit($tmp_max_execution_time); |
| 26 |
|
| 27 |
return $tmp_max_execution_time == ini_get('max_execution_time'); |
| 28 |
} |
| 29 |
|
| 30 |
function format_time($seconds) |
| 31 |
{ |
| 32 |
$t = round($seconds); |
| 33 |
|
| 34 |
$hours = ($t / 3600); |
| 35 |
$minutes = (floor($t / 60) % 60); |
| 36 |
$seconds = $t % 60; |
| 37 |
|
| 38 |
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get Importer log file path based on specified session id. |
| 43 |
* |
| 44 |
* @param int $id Importer Id |
| 45 |
* @param string $session Importer session id |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public static function get_importer_log_file_path($id, $session) |
| 49 |
{ |
| 50 |
/** |
| 51 |
* @var ImporterManager $importer_manager |
| 52 |
*/ |
| 53 |
$importer_manager = Container::getInstance()->get('importer_manager'); |
| 54 |
return $importer_manager->get_config_path($id, false, $session) . '.logs-' . $session; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get importer status file path |
| 59 |
* |
| 60 |
* @param int $id Importer Id |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public static function get_importer_status_file_path($id) |
| 64 |
{ |
| 65 |
/** |
| 66 |
* @var ImporterManager $importer_manager |
| 67 |
*/ |
| 68 |
$importer_manager = Container::getInstance()->get('importer_manager'); |
| 69 |
return $importer_manager->get_config_path($id) . '.status'; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Write importer status log file message |
| 74 |
* |
| 75 |
* @param int $id Importer id |
| 76 |
* @param string $session Importer session |
| 77 |
* @param string $message Message to log |
| 78 |
* @param string $type |
| 79 |
* @param boolean $counter |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public static function write_status_log_file_message($id, $session, $message, $type = 'S', $counter = false) |
| 83 |
{ |
| 84 |
$file_path = self::get_importer_log_file_path($id, $session); |
| 85 |
$fh = fopen($file_path, 'a'); |
| 86 |
|
| 87 |
if (!is_writable($file_path)) { |
| 88 |
throw new \Exception(sprintf(__("Importer status file is not writable: %s.", 'jc-importer'), $file_path)); |
| 89 |
} |
| 90 |
|
| 91 |
if (false === $fh) { |
| 92 |
throw new \Exception(sprintf(__("Unable to open Importer status file: %s.", 'jc-importer'), $file_path)); |
| 93 |
} |
| 94 |
|
| 95 |
fputcsv($fh, [$counter, $type, $message]); |
| 96 |
fclose($fh); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Write importer status to file |
| 101 |
* |
| 102 |
* @param int $id |
| 103 |
* @param ImporterState $state |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public static function write_status_session_to_file($id, $state) |
| 107 |
{ |
| 108 |
$session = $state->get_session(); |
| 109 |
$session_data = self::get_last_session_from_status($id, $session); |
| 110 |
if ($session_data) { |
| 111 |
$session_data['data'] = $state->get_raw(); |
| 112 |
self::update_status_session_file($id, $session_data); |
| 113 |
} else { |
| 114 |
self::update_status_session_file($id, [ |
| 115 |
'start' => '-1', |
| 116 |
'length' => 0, |
| 117 |
'data' => $state->get_raw() |
| 118 |
]); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public static function get_last_session_from_status($id, $session) |
| 123 |
{ |
| 124 |
|
| 125 |
$file = self::get_importer_status_file_path($id); |
| 126 |
if (!file_exists($file)) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
$line = ''; |
| 131 |
|
| 132 |
$f = fopen($file, 'r'); |
| 133 |
$cursor = -1; |
| 134 |
|
| 135 |
fseek($f, $cursor, SEEK_END); |
| 136 |
$char = fgetc($f); |
| 137 |
|
| 138 |
/** |
| 139 |
* Trim trailing newline chars of the file |
| 140 |
*/ |
| 141 |
while ($char === "\n" || $char === "\r") { |
| 142 |
fseek($f, $cursor--, SEEK_END); |
| 143 |
$char = fgetc($f); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Read until the start of file or first newline char |
| 148 |
*/ |
| 149 |
while ($char !== false && $char !== "\n" && $char !== "\r") { |
| 150 |
/** |
| 151 |
* Prepend the new char |
| 152 |
*/ |
| 153 |
$line = $char . $line; |
| 154 |
fseek($f, $cursor--, SEEK_END); |
| 155 |
$char = fgetc($f); |
| 156 |
} |
| 157 |
|
| 158 |
$json_data = json_decode($line, true); |
| 159 |
|
| 160 |
$ftell = ftell($f); |
| 161 |
$result = isset($json_data['id']) && $json_data['id'] === $session ? ['start' => $ftell > 1 ? $ftell : 0, 'length' => strlen($line), 'data' => $json_data] : false; |
| 162 |
|
| 163 |
fclose($f); |
| 164 |
return $result; |
| 165 |
} |
| 166 |
|
| 167 |
public static function update_status_session_file($id, $data) |
| 168 |
{ |
| 169 |
$file = self::get_importer_status_file_path($id); |
| 170 |
|
| 171 |
if (false === file_exists($file) && false === file_put_contents($file, '')) { |
| 172 |
throw new \Exception(sprintf(__("Unable to create status file: %s", 'jc-importer'), $file)); |
| 173 |
} |
| 174 |
|
| 175 |
if (!is_writable($file)) { |
| 176 |
throw new \Exception(sprintf(__("Unable to write status file: %s", 'jc-importer'), $file)); |
| 177 |
} |
| 178 |
|
| 179 |
$f = fopen($file, 'r+'); |
| 180 |
|
| 181 |
$session_data = json_encode($data['data']); |
| 182 |
|
| 183 |
if ($data['start'] > -1) { |
| 184 |
|
| 185 |
// get end of file after existing status |
| 186 |
fseek($f, $data['start'] + $data['length']); |
| 187 |
$end_content = fgets($f); |
| 188 |
|
| 189 |
if (empty($end_content)) { |
| 190 |
$end_content = "\n"; |
| 191 |
} |
| 192 |
|
| 193 |
fseek($f, $data['start'], SEEK_SET); |
| 194 |
fputs($f, $session_data . $end_content); |
| 195 |
} else { |
| 196 |
fseek($f, 0, SEEK_END); |
| 197 |
fputs($f, $session_data . "\n"); |
| 198 |
} |
| 199 |
|
| 200 |
// end file here |
| 201 |
ftruncate($f, ftell($f)); |
| 202 |
|
| 203 |
fclose($f); |
| 204 |
} |
| 205 |
} |
| 206 |
|