Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
replace_url.php
1 <?php
25 error_reporting(E_ALL);
26 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
27 
28 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
29 if (empty($SYSTEM_ROOT)) {
30  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
31  exit();
32 }
33 
34 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
35  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
36  exit();
37 }
38 
39 require_once $SYSTEM_ROOT.'/core/include/init.inc';
40 
41 // ask for the root password for the system
42 echo 'Enter the root password for "'.SQ_CONF_SYSTEM_NAME.'": ';
43 system('stty -echo');
44 $root_password = rtrim(fgets(STDIN, 4094));
45 system('stty echo');
46 
47 // check that the correct root password was entered
48 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
49 if (!$root_user->comparePassword($root_password)) {
50  echo "ERROR: The root password entered was incorrect\n";
51  exit();
52 }
53 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
54  trigger_error("Failed logging in as root user\n", E_USER_ERROR);
55 }
56 
57 echo "\n";
58 
59 $db_type = MatrixDAL::getDbType();
60 
61 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
62 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
63 
64 $sql = 'SELECT urlid, url, assetid FROM sq_ast_url';
65 $root_urls = MatrixDAL::executeSqlGroupedAssoc($sql);
66 ksort($root_urls);
67 
68 foreach ($root_urls as $urlid => $url) {
69  echo $urlid.'. '.$root_urls[$urlid][0]['url']."\n";
70 }
71 
72 echo "\n";
73 
74 $chosen_url = -1;
75 
76 while (!in_array($chosen_url, array_keys($root_urls))) {
77  $chosen_url = get_line('Please enter the urlid of the url to change: ');
78  if (!in_array($chosen_url, array_keys($root_urls))) {
79  echo 'Invalid urlid!'."\n";
80  }
81 }
82 
83 $from_url = $root_urls[$chosen_url][0]['url'];
84 $from_urlid = $chosen_url;
85 $from_site_assetid = $root_urls[$chosen_url][0]['assetid'];
86 $to_url = get_line('Please enter the url to change to: ');
87 
88 $root_ok = FALSE;
89 while (!$root_ok) {
90  $system_root_urls = explode("\n", trim(SQ_CONF_SYSTEM_ROOT_URLS));
91  $matching_from_roots = Array();
92  $matching_roots = Array();
93 
94  // We are matching system root URLs for two reasons:
95  // - Finding URLs that __data paths need to be changed to, and
96  // - Alerting the user that their new URL does not match a system root URL
97  // (which is a problem and should be stopped before the query runs)
98  foreach ($system_root_urls as $root_url) {
99  if (substr($to_url.'/', 0, strlen($root_url) + 1) == $root_url.'/') {
100  $matching_roots[] = $root_url;
101  }
102 
103  if (substr($from_url.'/', 0, strlen($root_url) + 1) == $root_url.'/') {
104  $matching_from_roots[] = $root_url;
105  }
106  }
107 
108  if (empty($matching_roots)) {
109  echo 'The new URL entered, "'.$to_url.'", is not based upon an existing System Root URL.'."\n";
110  $to_url = get_line('Please re-enter the new URL: ');
111  } else {
112  $root_ok = TRUE;
113  }
114 }//end while root not OK
115 
116 $confirm = null;
117 while ($confirm != 'y' && $confirm != 'n') {
118  $confirm = strtolower(get_line('Change '.$from_url .' to '.$to_url.' (y/n)? : '));
119  if ($confirm != 'y' && $confirm != 'n') {
120  echo 'Please answer y or n'."\n";
121  } else if ($confirm == 'n') {
122  exit();
123  }
124 }
125 
126 // update any urls that use this url in the lookup and lookup value tables
127 foreach (Array('sq_ast_lookup_value', 'sq_ast_lookup') as $tablename) {
128 
129  $sql = 'UPDATE
130  '.$tablename.'
131  SET
132  url = :to_url || SUBSTR(url, :from_url_length + 1)
133  WHERE
134  url LIKE :from_url_wildcard';
135 
136  if ($tablename == 'sq_ast_lookup') {
137  $sql .= ' AND root_urlid = :from_urlid';
138  } else if ($tablename == 'sq_ast_lookup_value') {
139  $sql .= ' AND url IN (
140  SELECT
141  l.url
142  FROM
143  sq_ast_lookup l
144  INNER JOIN
145  sq_ast_lookup_value v ON ((l.url = v.url) OR (l.url || \'/\' = v.url))
146  WHERE
147  l.root_urlid = :from_urlid
148  )';
149  }
150 
151  $query = MatrixDAL::preparePdoQuery($sql);
152  MatrixDAL::bindValueToPdo($query, 'to_url', $to_url);
153  MatrixDAL::bindValueToPdo($query, 'from_url_wildcard', $from_url.'%');
154  MatrixDAL::bindValueToPdo($query, 'from_url_length', strlen($from_url));
155  MatrixDAL::bindValueToPdo($query, 'from_urlid', $from_urlid);
156  MatrixDAL::execPdoQuery($query);
157 
158 }//end foreach table
159 
160 
161 // update the root url in the asset url table to the new url
162 $sql = 'UPDATE
163  sq_ast_url
164  SET
165  url = :url
166  WHERE
167  urlid = :urlid';
168 
169 $query = MatrixDAL::preparePdoQuery($sql);
170 MatrixDAL::bindValueToPdo($query, 'url', $to_url);
171 MatrixDAL::bindValueToPdo($query, 'urlid', $from_urlid);
173 
174 // If there is a static root URL, there is nothing to change for __data URLs.
175 // Otherwise, try and update them to the closest root URL we can find
176 if (trim(SQ_CONF_STATIC_ROOT_URL) == '') {
177  // First, work out the roots that have changed - if some have not changed
178  // there is no point to be processing them
179  $common_roots = array_intersect($matching_roots, $matching_from_roots);
180  $matching_roots = array_diff($matching_roots, $common_roots);
181  $matching_from_roots = array_diff($matching_from_roots, $common_roots);
182  $file_children = $GLOBALS['SQ_SYSTEM']->am->getChildren($from_site_assetid, 'file', FALSE);
183 
184  $limit_clause = (strpos($db_type, 'oci') !== FALSE) ? 'AND ROWNUM = 1' : 'LIMIT 1';
185 
186  // Update the roots up to the number that we can update
187  for ($x = 0; $x < min(count($matching_roots), count($matching_from_roots)); $x++) {
188  // Change the lookup values first
189  $sql = 'UPDATE sq_ast_lookup_value SET url = :to_url || SUBSTR(url, :from_url_length + 1) WHERE url IN (SELECT url FROM sq_ast_lookup WHERE assetid IN (SELECT minorid FROM sq_ast_lnk WHERE linkid IN (SELECT linkid FROM sq_ast_lnk_tree t1 WHERE treeid LIKE (SELECT treeid || \'_%\' FROM sq_ast_lnk_tree t2 WHERE linkid IN (SELECT linkid FROM sq_ast_lnk WHERE minorid = :site_assetid) '.$limit_clause.')))
190  AND url LIKE :from_url || \'/__data/%\')';
191 
192  $query = MatrixDAL::preparePdoQuery($sql);
193  MatrixDAL::bindValueToPdo($query, 'site_assetid', $from_site_assetid);
194  MatrixDAL::bindValueToPdo($query, 'from_url', $matching_from_roots[$x]);
195  MatrixDAL::bindValueToPdo($query, 'from_url_length', strlen($matching_from_roots[$x]));
196  MatrixDAL::bindValueToPdo($query, 'to_url', $matching_roots[$x]);
197  MatrixDAL::execPdoQuery($query);
198 
199  $sql = 'UPDATE sq_ast_lookup SET url = :to_url || SUBSTR(url, :from_url_length + 1) WHERE assetid IN (SELECT minorid FROM sq_ast_lnk WHERE linkid IN (SELECT linkid FROM sq_ast_lnk_tree t1 WHERE treeid LIKE (SELECT treeid || \'_%\' FROM sq_ast_lnk_tree t2 WHERE linkid IN (SELECT linkid FROM sq_ast_lnk WHERE minorid = :site_assetid) '.$limit_clause.')))
200  AND url LIKE :from_url || \'/__data/%\'';
201  $query = MatrixDAL::preparePdoQuery($sql);
202  MatrixDAL::bindValueToPdo($query, 'site_assetid', $from_site_assetid);
203  MatrixDAL::bindValueToPdo($query, 'from_url', $matching_from_roots[$x]);
204  MatrixDAL::bindValueToPdo($query, 'from_url_length', strlen($matching_from_roots[$x]));
205  MatrixDAL::bindValueToPdo($query, 'to_url', $matching_roots[$x]);
206  MatrixDAL::execPdoQuery($query);
207  }
208 
209  // We have new URLs
210  // Going to pass on adding new lookups to this situation, because lookups are usually meant
211  // to be URL-based. How can we tell whether a lookup value is meant to be per-asset or per-URL?
212  for (; $x < count($matching_roots); $x++) {
213  $sql = 'INSERT INTO sq_ast_lookup (url, root_urlid, http, https, assetid) SELECT DISTINCT :to_url || SUBSTR(url, STRPOS(url, \'/__data/\')), 0, http, https, assetid FROM sq_ast_lookup WHERE assetid IN (SELECT minorid FROM sq_ast_lnk WHERE linkid IN (SELECT linkid FROM sq_ast_lnk_tree t1 WHERE treeid LIKE (SELECT treeid || \'_%\' FROM sq_ast_lnk_tree t2 WHERE linkid IN (SELECT linkid FROM sq_ast_lnk WHERE minorid = :site_assetid) '.$limit_clause.')))';
214  if (MatrixDAL::getDbType() == 'oci') {
215  // String position function is called INSTR() in Oracle
216  $sql = str_replace('STRPOS(', 'INSTR(', $sql);
217  }
218  $query = MatrixDAL::preparePdoQuery($sql);
219  MatrixDAL::bindValueToPdo($query, 'site_assetid', $from_site_assetid);
220  MatrixDAL::bindValueToPdo($query, 'to_url', $matching_roots[$x]);
221  MatrixDAL::execPdoQuery($query);
222  }//end for - remaining to URLs
223 
224  // More URLs beforehand than what we have now = have to delete the rest
225  for (; $x < count($matching_from_roots); $x++) {
226  // Delete the lookup values first
227  $sql = 'DELETE FROM sq_ast_lookup_value WHERE url IN (SELECT url FROM sq_ast_lookup WHERE assetid IN (SELECT minorid FROM sq_ast_lnk WHERE linkid IN (SELECT linkid FROM sq_ast_lnk_tree t1 WHERE treeid LIKE (SELECT treeid || \'_%\' FROM sq_ast_lnk_tree t2 WHERE linkid IN (SELECT linkid FROM sq_ast_lnk WHERE minorid = :site_assetid) '.$limit_clause.')))
228  AND url LIKE :from_url || \'/__data/%\')';
229  $query = MatrixDAL::preparePdoQuery($sql);
230  MatrixDAL::bindValueToPdo($query, 'site_assetid', $from_site_assetid);
231  MatrixDAL::bindValueToPdo($query, 'from_url', $matching_from_roots[$x]);
232  MatrixDAL::execPdoQuery($query);
233 
234  $sql = 'DELETE FROM sq_ast_lookup WHERE assetid IN (SELECT minorid FROM sq_ast_lnk WHERE linkid IN (SELECT linkid FROM sq_ast_lnk_tree t1 WHERE treeid LIKE (SELECT treeid || \'_%\' FROM sq_ast_lnk_tree t2 WHERE linkid IN (SELECT linkid FROM sq_ast_lnk WHERE minorid = :site_assetid) '.$limit_clause.')))
235  AND url LIKE :from_url || \'/__data/%\'';
236  $query = MatrixDAL::preparePdoQuery($sql);
237  MatrixDAL::bindValueToPdo($query, 'site_assetid', $from_site_assetid);
238  MatrixDAL::bindValueToPdo($query, 'from_url', $matching_from_roots[$x]);
239  MatrixDAL::execPdoQuery($query);
240  }//end for - remaining from URLs
241 
242 }//end if - no static root set
243 
244 bam('LOOKUPS CHANGED FROM '.$from_url.' TO '.$to_url);
245 
246 $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
247 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
248 
257 function get_line($prompt='')
258 {
259  echo $prompt;
260  // now get their entry and remove the trailing new line
261  return rtrim(fgets(STDIN, 4096));
262 
263 }//end get_line()
264 
265 ?>