source: trunk/scripts/migrate-authors-to-task-tags @ 1184

Revision 1100, 3.2 KB checked in by bogdan2412, 2 years ago (diff)

Updated database dump

Updated db-strip script to remove user's remote ip information and also cleanup
entries previously left orphan in ia_user_round, ia_round_task and in
ia_*_tags. Also made it erase more useless stuff from the forum while keeping
more users, more jobs and more textblock history.

The total size of the dump is actually smaller now, even though it has more
relevant data.

Also fixed a bug in textblock_grep which caused the grep macro to stop working.

  • Property svn:executable set to *
Line 
1#! /usr/bin/env php
2<?php
3
4require_once(dirname($argv[0]) . "/utilities.php");
5require_once(IA_ROOT_DIR . "common/db/task.php");
6require_once(IA_ROOT_DIR . "common/tags.php");
7db_connect();
8
9$tasks = task_get_all();
10$author_tags = array();
11foreach($tasks as &$task) {
12    // Special cases to make my life easier.
13    if ($task["author"] == "") {
14        $task["author"] = "Necunoscut";
15        task_update($task);
16    }
17    if ($task["author"] == "Alin Burtza") {
18        $task["author"] = "Alin Burta";
19        task_update($task);
20    }
21    if ($task["author"] == "Maria si Adrian Nita") {
22        $task["author"] = "Maria Nita si Adrian Nita";
23        task_update($task);
24    }
25
26    // Split authors separated by , & ; or si
27    $authors = preg_split("/,|&|;|(\ssi\s)/", $task["author"]);
28    $authors = array_map('trim', $authors);
29    $author_tags = array_unique(array_merge($author_tags, $authors));
30}
31
32// Split every author into individual words so that when compared "A B"
33// is the same as "B A".
34$authors = array();
35foreach($author_tags as $author) {
36    $authors[$author] = preg_split("/[^a-zA-Z.]/", $author);
37    sort($authors[$author]);
38}
39
40// Check if one array is completely included in another.
41// Used to group "Cosmin-Silvestru Negruseri" and "Cosmin Negruseri" together.
42function included($a, $b) {
43    $intersection = array_values(array_intersect($a, $b));
44    sort($intersection);
45    return $intersection == $a;
46}
47
48// Group same authors together.
49$grouped = array();
50foreach($authors as $key => $author) {
51    if (isset($grouped[$key])) {
52        continue;
53    }
54    // Skip if included in other.
55    $ok = true;
56    foreach($authors as $other) {
57        if ($author == $other) {
58            continue;
59        }
60        if (included($author, $other)) {
61            $ok = false;
62            break;
63        }
64    }
65    if (!$ok) {
66        continue;
67    }
68
69    $group = array();
70    foreach($authors as $key_other => $other) {
71        if (included($other, $author)) {
72            $group[] = $key_other;
73        }
74    }
75
76    // Choose a tag for the group
77    // The admin should check that each tag is in First_Name Last_Name form.
78    $tag = read_line(sprintf(
79        "Alege un tag de forma 'Prenume Nume' pentru grupa:\n- %s\n",
80        implode("\n- ", $group)), $group[0]);
81    foreach($group as $author) {
82        $grouped[$author] = $tag;
83    }
84}
85
86// Add author tags to tasks
87foreach($tasks as $task) {
88    // Split authors separated by , & or si
89    $authors = preg_split("/,|&|;|(\ssi\s)/", $task["author"]);
90    $authors = array_map('trim', $authors);
91
92    tag_clear("task", $task["id"], "author");
93    foreach($authors as $author) {
94        tag_add("task", $task["id"], tag_assign_id(array(
95            "name" => $grouped[$author],
96            "type" => "author",
97            "parent" => 0
98        )));
99    }
100
101    // Change all authors in ia_task to match tags until we remove the column.
102    $new_authors = tag_build_list("task", $task["id"], "author");
103    if ($task["author"] != $new_authors) {
104        log_print(sprintf("Changing task '%s' author from '%s' to '%s'",
105            $task["id"], $task["author"], $new_authors));
106        $task["author"] = $new_authors;
107        task_update($task);
108    }
109}
110
111db_query("ALTER TABLE ia_task DROP COLUMN author");
112
113?>
Note: See TracBrowser for help on using the repository browser.