40 public static function cliInterfaceAddUrl()
43 pre_echo(
"To use this script, you would need the following information\n
47 An existing URL of the site\n
48 Make sure you have all you information you need before Proceeding\n");
51 while ($http !=
'0' && $http !=
'1') {
52 $http = get_line(
'Please specify whether this URL use HTTP protocol or not (1 or 0): ');
56 while ($https !=
'0' && $https !=
'1') {
57 $https = get_line(
'Please specify whether this URL use HTTPS protocol or not (1 or 0): ');
60 if ($http != 1 && $https != 1) {
61 echo
"Please select either HTTP or HTTPS as protocol\n";
67 while (is_null($assetid)) {
68 $assetid = get_line(
'Enter the Site Assetid to apply the URL to: ');
69 assert_valid_assetid($assetid);
71 $asset = self::checkAssetTypes($assetid);
73 echo
"Asset must be either a site, design folder, media folder, or user folder\n";
81 require_once $SYSTEM_ROOT.
'/data/private/conf/main.inc';
83 $update_file_public_live_assets = FALSE;
85 while (is_null($new_url)) {
86 $new_url = get_line(
'Enter the New URL:');
88 if (!(self::checkNewUrlExists($new_url))) {
89 echo
"The new URL : ".$new_url.
" is already exist.\n";
94 if (!(self::checkNewUrlInSystemRootUrls($SYSTEM_ROOT, $new_url, $update_file_public_live_assets))) {
95 echo
"The provided URL is not based upon an existing System Root URL\n";
96 echo
"Existing System Root URLs are : \n".SQ_CONF_SYSTEM_ROOT_URLS.
"\n";
103 $existing_url = NULL;
104 while (is_null($existing_url)) {
105 $existing_url = get_line(
'Enter the existing URL: ');
108 if ($new_url == $existing_url) {
109 echo
"Does not allow to alter existing url\n";
113 $site_urls = Array();
114 $urls = $asset->getURLs();
115 foreach ($urls as $url_info) {
116 $site_urls[] = $url_info[
'url'];
118 if (!in_array($existing_url, $site_urls)) {
119 echo
"The existing URL does not belong to the site with id: $assetid \n";
124 $existing_urlid = self::checkExistingUrlExists($existing_url);
125 if (!$existing_urlid) {
126 echo
"The existing URL : ".$existing_url.
" does not exist\n";
134 'new_url' => $new_url,
135 'existing_url' => $existing_url,
136 'siteid' => $assetid,
137 'update_file_public_live_assets' => $update_file_public_live_assets,
138 'existing_urlid' => $existing_urlid[0][
'urlid'],
150 public static function addUrl($http, $https, $new_url, $existing_url, $assetid, $update_file_public_live_assets, $existing_urlid, $SYSTEM_ROOT, $quiet=FALSE)
153 $new_url = strip_url($new_url);
155 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
158 $static_root_url = SQ_CONF_STATIC_ROOT_URL;
159 if (!empty($static_root_url)) {
160 $update_file_public_live_assets = FALSE;
164 if ($update_file_public_live_assets) {
167 $asset_types_list_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeHierarchy(
'file');
169 $asset_types_list = Array();
170 self::getTypeListRecursive($asset_types_list_info, $asset_types_list);
172 $asset_types_list[] =
'file';
174 $children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($assetid, $asset_types_list, TRUE);
178 foreach ($children as $id => $content) {
180 unset($children[$id]);
184 $children = array_keys($children);
186 $children_to_update = Array();
187 foreach ($children as $child_id) {
188 $child_asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($child_id);
189 if ($child_asset->usePublicPath()) {
198 $sql_update_sq_ast_url =
'INSERT INTO sq_ast_url (http, https, assetid, url, urlid) values (
199 :http, :https, :assetid, :new_url, :new_urlid)';
205 $sql_update_sq_ast_lookup_value =
207 'INSERT INTO sq_ast_lookup_value (url, name, value, depth)
208 SELECT replace(url, :existing_url, :new_url), name, value, depth
209 FROM sq_ast_lookup_value v
212 (select k. minorid from sq_ast_lnk k where k.value = \'paint_layout::system::frontend\' or k.value = \'override::paint_layout::system::frontend\'
213 and k.majorid in (select kk.majorid from sq_ast_lnk kk
214 where kk.majorid = (select l.assetid from sq_ast_lookup l where l.url = v.url OR l.url || \'/\' = v.url) and kk.minorid = v.value))
215 OR v.name like \'design%\' or v.name like \'override::design%\')
216 AND rtrim(url, \'/\') in (SELECT url FROM sq_ast_lookup WHERE root_urlid = :existing_urlid)';
222 $sql_update_sq_ast_lookup =
'INSERT INTO sq_ast_lookup (http, https, assetid, url, root_urlid)
223 SELECT :http, :https, assetid, replace(url, :existing_url, :new_url), :new_urlid
224 FROM sq_ast_lookup WHERE url like :existing_url_wildcard AND root_urlid = :existing_urlid';
226 $sql_update_sq_ast_lookup_public =
'UPDATE sq_ast_lookup set root_urlid = 0 WHERE url like :new_url_wildcard AND url like :data_url AND root_urlid = :new_urlid';
257 pre_echo($sql_update_sq_ast_url);
258 pre_echo($sql_update_sq_ast_lookup_value);
259 pre_echo($sql_update_sq_ast_lookup);
270 if (!defined(SQ_CONF_SYSTEM_ROOT_URLS)) {
271 require_once $SYSTEM_ROOT.
'/data/private/conf/main.inc';
273 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
274 if ($update_file_public_live_assets) {
278 foreach ($root_urls as $url) {
279 if (strpos($existing_url, $url) !== FALSE) {
280 $relative_root = $url;
282 if ($existing_url == $url) {
283 $absolute_root = $url;
287 $existing_url_public = (empty($absolute_root)) ? $relative_root : $absolute_root;
290 $absolute_new_root =
'';
291 $relative_new_root =
'';
292 foreach ($root_urls as $url) {
293 if (strpos($new_url, $url) !== FALSE) {
294 $relative_new_root = $url;
296 if ($new_url == $url) {
297 $absolute_new_root = $url;
301 $new_url_public = (empty($absolute_new_root)) ? $relative_new_root : $absolute_new_root;
304 if (!empty($children_to_update)) {
305 $in_clauses = Array();
306 foreach (array_chunk($children_to_update, 999) as $chunk) {
307 $in_clauses[] =
' assetid IN ('.implode(
', ', $chunk).
')';
311 $num_children_to_update = count($children_to_update);
312 foreach ($in_clauses as $condition) {
313 $sql_update_sq_ast_lookup_public_file =
'INSERT INTO sq_ast_lookup (http, https, assetid, url, root_urlid)
314 SELECT :http, :https, assetid, replace(url, :existing_url, :new_url), 0
315 FROM sq_ast_lookup WHERE root_urlid = 0 AND '. $condition.
' AND url like :existing_url_wildcard';
316 $added_condition =
' AND replace(url, :existing_url_1, :new_url_1) NOT IN (
317 SELECT url from sq_ast_lookup WHERE root_urlid = 0 AND '. $condition.
' AND url like replace(url,:existing_url_2, :new_url_2))';
319 $sql_update_sq_ast_lookup_public_file .= $added_condition;
324 if ($existing_url_public[strlen($existing_url_public)-1] !=
'/') {
325 $existing_url_public .=
'/';
328 if ($new_url_public[strlen($new_url_public)-1] !=
'/') {
329 $new_url_public .=
'/';
332 if (!$quiet) pre_echo($sql_update_sq_ast_lookup_public_file);
345 echo
"\n Finished Updating ".(($count*1000 > $num_children_to_update) ? $num_children_to_update : $count*1000) .
" files out of ".$num_children_to_update.
"\n";
353 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
362 public static function cliInterfaceRemoveUrl()
367 pre_echo(
"!Important: Please make sure that you know how to use this script, and remember to back up the database before proceeding\n
368 (Press Ctrl+C to terminate the script)\n");
371 while (is_null($remove_url)) {
372 $remove_url = get_line(
'Enter the URL to be removed: ');
375 $remove_url_info = self::checkExistingUrlExists($remove_url);
376 if ($remove_url_info === FALSE) {
377 echo
'The provided URL : '.$remove_url.
' does not exist';
381 $remove_urlid = $remove_url_info[0][
'urlid'];
382 $remove_assetid = $remove_url_info[0][
'assetid'];
385 'remove_urlid' => $remove_urlid,
386 'remove_assetid' => $remove_assetid,
387 'remove_url' => $remove_url,
398 public static function removeUrl($remove_urlid, $remove_assetid, $remove_url, $quiet=FALSE, $SYSTEM_ROOT)
402 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
405 $sql_get_sub_url_root_urlid =
'SELECT url FROM sq_ast_url WHERE
406 url NOT LIKE :remove_url AND
407 url LIKE :remove_url_wildcard';
415 $asset_types_list_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeHierarchy(
'file');
417 $asset_types_list = Array();
418 self::getTypeListRecursive($asset_types_list_info, $asset_types_list);
420 $asset_types_list[] =
'file';
422 $children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($remove_assetid, $asset_types_list, TRUE);
425 foreach ($children as $id => $content) {
427 unset($children[$id]);
431 $children = array_keys($children);
433 $children_to_update = Array();
435 foreach ($children as $child_id) {
436 $child_asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($child_id);
437 if ($child_asset->usePublicPath()) {
438 $get_count_links_query =
'SELECT COUNT(*) FROM sq_ast_lnk WHERE minorid = '.MatrixDAL::quote($child_id);
442 $site_parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($child_id,
'site', FALSE);
444 if (!(count($site_parents) > 1)) {
456 if (!empty($children_to_update)) {
457 $in_clauses = Array();
458 foreach (array_chunk($children_to_update, 999) as $chunk) {
459 $in_clauses[] =
' assetid IN ('.implode(
', ', $chunk).
')';
462 require_once $SYSTEM_ROOT.
'/data/private/conf/main.inc';
463 $root_urls = Array();
464 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
467 $absolute_root_remove =
'';
468 $relative_root_remove =
'';
469 foreach ($root_urls as $url) {
470 if (strpos($remove_url, $url) !== FALSE) {
471 $relative_root_remove = $url;
473 if ($remove_url == $url) {
474 $absolute_root_remove = $url;
479 $remove_url_public = (empty($absolute_root_remove)) ? $relative_root_remove : $absolute_root_remove;
482 $num_children_to_update = count($children_to_update);
483 foreach ($in_clauses as $condition) {
485 $sql_update_sq_ast_lookup_public =
'DELETE FROM sq_ast_lookup WHERE root_urlid = 0 AND url LIKE :remove_url_wildcard AND url LIKE :data_url';
486 $sql_update_sq_ast_lookup_public .=
' AND '.$condition;
488 foreach ($avoid_urls as $index => $data) {
489 $sql_update_sq_ast_lookup_public .=
' AND url NOT LIKE ' .
MatrixDAL::quote($data[
'url'].
'%');
492 if (!$quiet) pre_echo($sql_update_sq_ast_lookup_public);
499 echo
"\n Finished deleting ".(($count*1000 > $num_children_to_update) ? $num_children_to_update : $count*1000) .
" files out of ".$num_children_to_update;
506 $sql_update_sq_ast_lookup_value =
'DELETE FROM sq_ast_lookup_value WHERE url like '.MatrixDAL::quote($remove_url.
'%');
507 foreach ($avoid_urls as $index => $data) {
508 $sql_update_sq_ast_lookup_value .=
' AND url NOT LIKE ' .
MatrixDAL::quote($data[
'url'].
'%');
511 $sql_update_sq_ast_lookup =
'DELETE FROM sq_ast_lookup WHERE root_urlid = '.MatrixDAL::quote($remove_urlid).
' AND url like '.
MatrixDAL::quote($remove_url.
'%').
'';
513 foreach ($avoid_urls as $index => $data) {
514 $sql_update_sq_ast_lookup .=
' AND url NOT LIKE ' .
MatrixDAL::quote($data[
'url'].
'%');
517 $sql_update_sq_ast_url =
'DELETE FROM sq_ast_url WHERE urlid = '.MatrixDAL::quote($remove_urlid).
' AND url like '.
MatrixDAL::quote($remove_url).
'';
528 pre_echo($sql_update_sq_ast_lookup_value);
529 pre_echo($sql_update_sq_ast_lookup);
530 pre_echo($sql_update_sq_ast_url);
533 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
542 private static function checkAssetTypes($assetid)
544 $allow_asset_types = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeHierarchy(
'web_folder',
'system'));
545 $allow_asset_types[] =
'site';
546 $asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
547 if (!in_array($asset->type(), $allow_asset_types)) {
561 $sql_check_existing_url =
'SELECT urlid, assetid FROM sq_ast_url WHERE url LIKE :existing_url';
566 if (empty($existing_urlid)) {
569 return $existing_urlid;
578 private static function checkNewUrlInSystemRootUrls($system_root, $new_url, &$update_file_public_live_assets)
580 require_once $system_root.
'/data/private/conf/main.inc';
581 $update_file_public_live_assets = FALSE;
582 $root_urls = Array();
584 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
585 $in_root_urls = FALSE;
586 $root_urls_string =
'';
587 foreach ($root_urls as $root_url) {
588 $root_urls_string .= $root_url.
"\n";
589 if (strpos($new_url, $root_url) === 0) {
590 $in_root_urls = TRUE;
593 if (($new_url == $root_url) || (strpos($new_url, $root_url) === 0 && strlen($new_url) > strlen($root_url))){
594 $update_file_public_live_assets = TRUE;
600 if (!$in_root_urls) {
613 private static function checkNewUrlExists($new_url)
616 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAssetFromURL(NULL, strip_url($new_url), TRUE, TRUE);
617 if($asset != NULL)
return FALSE;
619 $sql_check_new_url =
'SELECT url FROM sq_ast_url WHERE url = :new_url';
624 if (!empty($new_url_check)) {
636 private static function getTypeListRecursive($asset_types_list_info, &$asset_types_list)
638 foreach ($asset_types_list_info as $asset_type => $type_info)
640 $asset_types_list[] = $asset_type;
641 if (isset($type_info[
'subs'])) {
642 self::getTypeListRecursive($type_info[
'subs'], $asset_types_list);