40 echo
"Move assets into dated folders\n\n";
41 echo
"Usage: move_assets_to_dated_folders [system root] --root=[root node asset ID] --type=[asset type code]\n";
42 echo
" --field=[asset date field] --period=[time period] (--folder-link-type=[folder link type])\n";
43 echo
" (--move-asset-status=[move asset status]) (--make-folders-live)\n\n";
44 echo
"REQUIRED PARAMETERS ==========\n";
45 echo
"system root : The Matrix System Root directory\n";
46 echo
"root node asset ID : The asset under which child assets are to be moved\n";
47 echo
"asset type code : The type of assets to be moved\n";
48 echo
"asset date field : The date field determining the destination for each asset - either 'created' or 'published'\n";
49 echo
"time period : A specification of the folder structure to be created - either 'year', 'month' or 'day'\n";
50 echo
" eg; when 'asset date field' is 'created', 'day' will create or re-use a structure of 2008 > 03 > 28 for assets created on 28th March 2008\n\n";
51 echo
"OPTIONAL PARAMETERS ==========\n";
52 echo
"folder link type : The link type for newly-created folders (either 1 or 2). Type 2 links are used by default\n";
53 echo
"move asset status : The status code which must be matched for each asset before moving (eg; set to '16' to move only 'Live' assets)\n";
54 echo
"-make-folders-live : Set the new date folders to 'Live' upon creation. Otherwise they will be 'Under Construction'\n\n";
73 function searchExistingDatedFolder($parent_id, $create_timestamp, $period, $folder_link_type, $make_folders_live=FALSE)
75 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
78 $year = date(
'Y', $create_timestamp);
79 $month = str_pad(date(
'm', $create_timestamp), 2,
'0', STR_PAD_LEFT);
80 $day = str_pad(date(
'd', $create_timestamp), 2,
'0', STR_PAD_LEFT);
83 $matching_year_folder_id = 0;
84 $matching_month_folder_id = 0;
85 $matching_day_folder_id = 0;
86 $matching_folder_id = 0;
88 $matching_year_folders = searchExistingAsset($parent_id, $year,
'folder');
90 $num_found_assets = count($matching_year_folders);
92 if ($num_found_assets > 1) {
93 echo
"\n- FAILED - found ".$num_found_assets.
' year folders for '.$year.
"\n";
97 if ($num_found_assets == 1) {
98 $matching_year_folder_id = $matching_year_folders[0];
101 if ($matching_year_folder_id == 0) {
102 echo
"\n- Creating Year Folder ".$year.
'... ';
103 $matching_year_folder_id = createAsset(
'folder', $year, $parent_id, $folder_link_type);
104 echo
'asset #'.$matching_year_folder_id.
"\n";
105 if ($make_folders_live) {
106 setAssetStatus($matching_year_folder_id, SQ_STATUS_LIVE);
110 $matching_folder_id = $matching_year_folder_id;
113 if (($matching_year_folder_id > 0) && ($period !=
'year')) {
114 $matching_month_folders = searchExistingAsset($matching_year_folder_id, $month,
'folder');
116 $num_found_assets = count($matching_month_folders);
118 if ($num_found_assets > 1) {
119 echo
"\n- FAILED - found ".$num_found_assets.
' month folders for year/month '.$year.
'/'.$month.
"\n";
123 if ($num_found_assets == 1) {
124 $matching_month_folder_id = $matching_month_folders[0];
127 if ($matching_month_folder_id == 0) {
128 echo
"\n- Creating Month Folder ".$month.
' under Year '.$year.
'... ';
129 $matching_month_folder_id = createAsset(
'folder', $month, $matching_year_folder_id, $folder_link_type);
130 echo
'asset #'.$matching_month_folder_id.
"\n";
131 if ($make_folders_live) {
132 setAssetStatus($matching_month_folder_id, SQ_STATUS_LIVE);
136 $matching_folder_id = $matching_month_folder_id;
140 if (($matching_month_folder_id > 0) && ($period ==
'day')) {
141 $matching_day_folders = searchExistingAsset($matching_month_folder_id, $day,
'folder');
143 $num_found_assets = count($matching_day_folders);
145 if ($num_found_assets > 1) {
146 echo
"\n- FAILED - found ".$num_found_assets.
' day folders for year/month/day '.$year.
'/'.$month.
'/'.$day.
"\n";
150 if ($num_found_assets == 1) {
151 $matching_day_folder_id = $matching_day_folders[0];
154 if ($matching_day_folder_id == 0) {
155 echo
"\n- Creating Day Folder ".$day.
' under Year/Month '.$year.
'/'.$month.
'... ';
156 $matching_day_folder_id = createAsset(
'folder', $day, $matching_month_folder_id, $folder_link_type);
157 echo
'asset #'.$matching_day_folder_id.
"\n";
158 if ($make_folders_live) {
159 setAssetStatus($matching_day_folder_id, SQ_STATUS_LIVE);
163 $matching_folder_id = $matching_day_folder_id;
166 return $matching_folder_id;
182 function searchExistingAsset($parent_id, $asset_name, $asset_type_code=
'')
185 $sql =
'SELECT l.minorid, a.name '.
186 'FROM sq_ast_lnk l, sq_ast a '.
187 'WHERE l.majorid = :majorid ';
189 if (!empty($asset_type_code)) {
190 $sql .=
'AND a.type_code = :type_code ';
193 $sql .=
'AND a.assetid = l.minorid '.
194 'AND a.name = :asset_name';
200 if (!empty($asset_type_code)) {
204 }
catch (Exception $e) {
205 throw new Exception(
'Unable to search for an existing '.$asset_name.
' asset: '.$e->getMessage());
208 return $matching_assets;
224 function createAsset($asset_type, $asset_name, $parent_asset_id, $link_type=SQ_LINK_TYPE_1)
226 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
227 $am->includeAsset($asset_type);
230 $new_asset =
new $asset_type();
232 $parent_asset =& $am->getAsset($parent_asset_id);
233 if ($parent_asset->id) {
234 $new_asset->setAttrValue(
'name', $asset_name);
237 'asset' => &$parent_asset,
238 'link_type' => $link_type,
240 'sort_order' => NULL,
245 if ($new_asset->create($link)) {
246 $new_asset_id = $new_asset->id;
249 $am->forgetAsset($parent_asset);
251 return $new_asset_id;
269 function moveAssetToDatedFolder($asset_id, $parent_id, $asset_date_field, $time_period, $folder_link_type, $make_folders_live=FALSE)
272 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
274 $asset =& $am->getAsset($asset_id);
276 $asset_timestamp = $asset->created;
277 if ($asset_date_field ==
'published') {
278 $asset_timestamp = $asset->published;
281 if ($asset_timestamp != NULL) {
283 $destination_folder_id = searchExistingDatedFolder($parent_id, $asset_timestamp, $time_period, $folder_link_type, $make_folders_live);
285 if ($destination_folder_id > 0) {
286 if ($asset_id == $destination_folder_id) {
287 echo
'asset is our destination dated folder. SKIPPING this asset. ';
292 $result = moveAsset($asset->id, $parent_id, $destination_folder_id);
296 echo
'asset '.$asset_date_field.
' date does not exist. SKIPPING this asset. ';
303 $am->forgetAsset($asset);
320 function moveAsset($source_asset_id, $source_parent_id, $destination_parent_id)
324 $links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($source_asset_id, SQ_SC_LINK_SIGNIFICANT,
'', TRUE,
'minor');
325 foreach ($links as $link) {
326 if ($link[
'majorid'] == $source_parent_id) {
327 $link_info = $GLOBALS[
'SQ_SYSTEM']->am->getLinkById($link[
'linkid'], $source_asset_id,
'minor');
329 $assets[$source_asset_id][] = Array(
330 'linkid' => $link_info[
'linkid'],
331 'link_type' => $link_info[
'link_type'],
332 'parentid' => $link_info[
'majorid'],
337 $hh =& $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
339 'link_action' =>
'move',
341 'to_parent_assetid' => $destination_parent_id,
342 'to_parent_pos' => 1,
345 $errors = $hh->freestyleHipo(
'hipo_job_create_links', $vars);
346 $result = (count($errors) == 0);
347 if (!$result) print_r($errors);
363 function setAssetStatus($asset_id, $status)
365 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
367 'new_status' => $status,
368 'assetid' => $asset_id,
370 $hh =& $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
371 $errors = $hh->freestyleHipo(
'hipo_job_edit_status', $vars);
372 return (count($errors) == 0);
379 error_reporting(E_ALL);
380 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
382 if ((php_sapi_name() !=
'cli')) {
384 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
387 require_once
'Console/Getopt.php';
390 $longopt = Array(
'root=',
'type=',
'field=',
'period=',
'folder-link-type=',
'make-folders-live',
'move-asset-status=');
392 $args = Console_Getopt::readPHPArgv();
394 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
395 if (empty($options[0])) {
400 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
401 if (empty($SYSTEM_ROOT)) {
402 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
407 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
408 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
413 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
414 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
418 $asset_type_code =
'';
419 $asset_date_field =
'';
421 $folder_link_type = SQ_LINK_TYPE_2;
422 $make_folders_live = FALSE;
423 $move_asset_status = -1;
425 $mandatory_options = Array(
'--root',
'--type',
'--field',
'--period');
426 $found_options = Array();
429 foreach ($options[0] as $option) {
430 $found_options[] = $option[0];
434 $num_found_options = 0;
435 foreach ($found_options as $found_option) {
436 if (in_array($found_option, $mandatory_options)) {
437 $num_found_options++;
441 if ($num_found_options <> count($mandatory_options)) {
447 foreach ($options[0] as $option) {
448 switch ($option[0]) {
450 $root_node = (isset($option[1])) ? (
int)$option[1] : 0;
451 if ($root_node <= 0) {
452 echo
"Please specify a root node asset ID (--root)\n";
457 $parent_asset =& $am->getAsset($root_node);
458 if (!$parent_asset->id) {
459 echo
"The specified root node asset was not found\n";
463 $am->forgetAsset($parent_asset);
467 $asset_type_code = (isset($option[1])) ? $option[1] :
'';
468 if (empty($asset_type_code)) {
469 echo
"Please specify an asset type (--type)\n";
474 $am->includeAsset($asset_type_code);
479 $asset_date_field = (isset($option[1])) ? strtolower($option[1]) :
'';
480 if (empty($asset_date_field)) {
481 echo
"Please specify an asset date field (--field)\n";
484 }
else if (($asset_date_field !=
'created') && ($asset_date_field !=
'published')) {
485 echo
"Please specify either 'created' or 'published' for the asset date field\n";
492 $time_period = (isset($option[1])) ? strtolower($option[1]) :
'';
493 if (empty($time_period)) {
494 echo
"Please specify a time period (--period)\n";
497 }
else if (($time_period !=
'year') && ($time_period !=
'month') && ($time_period !=
'day')) {
498 echo
"Please specify either 'year', 'month' or 'day' for the time period\n";
504 case '--folder-link-type':
505 $folder_link_type = (isset($option[1])) ? (
int)$option[1] : SQ_LINK_TYPE_2;
506 if (($folder_link_type != SQ_LINK_TYPE_1) && ($folder_link_type != SQ_LINK_TYPE_2)) {
507 echo
"Please specify either link type 1 or 2 for the folder link type (--folder-link-type)\n";
513 case '--make-folders-live':
514 $make_folders_live = TRUE;
517 case '--move-asset-status':
518 $move_asset_status = (isset($option[1])) ? (
int)$option[1] : -1;
524 $root_user =& $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
527 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
528 echo
"ERROR: Failed login as root user\n";
535 $assets_to_move = $am->getChildren($root_node, $asset_type_code, TRUE, NULL, NULL, NULL, TRUE, 1, 1);
536 $failed_asset_move = Array();
538 if (count($assets_to_move) > 0) {
540 foreach (array_keys($assets_to_move) as $asset_id_to_move) {
542 $asset =& $am->getAsset($asset_id_to_move);
543 if (($move_asset_status == -1) || ($asset->status == $move_asset_status)) {
544 echo
'- Moving asset #'.$asset_id_to_move.
'... ';
545 $result = moveAssetToDatedFolder($asset_id_to_move, $root_node, $asset_date_field, $time_period, $folder_link_type, $make_folders_live);
550 $failed_asset_move[] = $asset_id_to_move;
553 $am->forgetAsset($asset);
556 if (!empty($failed_asset_move) > 0) {
557 echo
"\n*** The following assets could not be moved due to errors detailed above:\n";
558 foreach ($failed_asset_move as $failed_asset_move_id) {
559 echo
'- Asset # '.$failed_asset_move_id.
"\n";
564 echo
"- No matching assets were found to move. Script aborted\n";
567 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
568 echo
"- All done!\n";