source: trunk/scripts/textblock-add-missing-security @ 1184

Revision 1066, 1.9 KB checked in by bogdan2412, 2 years ago (diff)

Solves Ticket #357

This script searches through all revisions with invalid security and sets it to the security of the first revision in which security is specified.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env php
2<?php
3require_once(dirname($argv[0]) . "/utilities.php");
4require_once(IA_ROOT_DIR . "www/format/format.php");
5
6db_connect();
7
8$invalid_security = "
9    security NOT IN ('public', 'private', 'protected') AND
10    security NOT LIKE 'task: %' AND
11    security NOT LIKE 'round: %'
12";
13$query = "
14SELECT name, timestamp, security FROM ia_textblock_revision WHERE
15    ($invalid_security)
16";
17$textblocks = db_fetch_all($query);
18
19foreach ($textblocks as $textblock) {
20    if ($textblock["security"] !== "") {
21        log_warn("Invalid textblock security for " . $textblock["name"]);
22        continue;
23    }
24
25    # Fetch the security of the first revision which has security set
26    $result = db_fetch(sprintf("
27        SELECT security FROM ia_textblock_revision
28        WHERE name = %s AND NOT (%s)
29        ORDER BY timestamp ASC LIMIT 1
30    ", db_quote($textblock["name"]), $invalid_security));
31
32    if ($result === NULL) {
33        # Fetch the security of the latest revision for the current page
34        $result = db_fetch(sprintf(
35            "SELECT security FROM ia_textblock WHERE name = %s",
36            db_quote($textblock["name"])
37        ));
38        $new_security = array_pop($result);
39    } else {
40        $new_security = array_pop($result);
41    }
42
43    log_print(sprintf(
44        "Changing '%s' revision from '%s' security to '%s'",
45        $textblock["name"], format_date($textblock["timestamp"]),
46        $new_security
47    ));
48
49    db_query(sprintf(
50        "UPDATE ia_textblock_revision SET security = %s
51         WHERE name = %s AND timestamp = %s",
52        db_quote($new_security), db_quote($textblock["name"]),
53        db_quote($textblock["timestamp"])
54    ));
55}
56
57db_query("
58ALTER TABLE `ia_textblock`
59    MODIFY COLUMN `security` VARCHAR(64) NOT NULL
60");
61
62db_query("
63ALTER TABLE `ia_textblock_revision`
64    MODIFY COLUMN `name` VARCHAR(64) NOT NULL,
65    MODIFY COLUMN `security` VARCHAR(64) NOT NULL
66");
67?>
Note: See TracBrowser for help on using the repository browser.