26 error_reporting(E_ALL);
28 if ((php_sapi_name() !=
'cli')) trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
30 if (count($_SERVER[
'argv']) != 4) {
31 echo
"This script needs to be run in the following format:\n\n";
32 echo
"\tphp rename_type_code.php [SYSTEM_ROOT] [old type code] [new type code]\n\n";
33 echo
"\tEg. php scripts/rename_type_code.php . report_broken_links report_broken_links_renamed\n";
37 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
38 if (empty($SYSTEM_ROOT)) {
39 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
43 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
44 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
48 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
50 $am = &$GLOBALS[
'SQ_SYSTEM']->am;
52 if (isset($_SERVER[
'argv'][2])) $original_type_code = $_SERVER[
'argv'][2];
54 if (empty($original_type_code) || !$am->installed($original_type_code)) {
55 trigger_error(
'The type code you are trying to rename is not installed in the system', E_USER_ERROR);
58 if (isset($_SERVER[
'argv'][3])) $new_type_code = $_SERVER[
'argv'][3];
60 if (empty($new_type_code) || $new_type_code == $original_type_code) {
61 trigger_error(
'The new type code is the same as the one you are changing', E_USER_ERROR);
70 'sq_rb_ast_edit_access',
74 'inhd_type_code' => Array(
80 'asset_type' => Array(
83 'parent_type' => Array(
88 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
92 foreach ($db_chng as $col => $tables) {
94 foreach ($tables as $table) {
96 foreach ($result as $result_row) {
97 list($fileid, $file_path) = $result_row;
98 if (preg_match(
'|/'.$original_type_code.
'/|', $file_path)) {
99 $sql .=
'UPDATE '.$table.
'
100 SET '.$col.
' = :new_value
101 WHERE fileid = :fileid';
103 'new_value' => preg_replace(
'|/'.$original_type_code.
'/|',
'/'.$new_type_code.
'/', $file_path),
108 'bind_vars' => $bind_vars,
114 foreach ($tables as $table) {
115 $sql .=
'UPDATE '.$table.
'
116 SET '.$col.
' = :new_type_code
117 WHERE '.$col.
' = :type_code';
119 'new_type_code' => $new_type_code,
120 'type_code' => $original_type_code,
124 'bind_vars' => $bind_vars,
130 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
133 foreach ($queries as $query_el) {
135 foreach ($query_el[
'bind_vars'] as $bind_var => $bind_value) {
143 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
144 echo
"\nDatabase changes successful.\n";
147 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
150 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
153 'data/public/asset_types',
154 'data/public/assets',
155 'data/file_repository/assets',
156 'data/private/asset_types',
157 'data/private/assets',
160 foreach ($dir_chng as $dir) {
161 if (is_dir($SYSTEM_ROOT.
'/'.$dir.
'/'.$original_type_code)) {
162 if (rename($SYSTEM_ROOT.
'/'.$dir.
'/'.$original_type_code, $SYSTEM_ROOT.
'/'.$dir.
'/'.$new_type_code)) {
163 echo
"\n".
'Successfully renamed '.$SYSTEM_ROOT.
'/'.$dir.
'/'.$original_type_code.
' to '.$SYSTEM_ROOT.
'/'.$dir.
'/'.$new_type_code;
170 require_once $SYSTEM_ROOT.
'/install/install.inc';
171 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';