import
[web.mtrack] / bin / modify.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 if (!file_exists("bin/init.php")) {
5   echo "You must run me from the top-level mtrack dir\n";
6   exit(1);
7 }
8
9 /* People doing this are not necessarily sane, make sure we have PDO and
10  * pdo_sqlite */
11 if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
12   echo "Mtrack requires PDO and pdo_sqlite to function\n";
13   exit(1);
14 }
15
16 $projects = array();
17 $repos = array();
18 $tracs = array();
19 $links = array();
20 $config_file_name = 'config.ini';
21
22 $args = array();
23 array_shift($argv);
24 while (count($argv)) {
25   $arg = array_shift($argv);
26
27   if ($arg == '--config-file') {
28     if (count($argv) < 1) {
29       usage("Missing argument to --config-file");
30     }
31     $config_file_name = array_shift($argv);
32     continue;
33   }
34   if ($arg == '--trac') {
35     if (count($argv) < 2) {
36       usage("Missing arguments to --trac");
37     }
38     $pname = array_shift($argv);
39     $tracdb = array_shift($argv);
40
41     if (!file_exists($tracdb)) {
42       usage("Tracdb path must be a sqlite database");
43     }
44     $tracs[$tracdb] = $pname;
45     $projects[$pname] = $pname;
46     continue;
47   }
48   if ($arg == '--repo') {
49     if (count($argv) < 3) {
50       usage("Missing arguments to --repo");
51     }
52     $rname = array_shift($argv);
53     $rtype = array_shift($argv);
54     $rpath = array_shift($argv);
55
56     switch ($rtype) {
57       case 'hg':
58         if (!is_dir("$rpath/.hg")) {
59           usage("Repo path must be an hg repo dir");
60         }
61         break;
62       case 'svn':
63         if (!file_exists("$rpath/format")) {
64           usage("Repo path must be a svn repo");
65         }
66         break;
67       default:
68         usage("Invalid repo type $rtype");
69     }
70
71     $repos[$rname] = array($rname, $rtype, $rpath);
72     continue;
73   }
74
75   if ($arg == '--link') {
76     if (count($argv) < 3) {
77       usage("Missing arguments to --link");
78     }
79     $pname = array_shift($argv);
80     $rname = array_shift($argv);
81     $rloc = array_shift($argv);
82
83     $links[] = array($pname, $rname, $rloc);
84     $projects[$pname] = $pname;
85     continue;
86   }
87
88   $args[] = $arg;
89 }
90
91 if (count($args)) {
92   usage("Unhandled arguments");
93 }
94
95 putenv("MTRACK_CONFIG_FILE=" . $config_file_name);
96
97 require_once 'inc/common.php';
98 include 'bin/import-trac.php';
99
100 MTrackACL::$batch = true;
101 MTrackSearchDB::setBatchMode();
102
103 $db = MTrackDB::get();
104 MTrackChangeset::$use_txn = false;
105 $db->beginTransaction();
106
107 $CS = MTrackChangeset::begin('~modify~', 'setup modified');
108
109 foreach ($projects as $pname) {
110   $p = MTrackProject::loadByName($pname);
111   if ($p === null) {
112     $p = new MTrackProject;
113     $p->shortname = $pname;
114     $p->name = $pname;
115     $p->save($CS);
116   }
117   $projects[$pname] = $p;
118 }
119
120 foreach ($repos as $repo) {
121   $r = new MTrackRepo;
122   $r->shortname = $repo[0];
123   $r->scmtype = $repo[1];
124   $r->repopath = $repo[2];
125
126   foreach ($links as $link) {
127     list($pname, $rname, $loc) = $link;
128     if ($rname == $r->shortname) {
129       $p = $projects[$pname];
130       $r->addLink($p, $loc);
131     }
132   }
133
134   $r->save($CS);
135   $repos[$r->shortname] = $r;
136 }
137
138 $CS->commit();
139
140 $i = 0;
141 foreach ($tracs as $tracdb => $pname) {
142   import_from_trac($projects[$pname], $tracdb, true);
143 }
144 echo "Updating ACL tree\n"; flush();
145 MTrackACL::applyBatch();
146 echo "Committing\n"; flush();
147 $db->commit();
148 MTrackSearchDB::optimize();
149 echo "Done\n";
150
151 function usage($msg = '')
152 {
153   require_once 'inc/common.php';
154   echo $msg, <<<TXT
155
156 Usage: modify
157   --repo {name} {type} {repopath}
158                            define a repo named {name} of {type} at {repopath}
159   --link {project} {repo} {location}
160                            link a repo location to a project
161   --trac {project} {tracenv}
162                            import data from a trac environment at {tracenv}
163                            and associate with project {project}
164
165   --config-file {filename} Where to find the configuration file.
166                            defaults to config.ini in the current directory.
167
168
169 Supported repo types:
170
171
172 TXT;
173
174   foreach (MTrackRepo::getAvailableSCMs() as $t => $r) {
175     $d = $r->getSCMMetaData();
176     printf("  %10s   %s\n", $t, $d['name']);
177   }
178   echo "\n\n\n";
179
180   exit(1);
181 }