Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
rename_type_code.php
1 <?php
26 error_reporting(E_ALL);
27 
28 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
29 
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";
34  exit(1);
35 }
36 
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";
40  exit();
41 }
42 
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";
45  exit();
46 }
47 
48 require_once $SYSTEM_ROOT.'/core/include/init.inc';
49 
50 $am = &$GLOBALS['SQ_SYSTEM']->am;
51 
52 if (isset($_SERVER['argv'][2])) $original_type_code = $_SERVER['argv'][2];
53 
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);
56 }
57 
58 if (isset($_SERVER['argv'][3])) $new_type_code = $_SERVER['argv'][3];
59 
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);
62 }
63 
64 $db_chng = Array(
65  'type_code' => Array(
66  'sq_ast',
67  'sq_rb_ast',
68  'sq_ast_attr',
69  'sq_ast_edit_access',
70  'sq_rb_ast_edit_access',
71  'sq_ast_typ',
72  'sq_ast_typ_inhd',
73  ),
74  'inhd_type_code' => Array(
75  'sq_ast_typ_inhd',
76  ),
77  'path' => Array(
78  'sq_file_vers_file',
79  ),
80  'asset_type' => Array(
81  'sq_trig_hash',
82  ),
83  'parent_type' => Array(
84  'sq_trig_hash',
85  ),
86  );
87 
88 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
89 
90 $queries = Array();
91 
92 foreach ($db_chng as $col => $tables) {
93  if ($col == 'path') {
94  foreach ($tables as $table) {
95  $result = MatrixDAL::executeSqlAll('SELECT fileid, '.$col.' FROM '.$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';
102  $bind_vars = Array(
103  'new_value' => preg_replace('|/'.$original_type_code.'/|', '/'.$new_type_code.'/', $file_path),
104  'fileid' => $fileid,
105  );
106  $queries[] = Array(
107  'sql' => $sql,
108  'bind_vars' => $bind_vars,
109  );
110  }
111  }
112  }
113  } else {
114  foreach ($tables as $table) {
115  $sql .= 'UPDATE '.$table.'
116  SET '.$col.' = :new_type_code
117  WHERE '.$col.' = :type_code';
118  $bind_vars = Array(
119  'new_type_code' => $new_type_code,
120  'type_code' => $original_type_code,
121  );
122  $queries[] = Array(
123  'sql' => $sql,
124  'bind_vars' => $bind_vars,
125  );
126  }
127  }
128 }
129 
130 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
131 
132 try {
133  foreach ($queries as $query_el) {
134  $query = MatrixDAL::preparePdoQuery($query_el['sql']);
135  foreach ($query_el['bind_vars'] as $bind_var => $bind_value) {
136  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
137  }
138  MatrixDAL::execPdoQuery($query);
139  unset($query);
140  }
141 
142  // all good
143  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
144  echo "\nDatabase changes successful.\n";
145 } catch (DALException $e) {
146  // no good
147  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
148 }
149 
150 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
151 
152 $dir_chng = Array(
153  'data/public/asset_types',
154  'data/public/assets',
155  'data/file_repository/assets',
156  'data/private/asset_types',
157  'data/private/assets',
158  );
159 
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;
164  }
165  }
166 }
167 
168 echo "\n";
169 
170 require_once $SYSTEM_ROOT.'/install/install.inc';
171 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
172 
173 cache_asset_types();
174 
175 
176 
177 ?>