aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/HelpContent/Migrate.php
blob: 8ab790a00f1c55aa30be1953dbd947682d6093da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace Studip\Cli\Commands\HelpContent;

use DBManager;
use PDO;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class Migrate extends Command
{
    protected static $defaultName = 'help-content:migrate';

    protected function configure(): void
    {
        $this->setDescription('Migrate help-content.');
        $this->addArgument('version', InputArgument::REQUIRED, 'Version of the help content');
        $this->addArgument('language', InputArgument::REQUIRED, 'Language of the help content');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $version = $input->getArgument('version');
        $language = $input->getArgument('language');
        $help_content_path = $GLOBALS['STUDIP_BASE_PATH'] . '/doc/helpbar';

        $query = 'SELECT * FROM help_content WHERE studip_version = ? LIMIT 1';
        $statement = DBManager::get()->prepare($query);
        $statement->execute([$version]);
        $ret = $statement->fetchGrouped(PDO::FETCH_ASSOC);

        if ($ret && count($ret)) {
            $io->info('Helpbar content already present for this version!');
            return Command::SUCCESS;
        }

        $filename = $help_content_path . '/' . $language . '/helpcontent.json';

        if (!is_file($filename)) {
            $io->error('File not found: ' . $filename);
            return Command::FAILURE;
        }

        $json = json_decode(file_get_contents($filename), true);

        if ($json === null) {
            $io->error('Helpbar content could not be loaded. File: ' . $filename);
            return Command::FAILURE;
        }

        $count = [];
        foreach ($json as $row) {
            if (!is_array($row['text'])) {
                $row['text'] = [$row['text']];
            }
            if (!$row['label'] || !$row['icon']) {
                $row['label'] = '';
            }
            $installation_id = \Config::get()->STUDIP_INSTALLATION_ID;
            foreach ($row['text'] as $index => $text) {
                $count[$language . $row['route']]++;
                $statement = DBManager::get()->prepare(
                    "INSERT INTO help_content (
                          `content_id`,
                          `language`,
                          `label`,
                          `icon`,
                          `content`,
                          `route`,
                          `studip_version`,
                          `position`,
                          `custom`,
                          `visible`,
                          `author_id`,
                          `installation_id`,
                          `mkdate`
                      ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 1, '', ?, UNIX_TIMESTAMP())"
                );
                $statement->execute([
                    md5(uniqid(rand(), true)),
                    $language,
                    $index == 0 ? $row['label'] : '',
                    $index == 0 ? $row['icon'] : '',
                    $text,
                    $row['route'],
                    $version,
                    $count[$language . $row['route']],
                    $installation_id,
                ]);
            }
        }

        if (count($count)) {
            if (!\Config::get()->getValue('HELP_CONTENT_CURRENT_VERSION')) {
                \Config::get()->create('HELP_CONTENT_CURRENT_VERSION', [
                    'value' => $version,
                    'is_default' => 0,
                    'type' => 'string',
                    'range' => 'global',
                    'section' => 'global',
                    'description' => _('Aktuelle Version der Helpbar-Einträge in Stud.IP'),
                ]);
            } else {
                \Config::get()->store('HELP_CONTENT_CURRENT_VERSION', $version);
            }
        }
        $io->success('help content added for ' . count($count) . ' routes.');
        return Command::SUCCESS;
    }
}