summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/ConfigFileDiffTable.php
blob: 1d14d5e0a000efdaf02c6810f8e5a256a698583b (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\Util;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;

class ConfigFileDiffTable extends ZfQueryBasedTable
{
    use DbHelper;

    protected $leftChecksum;

    protected $rightChecksum;

    /**
     * @param $leftSum
     * @param $rightSum
     * @param Db $connection
     * @return static
     */
    public static function load($leftSum, $rightSum, Db $connection)
    {
        $table = new static($connection);
        $table->getAttributes()->add('class', 'config-diff');
        return $table->setLeftChecksum($leftSum)
            ->setRightChecksum($rightSum);
    }

    public function renderRow($row)
    {
        $tr = $this::row([
            $this->getFileFiffLink($row),
            $row->file_path,
        ]);

        $tr->getAttributes()->add('class', 'file-' . $row->file_action);
        return $tr;
    }

    protected function getFileFiffLink($row)
    {
        $params = array('file_path' => $row->file_path);

        if ($row->file_checksum_left === $row->file_checksum_right) {
            $params['config_checksum'] = $row->config_checksum_right;
        } elseif ($row->file_checksum_left === null) {
            $params['config_checksum'] = $row->config_checksum_right;
        } elseif ($row->file_checksum_right === null) {
            $params['config_checksum'] = $row->config_checksum_left;
        } else {
            $params['left']  = $row->config_checksum_left;
            $params['right'] = $row->config_checksum_right;
            return Link::create(
                $row->file_action,
                'director/config/filediff',
                $params
            );
        }

        return Link::create($row->file_action, 'director/config/file', $params);
    }

    public function setLeftChecksum($checksum)
    {
        $this->leftChecksum = $checksum;
        return $this;
    }

    public function setRightChecksum($checksum)
    {
        $this->rightChecksum = $checksum;
        return $this;
    }

    public function getTitles()
    {
        return array(
            $this->translate('Action'),
            $this->translate('File'),
        );
    }

    public function prepareQuery()
    {
        $db = $this->db();

        $left = $db->select()
            ->from(
                array('cfl' => 'director_generated_config_file'),
                array(
                    'file_path' => 'COALESCE(cfl.file_path, cfr.file_path)',
                    'config_checksum_left'  => $this->dbHexFunc('cfl.config_checksum'),
                    'config_checksum_right' => $this->dbHexFunc('cfr.config_checksum'),
                    'file_checksum_left'    => $this->dbHexFunc('cfl.file_checksum'),
                    'file_checksum_right'   => $this->dbHexFunc('cfr.file_checksum'),
                    'file_action'           => '(CASE WHEN cfr.config_checksum IS NULL'
                        . " THEN 'removed' WHEN cfl.file_checksum = cfr.file_checksum"
                        . " THEN 'unmodified' ELSE 'modified' END)",
                )
            )->joinLeft(
                array('cfr' => 'director_generated_config_file'),
                $db->quoteInto(
                    'cfl.file_path = cfr.file_path AND cfr.config_checksum = ?',
                    $this->quoteBinary(hex2bin($this->rightChecksum))
                ),
                array()
            )->where(
                'cfl.config_checksum = ?',
                $this->quoteBinary(hex2bin($this->leftChecksum))
            );

        $right = $db->select()
            ->from(
                array('cfl' => 'director_generated_config_file'),
                array(
                    'file_path' => 'COALESCE(cfr.file_path, cfl.file_path)',
                    'config_checksum_left'  => $this->dbHexFunc('cfl.config_checksum'),
                    'config_checksum_right' => $this->dbHexFunc('cfr.config_checksum'),
                    'file_checksum_left'    => $this->dbHexFunc('cfl.file_checksum'),
                    'file_checksum_right'   => $this->dbHexFunc('cfr.file_checksum'),
                    'file_action'           => "('created')",
                )
            )->joinRight(
                array('cfr' => 'director_generated_config_file'),
                $db->quoteInto(
                    'cfl.file_path = cfr.file_path AND cfl.config_checksum = ?',
                    $this->quoteBinary(hex2bin($this->leftChecksum))
                ),
                array()
            )->where(
                'cfr.config_checksum = ?',
                $this->quoteBinary(hex2bin($this->rightChecksum))
            )->where('cfl.file_checksum IS NULL');

        return $db->select()->union(array($left, $right))->order('file_path');
    }
}