27 error_reporting(E_ALL);
28 ini_set(
'memory_limit',
'-1');
29 if ((php_sapi_name() !=
'cli')) trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
31 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
32 if (empty($SYSTEM_ROOT)) {
33 printUsage(
"ERROR: You need to supply the path to the System Root as the first argument");
36 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
37 printUsage(
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.");
41 $FROM_PARENT_ID = (isset($_SERVER[
'argv'][2])) ? $_SERVER[
'argv'][2] :
'';
42 if (empty($FROM_PARENT_ID)) {
43 printUsage(
'ERROR: You need to provide the from-parent-id as the second argument');
47 $TO_PARENT_ID = (isset($_SERVER[
'argv'][3])) ? $_SERVER[
'argv'][3] :
'';
48 if (empty($TO_PARENT_ID)) {
49 printUsage(
'ERROR: You need to provide the to-parent-id as the third argument');
53 $ASSET_TYPE = (isset($_SERVER[
'argv'][4])) ? $_SERVER[
'argv'][4] :
'';
54 if (empty($ASSET_TYPE)) {
55 printUsage(
'ERROR: You need to enter the asset_type as the fourth argument');
58 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
61 $LINK_TYPE = (isset($_SERVER[
'argv'][5])) ? $_SERVER[
'argv'][5] : SQ_SC_LINK_SIGNIFICANT;
63 $root_user = &$GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
66 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
67 echo
"ERROR: Failed login in as root user\n";
71 moveAssets($FROM_PARENT_ID, $TO_PARENT_ID, $ASSET_TYPE, $LINK_TYPE);
73 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
75 echo
"Finish moving assets!\n";
88 function moveAssets($from_parent_assetid, $to_parent_assetid, $type_code =
'', $link_type = NULL, $max_size = 1000, $chunk_size = 100)
91 $child_assetids = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($from_parent_assetid, $type_code, TRUE, NULL, NULL, NULL, TRUE, 1, 1);
94 $child_assetids = array_keys($child_assetids);
97 $child_assetids = array_slice($child_assetids, 0, $max_size);
100 $child_assetids = array_chunk($child_assetids, $chunk_size);
103 $total_moved_asset = 0;
104 foreach ($child_assetids as $assetid_chunk) {
106 echo
"Moving assets ...\n";
109 moveAssetChunk($assetid_chunk, $from_parent_assetid, $to_parent_assetid, $link_type);
112 $total_moved_asset += count($assetid_chunk);
113 echo
"Total of $total_moved_asset assets have been moved from #$from_parent_assetid to #$to_parent_assetid\n";
129 function moveAssetChunk($assetids, $from_parent_assetid, $to_parent_assetid, $link_type = NULL)
131 $am = $GLOBALS[
'SQ_SYSTEM']->am;
134 foreach ($assetids as $assetid) {
136 $links = $am->getLinkByAsset($assetid, $from_parent_assetid, $link_type, NULL,
'minor', TRUE);
138 foreach ($links as $link) {
139 $assets[$assetid][] = Array(
140 'linkid' => $link[
'linkid'],
141 'link_type' => $link[
'link_type'],
142 'parentid' => $from_parent_assetid,
148 $hh = $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
152 'link_action' =>
'move',
154 'to_parent_assetid' => $to_parent_assetid,
155 'to_parent_pos' => getNextSortOrder($to_parent_assetid),
158 $hh->freestyleHipo(
'hipo_job_create_links', $vars);
169 function getNextSortOrder($parent_assetid)
171 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db');
174 COUNT(*) as count, MAX(sort_order) as max
186 }
catch (Exception $e) {
187 throw new Exception(
"Unable to get the last sort order of the parent asset #$parent_assetid , due to database error: ".$e->getMessage());
190 $next_sort_order = ($row[
'count'] > 0)? max($row[
'count'], $row[
'max']+1) : 0;
192 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
194 return $next_sort_order;
205 function printUsage($error)
208 echo
"This script move assets of a certain type from a parent asset ID to another parent asset ID. The default maximum of 1000 assets can be moved to protect againts the problem of purging too many assets in trash.\n";
209 echo
"Usage: move_assets.php SYSTEM_ROOT FROM_ASSET_ID TO_ASSET_ID ASSET_TYPE [LINK_TYPE]\n\n";