| Conditions | 34 |
| Paths | 800 |
| Total Lines | 197 |
| Code Lines | 132 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 76 | protected function execute(InputInterface $input, OutputInterface $output): int { |
||
| 77 | if ($this->memoryLimit !== -1) { |
||
| 78 | $limitInMiB = round($this->memoryLimit / 1024 /1024, 1); |
||
| 79 | $thresholdInMiB = round($this->memoryTreshold / 1024 /1024, 1); |
||
| 80 | $output->writeln("Memory limit is $limitInMiB MiB"); |
||
| 81 | $output->writeln("Memory threshold is $thresholdInMiB MiB"); |
||
| 82 | $output->writeln(""); |
||
| 83 | $memoryCheckEnabled = true; |
||
| 84 | } else { |
||
| 85 | $output->writeln("No memory limit in place - disabled memory check. Set a PHP memory limit to automatically stop the execution of this migration script once memory consumption is close to this limit."); |
||
| 86 | $output->writeln(""); |
||
| 87 | $memoryCheckEnabled = false; |
||
| 88 | } |
||
| 89 | |||
| 90 | $dryMode = $input->getOption('dry'); |
||
| 91 | |||
| 92 | if ($dryMode) { |
||
| 93 | $output->writeln("INFO: The migration is run in dry mode and will not modify anything."); |
||
| 94 | $output->writeln(""); |
||
| 95 | } |
||
| 96 | |||
| 97 | $verbose = $output->isVerbose(); |
||
| 98 | |||
| 99 | $instanceId = $this->config->getSystemValueString('instanceid'); |
||
| 100 | |||
| 101 | $output->writeln("This will migrate all previews from the old preview location to the new one."); |
||
| 102 | $output->writeln(''); |
||
| 103 | |||
| 104 | $output->writeln('Fetching previews that need to be migrated …'); |
||
| 105 | /** @var \OCP\Files\Folder $currentPreviewFolder */ |
||
| 106 | $currentPreviewFolder = $this->rootFolder->get("appdata_$instanceId/preview"); |
||
| 107 | |||
| 108 | $directoryListing = $currentPreviewFolder->getDirectoryListing(); |
||
| 109 | |||
| 110 | $total = count($directoryListing); |
||
| 111 | /** |
||
| 112 | * by default there could be 0-9 a-f and the old-multibucket folder which are all fine |
||
| 113 | */ |
||
| 114 | if ($total < 18) { |
||
| 115 | $directoryListing = array_filter($directoryListing, function ($dir) { |
||
| 116 | if ($dir->getName() === 'old-multibucket') { |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | |||
| 120 | // a-f can't be a file ID -> removing from migration |
||
| 121 | if (preg_match('!^[a-f]$!', $dir->getName())) { |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | |||
| 125 | if (preg_match('!^[0-9]$!', $dir->getName())) { |
||
| 126 | // ignore folders that only has folders in them |
||
| 127 | if ($dir instanceof Folder) { |
||
| 128 | foreach ($dir->getDirectoryListing() as $entry) { |
||
| 129 | if (!$entry instanceof Folder) { |
||
| 130 | return true; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | return true; |
||
| 137 | }); |
||
| 138 | $total = count($directoryListing); |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($total === 0) { |
||
| 142 | $output->writeln("All previews are already migrated."); |
||
| 143 | return 0; |
||
| 144 | } |
||
| 145 | |||
| 146 | $output->writeln("A total of $total preview files need to be migrated."); |
||
| 147 | $output->writeln(""); |
||
| 148 | $output->writeln("The migration will always migrate all previews of a single file in a batch. After each batch the process can be canceled by pressing CTRL-C. This fill finish the current batch and then stop the migration. This migration can then just be started and it will continue."); |
||
| 149 | |||
| 150 | if ($input->getOption('batch')) { |
||
| 151 | $output->writeln('Batch mode active: migration is started right away.'); |
||
| 152 | } else { |
||
| 153 | $helper = $this->getHelper('question'); |
||
| 154 | $question = new ConfirmationQuestion('<info>Should the migration be started? (y/[n]) </info>', false); |
||
| 155 | |||
| 156 | if (!$helper->ask($input, $output, $question)) { |
||
| 157 | return 0; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | // register the SIGINT listener late in here to be able to exit in the early process of this command |
||
| 162 | pcntl_signal(SIGINT, [$this, 'sigIntHandler']); |
||
| 163 | |||
| 164 | $output->writeln(""); |
||
| 165 | $output->writeln(""); |
||
| 166 | $section1 = $output->section(); |
||
|
|
|||
| 167 | $section2 = $output->section(); |
||
| 168 | $progressBar = new ProgressBar($section2, $total); |
||
| 169 | $progressBar->setFormat("%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% Used Memory: %memory:6s%"); |
||
| 170 | $time = (new \DateTime())->format('H:i:s'); |
||
| 171 | $progressBar->setMessage("$time Starting …"); |
||
| 172 | $progressBar->maxSecondsBetweenRedraws(0.2); |
||
| 173 | $progressBar->start(); |
||
| 174 | |||
| 175 | foreach ($directoryListing as $oldPreviewFolder) { |
||
| 176 | pcntl_signal_dispatch(); |
||
| 177 | $name = $oldPreviewFolder->getName(); |
||
| 178 | $time = (new \DateTime())->format('H:i:s'); |
||
| 179 | $section1->writeln("$time Migrating previews of file with fileId $name …"); |
||
| 180 | $progressBar->display(); |
||
| 181 | |||
| 182 | if ($this->stopSignalReceived) { |
||
| 183 | $section1->writeln("$time Stopping migration …"); |
||
| 184 | return 0; |
||
| 185 | } |
||
| 186 | if (!$oldPreviewFolder instanceof Folder) { |
||
| 187 | $section1->writeln(" Skipping non-folder $name …"); |
||
| 188 | $progressBar->advance(); |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | if ($name === 'old-multibucket') { |
||
| 192 | $section1->writeln(" Skipping fallback mount point $name …"); |
||
| 193 | $progressBar->advance(); |
||
| 194 | continue; |
||
| 195 | } |
||
| 196 | if (in_array($name, ['a', 'b', 'c', 'd', 'e', 'f'])) { |
||
| 197 | $section1->writeln(" Skipping hex-digit folder $name …"); |
||
| 198 | $progressBar->advance(); |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | if (!preg_match('!^\d+$!', $name)) { |
||
| 202 | $section1->writeln(" Skipping non-numeric folder $name …"); |
||
| 203 | $progressBar->advance(); |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | |||
| 207 | $newFoldername = Root::getInternalFolder($name); |
||
| 208 | |||
| 209 | $memoryUsage = memory_get_usage(); |
||
| 210 | if ($memoryCheckEnabled && $memoryUsage > $this->memoryTreshold) { |
||
| 211 | $section1->writeln(""); |
||
| 212 | $section1->writeln(""); |
||
| 213 | $section1->writeln(""); |
||
| 214 | $section1->writeln(" Stopped process 25 MB before reaching the memory limit to avoid a hard crash."); |
||
| 215 | $time = (new \DateTime())->format('H:i:s'); |
||
| 216 | $section1->writeln("$time Reached memory limit and stopped to avoid hard crash."); |
||
| 217 | return 1; |
||
| 218 | } |
||
| 219 | |||
| 220 | $previews = $oldPreviewFolder->getDirectoryListing(); |
||
| 221 | if ($previews !== []) { |
||
| 222 | try { |
||
| 223 | $this->rootFolder->get("appdata_$instanceId/preview/$newFoldername"); |
||
| 224 | } catch (NotFoundException $e) { |
||
| 225 | if ($verbose) { |
||
| 226 | $section1->writeln(" Create folder preview/$newFoldername"); |
||
| 227 | } |
||
| 228 | if (!$dryMode) { |
||
| 229 | $this->rootFolder->newFolder("appdata_$instanceId/preview/$newFoldername"); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | foreach ($previews as $preview) { |
||
| 234 | pcntl_signal_dispatch(); |
||
| 235 | $previewName = $preview->getName(); |
||
| 236 | |||
| 237 | if ($preview instanceof Folder) { |
||
| 238 | $section1->writeln(" Skipping folder $name/$previewName …"); |
||
| 239 | $progressBar->advance(); |
||
| 240 | continue; |
||
| 241 | } |
||
| 242 | if ($verbose) { |
||
| 243 | $section1->writeln(" Move preview/$name/$previewName to preview/$newFoldername"); |
||
| 244 | } |
||
| 245 | if (!$dryMode) { |
||
| 246 | try { |
||
| 247 | $preview->move("appdata_$instanceId/preview/$newFoldername/$previewName"); |
||
| 248 | } catch (\Exception $e) { |
||
| 249 | $this->logger->logException($e, ['app' => 'core', 'message' => "Failed to move preview from preview/$name/$previewName to preview/$newFoldername"]); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | if ($oldPreviewFolder->getDirectoryListing() === []) { |
||
| 255 | if ($verbose) { |
||
| 256 | $section1->writeln(" Delete empty folder preview/$name"); |
||
| 257 | } |
||
| 258 | if (!$dryMode) { |
||
| 259 | try { |
||
| 260 | $oldPreviewFolder->delete(); |
||
| 261 | } catch (\Exception $e) { |
||
| 262 | $this->logger->logException($e, ['app' => 'core', 'message' => "Failed to delete empty folder preview/$name"]); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | $section1->writeln(" Finished migrating previews of file with fileId $name …"); |
||
| 267 | $progressBar->advance(); |
||
| 268 | } |
||
| 269 | |||
| 270 | $progressBar->finish(); |
||
| 271 | $output->writeln(""); |
||
| 272 | return 0; |
||
| 273 | } |
||
| 280 |