Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
move_assets.php
1 <?php
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);
30 
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");
34 }
35 
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.");
38 }
39 
40 //FROM PARENT ID
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');
44 }
45 
46 //TO PARENT ID
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');
50 }
51 
52 //ASSET_TYPE
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');
56 }
57 
58 require_once $SYSTEM_ROOT.'/core/include/init.inc';
59 
60 //LINK_TYPE
61 $LINK_TYPE = (isset($_SERVER['argv'][5])) ? $_SERVER['argv'][5] : SQ_SC_LINK_SIGNIFICANT;
62 
63 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
64 
65 // log in as root
66 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
67  echo "ERROR: Failed login in as root user\n";
68  exit();
69 }
70 
71 moveAssets($FROM_PARENT_ID, $TO_PARENT_ID, $ASSET_TYPE, $LINK_TYPE);
72 
73 $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
74 
75 echo "Finish moving assets!\n";
76 
77 
88 function moveAssets($from_parent_assetid, $to_parent_assetid, $type_code = '', $link_type = NULL, $max_size = 1000, $chunk_size = 100)
89 {
90  //get the assetids to move
91  $child_assetids = $GLOBALS['SQ_SYSTEM']->am->getChildren($from_parent_assetid, $type_code, TRUE, NULL, NULL, NULL, TRUE, 1, 1);
92 
93  //convert it to one dimension array
94  $child_assetids = array_keys($child_assetids);
95 
96  //get no more than $max_size assets
97  $child_assetids = array_slice($child_assetids, 0, $max_size);
98 
99  //split assetid array to smaller chunks
100  $child_assetids = array_chunk($child_assetids, $chunk_size);
101 
102  //move each chunk of assets to the new parent
103  $total_moved_asset = 0;
104  foreach ($child_assetids as $assetid_chunk) {
105  //we are working, please wait
106  echo "Moving assets ...\n";
107 
108  //move asset chunk
109  moveAssetChunk($assetid_chunk, $from_parent_assetid, $to_parent_assetid, $link_type);
110 
111  //output the progress
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";
114 
115  }
116 
117 }//end moveAssets()
118 
119 
129 function moveAssetChunk($assetids, $from_parent_assetid, $to_parent_assetid, $link_type = NULL)
130 {
131  $am = $GLOBALS['SQ_SYSTEM']->am;
132 
133  $assets = Array();
134  foreach ($assetids as $assetid) {
135  //get all links between the moving assetids and the parent parent id
136  $links = $am->getLinkByAsset($assetid, $from_parent_assetid, $link_type, NULL, 'minor', TRUE);
137  //put all the link information to the assets array
138  foreach ($links as $link) {
139  $assets[$assetid][] = Array(
140  'linkid' => $link['linkid'],
141  'link_type' => $link['link_type'],
142  'parentid' => $from_parent_assetid,
143  );
144  }
145  }
146 
147  //get HIPO Herder
148  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
149 
150  //create running vars
151  $vars = Array(
152  'link_action' => 'move',
153  'assets' => $assets,
154  'to_parent_assetid' => $to_parent_assetid,
155  'to_parent_pos' => getNextSortOrder($to_parent_assetid),
156  );
157 
158  $hh->freestyleHipo('hipo_job_create_links', $vars);
159 
160 }//end moveAssetChunk()
161 
162 
169 function getNextSortOrder($parent_assetid)
170 {
171  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db');
172 
173  $sql = 'SELECT
174  COUNT(*) as count, MAX(sort_order) as max
175  FROM
176  sq_ast_lnk
177  WHERE
178  majorid = :majorid';
179 
180  try {
181  $query = MatrixDAL::preparePdoQuery($sql);
182  MatrixDAL::bindValueToPdo($query, 'majorid', $parent_assetid);
183  $result = MatrixDAL::executePdoAll($query);
184  $row = $result[0];
185  unset($result);
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());
188  }
189 
190  $next_sort_order = ($row['count'] > 0)? max($row['count'], $row['max']+1) : 0;
191 
192  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
193 
194  return $next_sort_order;
195 
196 }//end getNextSortOrder()
197 
198 
205 function printUsage($error)
206 {
207  echo "$error\n\n";
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";
210 
211  exit();
212 
213 }//end printUsage()
214 
215 
216 ?>