Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
wipeempty.php
1<?php
3
8
15{
16 use Translate\Controller\Stepper;
17 use Translate\Controller\ProcessParams;
18
19 const SETTING_ID = 'WIPE_EMPTY';
20
22 private $pathList;
23
25 private $seekOffset;
26
27
35 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
36 {
37 $this->keepField(['pathList', 'seekOffset']);
38
39 parent::__construct($name, $controller, $config);
40 }
41
42
50 public function run($pathList)
51 {
52 if ($this->isNewProcess)
53 {
54 $pathList = \preg_split("/[\r\n]+/", $pathList);
55 \array_walk($pathList, 'trim');
56 $pathList = \array_unique(\array_filter($pathList));
57
58 if (empty($pathList))
59 {
60 $this->addError(new Main\Error(Loc::getMessage('TR_CLEAN_EMPTY_PATH_LIST')));
61
62 return [
63 'STATUS' => Translate\Controller\STATUS_COMPLETED,
64 ];
65 }
66
67 foreach ($pathList as $testPath)
68 {
69 if (Translate\IO\Path::isPhpFile($testPath))
70 {
71 if (Translate\IO\Path::isLangDir($testPath))
72 {
73 $this->pathList[] = $testPath;
74 }
75 else
76 {
77 $this->addError(new Main\Error(Loc::getMessage('TR_CLEAN_FILE_NOT_LANG', ['#FILE#' => $testPath])));
78 }
79 }
80 else
81 {
82 if (Translate\IO\Path::isLangDir($testPath))
83 {
84 $this->pathList[] = $testPath;
85 }
86 else
87 {
88 // load lang folders
89 $pathFilter = [];
90 $pathFilter[] = [
91 'LOGIC' => 'OR',
92 '=PATH' => \rtrim($testPath, '/'),
93 '=%PATH' => \rtrim($testPath, '/'). '/%'
94 ];
95 $pathLangRes = Index\Internals\PathLangTable::getList([
96 'filter' => $pathFilter,
97 'order' => ['ID' => 'ASC'],
98 'select' => ['PATH'],
99 ]);
100 while ($pathLang = $pathLangRes->fetch())
101 {
102 $this->pathList[] = $pathLang['PATH'];
103 }
104 }
105 }
106 }
107
108 $this->totalItems = \count($this->pathList);
109 $this->processedItems = 0;
110
111 if ($this->totalItems == 0)
112 {
113 return [
114 'STATUS' => Translate\Controller\STATUS_COMPLETED,
115 'PROCESSED_ITEMS' => 0,
116 'TOTAL_ITEMS' => 0,
117 ];
118 }
119
120 $this->saveProgressParameters();
121 $this->isNewProcess = false;
122 }
123
124 return $this->performStep('runWiping');
125 }
126
132 private function runWiping(): array
133 {
134 $processedItemCount = 0;
135 for ($pos = ((int)$this->seekOffset > 0 ? (int)$this->seekOffset : 0), $total = count($this->pathList); $pos < $total; $pos ++)
136 {
137 $testPath = $this->pathList[$pos];
138
139 $isOk = true;
140
141 // file
142 if (Translate\IO\Path::isPhpFile($testPath))
143 {
144 $testPath = Translate\IO\Path::replaceLangId($testPath, '#LANG_ID#');
145
146 foreach (self::$enabledLanguagesList as $langId)
147 {
148 $langRelPath = Translate\IO\Path::replaceLangId($testPath, $langId);
149 $langFullPath = Translate\IO\Path::tidy(self::$documentRoot.'/'.$langRelPath);
150 $langFullPath = Main\Localization\Translation::convertLangPath($langFullPath, $langId);
151
152 if ($this->removeEmptyParents($langFullPath))
153 {
154 Translate\Index\Internals\FileIndexTable::purge(new Translate\Filter(['path' => $testPath, 'langId' => $langId]));
155 }
156 else
157 {
158 $isOk = false;
159 }
160 }
161 }
162
163 // folder
164 else
165 {
166 if (\mb_substr($testPath, -5) === '/lang')
167 {
168 $testPath .= '/#LANG_ID#';
169 }
170 else
171 {
172 $testPath = Translate\IO\Path::replaceLangId($testPath, '#LANG_ID#');
173 }
174
175 foreach (self::$enabledLanguagesList as $langId)
176 {
177 $langRelPath = Translate\IO\Path::replaceLangId($testPath. '/.nonExistentTestFile.php', $langId);
178 $langFullPath = Translate\IO\Path::tidy(self::$documentRoot.'/'.$langRelPath);
179 $langFullPath = Main\Localization\Translation::convertLangPath($langFullPath, $langId);
180
181 if ($this->removeEmptyParents($langFullPath))
182 {
183 Translate\Index\Internals\FileIndexTable::purge(new Translate\Filter(['path' => $testPath, 'langId' => $langId]));
184 }
185 else
186 {
187 $isOk = false;
188 }
189 }
190 }
191
192 if ($isOk)
193 {
194 Translate\Index\Internals\PathIndexTable::purge(new Translate\Filter(['path' => $testPath, 'recursively' => true]));
195 }
196
197
198 $processedItemCount ++;
199
200 if (isset($this->pathList[$pos + 1]))
201 {
202 $this->seekOffset = $pos + 1;//next
203 }
204 else
205 {
206 $this->seekOffset = null;
207 $this->declareAccomplishment();
209 }
210
211 if ($this->instanceTimer()->hasTimeLimitReached())
212 {
213 break;
214 }
215 }
216
217 $this->processedItems += $processedItemCount;
218
219 if ($this->instanceTimer()->hasTimeLimitReached() !== true)
220 {
221 $this->declareAccomplishment();
223 }
224
225 return [
226 'PROCESSED_ITEMS' => $this->processedItems,
227 'TOTAL_ITEMS' => $this->totalItems,
228 ];
229 }
230
238 private function removeEmptyParents($langFullPath): bool
239 {
240 try
241 {
242 $langFile = Translate\File::instantiateByPath($langFullPath);
243 if ($langFile instanceof Translate\File)
244 {
245 return $langFile->removeEmptyParents();
246 }
247 }
248 catch (Main\ArgumentException $ex)
249 {
250 }
251
252 return false;
253 }
254
255
262 {
263 return self::SETTING_ID;
264 }
265}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($name, Main\Engine\Controller $controller, array $config=[])
Definition wipeempty.php:35
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136
addError(Main\Error $error)
Definition error.php:22