aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/1.231_add_files_search_index.php
blob: 5ac455ac5004a1b79769274df9567b8cac25b60b (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
 * Creates the neccessary tables for the files search index.
 *
 * @author  <mlunzena@uos.de>
 * @license GPL2 or any later version
 */
class AddFilesSearchIndex extends Migration
{
    public function description()
    {
        return 'Sets up the database tables for the files search index';
    }

    /**
     * @SuppressWarnings(PHPMD.ShortMethodName)
     */
    public function up()
    {
        $this->createTables();

        if ($this->registerWidgets()) {
            $this->setupDefaultWidgets();
        }

        $this->installCronjob();
    }

    public function down()
    {
        $dbm = \DBManager::get();
        $dbm->execute('DROP TABLE `files_search_index`, `files_search_attributes`');
    }

    // Get version of database system (MySQL/MariaDB/Percona)
    private static function isMySQL55()
    {
        $data = \DBManager::get()->fetchFirst("SELECT VERSION() AS version");
        $version = $data[0];
        return version_compare($version, '5.6', '<');
    }

    // The primary key is named FTS_DOC_ID according to
    // https://dev.mysql.com/doc/refman/5.6/en/innodb-fulltext-index.html
    private function createTables()
    {
        $engine = self::isMySQL55() ? 'MyISAM' : 'InnoDB';
        $dbm = \DBManager::get();
        $dbm->execute(
            'CREATE TABLE IF NOT EXISTS `files_search_index` (
             `file_ref_id` varchar(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
             `text` text COLLATE utf8mb4_unicode_ci NOT NULL,
             `relevance` float NOT NULL,
             KEY `file_ref_id` (`file_ref_id`),
             FULLTEXT KEY `text` (`text`)
            ) ENGINE=' . $engine . ' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC'
        );
        $dbm->execute('
            CREATE TABLE IF NOT EXISTS `files_search_attributes` (
             `id` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
             `file_ref_user_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
             `file_ref_mkdate` int(10) unsigned NOT NULL,
             `file_ref_chdate` int(10) unsigned NOT NULL,
             `folder_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
             `folder_range_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
             `folder_range_type` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
             `folder_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
             `course_status` tinyint(4) unsigned DEFAULT NULL,
             `semester_start` int(20) unsigned DEFAULT NULL,
             `semester_end` int(20) unsigned DEFAULT NULL,
             PRIMARY KEY (`id`),
             KEY `folder_range_id` (`folder_range_id`),
             KEY `folder_range_type` (`folder_range_type`),
             KEY `semester_start` (`semester_start`),
             KEY `semester_end` (`semester_end`)
            ) ENGINE=' . $engine . ' DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC
        ');
    }

    private function registerWidgets()
    {
        $widgets = [
            'app/widgets/files_dashboard/MyPublicFilesWidget.php' => 'Widgets\\FilesDashboard\\MyPublicFilesWidget',

            'app/widgets/files_dashboard/LatestFilesWidget.php' => 'Widgets\\FilesDashboard\\LatestFilesWidget'
        ];

        foreach ($widgets as $path => $class) {
            if (!file_exists($path)) {
                return false;
            }

            require $path;
            \Widgets\Widget::registerWidget(new $class());
        }

        return true;
    }

    private function setupDefaultWidgets()
    {
        $widgets = [
            'Widgets\\FilesDashboard\\LatestFilesWidget' => ['width' => 3, 'height' => 3],
            'Widgets\\FilesDashboard\\MyPublicFilesWidget' => ['width' => 3, 'height' => 3],
        ];

        foreach (['user', 'autor', 'tutor', 'dozent', 'admin', 'root'] as $perm) {
            $this->createDefaultContainer('user', $perm, 'dashboard', $widgets);
        }
    }

    private function createDefaultContainer($rangeType, $rangeId, $scope, array $widgets)
    {
        $containerId = $this->createNewContainer($rangeType, $rangeId, $scope);

        $xCoord = 0;
        $yCoord = 0;
        $yMax   = 0;
        foreach ($widgets as $class => $additional) {
            $this->addWidgetToContainerByClass($containerId, $class, array_merge([
                'x' => $xCoord,
                'y' => $yCoord,
            ], $additional));

            $yMax = max($yMax, $additional['height'] ?: 1);

            $xCoord += $additional['width'];
            if ($xCoord >= 6) {
                $xCoord = 0;

                $yCoord += $yMax;
                $yMax = 0;
            }
        }
    }

    private function createNewContainer($rangeType, $rangeId, $scope)
    {
        $query = 'INSERT INTO `widget_containers` (
                    `range_id`, `range_type`, `scope`,
                    `mkdate`, `chdate`
                  ) VALUES (
                    :id, :type, :scope,
                    UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                  )';
        $statement = \DBManager::get()->prepare($query);
        $statement->bindValue(':id', $rangeId);
        $statement->bindValue(':type', $rangeType);
        $statement->bindValue(':scope', $scope);
        $statement->execute();

        return \DBManager::get()->lastInsertId();
    }

    /**
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    private function addWidgetToContainerByClass($containerId, $widgetClass, array $data = [])
    {
        $query = "INSERT INTO `widget_elements` (
                    `container_id`, `widget_id`,
                    `x`, `y`, `width`, `height`,
                    `locked`, `removable`, `options`,
                    `mkdate`, `chdate`
                  )
                  SELECT :container_id, `widget_id`,
                         :x, :y, :width, :height,
                         1, 0, '[]',
                         UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                  FROM `widgets`
                  WHERE `class` = :class";
        $statement = \DBManager::get()->prepare($query);
        $statement->bindValue(':container_id', $containerId);
        $statement->bindValue(':x', $data['x'] ?: 0);
        $statement->bindValue(':y', $data['y'] ?: 0);
        $statement->bindValue(':width', $data['width'] ?: 1);
        $statement->bindValue(':height', $data['height'] ?: 1);
        $statement->bindValue(':class', $widgetClass);
        $statement->execute();
    }

    private function installCronjob()
    {
        require_once 'lib/classes/FilesSearch/Cronjob.php';
        \FilesSearch\Cronjob::register()->schedule(55, 0)->activate(true);
    }
}