Last active
August 29, 2015 14:07
-
-
Save hypeJunction/b7584e95bb74c64f0886 to your computer and use it in GitHub Desktop.
Update Elgg bookmarks address hostname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Update bookmarks address host | |
* | |
* @param string $src_url Source url e.g. http://www.old-example.com/ | |
* @param string $target_url Target url e.g. http://subdomain.new-example.com/ | |
* @return boolean | |
*/ | |
function bookmarks_modify_address_host($src_url, $target_url = null) { | |
if (!is_callable('http_build_url')) { | |
elgg_log('PECL pecl_http >= 0.21.0 is required to modify bookmarks addresses', 'ERROR'); | |
return false; | |
} | |
if (!$src_url) { | |
return false; | |
} | |
if (!$target_url) { | |
$target_url = elgg_get_site_url(); | |
} | |
set_time_limit(0); | |
$old_host = parse_url($src_url, PHP_URL_HOST); | |
$new_host = parse_url($target_url, PHP_URL_HOST); | |
if (!$old_host || !$new_host) { | |
elgg_log('Invalid source or target URL supplied', 'ERROR'); | |
return false; | |
} | |
$bookmarks = new ElggBatch('elgg_get_entities', array( | |
'types' => 'object', | |
'subtypes' => 'bookmarks', | |
'limit' => 0 | |
)); | |
$ia = elgg_set_ignore_access(true); | |
$ha = access_get_show_hidden_status(); | |
access_show_hidden_entities(true); | |
foreach ($bookmarks as $bookmark) { | |
$url_components = parse_url($bookmark->address); | |
if ($url_components['host'] == $old_host) { | |
$url_components['host'] = $new_host; | |
$bookmark->address = http_build_query($url_components); | |
} | |
} | |
access_show_hidden_entities($ha); | |
elgg_set_ignore_access($ia); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment