3 /* Script for updating the database structure.
7 require_once("Console/Getopt.php");
8 require_once("diogenes/diogenes.database-creator.inc.php");
10 /** The DiogenesDbInit class handles database upgrades between
13 class DiogenesDbInit
extends DiogenesDatabaseCreator
15 /** database versions history */
16 var $versions = array("0.9.9.3", "0.9.10", "0.9.12", "0.9.15", "0.9.16", "0.9.16+0.9.17pre15", "0.9.16+0.9.17pre19", "0.9.16+0.9.17pre21");
19 * Upgrades the database from one version to the next
23 function upgradeDb($newversion)
25 // pre-upgrade master tables
26 $this->info("* Pre-upgrading master tables : diogenes_*");
27 $this->preupgradeMaster($newversion);
30 $res = $this->dbh
->query("select alias from diogenes_site");
31 while (list($alias) = mysql_fetch_row($res))
33 $this->info("* Upgrading barrel '$alias'");
34 $this->upgradeBarrel($alias, $newversion);
36 mysql_free_result($res);
38 // upgrade master tables
39 $this->info("* Upgrading master tables : diogenes_*");
40 $this->upgradeMaster($newversion);
44 /** Upgrades the master (i.e. common) tables before touching the barrel tables.
48 function preupgradeMaster($newversion)
51 case "0.9.16+0.9.17pre15";
52 $this->info(" - adding 'barrel' field to 'diogenes_options' table");
53 $this->dbh
->query("ALTER TABLE `diogenes_option` ADD `barrel` VARCHAR( 16 ) NOT NULL FIRST;");
55 "ALTER TABLE `diogenes_option` DROP PRIMARY KEY ,
56 ADD PRIMARY KEY ( `barrel` , `name` )");
65 /** Upgrade a barrel's tables
70 function upgradeBarrel($alias, $newversion)
72 # $this->info("Processing : {$alias}_menu, {$alias}_page and {$alias}_option");
76 $this->info(" - upgrading : {$alias}_menu");
77 // these field where NULL, change to NOT NULL
78 $this->dbh
->query("ALTER TABLE `{$alias}_menu` CHANGE `link` `link` TEXT NOT NULL");
79 $this->dbh
->query("ALTER TABLE `{$alias}_menu` CHANGE `ordre` `ordre` SMALLINT( 6 ) UNSIGNED NOT NULL");
80 $this->dbh
->query("ALTER TABLE `{$alias}_menu` CHANGE `MIDpere` `MIDpere` SMALLINT( 6 ) UNSIGNED NOT NULL");
82 // break down old 'link' column into 'link' and 'PID'
83 $this->dbh
->query("ALTER TABLE `{$alias}_menu` ADD `PID` SMALLINT( 6 ) UNSIGNED NOT NULL");
84 $res2 = $this->dbh
->query("select MID,link from {$alias}_menu");
85 while (list($MID,$link) = mysql_fetch_row($res2)) {
86 switch (substr($link,0,3)) {
88 $pid = substr($link,3);
89 $this->dbh
->query("UPDATE `{$alias}_menu` SET link='',PID='$pid' WHERE MID='$MID'");
92 $adr = substr($link,3);
93 $this->dbh
->query("UPDATE `{$alias}_menu` SET link='$adr' WHERE MID='$MID'");
97 mysql_free_result($res2);
99 $this->info(" - creating : {$alias}_option");
100 $this->dbh
->query("CREATE TABLE `{$alias}_option` (name VARCHAR( 32 ) NOT NULL, value TEXT NOT NULL, PRIMARY KEY (`name`)) TYPE=MyISAM;");
102 $this->info(" - registering title, description and keywords");
103 $res2 = $this->dbh
->query("select title,description,keywords from diogenes_site where alias='$alias'");
104 list($title,$description,$keywords) = mysql_fetch_row($res2);
105 $this->dbh
->query("replace into `{$alias}_option` set name='title',value='$title'");
106 $this->dbh
->query("replace into `{$alias}_option` set name='description',value='$description'");
107 $this->dbh
->query("replace into `{$alias}_option` set name='keywords',value='$keywords'");
111 $this->info(" - adding 'template' field");
112 $this->dbh
->query("ALTER TABLE `{$alias}_page` ADD `template` VARCHAR(255) NOT NULL");
113 $this->info(" - dropping 'hits' field");
114 $this->dbh
->query("ALTER TABLE `{$alias}_page` DROP `hits`");
115 $this->info(" - replacing 'visible' field by 'status'");
116 $this->dbh
->query("ALTER TABLE `{$alias}_page` DROP `visible`");
117 $this->dbh
->query("ALTER TABLE `{$alias}_page` ADD `status` tinyint(1) unsigned NOT NULL");
118 $this->info(" - modifying 'perms' and 'wperms' to add 'forbidden' access");
119 $this->dbh
->query("ALTER TABLE `{$alias}_page` CHANGE `perms` `perms` ENUM( 'public', 'auth', 'user', 'admin', 'forbidden' ) DEFAULT 'public' NOT NULL");
120 $this->dbh
->query("ALTER TABLE `{$alias}_page` CHANGE `wperms` `wperms` ENUM( 'public', 'auth', 'user', 'admin', 'forbidden' ) DEFAULT 'admin' NOT NULL");
124 $this->info(" - changing page id fields to INT UNSIGNED");
125 $this->dbh
->query("ALTER TABLE `{$alias}_page` CHANGE `PID` `PID` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT");
126 $this->dbh
->query("ALTER TABLE `{$alias}_menu` CHANGE `PID` `PID` INT( 10 ) UNSIGNED NOT NULL default '0'");
127 $this->info(" - changing menu id fields to INT UNSIGNED");
128 $this->dbh
->query("ALTER TABLE `{$alias}_menu` CHANGE `MID` `MID` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT");
129 $this->dbh
->query("ALTER TABLE `{$alias}_menu` CHANGE `MIDpere` `MIDpere` INT( 10 ) UNSIGNED NOT NULL default '0'");
130 $this->dbh
->query("ALTER TABLE `{$alias}_menu` CHANGE `ordre` `ordre` INT( 10 ) UNSIGNED NOT NULL default '0'");
133 case "0.9.16+0.9.17pre15":
134 $this->info(" - merging '{$alias}_option' into 'diogenes_options'");
135 $res = $this->dbh
->query("select name,value from `{$alias}_option`");
136 while (list($o_name,$o_value) = mysql_fetch_row($res))
138 $this->dbh
->query("insert into `diogenes_option` set barrel='$alias',name='$o_name',value='".addslashes($o_value)."'");
140 mysql_free_result($res);
141 $this->info(" - dropping '{$alias}_option'");
142 $this->dbh
->query("drop table `{$alias}_option`");
144 $this->info(" - adding 'parent' field to `{$alias}_page`");
145 $this->dbh
->query("ALTER TABLE `{$alias}_page` ADD `parent` INT( 10 ) UNSIGNED NOT NULL default '0' AFTER `PID`");
146 $res = $this->dbh
->query("select PID from `{$alias}_page` where location=''");
147 list($homepage) = mysql_fetch_row($res);
148 $this->dbh
->query("update `{$alias}_page` set parent='$homepage' where location!=''");
150 $this->info(" - ordering `{$alias}_page` entries by `location`");
151 $this->dbh
->query("ALTER TABLE `{$alias}_page` CHANGE `location` `location` VARCHAR( 255 ) NOT NULL");
152 $this->dbh
->query("ALTER TABLE `{$alias}_page` ORDER BY `location`");
156 $this->info(" - no changes needed.");
162 /** Upgrades the master (i.e. common) tables after the barrel tables have been updated.
166 function upgradeMaster($newversion)
168 // upgrade master tables
169 switch($newversion) {
171 $this->info(" - dropping fields : title, description, keywords");
172 $this->dbh
->query("ALTER TABLE `diogenes_site` DROP `title`");
173 $this->dbh
->query("ALTER TABLE `diogenes_site` DROP `description`");
174 $this->dbh
->query("ALTER TABLE `diogenes_site` DROP `keywords`");
178 $this->info(" - adding field : email");
179 $this->dbh
->query("ALTER TABLE `diogenes_auth` ADD `email` VARCHAR( 255 ) NOT NULL");
183 $this->info(" - changing user id fields to INT UNSIGNED");
184 $this->dbh
->query("ALTER TABLE `diogenes_auth` CHANGE `user_id` `user_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT");
185 $this->dbh
->query("ALTER TABLE `diogenes_logsessions` CHANGE `uid` `uid` INT(10) UNSIGNED NOT NULL default '0'");
186 $this->dbh
->query("ALTER TABLE `diogenes_logsessions` CHANGE `suid` `suid` INT(10) UNSIGNED NOT NULL default '0'");
187 $this->dbh
->query("ALTER TABLE `diogenes_perm` CHANGE `uid` `uid` INT(10) UNSIGNED NOT NULL default '0'");
190 case "0.9.16+0.9.17pre15":
191 $this->info(" - creating 'diogenes_plugin' table");
193 "CREATE TABLE `diogenes_plugin` (
194 `plugin` varchar(32) NOT NULL default '',
195 `barrel` varchar(16) NOT NULL default '',
196 `page` int(10) unsigned NOT NULL default '0',
197 `pos` int(10) unsigned NOT NULL default '0',
198 `params` text NOT NULL,
199 PRIMARY KEY (`plugin`,`barrel`,`page`),
204 case "0.9.16+0.9.17pre19":
205 $this->info(" - changing id of `diogenes_logsessions` to INT(10) UNSIGNED");
206 $this->dbh
->query("ALTER TABLE `diogenes_logsessions` CHANGE `id` `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT");
209 case "0.9.16+0.9.17pre21":
210 $this->dbh
->query("INSERT INTO diogenes_logactions VALUES (13, 'barrel_options', 'the barrel options were updated');");
211 $this->dbh
->query("INSERT INTO diogenes_logactions VALUES (14, 'barrel_plugins', 'the barrel plugins were modified');");
212 $this->dbh
->query("INSERT INTO diogenes_logactions VALUES (15, 'page_props', 'the page properties were updated');");
213 $this->dbh
->query("INSERT INTO diogenes_logactions VALUES (16, 'page_plugins', 'the page plugins were modified');");
217 $this->info(" - no changes needed.");
226 $creator = new DiogenesDbInit("diogenes_option");
227 $creator->parseOptions($argv, "diogenes", "localhost", "diogenes", "");