Issues (319)

src/Command/UpdateHeadersDbCommand.php (1 issue)

Severity
1
<?php namespace App\Command;
2
3
use App\Service\TextService;
4
use Symfony\Component\Console\Input\InputInterface;
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
class UpdateHeadersDbCommand extends Command {
8
9
	private $output;
10
11
	public function getName() {
12
		return 'db:update-headers';
13
	}
14
15
	public function getDescription() {
16
		return 'Update text headers in the database';
17
	}
18
19
	public function getHelp() {
20
		return 'The <info>%command.name%</info> command updates the text headers in the database.';
21
	}
22
23
	protected function getRequiredArguments() {
24
		return [
25
			'texts' => 'Texts which headers should be updated (comma separated)',
26
		];
27
	}
28
29
	protected function getBooleanOptions() {
30
		return [
31
			'dump-sql' => 'Output SQL queries instead of executing them',
32
		];
33
	}
34
35
	/** {@inheritdoc} */
36
	protected function execute(InputInterface $input, OutputInterface $output): int {
37
		$this->output = $output;
38
		$texts = trim($input->getArgument('texts'));
39
		$dumpSql = $input->getOption('dump-sql') === true;
40
		$this->updateHeaders($texts, $dumpSql);
41
		$output->writeln('/*Done.*/');
42
		return self::SUCCESS;
43
	}
44
45
	/**
46
	 * @param string $texts
47
	 * @param bool $dumpSql
48
	 */
49
	private function updateHeaders($texts, $dumpSql) {
50
		$queries = [];
51
		$dql = 'SELECT t FROM App\Entity\Text t WHERE t.headlevel > 0';
52
		if ($texts) {
53
			$dql .= " AND t.id IN ($texts)";
54
		}
55
		$iterableResult = $this->em->createQuery($dql)->iterate();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\Query::iterate() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
		$iterableResult = /** @scrutinizer ignore-deprecated */ $this->em->createQuery($dql)->iterate();
Loading history...
56
		$textService = new TextService($this->olddb());
57
		foreach ($iterableResult AS $row) {
58
			$text = $row[0];
59
			if ($text->isCompilation()) {
60
				$file = tempnam(sys_get_temp_dir(), 'text');
61
				file_put_contents($file, $text->getRawContent());
62
			} else {
63
				$file = $this->webDir($text->getMainContentFile());
64
			}
65
			$queries = array_merge($queries, $textService->buildTextHeadersUpdateQuery($file, $text->getId(), $text->getHeadlevel()));
66
			$this->em->setFree($text); // free memory
67
		}
68
69
		if ($dumpSql) {
70
			$this->printQueries($queries);
71
		} else {
72
			$this->executeUpdates($queries, $this->em->getConnection());
73
		}
74
	}
75
76
}
77