aboutsummaryrefslogtreecommitdiff
path: root/cli/biest7866-fix.php
blob: ca1f90c094dad07855a0b3ca3ea06dc567902df8 (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
#!/usr/bin/env php
<?php
/**
 * This script sets folder range_ids to the range_ids of their parent folder.
 *
 * @author Thomas Hackl <thomas.hackl@uni-passau.de>
 * @see    https://develop.studip.de/trac/ticket/7866
 */

require_once __DIR__ . '/studip_cli_env.inc.php';

/**
 * Sets the range_id of all child folders to the given range_id.
 * @param $parent_folder
 * @param $range_id
 */
function setFolderRangeId($parent_folder, $range_id) {
    // Update all child folder range_ids.
    DBManager::get()->execute(
        "UPDATE `folders` SET `range_id` = :range WHERE `parent_id` = :parent",
        [
            'range' => $range_id,
            'parent' => $parent_folder
        ]
    );

    // Recursion: set correct range_id for child folders with wrong range_id.
    $children = DBManager::get()->fetchAll(
        "SELECT `id`, `range_id` FROM `folders` WHERE `parent_id` = :parent",
        [
            'parent' => $parent_folder
        ]
    );
    foreach ($children as $child) {
        if ($child['range_id'] != $range_id) {
            echo sprintf("Folder %s -> range_id %s.\n", $child['id'], $range_id);
        }
        setFolderRangeId($child['id'], $range_id);
    }
}

// Fetch all root folders and process their children recursively.
$root_folders = DBManager::get()->fetchAll("SELECT `id`, `range_id` FROM `folders` WHERE `parent_id` = ''");

foreach ($root_folders as $r) {
    setFolderRangeId($r['id'], $r['range_id']);
}