PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/index.php

https://github.com/earlfogel/Cascade-to-Cascade-Migration-Scripts
PHP | 2365 lines | 1990 code | 164 blank | 211 comment | 607 complexity | 5f2a55f79a7284e40497b92d56c4e059 MD5 | raw file
  1. <?php
  2. /*
  3. This script copies assets between sites in Cascade or from one
  4. instance of Cascade to another. You can recursively copy folders
  5. or containers and all their contents, or copy entire sites.
  6. This is version 1.6.5 for Cascade 7
  7. Based on the copy-folder script in Hannon Hill's CAST toolkit.
  8. */
  9. error_reporting(E_ALL ^ E_NOTICE); # ignore undefined variables
  10. ini_set("display_errors",1);
  11. ini_set("max_execution_time",600);
  12. ini_set("memory_limit",'512M');
  13. include_once("cascade_soap_lib.php");
  14. /* Configuration */
  15. $environments = array(
  16. 'Cascade Development' => 'dev.example.edu',
  17. 'Cascade Test' => 'test.example.edu',
  18. 'Cascade Production' => 'prod.example.edu',
  19. );
  20. $secure = 1; # whether to use http or https to connect to web services
  21. /* You shouldn't have to change anything below this line */
  22. $verbose = 2;
  23. $dryrun = 0;
  24. $exit_on_error = 1;
  25. $inherit_permissions = 0;
  26. $copytype = 'folder';
  27. $oldPath = '/';
  28. $firstPass = false;
  29. $multiPass = false;
  30. ob_implicit_flush(true);
  31. ob_end_flush();
  32. ?>
  33. <!-- Latest compiled and minified CSS -->
  34. <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
  35. <!-- Latest compiled and minified JavaScript -->
  36. <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
  37. <?php
  38. if (!empty($_POST) && validateInput()) {
  39. showForm();
  40. echo "<pre>";
  41. if ($dryrun) out1("Dry Run");
  42. if ($multiPass) {
  43. $firstPass = true;
  44. out1("First pass...");
  45. }
  46. update();
  47. setPermissions();
  48. if ($multiPass) {
  49. $firstPass = false;
  50. echo "</pre>";
  51. echo "<pre>";
  52. out1("Second pass...");
  53. editPages();
  54. editPageConfigurationSets();
  55. }
  56. if ($dryrun) {
  57. out1("Dry Run Complete");
  58. } else {
  59. out1("Done.");
  60. }
  61. echo "</pre>";
  62. } else {
  63. showForm();
  64. }
  65. function update() {
  66. global $host, $host2;
  67. global $uname, $uname2;
  68. global $pass, $pass2;
  69. global $oldPath, $newPath;
  70. global $oldSite, $newSite;
  71. global $readClient, $writeClient;
  72. global $copytype;
  73. global $dryrun;
  74. global $verbose;
  75. global $secure;
  76. $proto = 'http';
  77. if ($secure) $proto = 'https';
  78. $readClient = new CascadeSoapClient();
  79. if (preg_match('/^http/', $host)) {
  80. $readClient->url = $host . "/ws/services/AssetOperationService?wsdl";
  81. } else {
  82. $readClient->url = "$proto://" . $host . "/ws/services/AssetOperationService?wsdl";
  83. }
  84. $readClient->username = $uname;
  85. $readClient->password = $pass;
  86. $readClient->connect();
  87. $writeClient = new CascadeSoapClient();
  88. if (preg_match('/^http/', $host2)) {
  89. $writeClient->url = $host2 . "/ws/services/AssetOperationService?wsdl";
  90. } else {
  91. $writeClient->url = "$proto://$host2/ws/services/AssetOperationService?wsdl";
  92. }
  93. $writeClient->username = $uname2;
  94. $writeClient->password = $pass2;
  95. $writeClient->connect();
  96. if ($oldSite == 'Global')
  97. $oldSite = '';
  98. if ($newSite == 'Global')
  99. $newSite = '';
  100. #
  101. # admin areas to include when we copy sites
  102. #
  103. $adminAreas = array(
  104. 'assetFactory', 'contentType', 'dataDefinition',
  105. 'metadataSet', 'pageConfigurationSet', 'publishSet',
  106. 'workflowDefinition'
  107. );
  108. if ($oldSite == '' && $newSite == '') { # old "sites"
  109. array_push($adminAreas, 'target');
  110. } else if ($oldSite != '' && $newSite != '') { # new "Sites"
  111. array_push($adminAreas, 'transport');
  112. array_push($adminAreas, 'destination');
  113. }
  114. echo "Copying $copytype $host/$oldSite:$oldPath to $host2/$newSite:$newPath\n";
  115. if ($copytype == 'folder' || $copytype == 'site') {
  116. add_folder(NULL,$oldPath);
  117. } else if (preg_match('/Container$/',$copytype)) {
  118. add_container(NULL,$oldPath,$copytype);
  119. } else if (in_array($copytype,$adminAreas)) {
  120. checkAdminAsset(NULL,$oldPath,$copytype);
  121. } else if ($copytype == 'user') { // copy one or more users
  122. $oldPaths = preg_split("/ +/", $oldPath);
  123. foreach ($oldPaths as &$oldPath) {
  124. checkUser($oldPath);
  125. }
  126. } else if ($copytype == 'group') { // copy one or more groups
  127. $oldPaths = preg_split("/ +/", $oldPath);
  128. foreach ($oldPaths as &$oldPath) {
  129. checkGroup($oldPath);
  130. }
  131. } else { // home area asset
  132. checkAsset(NULL,$oldPath,$copytype);
  133. }
  134. if ($copytype == 'site') {
  135. $checkPath = preg_replace("#^.*/#","/",$oldPath);
  136. foreach ($adminAreas as &$adminArea) {
  137. if ($verbose>2) out2("\nChecking ${adminArea}s ...");
  138. if ($adminArea == 'destination') {
  139. $type = "siteDestinationContainer";
  140. } else if ($adminArea == 'target') {
  141. $type = "target";
  142. } else {
  143. $type = $adminArea . "Container";
  144. }
  145. add_container(NULL,$checkPath,$type);
  146. }
  147. }
  148. }
  149. /*
  150. * On the second pass, edit the pages that are in the structuredPages array
  151. */
  152. function editPages() {
  153. global $oldPath;
  154. global $oldSite, $newSite;
  155. global $readClient, $writeClient;
  156. global $added;
  157. global $exit_on_error;
  158. global $structuredPages;
  159. //if it's the second pass and there are pages to edit
  160. if(is_array($structuredPages)) {
  161. //loop through each page and get the page from read and update write based on it
  162. foreach($structuredPages as $pagePath) {
  163. //get the page from the readClient
  164. $readAsset = getReadAsset($pagePath, "page");
  165. $writeAsset = getWriteAsset($pagePath, "page");
  166. //replace the write page DD with the read DD
  167. $writeAsset->page->structuredData = $readAsset->page->structuredData;
  168. adjustStructuredData(
  169. $writeAsset->page->path,
  170. $writeAsset->page->structuredData->structuredDataNodes);
  171. //edit the asset on the writeClient
  172. updateWriteAsset("page", $writeAsset);
  173. } //end foreach structured page
  174. } //end if structured pages and second pass
  175. return true;
  176. }
  177. /*
  178. * On the second pass, edit the pages that are in the structuredPages array
  179. */
  180. function editPageConfigurationSets() {
  181. global $oldPath;
  182. global $oldSite, $newSite;
  183. global $readClient, $writeClient;
  184. global $pageConfigurationSets;
  185. $type = "pageConfigurationSet";
  186. //if it's the second pass and there are pages to edit
  187. if(is_array($pageConfigurationSets)) {
  188. //edit each config set, copying pageConfigurations from source asset
  189. foreach($pageConfigurationSets as $path) {
  190. //get the asset from the readClient
  191. $readAsset = getReadAsset($path, $type);
  192. $writeAsset = getWriteAsset($path, $type);
  193. //replace the write page configurations with the read
  194. $writeAsset->$type->pageConfigurations =
  195. $readAsset->$type->pageConfigurations;
  196. adjustPageConfigurations($path,$writeAsset->$type->pageConfigurations);
  197. //edit the asset on the writeClient
  198. updateWriteAsset($type, $writeAsset);
  199. }
  200. }
  201. return true;
  202. }
  203. /*
  204. * read the asset from the oldSite and return the object
  205. */
  206. function getReadAsset($path, $type) {
  207. global $oldPath;
  208. global $oldSite, $newSite;
  209. global $readClient, $writeClient;
  210. global $exit_on_error;
  211. if (isset($id)) {
  212. $ident->id = $id;
  213. }
  214. $ident->path = $path;
  215. $ident->siteName = $oldSite;
  216. $ident->type = strtolower($type);
  217. $readClient->read($ident);
  218. if (!$readClient->success) {
  219. echo "getReadAsset(): Read failure on $type $path: " . cleanup($readClient->response);
  220. print_r($ident);
  221. if ($exit_on_error) cleanexit(); else return;
  222. }
  223. return $readClient->asset;
  224. }
  225. /*
  226. * read the asset from the newSite and return the object
  227. */
  228. function getWriteAsset($path, $type) {
  229. global $oldPath;
  230. global $oldSite, $newSite;
  231. global $readClient, $writeClient;
  232. global $exit_on_error;
  233. if (isset($id)) {
  234. $ident->id = $id;
  235. }
  236. $ident->path = $path;
  237. $ident->siteName = $newSite;
  238. $ident->type = strtolower($type);
  239. $writeClient->read($ident);
  240. if (!$writeClient->success) {
  241. echo "getWriteAsset(): Read failure on $type $path: " . cleanup($writeClient->response);
  242. print_r($ident);
  243. if ($exit_on_error) cleanexit(); else return;
  244. }
  245. return $writeClient->asset;
  246. }
  247. /*
  248. * update the asset on the newSite
  249. */
  250. function updateWriteAsset($type, $writeAsset) {
  251. global $readClient, $writeClient;
  252. global $dryrun;
  253. global $verbose;
  254. global $exit_on_error;
  255. global $newId;
  256. if (!$dryrun) {
  257. $writeClient->edit($writeAsset);
  258. if (!$writeClient->success) {
  259. echo "updateWriteAsset(): Failed: " . $writeAsset->$type->path . "\n";
  260. print_r($writeAsset);
  261. echo cleanup($writeClient->response);
  262. print_r($readAsset);
  263. if ($exit_on_error) cleanexit(); else return false;
  264. } //is not successful write
  265. } //if not dry run
  266. if($verbose>1) echo "Updated $type " . $writeAsset->$type->path . "\n";
  267. return true;
  268. }
  269. /*
  270. * Get the ID of the asset on the newSite given the path and type
  271. */
  272. function getWriteAssetID($path, $type) {
  273. global $oldPath;
  274. global $oldSite, $newSite;
  275. global $readClient, $writeClient;
  276. $ident->path = $path;
  277. $ident->siteName = $newSite;
  278. $ident->type = $type;
  279. $writeClient->read($ident);
  280. if (!$writeClient->success) {
  281. echo "getWriteAssetID('$path', '$type'): Read failure on $type $path: " . cleanup($writeClient->response);
  282. print_r($ident);
  283. if ($exit_on_error) cleanexit(); else return;
  284. }
  285. return $writeClient->asset->$type->id;
  286. }
  287. /*
  288. * Read the folder, push its children, make the folder, then pop remaining children
  289. */
  290. function add_folder($id,$path) {
  291. global $oldPath;
  292. global $oldSite, $newSite;
  293. global $readClient, $writeClient;
  294. global $folderStack;
  295. global $pageStack;
  296. global $refStack;
  297. global $genericStack;
  298. global $templateStack;
  299. global $skipPattern;
  300. global $added;
  301. global $exit_on_error;
  302. $type = 'folder';
  303. #
  304. # have we already checked folder *and* children?
  305. #
  306. if (isset($added["$type.$path"])) {
  307. return;
  308. } else {
  309. $added["$type.$path"] = 1;
  310. }
  311. if (isset($skipPattern) && preg_match("#$skipPattern#i",$path)) {
  312. echo "*** Skipping $type $path\n";
  313. return;
  314. }
  315. #
  316. # create the folder itself
  317. #
  318. checkFolder($id, $path);
  319. #
  320. # and its children
  321. #
  322. if (isset($id)) {
  323. $ident->id = $id;
  324. }
  325. $ident->path = $path;
  326. $ident->siteName = $oldSite;
  327. $ident->type = "folder";
  328. $readClient->read($ident);
  329. if (!$readClient->success) {
  330. echo "Failed reading: $type " . $path . "\n";
  331. echo print_r($ident);
  332. echo cleanup($readClient->response);
  333. if ($exit_on_error) cleanexit(); else return;
  334. }
  335. $asset = $readClient->asset;
  336. if (isset($asset->$type->children)
  337. && isset($asset->$type->children->child)) {
  338. $children = $asset->$type->children->child;
  339. if (!is_array($children)) {
  340. $children = array( $children );
  341. }
  342. } else {
  343. $children = "";
  344. }
  345. if (is_array($children)) {
  346. while ($cur = array_pop($children)) {
  347. if ($cur->type == "folder") {
  348. $folderStack[] = $cur;
  349. } else if ($cur->type == "page") {
  350. $pageStack[] = $cur;
  351. } else if ($cur->type == "reference") {
  352. $refStack[] = $cur;
  353. } else if ($cur->type == "template") {
  354. $templateStack[] = $cur;
  355. } else {
  356. $genericStack[] = $cur;
  357. }
  358. }
  359. }
  360. if (is_array($folderStack)) {
  361. while ($cur = array_pop($folderStack)) {
  362. add_folder($cur->id,$cur->path->path);
  363. }
  364. }
  365. if (is_array($genericStack)) {
  366. while ($cur = array_pop($genericStack)) {
  367. checkAsset($cur->id,$cur->path->path, $cur->type);
  368. }
  369. }
  370. if (is_array($templateStack)) {
  371. while ($cur = array_pop($templateStack)) {
  372. checkAsset($cur->id,$cur->path->path, $cur->type);
  373. }
  374. }
  375. if (is_array($pageStack)) {
  376. while ($cur = array_pop($pageStack)) {
  377. checkAsset($cur->id,$cur->path->path, $cur->type);
  378. }
  379. }
  380. if (is_array($refStack)) {
  381. while ($cur = array_pop($refStack)) {
  382. checkAsset($cur->id,$cur->path->path, $cur->type);
  383. }
  384. }
  385. }
  386. /*
  387. * Read in the file, block, symlink, or ... and add it directly
  388. */
  389. function checkAsset($id, $path, $type) {
  390. global $oldPath, $newPath;
  391. global $oldSite, $newSite;
  392. global $readClient, $writeClient;
  393. global $skipPattern;
  394. global $dryrun;
  395. global $verbose;
  396. global $checked;
  397. global $exit_on_error;
  398. global $newId;
  399. global $targetStuff;
  400. global $firstPass;
  401. global $structuredPages;
  402. if (preg_match("/block_.*/",$type)) {
  403. $type = "block";
  404. } else if (preg_match("/format_.*/",$type)) {
  405. $type = "format";
  406. }
  407. #
  408. # see if we've already checked it
  409. #
  410. if (isset($checked["$type.$path"])) {
  411. if ($verbose>3) out3("*** Checking $type: " . getPath($path) . " (we've been here before)");
  412. return;
  413. } else {
  414. $checked["$type.$path"] = 1;
  415. }
  416. if ($dryrun && $type == 'file' && $verbose<2) {
  417. return; # speed things up a bit
  418. }
  419. if (isset($skipPattern) && preg_match("#$skipPattern#i",$path)) {
  420. echo "*** Skipping $type $path\n";
  421. return;
  422. }
  423. #
  424. # reference to another site
  425. #
  426. if (preg_match('/^(.*):(.*)/',$path,$matches)) {
  427. global $host, $host2;
  428. if ($host == $host2) {
  429. if ($verbose>2) out2("Ok: $type $path");
  430. return;
  431. } else {
  432. $site = $matches[1];
  433. $path = $matches[2];
  434. if ($verbose>2) out2("Checking $type $site:$path");
  435. $newident->path = $path;
  436. $newident->siteName = $site;
  437. $newident->type = strtolower($type);
  438. $writeClient->read($newident);
  439. if (!$writeClient->success) {
  440. if (preg_match('/Unable to identify an entity/',$writeClient->response)) {
  441. echo "
  442. *** Found cross-site reference to $site $type $path, which does not exist in the
  443. target environment. You must change this before the copy can proceed.
  444. ";
  445. } else {
  446. echo "Failed reading: " . $site . ': ' . $path . "\n";
  447. echo print_r($newident);
  448. echo cleanup($writeClient->response);
  449. }
  450. if ($exit_on_error) cleanexit(); else return;
  451. }
  452. return;
  453. }
  454. }
  455. #
  456. # read source asset
  457. #
  458. if ($verbose>2) out2("Checking $type $path");
  459. if (isset($id)) {
  460. $ident->id = $id;
  461. }
  462. $ident->path = $path;
  463. $ident->siteName = $oldSite;
  464. $ident->type = $type;
  465. $readClient->read($ident);
  466. if (!$readClient->success) {
  467. echo "Read failure on $type $path: " . cleanup($readClient->response);
  468. print_r($ident);
  469. if ($exit_on_error) cleanexit(); else return;
  470. }
  471. $asset = $readClient->asset;
  472. $otype = $type;
  473. if (!isset($asset->$type)) {
  474. if (isset($asset->indexBlock)) {
  475. $type = "indexBlock";
  476. } else if (isset($asset->textBlock)) {
  477. $type = "textBlock";
  478. } else if (isset($asset->feedBlock)) {
  479. $type = "feedBlock";
  480. } else if (isset($asset->xmlBlock)) {
  481. $type = "xmlBlock";
  482. } else if (isset($asset->xhtmlBlock)) {
  483. $type = "xhtmlBlock";
  484. } else if (isset($asset->xhtmlDataDefinitionBlock)) {
  485. $type = "xhtmlDataDefinitionBlock";
  486. } else if (isset($asset->xsltFormat)) {
  487. $type = "xsltFormat";
  488. } else if (isset($asset->scriptFormat)) {
  489. $type = "scriptFormat";
  490. }
  491. }
  492. #
  493. # workaround http://issues.hannonhill.com/browse/CSI-145
  494. # if asset is in a different site, we're done
  495. # (this should never happen)
  496. #
  497. if ($asset->$type->siteName != $oldSite) {
  498. global $host, $host2;
  499. if ($host == $host2) {
  500. $newId[$id] = $id;
  501. }
  502. if ($verbose>2)
  503. out2("Skipping $type $path (site " . $asset->$type->siteName . "!=" . $oldSite . ")");
  504. return;
  505. }
  506. //remove the structured data from the page on the first pass
  507. if($firstPass && isset($asset->page->structuredData)) {
  508. unset($asset->page->structuredData);
  509. $structuredPages[$asset->page->path] = $asset->page->path;
  510. }
  511. #
  512. # save asset id to get around CSI-108
  513. #
  514. $ident->id = $asset->$type->id;
  515. #
  516. # see if asset already exists in new location
  517. #
  518. $parent = preg_replace("#/[^/]*$#", "", $path);
  519. if (isset($checked["folder.$parent"])) {
  520. # we know it doesn't exist
  521. } else if (isset($newId["$otype.$path"])) {
  522. # we know it does exist
  523. $newId[$asset->$type->id] = $newId["$otype.$path"];
  524. return;
  525. } else {
  526. if ($verbose>3) out3("Checking new $type: " . getPath($path));
  527. $newident->path = getPath($path);
  528. $newident->siteName = $newSite;
  529. $newident->type = $ident->type;
  530. $writeClient->read($newident);
  531. if ($writeClient->success) {
  532. $newasset = $writeClient->asset;
  533. $newId[$asset->$type->id] = $newasset->$type->id;
  534. return;
  535. } else if (preg_match('/Unable to identify/',$writeClient->response)) {
  536. } else {
  537. echo "Read failure on destination: " . getPath($path) . cleanup($writeClient->response);
  538. print_r($newident);
  539. if ($exit_on_error) cleanexit(); else return;
  540. }
  541. }
  542. if ($type == 'xsltFormat') {
  543. $asset->$type->xml = preg_replace(
  544. '#1.0"xmlns#', '1.0" xmlns', $asset->$type->xml);
  545. }
  546. if ($type == 'file' || preg_match('/\.css$/',$asset->$type->name)) {
  547. $asset->$type->data =
  548. preg_replace('#\[system-asset(?::id=\w+)?\]([^\[]*)\[/system-asset\]#',
  549. '/renderfile/global$1', $asset->$type->data);
  550. $asset->$type->text =
  551. preg_replace('#\[system-asset(?::id=\w+)?\]([^\[]*)\[/system-asset\]#',
  552. '/renderfile/global$1', $asset->$type->text);
  553. }
  554. if ($type == 'page' && $oldPath != $newPath) {
  555. $asset->$type->xhtml = preg_replace(
  556. "#$oldPath#", $newPath, $asset->$type->xhtml);
  557. }
  558. # remove [system-asset] tags from symlinks
  559. if ($type == 'symlink' && isset($asset->$type->linkURL)) {
  560. $asset->$type->linkURL =
  561. preg_replace('#\[system-asset(?::id=\w+)?\]([^\[]*)\[/system-asset\]#',
  562. '$1', $asset->$type->linkURL);
  563. }
  564. if (isset($asset->$type->parentFolderPath)) {
  565. checkFolder($asset->$type->parentFolderId,$asset->$type->parentFolderPath);
  566. $asset->$type->parentFolderPath = getPath($asset->$type->parentFolderPath);
  567. }
  568. if (isset($asset->$type->metadataSetPath)) {
  569. checkAdminAsset($asset->$type->metadataSetId,$asset->$type->metadataSetPath,'metadataSet');
  570. }
  571. if (isset($asset->$type->configurationSetPath)) {
  572. checkAdminAsset($asset->$type->configurationSetId,$asset->$type->configurationSetPath,'pageConfigurationSet');
  573. } else {
  574. unset($asset->$type->configurationSetPath);
  575. }
  576. if (isset($asset->$type->contentTypePath)) {
  577. checkAdminAsset($asset->$type->contentTypeId,$asset->$type->contentTypePath,"contentType");
  578. }
  579. if (isset($asset->$type->indexedContentTypePath)) {
  580. checkAdminAsset($asset->$type->IndexedContentTypeId,$asset->$type->indexedContentTypePath,"contentType");
  581. }
  582. if (isset($asset->$type->indexedFolderPath)) {
  583. if (want($asset->$type->indexedFolderPath,'folder')) {
  584. checkAsset($asset->id,$asset->$type->indexedFolderPath,'folder');
  585. $asset->$type->indexedFolderPath = getPath($asset->$type->indexedFolderPath);
  586. } else {
  587. if ($verbose>1) echo "*** Adjusting indexedFolderPath in $type $path\n";
  588. $asset->$type->indexedFolderPath = null;
  589. }
  590. } else {
  591. # silly php, if it's not set, why do we need to unset it?
  592. if ($type == 'indexBlock') {
  593. #echo "*** Removing indexedFolderPath in $type $path\n";
  594. unset($asset->$type->indexedFolderPath);
  595. }
  596. }
  597. if (isset($asset->$type->expirationFolderPath)) {
  598. if (want($asset->$type->expirationFolderPath,'folder')) {
  599. checkAsset($asset->id,$asset->$type->expirationFolderPath,'folder');
  600. $asset->$type->expirationFolderPath = getPath($asset->$type->expirationFolderPath);
  601. } else {
  602. if ($verbose>1) echo "Adjusting expirationFolderPath in $type $path\n";
  603. $asset->$type->expirationFolderPath = null;
  604. }
  605. }
  606. if (isset($asset->$type->formatPath)) {
  607. checkAsset($asset->id,$asset->$type->formatPath,'format');
  608. $asset->$type->formatPath = getPath($asset->$type->formatPath);
  609. }
  610. if (isset($asset->$type->targetPath)) {
  611. if ($newSite) {
  612. # sites don't have targets, but we may need the file extension
  613. $targetStuff["$type.$path"] = getTargetStuff($asset->$type->targetPath);
  614. unset($asset->$type->targetPath);
  615. } else {
  616. checkTarget($asset->$type->targetPath);
  617. }
  618. }
  619. if (isset($asset->$type->structuredData) &&
  620. is_string($asset->$type->structuredData->definitionPath)) {
  621. checkAdminAsset($asset->$type->structuredData->definitionId,$asset->$type->structuredData->definitionPath,'dataDefinition');
  622. }
  623. if (isset($asset->$type->structuredData) &&
  624. isset($asset->$type->structuredData->structuredDataNodes)) {
  625. adjustStructuredData($path,$asset->$type->structuredData->structuredDataNodes);
  626. unset($asset->$type->structuredData->definitionId);
  627. } else {
  628. unset($asset->$type->structuredData);
  629. }
  630. if (isset($asset->$type->pageConfigurations)) {
  631. adjustPageConfigurations($asset->$type->configurationSetPath,$asset->$type->pageConfigurations);
  632. }
  633. if (isset($asset->$type->pageRegions)) {
  634. adjustPageRegions($path,$asset->$type->pageRegions);
  635. }
  636. if (isset($asset->$type->referencedAssetId)) {
  637. if ($asset->$type->referencedAssetType == 'folder') {
  638. checkFolder($asset->$type->referencedAssetId,$asset->$type->referencedAssetPath);
  639. } else {
  640. checkAsset($asset->id,$asset->$type->referencedAssetPath,
  641. $asset->$type->referencedAssetType);
  642. }
  643. $asset->$type->referencedAssetPath =
  644. getPath($asset->$type->referencedAssetPath);
  645. }
  646. setSite($asset,$type);
  647. removeIds($asset->$type);
  648. unset($asset->$type->path);
  649. if (isset($asset->$type->data)) {
  650. unset($asset->$type->text);
  651. //$asset->$type->data = base64_decode($asset->$type->data);
  652. }
  653. if ($verbose>1) echo "Creating: $type " . getPath($path) . "\n";
  654. if ($verbose==1) echo ".";
  655. if (!$dryrun) {
  656. $writeClient->create($asset);
  657. if ($writeClient->success) {
  658. $newId[$ident->id] = $writeClient->createdAssetId;
  659. #echo "Created $type " . $newId[$ident->id] . "\n";
  660. remember($ident);
  661. } else {
  662. echo "Failed: $type " . getPath($path) . "\n";
  663. if (isset($asset->$type->data)) {
  664. $asset->$type->data = "*** data ***";
  665. }
  666. print_r($asset);
  667. echo cleanup($writeClient->response);
  668. if ($exit_on_error) cleanexit(); else return;
  669. }
  670. }
  671. }
  672. function checkTarget($path) {
  673. global $readClient, $writeClient;
  674. global $oldSite, $newSite;
  675. global $dryrun;
  676. global $verbose;
  677. global $checked;
  678. global $exit_on_error;
  679. $type = "target";
  680. if (isset($checked["$type.$path"])) {
  681. return;
  682. } else {
  683. $checked["$type.$path"] = 1;
  684. }
  685. if ($path == '/') {
  686. return;
  687. }
  688. if ($verbose>2) out2("Checking $type $path ...");
  689. $ident->path = $path;
  690. $ident->siteName = $newSite;
  691. $ident->type = $type;
  692. $writeClient->read($ident);
  693. if (!$writeClient->success) {
  694. $ident->siteName = $oldSite;
  695. $readClient->read($ident);
  696. if (!$readClient->success) {
  697. echo "Failed reading: $type " . $path . "\n";
  698. echo print_r($ident);
  699. echo cleanup($readClient->response);
  700. if ($exit_on_error) cleanexit(); else return;
  701. }
  702. $asset = $readClient->asset;
  703. #
  704. # save asset id to get around CSI-108
  705. #
  706. $ident->id = $asset->$type->id;
  707. if ($asset->$type->parentTargetPath != "") {
  708. checkTarget($asset->$type->parentTargetPath);
  709. }
  710. $asset->$type->baseFolderPath = getPath($asset->$type->baseFolderPath);
  711. if ($asset->$type->cssFilePath != "") {
  712. checkAsset($asset->$type->cssFileId,$asset->$type->cssFilePath,'file');
  713. $asset->$type->cssFilePath = getPath($asset->$type->cssFilePath);
  714. }
  715. if ($asset->$type->publishIntervalUnits == "Hours") {
  716. $asset->$type->publishIntervalUnits = "hours";
  717. }
  718. setSite($asset,$type);
  719. removeIds($asset->$type);
  720. unset($asset->$type->path);
  721. unset($asset->$type->children);
  722. if ($verbose>1) echo "Creating: $type " . $path . "\n";
  723. if (!$dryrun) {
  724. $writeClient->create($asset);
  725. if ($writeClient->success) {
  726. remember($ident);
  727. } else {
  728. echo "\nFailed: $type " . $path . "\n";
  729. print_r($asset);
  730. echo cleanup($writeClient->response);
  731. if ($exit_on_error) cleanexit(); else return;
  732. }
  733. }
  734. }
  735. }
  736. function getTargetStuff($path) {
  737. global $readClient;
  738. global $oldSite;
  739. global $verbose;
  740. global $checked;
  741. global $exit_on_error;
  742. global $targetStuff;
  743. $type = "target";
  744. if (!isset($targetStuff["$type.$path"])) {
  745. if ($verbose>2) out2("Checking $type $path ...");
  746. $ident->path = $path;
  747. $ident->siteName = $oldSite;
  748. $ident->type = $type;
  749. $readClient->read($ident);
  750. if (!$readClient->success) {
  751. echo "Failed reading: $type " . $path . "\n";
  752. echo print_r($ident);
  753. echo cleanup($readClient->response);
  754. if ($exit_on_error) cleanexit(); else return;
  755. }
  756. $asset = $readClient->asset;
  757. $targetStuff["$type.$path"] =
  758. array($asset->$type->outputExtension, $asset->$type->serializationType);
  759. }
  760. return($targetStuff["$type.$path"]);
  761. }
  762. function checkFolder($id,$path) {
  763. global $readClient, $writeClient;
  764. global $oldPath, $newPath;
  765. global $oldSite, $newSite;
  766. global $skipPattern;
  767. global $dryrun;
  768. global $verbose;
  769. global $checked;
  770. global $exit_on_error;
  771. global $newId;
  772. $type = 'folder';
  773. if (isset($checked["$type.$path"])) {
  774. return;
  775. } else {
  776. $checked["$type.$path"] = 1;
  777. }
  778. if (isset($skipPattern) && preg_match("#$skipPattern#i",$path)) {
  779. echo "*** Skipping $type $path\n";
  780. return;
  781. }
  782. # if (!preg_match("#^$oldPath(/|$)#",$path)
  783. # && !preg_match("#^$path(/|$)#",$oldPath)) {
  784. # echo "*** Skipping $path\n";
  785. # return; # don't back up too far
  786. # }
  787. #
  788. # read source asset
  789. #
  790. if ($verbose>2) out2("Checking $type $path...");
  791. if (isset($id)) {
  792. $ident->id = $id;
  793. }
  794. $ident->path = $path;
  795. $ident->siteName = $oldSite;
  796. $ident->type = $type;
  797. $readClient->read($ident);
  798. if (!$readClient->success) {
  799. echo "Failed reading: $type " . $path . "\n";
  800. echo print_r($ident);
  801. echo cleanup($readClient->response);
  802. if ($exit_on_error) cleanexit(); else return;
  803. }
  804. $asset = $readClient->asset;
  805. #
  806. # save asset id to get around CSI-108
  807. #
  808. $ident->id = $asset->$type->id;
  809. #
  810. # see if asset already exists in new location
  811. # (even if we know its newID, we still need to check for children)
  812. #
  813. if ($verbose>3) out3("Checking new $type " . getPath($path) . " ...");
  814. $newident->path = getPath($path);
  815. $newident->siteName = $newSite;
  816. $newident->type = $ident->type;
  817. $writeClient->read($newident);
  818. if ($writeClient->success) {
  819. #
  820. # Note any children, since we don't need to create them
  821. #
  822. $newasset = $writeClient->asset;
  823. $newId[$asset->$type->id] = $newasset->$type->id;
  824. #echo "Found2 new Id for $type $path\n";
  825. if (isset($newasset->$type->children)
  826. && isset($newasset->$type->children->child)) {
  827. $children = $newasset->$type->children->child;
  828. if (!is_array($children)) {
  829. $children = array( $children );
  830. }
  831. #echo "Folder $path in $newSite has children\n";
  832. while ($cur = array_pop($children)) {
  833. $ctype = $cur->type;
  834. $cpath = $cur->path->path;
  835. #echo "Child $ctype $cpath\n";
  836. if (preg_match("/block_.*/",$ctype)) {
  837. $ctype = "block";
  838. } else if (preg_match("/format_.*/",$ctype)) {
  839. $ctype = "format";
  840. }
  841. if ($ctype != 'folder') {
  842. $checked["$ctype.$cpath"] = 1;
  843. }
  844. $newId["$ctype.$cpath"] = $cur->id;
  845. #echo "Saving new Id " . $cur->id . " for $ctype $cpath\n";
  846. #if ($verbose>2) echo "Ok: $ctype $cpath\n";
  847. }
  848. }
  849. return;
  850. }
  851. #
  852. # we need to create it
  853. #
  854. if (isset($asset->$type->parentFolderPath)) {
  855. checkFolder($asset->$type->parentFolderId,$asset->$type->parentFolderPath);
  856. }
  857. #
  858. # if this is the main folder we're copying, we may need to rename it
  859. #
  860. if ($path == $oldPath) {
  861. $oldName = preg_replace(",^.*/,", "", $oldPath);
  862. $newName = preg_replace(",^.*/,", "", $newPath);
  863. if ($oldName != $newName) {
  864. $asset->$type->name = $newName;
  865. }
  866. }
  867. $asset->$type->parentFolderPath = getPath($asset->$type->parentFolderPath);
  868. checkAdminAsset($asset->$type->metadataSetId,$asset->$type->metadataSetPath,'metadataSet');
  869. setSite($asset,$type);
  870. removeIds($asset->$type);
  871. unset($asset->$type->path);
  872. unset($asset->$type->children);
  873. if ($verbose) echo "Creating: $type " . getPath($path) . "\n";
  874. if (!$dryrun) {
  875. $writeClient->create($asset);
  876. if ($writeClient->success) {
  877. $newId[$ident->id] = $writeClient->createdAssetId;
  878. #echo "Created $type " . $newId[$ident->id] . "\n";
  879. remember($ident);
  880. } else {
  881. echo "\nFailed: $type " . getPath($path) . "\n";
  882. print_r($asset);
  883. echo cleanup($writeClient->response);
  884. if ($exit_on_error) cleanexit(); else return;
  885. }
  886. }
  887. }
  888. function checkAdminAsset($id,$path,$type) {
  889. global $readClient, $writeClient;
  890. global $host, $host2;
  891. global $oldSite, $newSite;
  892. global $dryrun;
  893. global $verbose;
  894. global $checked;
  895. global $exit_on_error;
  896. global $newId;
  897. global $firstPass;
  898. global $pageConfigurationSets;
  899. if (isset($checked["$type.$path"])) {
  900. return;
  901. } else {
  902. $checked["$type.$path"] = 1;
  903. }
  904. #
  905. # reference to another site
  906. #
  907. if (preg_match('/^(.*):(.*)/',$path,$matches)) {
  908. global $host, $host2;
  909. if ($host == $host2) {
  910. if ($verbose>2) out2("Ok: $type $path");
  911. return;
  912. } else {
  913. $site = $matches[1];
  914. $path = $matches[2];
  915. if ($verbose>2) out2("Checking $type $site:$path");
  916. $newident->path = $path;
  917. $newident->siteName = $site;
  918. $newident->type = strtolower($type);
  919. $writeClient->read($newident);
  920. if (!$writeClient->success) {
  921. if (preg_match('/Unable to identify an entity/',$writeClient->response)) {
  922. echo "
  923. *** Found cross-site reference to $site $type $path, which does not exist
  924. in target environment. You must change this before the copy can proceed.
  925. ";
  926. } else {
  927. echo "Failed reading: " . $site . ': ' . $path . "\n";
  928. echo print_r($newident);
  929. echo cleanup($writeClient->response);
  930. }
  931. if ($exit_on_error) cleanexit(); else return;
  932. }
  933. return;
  934. }
  935. }
  936. #
  937. # read source asset
  938. #
  939. if ($verbose>2) out2("Checking $type $path");
  940. if (isset($id)) {
  941. $ident->id = $id;
  942. }
  943. $ident->path = $path;
  944. $ident->siteName = $oldSite;
  945. $ident->type = strtolower($type);
  946. $readClient->read($ident);
  947. if (!$readClient->success) {
  948. echo "Read failure on $type $path: " . cleanup($readClient->response);
  949. echo print_r($ident);
  950. if ($exit_on_error) cleanexit(); else return;
  951. }
  952. $asset = $readClient->asset;
  953. $otype = $type;
  954. $container = $type.'Container';
  955. if ($type == 'destination') {
  956. if($oldSite == '') {
  957. $container = 'target';
  958. } else {
  959. $container = 'siteDestinationContainer';
  960. }
  961. } else if (preg_match('/Connector$/', $type)) {
  962. $container = 'connectorContainer';
  963. }
  964. if (!isset($asset->$type)) {
  965. if (isset($asset->fileSystemTransport)) {
  966. $type = "fileSystemTransport";
  967. $container = "transportContainer";
  968. } else if (isset($asset->ftpTransport)) {
  969. $type = "ftpTransport";
  970. $container = "transportContainer";
  971. } else if (isset($asset->databaseTransport)) {
  972. $type = "databaseTransport";
  973. $container = "transportContainer";
  974. } else if (isset($asset->metadataSet)) {
  975. $type = "metadataSet";
  976. $container = "metadataSetContainer";
  977. }
  978. }
  979. #
  980. # save asset id to get around CSI-108
  981. #
  982. $ident->id = $asset->$type->id;
  983. #
  984. # see if asset already exists in new location
  985. #
  986. $parent = preg_replace("#/[^/]*$#", "", $path);
  987. if (isset($checked["$container.$parent"])) {
  988. # we know it doesn't exist
  989. #echo "Not checking $type: $path (it can't exist)\n";
  990. } else if (isset($newId["$otype.$path"])) {
  991. # we know it does exist
  992. $newId[$asset->$type->id] = $newId["$otype.$path"];
  993. #echo "Found new Id for $type $path\n";
  994. return;
  995. } else {
  996. if ($verbose>3) out3("Checking new $type $path");
  997. $newident->path = $path;
  998. $newident->siteName = $newSite;
  999. $newident->type = $ident->type;
  1000. $writeClient->read($newident);
  1001. if ($writeClient->success) {
  1002. $newasset = $writeClient->asset;
  1003. $newId[$asset->$type->id] = $newasset->$type->id;
  1004. #echo "Found2 new Id for $type $path\n";
  1005. return;
  1006. } else if (preg_match('/Unable to identify/',$writeClient->response)) {
  1007. #echo "Can't find $type " . $ident->path . " in $newSite\n";
  1008. } else {
  1009. echo "Read failure on destination: " . getPath($path) . cleanup($writeClient->response);
  1010. print_r($newident);
  1011. if ($exit_on_error) cleanexit(); else return;
  1012. }
  1013. }
  1014. if (isset($asset->fileSystemTransport)) {
  1015. if (!$asset->$type->directory) {
  1016. echo "*** Skipping $type $path: directory is required\n";
  1017. return;
  1018. }
  1019. } else if (isset($asset->ftpTransport)) {
  1020. echo "*** Can't set password on $type: $path\n";
  1021. $asset->$type->password = 'UNKNOWN';
  1022. } else if (isset($asset->databaseTransport)) {
  1023. // Cascade 6.7.3 WS can't create database transport with siteId=0
  1024. //$asset->$type->transportSiteId = 1;
  1025. }
  1026. if (isset($asset->$type->parentContainerPath)) {
  1027. if ($type == 'destination' && $oldSite == '') {
  1028. checkTarget($asset->$type->parentContainerPath);
  1029. } else {
  1030. checkContainer($asset->$type->parentContainerId,$asset->$type->parentContainerPath,$container);
  1031. }
  1032. }
  1033. if ($type == 'destination') {
  1034. if (isset($asset->$type->transportPath)) {
  1035. checkAdminAsset($asset->$type->transportId,$asset->$type->transportPath,'transport');
  1036. }
  1037. }
  1038. if ($type == 'assetFactory') {
  1039. if ($asset->$type->assetType == 'format') {
  1040. # see http://issues.hannonhill.com/browse/CSI-222
  1041. echo "*** Skipping asset factory of type 'format': " . $path . "\n";
  1042. return;
  1043. }
  1044. if (isset($asset->$type->assetType) && isset($asset->$type->baseAssetPath)) {
  1045. if ($asset->$type->assetType == 'folder') {
  1046. checkFolder($asset->$type->baseAssetId,$asset->$type->baseAssetPath);
  1047. } else {
  1048. checkAsset($asset->id,$asset->$type->baseAssetPath,$asset->$type->assetType);
  1049. }
  1050. }
  1051. if (isset($asset->$type->placementFolderPath)) {
  1052. checkFolder($asset->$type->placementFolderId,$asset->$type->placementFolderPath);
  1053. }
  1054. # as per CSCD-4324 asset factory's with multiple applicable groups do not persist
  1055. if (isset($asset->$type->applicableGroups)) {
  1056. $asset->$type->applicableGroups =
  1057. preg_replace("/,/", ";", $asset->$type->applicableGroups);
  1058. }
  1059. if (isset($asset->$type->workflowDefinitionPath)) {
  1060. checkAdminAsset($asset->$type->workflowDefinitionId,$asset->$type->workflowDefinitionPath,'workflowDefinition');
  1061. }
  1062. } else if ($type == 'pageConfigurationSet') {
  1063. if (isset($asset->$type->pageConfigurations)) {
  1064. if($firstPass && $host == $host2 && $oldSite != '' && $newSite != '') {
  1065. // To avoid dependency loops when copying between sites,
  1066. // where both sites are in the same instance of Cascade,
  1067. // on the first pass we make an exact copy of the configuration sets
  1068. // (where all regions and templates refer to the original site)
  1069. // We record this here and fix it on the second pass
  1070. $pageConfigurationSets[$asset->$type->path] = $asset->$type->path;
  1071. } else {
  1072. //update configurations to point to the new site
  1073. adjustPageConfigurations($path,$asset->$type->pageConfigurations);
  1074. }
  1075. }
  1076. if ($oldSite == '' && $newSite != '') {
  1077. #echo "*** Please set Output File Extension in $type $path\n";
  1078. }
  1079. } else if ($type == 'publishSet') {
  1080. foreach (array('pages','files','folders') as $pstype) {
  1081. $area = $asset->$type->$pstype;
  1082. if (isset($area->publishableAssetIdentifier) &&
  1083. isset($area->publishableAssetIdentifier)) {
  1084. $children = $area->publishableAssetIdentifier;
  1085. if (!is_array($children)) {
  1086. $children = array( $children );
  1087. }
  1088. #echo "*** Removing Ids from $pstype in $path\n";
  1089. foreach ($children as &$item) {
  1090. unset($item->id);
  1091. }
  1092. }
  1093. }
  1094. } else if ($type == 'contentType') {
  1095. if (isset($asset->$type->metadataSetPath)) {
  1096. checkAdminAsset($asset->$type->metadataSetId,$asset->$type->metadataSetPath,'metadataSet');
  1097. }
  1098. if (isset($asset->$type->pageConfigurationSetPath)) {
  1099. checkAdminAsset($asset->$type->pageConfigurationSetId,$asset->$type->pageConfigurationSetPath,'pageConfigurationSet');
  1100. }
  1101. if (isset($asset->$type->dataDefinitionPath)) {
  1102. checkAdminAsset($asset->$type->dataDefinitionId,$asset->$type->dataDefinitionPath,'dataDefinition');
  1103. }
  1104. if (isset($asset->$type->contentTypePageConfigurations)) {
  1105. adjustPageConfigurations($asset->$type->pageConfigurationSetPath,$asset->$type->contentTypePageConfigurations);
  1106. }
  1107. }
  1108. setSite($asset,$type);
  1109. removeIds($asset->$type);
  1110. unset($asset->$type->path);
  1111. if ($verbose>1) echo "Creating: $type " . $path . "\n";
  1112. if ($verbose==1) echo ".";
  1113. if (!$dryrun) {
  1114. $writeClient->create($asset);
  1115. if ($writeClient->success) {
  1116. $newId[$ident->id] = $writeClient->createdAssetId;
  1117. #echo "Created $type " . $newId[$ident->id] . "\n";
  1118. remember($ident);
  1119. } else {
  1120. echo "\nFailed: $type " . $path . "\n";
  1121. print_r($asset);
  1122. echo cleanup($writeClient->response);
  1123. if ($exit_on_error) cleanexit(); else return;
  1124. }
  1125. }
  1126. }
  1127. function checkContainer($id,$path,$type) {
  1128. global $readClient, $writeClient;
  1129. global $oldSite, $newSite;
  1130. global $dryrun;
  1131. global $verbose;
  1132. global $checked;
  1133. global $exit_on_error;
  1134. global $newId;
  1135. if (isset($checked["$type.$path"])) {
  1136. return;
  1137. } else {
  1138. $checked["$type.$path"] = 1;
  1139. }
  1140. #
  1141. # read source asset
  1142. #
  1143. if ($verbose>2) out2("Checking $type $path...");
  1144. if (isset($id)) {
  1145. $ident->id = $id;
  1146. }
  1147. $ident->path = $path;
  1148. $ident->siteName = $oldSite;
  1149. $ident->type = strtolower($type);
  1150. $readClient->read($ident);
  1151. if (!$readClient->success) {
  1152. echo "Failed reading: $type " . $path . "\n";
  1153. echo print_r($ident);
  1154. echo cleanup($readClient->response);
  1155. if ($exit_on_error) cleanexit(); else return;
  1156. }
  1157. $asset = $readClient->asset;
  1158. #
  1159. # save asset id to get around CSI-108
  1160. #
  1161. $ident->id = $asset->$type->id;
  1162. #
  1163. # see if asset already exists in new location
  1164. # (even if we know its newID, we still need to check for children)
  1165. #
  1166. if ($verbose>3) out3("Checking new $type $path ...");
  1167. $newident->path = $path;
  1168. $newident->siteName = $newSite;
  1169. $newident->type = $ident->type;
  1170. $writeClient->read($newident);
  1171. if ($writeClient->success) {
  1172. #
  1173. # Note any children, since we don't need to create them
  1174. #
  1175. $newasset = $writeClient->asset;
  1176. $newId[$asset->$type->id] = $newasset->$type->id;
  1177. if (isset($newasset->$type->children)
  1178. && isset($newasset->$type->children->child)) {
  1179. $children = $newasset->$type->children->child;
  1180. if (!is_array($children)) {
  1181. $children = array( $children );
  1182. }
  1183. while ($cur = array_pop($children)) {
  1184. $ctype = $cur->type;
  1185. $cpath = $cur->path->path;
  1186. if ($ctype == 'assetfactory') $ctype = 'assetFactory';
  1187. if ($ctype == 'contenttype') $ctype = 'contentType';
  1188. if ($ctype == 'metadataset') $ctype = 'metadataSet';
  1189. if ($ctype == 'pageconfigurationset') $ctype = 'pageConfigurationSet';
  1190. if ($ctype == 'publishset') $ctype = 'publishSet';
  1191. if ($ctype == 'workflowdefinition') $ctype = 'workflowDefinition';
  1192. if ($ctype == 'datadefinition') $ctype = 'dataDefinition';
  1193. if (preg_match("/transport_.*/",$ctype)) {
  1194. $ctype = "transport";
  1195. }
  1196. if (!preg_match('/container$/',$ctype)) {
  1197. $checked["$ctype.$cpath"] = 1;
  1198. }
  1199. $newId["$ctype.$cpath"] = $cur->id;
  1200. #if ($verbose>2) echo "Ok: $ctype $cpath\n";
  1201. }
  1202. }
  1203. return;
  1204. }
  1205. if (isset($asset->$type->parentContainerPath)) {
  1206. checkContainer($asset->$type->parentContainerId,$asset->$type->parentContainerPath,$type);
  1207. }
  1208. setSite($asset,$type);
  1209. removeIds($asset->$type);
  1210. unset($asset->$type->path);
  1211. unset($asset->$type->children);
  1212. if ($verbose) echo "Creating: $type " . $path . "\n";
  1213. if (!$dryrun) {
  1214. $writeClient->create($asset);
  1215. if ($writeClient->success) {
  1216. $newId[$ident->id] = $writeClient->createdAssetId;
  1217. #echo "Created $type " . $newId[$ident->id] . "\n";
  1218. remember($ident);
  1219. } else {
  1220. echo "\nFailed: $type " . $path . "\n";
  1221. print_r($asset);
  1222. echo cleanup($writeClient->response);
  1223. if ($exit_on_error) cleanexit(); else return;
  1224. }
  1225. }
  1226. }
  1227. /*
  1228. * Copy a container and its contents
  1229. */
  1230. function add_container($id,$path,$type) {
  1231. global $oldPath;
  1232. global $oldSite, $newSite;
  1233. global $readClient, $writeClient;
  1234. global $added;
  1235. global $verbose;
  1236. global $exit_on_error;
  1237. #
  1238. # have we already checked container *and* children?
  1239. #
  1240. if (isset($added["$type.$group"])) {
  1241. return;
  1242. } else {
  1243. $added["$type.$path"] = 1;
  1244. }
  1245. #if ($verbose>2) echo "Adding $type $path...\n";
  1246. #
  1247. # create the container
  1248. #
  1249. if ($type == 'target') {
  1250. checkTarget($path);
  1251. } else {
  1252. checkContainer($id,$path,$type);
  1253. }
  1254. #
  1255. # and its children
  1256. #
  1257. if (isset($id)) {
  1258. $ident->id = $id;
  1259. }
  1260. $ident->path = $path;
  1261. $ident->siteName = $oldSite;
  1262. $ident->type = strtolower($type);
  1263. $readClient->read($ident);
  1264. if (!$readClient->success) {
  1265. echo "Failed reading: $type " . $path . "\n";
  1266. echo print_r($ident);
  1267. echo cleanup($readClient->response);
  1268. if ($exit_on_error) cleanexit(); else return;
  1269. }
  1270. $asset = $readClient->asset;
  1271. if (isset($asset->$type->children)
  1272. && isset($asset->$type->children->child)) {
  1273. $children = $asset->$type->children->child;
  1274. if (!is_array($children)) {
  1275. $children = array( $children );
  1276. }
  1277. } else {
  1278. $children = "";
  1279. }
  1280. #
  1281. # types of things we find in containers
  1282. #
  1283. $types = array(
  1284. "assetFactoryContainer",
  1285. "pageConfigurationSetContainer",
  1286. "contentTypeContainer",
  1287. "dataDefinitionContainer",
  1288. "metadataSetContainer",
  1289. "publishSetContainer",
  1290. "siteDestinationContainer",
  1291. "transportContainer",
  1292. "workflowDefinitionContainer",
  1293. "assetFactory",
  1294. "pageConfigurationSet",
  1295. "contentType",
  1296. "dataDefinition",
  1297. "metadataSet",
  1298. "publishSet",
  1299. "destination",
  1300. "target",
  1301. "transport",
  1302. "workflowDefinition",
  1303. "connectorContainer",
  1304. "twitterConnector",
  1305. "wordPressConnector",
  1306. "googleAnalyticsConnector",
  1307. );
  1308. $names['transport_db'] = 'transport';
  1309. $names['transport_fs'] = 'transport';
  1310. $names['transport_ftp'] = 'transport';
  1311. foreach ($types as $type) {
  1312. $names[strtolower($type)] = $type;
  1313. }
  1314. if (is_array($children)) {
  1315. while ($cur = array_pop($children)) {
  1316. if (array_key_exists($cur->type,$names)) {
  1317. if (preg_match('/container$/',$cur->type)) {
  1318. add_container($cur->id, $cur->path->path, $names[$cur->type]);
  1319. } else if ($cur->type == 'target') {
  1320. checkTarget($cur->path->path);
  1321. add_container($cur->id, $cur->path->path, $names[$cur->type]);
  1322. } else {
  1323. checkAdminAsset($cur->id, $cur->path->path, $names[$cur->type]);
  1324. }
  1325. } else {
  1326. echo "Oops: don't know what to do with "
  1327. . $cur->type . " " . $cur->path->path . "\n";
  1328. }
  1329. }
  1330. }
  1331. }
  1332. #
  1333. # We want assets if they are in the original site and path,
  1334. # or if they already exist in the new site and path.
  1335. # When we find references to assets we don't want, we
  1336. # try to change them, e.g. when creating a group, if we don't
  1337. # want the groupBaseFolder, then unset this field.
  1338. #
  1339. function want($path,$type) {
  1340. global $readClient, $writeClient;
  1341. global $oldPath;
  1342. global $newSite;
  1343. global $want;
  1344. if (isset($want["$type.$path"])) {
  1345. return($want["$type.$path"]);
  1346. }
  1347. $homearea = array(
  1348. 'block', 'file', 'folder', 'page', 'reference', 'xsltFormat',
  1349. 'scriptFormat', 'symlink', 'template' );
  1350. if (in_array($type,$homearea)) {
  1351. $checkPath = $oldPath;
  1352. } else if ($type == 'transport') {
  1353. $checkPath = 'FTP and SFTP/' . $oldPath;
  1354. } else if ($type == 'group') {
  1355. $checkPath = preg_replace(",/,", "|", $oldPath); # match any component
  1356. } else {
  1357. $checkPath = preg_replace("#^www/#","",$oldPath);
  1358. }
  1359. if (preg_match("#^$checkPath#", $path)
  1360. || preg_match("#^$path(/|$)#",$checkPath)) {
  1361. # we want it for sure
  1362. $want["$type.$path"] = true;
  1363. return(true);
  1364. } else {
  1365. # we only want it if it already exists
  1366. if ($type == 'group' || $type == 'user') {
  1367. $ident->id = getName($path);
  1368. } else {
  1369. $ident->path = getPath($path);
  1370. }
  1371. $ident->type = strtolower($type);
  1372. $ident->siteName = $newSite;
  1373. $writeClient->read($ident);
  1374. if ($writeClient->success) {
  1375. $want["$type.$path"] = true;
  1376. return(true);
  1377. }
  1378. }
  1379. $want["$type.$path"] = false;
  1380. return(false);
  1381. }
  1382. function checkGroup($group) {
  1383. global $readClient, $writeClient;
  1384. global $oldPath;
  1385. global $newSite;
  1386. global $dryrun;
  1387. global $verbose;
  1388. global $checked;
  1389. global $exit_on_error;
  1390. $type = "group";
  1391. if (isset($checked["$type.$group"])) {
  1392. return;
  1393. } else {
  1394. $checked["$type.$group"] = 1;
  1395. }
  1396. if ($verbose>2) out2("Checking $type " . getName($group) . "...");
  1397. $ident->id = getName($group);
  1398. $ident->type = strtolower($type);
  1399. $writeClient->read($ident);
  1400. if (!$writeClient->success) {
  1401. $ident->id = $group;
  1402. $readClient->read($ident);
  1403. if (!$readClient->success) {
  1404. echo "Failed reading: " . $group . "\n";
  1405. echo cleanup($readClient->response);
  1406. if ($exit_on_error) cleanexit(); else return;
  1407. }
  1408. $asset = $readClient->asset;
  1409. $asset->$type->groupName = getName($group);
  1410. #
  1411. # only keep members with accounts in new environment
  1412. #
  1413. global $host, $host2;
  1414. if ($host != $host2 && isset($asset->$type->users)) {
  1415. $users = preg_split('/;/',$asset->$type->users);
  1416. $newusers = array();
  1417. foreach ($users as &$user) {
  1418. $ident->id = $user;
  1419. $ident->type = 'user';
  1420. $writeClient->read($ident);
  1421. if ($writeClient->success) {
  1422. array_push($newusers,$user);
  1423. } else {
  1424. if ($verbose>1) echo "*** Skipping user $user\n";
  1425. }
  1426. }
  1427. $asset->$type->users = join(';',$newusers);
  1428. }
  1429. removeIds($asset->$type);
  1430. if (want($asset->$type->groupAssetFactoryContainerPath,'assetFactoryContainer')) {
  1431. checkContainer($asset->$type->groupAssetFactoryContainerId,$asset->$type->groupAssetFactoryContainerPath,'assetFactoryContainer');
  1432. } else {
  1433. if ($verbose>1) echo "*** Adjusting groupAssetFactoryContainerPath in $type $group\n";
  1434. $asset->$type->groupAssetFactoryContainerPath = null;
  1435. }
  1436. if (isset($asset->$type->groupStartingPagePath)) {
  1437. if (want($asset->$type->groupStartingPagePath,'page')) {
  1438. checkAsset($asset->id,$asset->$type->groupStartingPagePath,'page');
  1439. $asset->$type->groupStartingPagePath =
  1440. getPath($asset->$type->groupStartingPagePath);
  1441. } else {
  1442. if ($verbose>1) echo "*** Adjusting groupStartingPagePath in $type $group\n";
  1443. $asset->$type->groupStartingPagePath = null;
  1444. }
  1445. }
  1446. if (isset($asset->$type->groupBaseFolderPath)) {
  1447. if (want($asset->$type->groupBaseFolderPath,'folder')) {
  1448. checkFolder($asset->$type->$groupBaseFolderId,$asset->$type->groupBaseFolderPath);
  1449. $asset->$type->groupBaseFolderPath =
  1450. getPath($asset->$type->groupBaseFolderPath);
  1451. } else {
  1452. if ($verbose>1) echo "*** Adjusting groupBaseFolderPath in $type $group\n";
  1453. $asset->$type->groupBaseFolderPath = null;
  1454. }
  1455. }
  1456. if ($verbose) echo "Creating: $type " . getName($group) . "\n";
  1457. if (!$dryrun) {
  1458. $writeClient->create($asset);
  1459. if (!$writeClient->success) {
  1460. if (!preg_match('/already exists/', $writeClient->message)) {
  1461. echo "\nFailed: $type " . getName($group) . "\n";
  1462. print_r($asset);
  1463. echo cleanup($writeClient->response);
  1464. if ($exit_on_error) cleanexit(); else return;
  1465. }
  1466. }
  1467. }
  1468. }
  1469. }
  1470. function checkUser($user) {
  1471. global $readClient, $writeClient;
  1472. global $newSite;
  1473. global $dryrun;
  1474. global $verbose;
  1475. global $checked;
  1476. global $exit_on_error;
  1477. $type = "user";
  1478. if (isset($checked["$type.$user"])) {
  1479. return;
  1480. } else {
  1481. $checked["$type.$user"] = 1;
  1482. }
  1483. if ($verbose>2) out2("Checking $type " . getName($user) . "...");
  1484. $ident->id = getName($user);
  1485. $ident->type = strtolower($type);
  1486. $writeClient->read($ident);
  1487. if (!$writeClient->success) {
  1488. $ident->id = $user;
  1489. $readClient->read($ident);
  1490. if (!$readClient->success) {
  1491. echo "Failed reading: " . $user . "\n";
  1492. echo cleanup($readClient->response);
  1493. if ($exit_on_error) cleanexit(); else return;
  1494. }
  1495. $asset = $readClient->asset;
  1496. if (isset($asset->$type->defaultGroup)) {
  1497. checkGroup($asset->$type->defaultGroup);
  1498. $asset->$type->defaultGroup =
  1499. getName($asset->$type->defaultGroup);
  1500. }
  1501. if (isset($asset->$type->groups)) {
  1502. $groups = preg_split('/;/',$asset->$type->groups);
  1503. $newgroups = array();
  1504. foreach ($groups as &$group) {
  1505. if (getName($group) == $asset->$type->defaultGroup) {
  1506. array_push($newgroups,getName($group));
  1507. } else if (want($group,'group')) {
  1508. checkGroup($group);
  1509. array_push($newgroups,getName($group));
  1510. }
  1511. }
  1512. $asset->$type->groups = join(';',$newgroups);
  1513. }
  1514. echo "*** Can't set password on $type: $user\n";
  1515. if ($verbose) echo "Creating: $type " . getName($user) . "\n";
  1516. if (!$dryrun) {
  1517. $writeClient->create($asset);
  1518. if (!$writeClient->success) {
  1519. echo "\nFailed: $type " . getName($user) . "\n";
  1520. print_r($asset);
  1521. echo cleanup($writeClient->response);
  1522. if ($exit_on_error) cleanexit(); else return;
  1523. }
  1524. }
  1525. }
  1526. }
  1527. /*
  1528. * Gets the new folder path, by converting it from the old to the new
  1529. */
  1530. function getPath($folderPath) {
  1531. global $oldPath;
  1532. global $newPath;
  1533. if (!isset($folderPath)) {
  1534. return($folderPath);
  1535. }
  1536. if ($folderPath[0] == "/") {
  1537. $folderPath = substr($folderPath, 1);
  1538. }
  1539. //if it's in the current path
  1540. if (strpos($folderPath, $oldPath) === 0) {
  1541. /* tack newPath in front, then strip the trailing / if there is one */
  1542. $folderPath = $newPath . "/" . substr($folderPath, strlen($oldPath));
  1543. $folderPath = str_replace("//", "/", $folderPath);
  1544. if (substr($folderPath, strlen($folderPath) - 1) === "/") {
  1545. $folderPath = substr($folderPath, 0, strlen($folderPath) - 1);
  1546. }
  1547. }
  1548. if ($folderPath == "") $folderPath = "/";
  1549. return $folderPath;
  1550. }
  1551. /*
  1552. * Get the new group or username, by converting it from the old to the new
  1553. */
  1554. function getName($name) {
  1555. global $oldPath;
  1556. global $newPath;
  1557. global $newId;
  1558. $oldName = preg_replace(",^.*/,", "", $oldPath);
  1559. $newName = preg_replace(",^.*/,", "", $newPath);
  1560. if (preg_match("/^$oldName/", $name)) {
  1561. $name = preg_replace("/^$oldName/", $newName, $name);
  1562. }
  1563. return $name;
  1564. }
  1565. function removeIds($item) {
  1566. global $newId;
  1567. foreach ($item as $var => $value) {
  1568. if ($var == 'id' || $var == 'siteId') {
  1569. unset($item->$var);
  1570. } else if ($var == 'transportSiteId') {
  1571. # it's ok
  1572. } else if (preg_match("/^(.*)Id$/",$var,$matches)) {
  1573. if (!isset($value)) {
  1574. unset($item->$var);
  1575. } else if (isset($newId[$value])) {
  1576. $item->$var = $newId[$value];
  1577. #echo "** Changing $var from $value to " . $newId[$value] . "\n";
  1578. } else {
  1579. $type = $matches[1];
  1580. $pvar = $type . 'Path';
  1581. $path = $item->$pvar;
  1582. if ($type == 'baseAsset') {
  1583. $type = $item->assetType;
  1584. }
  1585. $type = strtolower($type);
  1586. #echo "** Looking for newId for $type "
  1587. #. " ($var " . $item->$var . ") "
  1588. #. " ($pvar " . $item->$pvar . ")"
  1589. #. "\n";
  1590. if (isset($path) && isset($newId["$type.$path"])) {
  1591. $item->$var = $newId["$type.$path"];
  1592. #echo "** Changing2 $var from $value to " . $newId["$type.$path"] . "\n";
  1593. } else {
  1594. if (isset($item->$var)) {
  1595. unset($item->$var);
  1596. #echo "** Clearing $var $value\n";
  1597. }
  1598. }
  1599. }
  1600. }
  1601. }
  1602. }
  1603. function adjustPageConfigurations($path,$pageConfigurations) {
  1604. global $oldSite, $newSite;
  1605. global $targetStuff;
  1606. global $warned;
  1607. if (isset($pageConfigurations) &&
  1608. isset($pageConfigurations->pageConfiguration)) {
  1609. $configs = $pageConfigurations->pageConfiguration;
  1610. if (!is_array($configs)) {
  1611. $configs = array( $configs );
  1612. }
  1613. foreach ($configs as &$config) {
  1614. if (isset($config->formatPath)) {
  1615. # remove old site name so copy will use new one
  1616. $config->formatPath = preg_replace("/^$oldSite:/", '', $config->formatPath);
  1617. }
  1618. if (isset($config->templatePath)) {
  1619. $config->templatePath = preg_replace("/^$oldSite:/", '', $config->templatePath);
  1620. checkAsset($config->templateId,$config->templatePath,'template');
  1621. $config->templatePath = getPath($config->templatePath);
  1622. }
  1623. if (isset($config->pageRegions)) {
  1624. adjustPageRegions($path,$config->pageRegions);
  1625. }
  1626. if ($oldSite == '' && $newSite != ''
  1627. && !isset($config->outputExtension)) {
  1628. if (isset($targetStuff["template." . $config->templatePath])) {
  1629. list ($config->outputExtension, $config->serializationType) =
  1630. $targetStuff["template." . $config->templatePath];
  1631. #echo "Set Extension for config " . $config->name . " to " . $config->outputExtension . "\n";
  1632. } else if (!isset($warned{"$path." . $config->name})) {
  1633. echo "*** Warning: Please set Output File Extension in configuration set $path, configuration " . $config->name . "\n";
  1634. $warned{"$path." . $config->name}++;
  1635. }
  1636. }
  1637. removeIds($config);
  1638. }
  1639. }
  1640. # Content Type Page Configurations
  1641. if (isset($pageConfigurations) &&
  1642. isset($pageConfigurations->contentTypePageConfiguration)) {
  1643. $configs = $pageConfigurations->contentTypePageConfiguration;
  1644. if (!is_array($configs)) {
  1645. $configs = array( $configs );
  1646. }
  1647. foreach ($configs as &$config) {
  1648. if (isset($config->destinations)
  1649. && isset($config->destinations->destination)) {
  1650. $destinations = $config->destinations->destination;
  1651. if (!is_array($destinations)) {
  1652. $destinations = array( $destinations );
  1653. }
  1654. foreach ($destinations as &$destination) {
  1655. if (isset($destination->path->path) &&
  1656. isset($destination->id)) {
  1657. checkAdminAsset($destination->id,$destination->path->path,'destination');
  1658. }
  1659. removeIds($destination);
  1660. if (isset($destination->path)) {
  1661. removeIds($destination->path);
  1662. }
  1663. }
  1664. }
  1665. }
  1666. }
  1667. }
  1668. function adjustPageRegions($path,$pageRegions) {
  1669. global $oldSite;
  1670. if (!isset($pageRegions)) {
  1671. return;
  1672. }
  1673. if (isset($pageRegions) &&
  1674. isset($pageRegions->pageRegion)) {
  1675. $regions = $pageRegions->pageRegion;
  1676. if (!is_array($regions)) {
  1677. $regions = array( $regions );
  1678. }
  1679. foreach ($regions as &$region) {
  1680. if (!$region->blockPath) unset($region->blockPath);
  1681. if (!$region->noBlock) unset($region->noBlock);
  1682. if (!$region->formatPath) unset($region->formatPath);
  1683. if (!$region->noFormat) unset($region->noFormat);
  1684. if (isset($region->blockPath)) {
  1685. $region->blockPath = preg_replace("/^$oldSite:/", '', $region->blockPath);
  1686. checkAsset($region->blockId,$region->blockPath,"block");
  1687. $region->blockPath = getPath($region->blockPath);
  1688. }
  1689. if (isset($region->templatePath)) {
  1690. $region->templatePath = preg_replace("/^$oldSite:/", '', $region->templatePath);
  1691. }
  1692. if (isset($region->formatPath)) {
  1693. $region->formatPath = preg_replace("/^$oldSite:/", '', $region->formatPath);
  1694. checkAsset($region->formatId,$region->formatPath,"format");
  1695. $region->formatPath = getPath($region->formatPath);
  1696. }
  1697. removeIds($region);
  1698. }
  1699. }
  1700. }
  1701. #
  1702. # remove empty fields (which cause problems in some Cascade versions)
  1703. # and remove asset IDs. Removing IDs makes Cascade use paths instead,
  1704. # so references to assets in the original site become references to
  1705. # the corresponding assets in the new one.
  1706. #
  1707. function adjustStructuredData($path,$structuredDataNodes) {
  1708. global $checked;
  1709. global $verbose;
  1710. if (isset($structuredDataNodes) &&
  1711. isset($structuredDataNodes->structuredDataNode)) {
  1712. $nodes = $structuredDataNodes->structuredDataNode;
  1713. if (!is_array($nodes)) {
  1714. $nodes = array( $nodes );
  1715. }
  1716. foreach ($nodes as &$node) {
  1717. removeIds($node);
  1718. if ($node->type == 'asset') {
  1719. if ($node->assetType != 'block') unset($node->blockPath);
  1720. if ($node->assetType != 'file') unset($node->filePath);
  1721. if ($node->assetType != 'page') unset($node->pagePath);
  1722. if ($node->assetType != 'symlink') unset($node->symlinkPath);
  1723. unset($node->text);
  1724. } else {
  1725. unset($node->assetType);
  1726. unset($node->blockPath);
  1727. unset($node->filePath);
  1728. unset($node->pagePath);
  1729. unset($node->symlinkPath);
  1730. }
  1731. if (isset($node->blockPath)) {
  1732. if (isset($newId["block." . $blockPath])) {
  1733. #echo "Found new Id for block " . $node->blockPath . "in $path\n";
  1734. $node->blockId = $newId["block." . $blockPath];
  1735. }
  1736. checkAsset($node->blockId,$node->blockPath,'block');
  1737. $node->blockPath = getPath($node->blockPath);
  1738. }
  1739. if (isset($node->filePath)) {
  1740. checkAsset($node->fileId,$node->filePath,'file');
  1741. $node->filePath = getPath($node->filePath);
  1742. }
  1743. if (isset($node->pagePath)) {
  1744. checkAsset($node->pageId,$node->pagePath,'page');
  1745. $node->pagePath = getPath($node->pagePath);
  1746. }
  1747. if (isset($node->symlinkPath)) {
  1748. checkAsset($node->symlinkId,$node->symlinkPath,'symlink');
  1749. $node->symlinkPath = getPath($node->symlinkPath);
  1750. }
  1751. if ($node->type == 'group') {
  1752. adjustStructuredData($path,$node->structuredDataNodes);
  1753. } else {
  1754. unset($node->structuredDataNodes);
  1755. }
  1756. #
  1757. # web services won't let us create a file field without a file ...
  1758. #
  1759. if ($node->type == 'asset') {
  1760. if ($node->assetType == 'file' && !isset($node->filePath)) {
  1761. $node->filePath = null;
  1762. } else if ($node->assetType == 'page' && !isset($node->pagePath)) {
  1763. $node->pagePath = null;
  1764. } else if ($node->assetType == 'block' && !isset($node->blockPath)) {
  1765. $node->blockPath = null;
  1766. }
  1767. }
  1768. if ($node->type == 'text' && !isset($node->text)) {
  1769. $node->text = null;
  1770. }
  1771. }
  1772. }
  1773. }
  1774. #
  1775. # remember things we've created so we can set permissions later
  1776. #
  1777. function remember($ident) {
  1778. global $created;
  1779. global $inherit_permissions;
  1780. if (!$inherit_permissions) {
  1781. $created[] = $ident;
  1782. }
  1783. }
  1784. function setPermissions() {
  1785. global $created;
  1786. global $verbose;
  1787. if (is_array($created)) {
  1788. if ($verbose>1)
  1789. echo "\nSetting access rights ...\n";
  1790. while ($ident = array_shift($created)) {
  1791. setAccessRights($ident);
  1792. }
  1793. }
  1794. }
  1795. function setAccessRights($ident) {
  1796. global $readClient, $writeClient;
  1797. global $dryrun;
  1798. global $verbose;
  1799. $readClient->readAccessRights($ident);
  1800. if ($readClient->success) {
  1801. $accessRights = $readClient->response->readAccessRightsReturn->accessRightsInformation;
  1802. $accessRights = adjustAccessRights($accessRights);
  1803. if ($verbose>2)
  1804. out2("Setting access rights on " . $ident->type . " " . getPath($ident->path));
  1805. if (!$dryrun) {
  1806. $writeClient->editAccessRights($accessRights, false);
  1807. if (!$writeClient->success) {
  1808. echo "\nFailed to set access rights: " . $ident->type . " " . getPath($ident->path) . "\n";
  1809. print_r($accessRights);
  1810. echo cleanup($writeClient->response);
  1811. if ($exit_on_error) cleanexit(); else return;
  1812. }
  1813. }
  1814. } else {
  1815. echo "\nFailed to read access rights: " . $ident->type . " " . getPath($ident->path) . "\n";
  1816. print_r($ident);
  1817. echo cleanup($readClient->response);
  1818. }
  1819. }
  1820. function adjustAccessRights($accessRights) {
  1821. global $newSite;
  1822. $accessRights->identifier->path->path =
  1823. getPath($accessRights->identifier->path->path);
  1824. if ($newSite == '') {
  1825. $accessRights->identifier->path->siteName = "";
  1826. } else {
  1827. $accessRights->identifier->path->siteName = $newSite;
  1828. }
  1829. unset($accessRights->identifier->path->siteId);
  1830. unset($accessRights->identifier->id);
  1831. #
  1832. # See http://issues.hannonhill.com/browse/CSI-219
  1833. #
  1834. if ($accessRights->identifier->type == 'structureddatadefinition') {
  1835. $accessRights->identifier->type = 'datadefinition';
  1836. }
  1837. if ($accessRights->identifier->type == 'structureddatadefinitioncontainer') {
  1838. $accessRights->identifier->type = 'datadefinitioncontainer';
  1839. }
  1840. if (isset($accessRights->aclEntries) &&
  1841. isset($accessRights->aclEntries->aclEntry)) {
  1842. $nodes = $accessRights->aclEntries->aclEntry;
  1843. if (!is_array($nodes)) {
  1844. $nodes = array( $nodes );
  1845. }
  1846. $newnodes = array();
  1847. foreach ($nodes as &$node) {
  1848. if ($node->type == 'group') {
  1849. if (want($node->name,'group')) {
  1850. checkGroup($node->name);
  1851. $node->name = getName($node->name);
  1852. array_push($newnodes,$node);
  1853. }
  1854. } else if ($node->type == 'user') {
  1855. checkUser($node->name);
  1856. array_push($newnodes,$node);
  1857. }
  1858. }
  1859. $accessRights->aclEntries->aclEntry = $newnodes;
  1860. }
  1861. return $accessRights;
  1862. }
  1863. function setSite($asset,$type) {
  1864. global $oldSite, $newSite;
  1865. if ($newSite == '') {
  1866. unset($asset->$type->siteName);
  1867. } else {
  1868. $asset->$type->siteName = $newSite;
  1869. }
  1870. }
  1871. function validateInput() {
  1872. global $host, $host2;
  1873. global $uname, $uname2;
  1874. global $pass, $pass2;
  1875. global $oldPath, $newPath;
  1876. global $oldSite, $newSite;
  1877. global $skipPattern;
  1878. global $copytype, $dryrun, $verbose;
  1879. global $exit_on_error;
  1880. global $inherit_permissions;
  1881. global $multiPass;
  1882. if ( !empty($_POST['host1'])
  1883. && !empty($_POST['site1'])
  1884. && !empty($_POST['username1'])
  1885. && !empty($_POST['password1'])
  1886. && !empty($_POST['folder1'])
  1887. && !empty($_POST['copytype'])
  1888. ) {
  1889. $host = trim($_POST['host1']);
  1890. if (empty($_POST['host2'])) {
  1891. $host2 = $host;
  1892. } else {
  1893. $host2 = $_POST['host2'];
  1894. }
  1895. $oldSite = trim($_POST['site1']);
  1896. if (empty($_POST['site2'])) {
  1897. $newSite = $oldSite;
  1898. } else {
  1899. $newSite = $_POST['site2'];
  1900. }
  1901. $uname = trim($_POST['username1']);
  1902. if (empty($_POST['username2'])) {
  1903. $uname2 = $uname;
  1904. } else {
  1905. $uname2 = $_POST['username2'];
  1906. }
  1907. $pass = $_POST['password1'];
  1908. if (empty($_POST['password2'])) {
  1909. $pass2 = $pass;
  1910. } else {
  1911. $pass2 = $_POST['password2'];
  1912. }
  1913. $oldPath = trim($_POST['folder1']);
  1914. if (empty($_POST['folder2'])) {
  1915. $newPath = $oldPath;
  1916. } else {
  1917. $newPath = $_POST['folder2'];
  1918. }
  1919. if (!empty($_POST['copytype'])) $copytype = $_POST['copytype'];
  1920. if (!empty($_POST['skipPattern'])) $skipPattern = trim($_POST['skipPattern']);
  1921. if (!empty($_POST['dryrun'])) $dryrun = $_POST['dryrun'];
  1922. if (!empty($_POST['verbose'])) $verbose++;
  1923. if (!empty($_POST['continue'])) $exit_on_error = 0;
  1924. if (!empty($_POST['inherit_permissions'])) {
  1925. $inherit_permissions = $_POST['inherit_permissions'];
  1926. }
  1927. if (!empty($_POST['multiPass'])) $multiPass = 1;
  1928. if ($host == $host2 && $oldSite == $newSite && $oldPath == $newPath) {
  1929. echo "<p>*** Sorry, you can't copy a folder over top of itself.</p><hr>\n";
  1930. return false;
  1931. }
  1932. return true;
  1933. } else {
  1934. echo "<p>*** Sorry, you did something wrong.</p><hr>\n";
  1935. return false;
  1936. }
  1937. }
  1938. function showForm() {
  1939. global $host, $host2;
  1940. global $uname, $uname2;
  1941. global $pass, $pass2;
  1942. global $oldPath, $newPath;
  1943. global $oldSite, $newSite;
  1944. global $skipPattern;
  1945. global $copytype, $dryrun, $verbose;
  1946. global $environments, $exit_on_error;
  1947. global $inherit_permissions;
  1948. global $multiPass;
  1949. #
  1950. # set up the form
  1951. #
  1952. $env1_str = $env2_str = '';
  1953. foreach ($environments as $name => $hostname) {
  1954. $select_str = '';
  1955. if ($host == $hostname) {
  1956. $select_str = "selected='selected'";
  1957. }
  1958. $env1_str .= "<option value='$hostname' $select_str>$name</option>\n";
  1959. $select_str = '';
  1960. if ($host2 == $hostname) {
  1961. $select_str = "selected='selected'";
  1962. }
  1963. $env2_str .= "<option value='$hostname' $select_str>$name</option>\n";
  1964. }
  1965. $copytype_str = '';
  1966. $types = array(
  1967. 'folder', 'site',
  1968. 'block', 'file', 'page', 'reference', 'format', 'symlink', 'template',
  1969. 'assetFactoryContainer', 'contentTypeContainer',
  1970. 'metadataSetContainer', 'pageConfigurationSetContainer', 'publishSetContainer',
  1971. 'dataDefinitionContainer', 'workflowDefinitionContainer',
  1972. 'siteDestinationContainer', 'transportContainer',
  1973. 'assetFactory', 'contentType',
  1974. 'metadataSet', 'pageConfigurationSet', 'publishSet',
  1975. 'dataDefinition', 'workflowDefinition', 'destination',
  1976. 'user', 'group',
  1977. );
  1978. if ($oldSite != '' && $newSite != '') { # new "Sites"
  1979. #array_push($types, 'connector');
  1980. array_push($types, 'transport');
  1981. array_push($types, 'destination');
  1982. }
  1983. foreach ($types as $type) {
  1984. $ptype = $type;
  1985. $ptype = preg_replace('/([A-Z])/', ' $1', $ptype);
  1986. $ptype = ucfirst($ptype);
  1987. if ($type == $copytype) {
  1988. $copytype_str .= "<option value='$type' selected='selected'>$ptype</option>\n";
  1989. } else {
  1990. $copytype_str .= "<option value='$type'>$ptype</option>\n";
  1991. }
  1992. }
  1993. $dryrun_str = '';
  1994. if ($dryrun) {
  1995. $dryrun_str = 'checked="checked"';
  1996. }
  1997. $verbose_str = '';
  1998. if ($verbose>2) {
  1999. $verbose_str = 'checked="checked"';
  2000. }
  2001. $inherit_str = '';
  2002. if ($inherit_permissions) {
  2003. $inherit_str = 'checked="checked"';
  2004. }
  2005. $multipass_str = '';
  2006. if ($multiPass) {
  2007. $multipass_str = 'checked="checked"';
  2008. }
  2009. $continue_str = '';
  2010. if ($exit_on_error == '0') {
  2011. $continue_str = 'checked="checked"';
  2012. }
  2013. $site2_str = '';
  2014. if ($newSite && $newSite != $oldSite) {
  2015. $site2_str = "value='$newSite'";
  2016. }
  2017. $username2_str = '';
  2018. if ($uname2 && $uname2 != $uname) {
  2019. $username2_str = "value='$uname2'";
  2020. }
  2021. $pass2_str = '';
  2022. if ($pass2 && $pass2 != $pass) {
  2023. $pass2_str = "value='$pass2'";
  2024. }
  2025. echo <<<END
  2026. <html>
  2027. <head>
  2028. <title>Copy assets between sites or environments</title>
  2029. </head>
  2030. <body>
  2031. <div class="container">
  2032. <h1>Copy Site</h1>
  2033. <form method="post">
  2034. <table border="0">
  2035. <tr><td></td><th align="left">Source</th><th align="left">Destination</th></tr>
  2036. <tr>
  2037. <th align="left">Environment</th>
  2038. <td>
  2039. <select name="host1">
  2040. <option value=""> -- please select -- </option>
  2041. $env1_str
  2042. </select><br />
  2043. </td><td>
  2044. <select name="host2">
  2045. <option value=""> -- please select -- </option>
  2046. $env2_str
  2047. </select><br />
  2048. </td></tr>
  2049. <tr><th align="left">Site</th>
  2050. <td>
  2051. <input type="text" name="site1" value="$oldSite" />
  2052. </td><td>
  2053. <input type="text" name="site2" $site2_str />
  2054. </td></tr>
  2055. <tr><th align="left"> Web Services Username</th>
  2056. <td>
  2057. <input type="text" name="username1" value="$uname" />
  2058. </td><td>
  2059. <input type="text" name="username2" $username2_str />
  2060. </td></tr>
  2061. <tr><th align="left">Password</th>
  2062. <td>
  2063. <input type="password" name="password1" value="$pass" />
  2064. </td><td>
  2065. <input type="password" name="password2" $pass2_str />
  2066. </td></tr>
  2067. <tr><th align="left">Folder/Container</th>
  2068. <td colspan="2">
  2069. <input type="text" name="folder1" value="$oldPath" size="40" />
  2070. <!--
  2071. </td><td>
  2072. <input type="text" name="folder2" value="$newPath" />
  2073. -->
  2074. </td></tr>
  2075. <tr><th align="left">Type of Asset to Copy</th>
  2076. <td colspan="2">
  2077. <select name="copytype">
  2078. $copytype_str
  2079. </select><br />
  2080. </td></tr>
  2081. <tr><th align="left">Assets to Skip</th>
  2082. <td colspan="2">
  2083. <input type="text" name="skipPattern" value="$skipPattern" />
  2084. <em>(e.g. images/ or .mp3)
  2085. </select><br />
  2086. </td></tr>
  2087. <tr><th align="left">Options</th>
  2088. <td colspan="2">
  2089. <input type="checkbox" name="dryrun" value="1" $dryrun_str /> Dry Run<br />
  2090. <input type="checkbox" name="verbose" value="3" $verbose_str /> Verbose<br />
  2091. <input type="checkbox" name="continue" value="1" $continue_str /> Persevere in the face of adversity<br />
  2092. <input type="checkbox" name="inherit_permissions" value="1" $inherit_str /> Inherit Permissions<br />
  2093. <input type="checkbox" name="multiPass" value="1" $multiPass_str /> Use multi-pass approach<br />
  2094. </td></tr>
  2095. <tr><td></td>
  2096. <td> <input type="submit" value="Submit" />
  2097. </td></tr>
  2098. </table>
  2099. </form>
  2100. <p><strong>Note:</strong>
  2101. We follow dependencies when copying, and may copy more than you think.
  2102. For example, when copying a folder,
  2103. we may need to copy the folder's metadata set, or groups that have access to that folder.
  2104. When copying a group, we may need to copy its base asset factory, and so on.
  2105. </p>
  2106. <p>
  2107. With <strong>Type=Folder</strong> we copy the folder and its contents.<br/>
  2108. With <strong>Type=Site</strong> we also copy admin containers with
  2109. the same name as the folder.<br/>
  2110. With <strong>Dry Run</strong> we report what needs to be done, but don't actually copy anything.<br/>
  2111. With <strong>Verbose</strong> we report in more detail.<br/>
  2112. With <strong>Persevere...</strong> we try to ignore errors and continue copying.<br/>
  2113. With <strong>Inherit Permissions</strong> we do not copy access rights, they are inherited from the parent folder/container instead.
  2114. </p>
  2115. <p>Occasionally interdependencies make it hard to copy some assets.
  2116. The <strong>multi-pass approach</strong> works around some dependency issues by creating
  2117. structured pages and configuration sets in one pass, and then edits them later.
  2118. Unfortunately this slows things down, because we read and write
  2119. each page and configuration set twice.
  2120. </p>
  2121. <p>To copy an entire site, first create the site, then do the copy,
  2122. setting <strong>Type=Site</strong> and <strong>Folder=/</strong>.
  2123. <br />
  2124. To copy from or to the Global area, set <strong>Site=Global</strong>.
  2125. </p>
  2126. <p>
  2127. You only need to specify the destination username and password if they differ from the source.
  2128. </p>
  2129. <hr/>
  2130. END;
  2131. }
  2132. #
  2133. # if an error occurs during the first pass
  2134. # try to fix the assets created so far
  2135. #
  2136. function cleanexit() {
  2137. global $dryrun;
  2138. global $been_here_done_this;
  2139. global $firstPass;
  2140. if (!$been_here_done_this) {
  2141. $been_here_done_this++;
  2142. setPermissions();
  2143. }
  2144. if ($firstPass) {
  2145. editPages();
  2146. editPageConfigurationSets();
  2147. }
  2148. if ($dryrun) {
  2149. out1("Dry Run Aborted.");
  2150. } else {
  2151. out1("Aborted.");
  2152. }
  2153. echo "</div></body></html>";
  2154. exit;
  2155. }
  2156. function cleanup($xml) {
  2157. $xml = preg_replace('/>/', ">\n", $xml);
  2158. return htmlspecialchars($xml);
  2159. }
  2160. function out1($text) {
  2161. echo "<strong>$text</strong>\n";
  2162. }
  2163. function out2($text) {
  2164. echo "<span style='color:#777777'>$text</span>\n";
  2165. }
  2166. function out3($text) {
  2167. echo "<span style='color:#999999'>$text</span>\n";
  2168. }
  2169. function warn($text) {
  2170. echo "<strong class='text-danger'>$text</strong>\n";
  2171. }
  2172. ?>