Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pack.php
1<?php
3
8
9
13class Pack
16{
17 use Translate\Controller\Stepper;
18 use Translate\Controller\ProcessParams;
19
21 public static $documentRoot;
22
24 private $seekPath;
25
27 private $languageId;
28
30 private $packFile;
31
33 private $totalFileCount;
34
36 private $tmpFolderPath;
37
39 private $archiveFilePath;
40
42 private $archiveFileName;
43
45 private $archiveFile;
46
48 private $downloadParams;
49
57 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
58 {
59 $this->keepField([
60 'packFile', 'languageId', 'tmpFolderPath', 'archiveFilePath',
61 'archiveFileName', 'seekPath', 'totalFileCount', 'downloadParams',
62 ]);
63
64 parent::__construct($name, $controller, $config);
65
66 self::$documentRoot = \rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
67 }
68
75 public function run($runBefore = false)
76 {
77 if ($runBefore)
78 {
79 $this->onBeforeRun();
80 }
81
82 // continue previous process
83 $progressParams = $this->getProgressParameters();
84 $this->packFile = (bool)$progressParams['packFile'];
85 $this->languageId = $progressParams['languageId'];
86 $this->tmpFolderPath = $progressParams['tmpFolderPath'];
87
88 $this->totalFileCount = (int)$progressParams['totalFileCount'];
89
90 if ($this->isNewProcess)
91 {
92 $this->totalItems = $this->totalFileCount;
93 $this->processedItems = 0;
94 $this->archiveFileName = $this->generateExportFileName();
95
96 $exportFolder = Translate\Config::getExportFolder();
97 if (!empty($exportFolder))
98 {
99 $tempDir = new Translate\IO\Directory($exportFolder);
100 }
101 else
102 {
103 $tempDir = Translate\IO\Directory::generateTemporalDirectory('translate');
104 }
105
106 if (!$tempDir->isExists() || !$tempDir->isDirectory())
107 {
108 $this->addError(new Error(
109 Loc::getMessage('TR_ERROR_CREATE_TEMP_FOLDER', ['#PATH#' => $tempDir->getPhysicalPath()])
110 ));
111 }
112 else
113 {
114 $this->archiveFilePath = $tempDir->getPhysicalPath(). '/'. $this->archiveFileName;
115 }
116
117 $this->seekPath = null;
118 }
119 else
120 {
121 $this->processedItems = (int)$progressParams['processedItems'];
122 $this->archiveFilePath = $progressParams['archiveFilePath'];
123 $this->archiveFileName = $progressParams['archiveFileName'];
124 $this->seekPath = $progressParams['seekPath'];
125
126 $tempDir = new Translate\IO\Directory($this->tmpFolderPath);
127 if (!$tempDir->isExists() || !$tempDir->isDirectory())
128 {
129 $this->addError(
130 new Error(Loc::getMessage('TR_ERROR_SOURCE_FOLDER', ['#PATH#' => $this->tmpFolderPath]))
131 );
132 }
133 }
134
135
136 $this->archiveFile = new Translate\IO\Archiver($this->archiveFilePath);
137
138 if ($this->isNewProcess)
139 {
140 if ($this->archiveFile->isExists())
141 {
142 $this->archiveFile->delete();
143 }
144 }
145
146 if ($this->isNewProcess)
147 {
148 $this->saveProgressParameters();
149
150 return [
151 'STATUS' => ($this->totalItems > 0 ? Translate\Controller\STATUS_PROGRESS : Translate\Controller\STATUS_COMPLETED),
152 'PROCESSED_ITEMS' => $this->processedItems,
153 'TOTAL_ITEMS' => $this->totalItems,
154 ];
155 }
156
157 $this->archiveFile->setOptions([
158 'COMPRESS' => $this->packFile,
159 ]);
160
161 return $this->performStep('runPacking');
162 }
163
167 private function runPacking(): array
168 {
169 $langDir = new Translate\IO\Directory($this->tmpFolderPath);
170
171 $this->totalItems = $this->totalFileCount;
172
173 $result = [];
174
175 switch ($this->archiveFile->pack($langDir, $this->seekPath))
176 {
177 case \IBXArchive::StatusContinue:
178 $this->seekPath = $this->archiveFile->getSeekPosition();
179 $this->processedItems += $this->archiveFile->getProcessedFileCount();
180
181 $this->saveProgressParameters();
182 break;
183
184 case \IBXArchive::StatusSuccess:
185 $this->processedItems += $this->archiveFile->getProcessedFileCount();
186 $this->declareAccomplishment();
187
188 $this->downloadParams = $this->getDownloadingParameters();
189 $result['FILE_NAME'] = $this->downloadParams['fileName'];
190 $result['DOWNLOAD_LINK'] = $this->generateDownloadLink();
191
192 $messagePlaceholders = [
193 '#TOTAL_FILES#' => $this->processedItems,
194 '#FILE_SIZE_FORMAT#' => \CFile::formatSize($this->downloadParams['fileSize']),
195 '#LANG#' => \mb_strtoupper($this->languageId),
196 '#FILE_PATH#' => $this->archiveFileName,
197 '#LINK#' => $result['DOWNLOAD_LINK'],
198 ];
199
200 $result['SUMMARY'] =
201 Loc::getMessage('TR_LANGUAGE_COLLECTED_ARCHIVE', $messagePlaceholders)."\n".
202 Loc::getMessage('TR_PACK_ACTION_EXPORT', $messagePlaceholders);
203
204 // we have to continue process in next action
205 $this->processToken = null;
206 $this->seekPath = null;
207 $this->saveProgressParameters();
208 break;
209
210 case \IBXArchive::StatusError:
211 if ($this->archiveFile->hasErrors())
212 {
213 $this->addErrors($this->archiveFile->getErrors());
214 }
215 else
216 {
217 $this->addError(
218 new Main\Error(Loc::getMessage('TR_ERROR_ARCHIVE'))
219 );
220 }
221 break;
222 }
223
224 $result['PROCESSED_ITEMS'] = $this->processedItems;
225 $result['TOTAL_ITEMS'] = $this->totalItems;
226
227 return $result;
228
229 }
230
231
237 private function generateDownloadLink(): string
238 {
239 return $this->controller->getActionUri(Grabber::ACTION_DOWNLOAD, ['langId' => $this->languageId])->getUri();
240 }
241
247 private function generateExportFileName(): string
248 {
249 if ($this->packFile && Translate\IO\Archiver::libAvailable())
250 {
251 $fileName = 'file-'.$this->languageId.'.tar.gz';
252 }
253 else
254 {
255 $fileName = 'file-'.$this->languageId.'.tar';
256 }
257
258 return $fileName;
259 }
260
266 private function getDownloadingParameters(): array
267 {
268 return [
269 'fileName' => $this->archiveFileName,
270 'filePath' => $this->archiveFilePath,
271 'fileType' => $this->packFile ? 'application/tar+gzip' : 'application/tar',
272 'fileSize' => $this->archiveFile->getSize(),
273 ];
274 }
275
282 {
283 $controller = $this->getController();
284 return $controller::SETTING_ID;
285 }
286}
addError(Error $error)
Definition action.php:200
addErrors(array $errors)
Definition action.php:213
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($name, Main\Engine\Controller $controller, array $config=[])
Definition pack.php:57
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136