GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — hypernext ( 21d444...9329a9 )
by Nico
31:36 queued 16:07
created

AddMissingLinks::execute()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 45
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 33
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 45
rs 8.0555
1
<?php declare(strict_types=1);
2
/**
3
 * @author Nicolas CARPi <[email protected]>
4
 * @copyright 2012 Nicolas CARPi
5
 * @see https://www.elabftw.net Official website
6
 * @license AGPL-3.0
7
 * @package elabftw
8
 */
9
10
namespace Elabftw\Commands;
11
12
use Elabftw\Elabftw\ContentParams;
13
use Elabftw\Elabftw\Db;
14
use Elabftw\Models\Experiments;
15
use Elabftw\Models\Items;
16
use Elabftw\Models\Links;
17
use Elabftw\Models\Templates;
18
use Elabftw\Models\Users;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Find links to items in entity bodies and add to 'linked items'
25
 * See #1470 https://github.com/elabftw/elabftw/issues/1470#issuecomment-527098716
26
 */
27
class AddMissingLinks extends Command
28
{
29
    // the name of the command (the part after "bin/console")
30
    protected static $defaultName = 'links:sync';
31
32
    /**
33
     * Set the help messages
34
     */
35
    protected function configure(): void
36
    {
37
        $this
38
            // the short description shown while running "php bin/console list"
39
            ->setDescription('Make sure links in body are also properly added as "Linked items"')
40
41
            // the full command description shown when running the command with
42
            // the "--help" option
43
            ->setHelp('Find links to items in the body of entities and add them to the "Linked items" of that entity.');
44
    }
45
46
    /**
47
     * Execute
48
     *
49
     * @return int 0
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53
        $Db = Db::getConnection();
54
55
        $tables = array('experiments', 'experiments_templates', 'items');
56
        $query = "SELECT `id`, `body`, `userid` FROM `table` WHERE `body` LIKE '%database.php?mode=view&amp;id=%';";
57
58
        foreach ($tables as $table) {
59
            echo 'Searching in ' . $table . "\n";
60
            $sql = str_replace('table', $table, $query);
61
            $req = $Db->prepare($sql);
62
            $req->execute();
63
            $res = $req->fetchAll();
64
65
            if (!empty($res)) {
66
                echo 'Found ' . count($res) . " entries with ids:\n";
67
                $count = 0;
68
                foreach ($res as $data) {
69
                    echo '  ' . $data['id'] . "\n";
70
                    switch ($table) {
71
                        case 'experiments':
72
                            $entity = new Experiments(new Users((int) $data['userid']), (int) $data['id']);
73
                            break;
74
                        case 'experiments_templates':
75
                            $entity = new Templates(new Users((int) $data['userid']), (int) $data['id']);
76
                            break;
77
                        case 'items':
78
                            $entity = new Items(new Users((int) $data['userid']), (int) $data['id']);
79
                            break;
80
                        default:
81
                            continue 2;
82
                    }
83
84
                    preg_match_all('/database\.php\?mode=view&amp;id=([0-9]+)/', $data['body'], $matches);
85
                    foreach ($matches[1] as $match) {
86
                        $out = (new Links($entity))->create(new ContentParams($match));
87
                        if ((int) $out !== 0) {
88
                            $count++;
89
                        }
90
                    }
91
                }
92
                echo 'Added ' . $count . " links.\n\n";
93
            }
94
        }
95
        return 0;
96
    }
97
}
98