PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/_build/build.transport.databackup.php

https://github.com/jgulledge19/DataBackup
PHP | 403 lines | 233 code | 47 blank | 123 comment | 27 complexity | ac99d029a1ad43d7b6abd9dde28abdc1 MD5 | raw file
  1. <?php
  2. /**
  3. * MyComponent Build Script
  4. *
  5. * Copyright 2011 Your Name <you@yourdomain.com>
  6. *
  7. * MyComponent is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option) any
  10. * later version.
  11. *
  12. * MyComponent is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * MyComponent; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package mycomponent
  21. * @subpackage build
  22. */
  23. /**
  24. * Build MyComponent Package
  25. *
  26. * Description: Build script for MyComponent package
  27. * @package mycomponent
  28. * @subpackage build
  29. */
  30. /* See the mycomponent/core/docs/tutorial.html file for
  31. * more detailed information about using the package
  32. *
  33. * Search and replace tasks:
  34. * (edit the resource and element names first if they have
  35. * different names than your package.)
  36. *
  37. * MyComponent -> Name of your package
  38. * mycomponent -> lowercase name of your package
  39. * Your Name -> Your Actual Name
  40. * Your Site -> Name of your site
  41. * yoursite -> domain name of your site
  42. * you@yourdomain.com -> your email address
  43. * Description -> Description of file or component
  44. *
  45. * 1/1/11 -> Current date
  46. * 2011 -> Current Year
  47. * D:\web\backups\
  48. */
  49. /* Set package info be sure to set all of these */
  50. define('PKG_NAME','Databackup');
  51. define('PKG_NAME_LOWER','databackup');
  52. define('PKG_VERSION','1.1.9');
  53. define('PKG_RELEASE','pl');
  54. define('PKG_CATEGORY','Databackup');
  55. /* Set package options - you can turn these on one-by-one
  56. * as you create the transport package
  57. * */
  58. $hasAssets = FALSE; /* Transfer the files in the assets dir. */
  59. $hasCore = true; /* Transfer the files in the core dir. */
  60. $hasSnippets = true;
  61. $hasChunks = FALSE;
  62. $hasResources = false;
  63. $hasValidator = false; /* Run a validator before installing anything */
  64. $hasResolver = FALSE; /* Run a resolver after installing everything */
  65. $hasSetupOptions = FALSE; /* HTML/PHP script to interact with user */
  66. $hasMenu = FALSE; /* Add items to the MODx Top Menu */
  67. $hasSettings = true; /* Add new MODx System Settings */
  68. /* Note: TVs are connected to their templates in the script resolver
  69. * (see _build/data/resolvers/install.script.php)
  70. */
  71. $hasTemplateVariables = false;
  72. $hasTemplates = false;
  73. /* Note: plugin events are connected to their plugins in the script
  74. * resolver (see _build/data/resolvers/install.script.php)
  75. */
  76. $hasPlugins = false;
  77. $hasPluginEvents = false;
  78. $hasPropertySets = false;
  79. /* Note: property sets are connected to elements in the script
  80. * resolver (see _build/data/resolvers/install.script.php)
  81. */
  82. $hasSubPackages = false; /* add in other component packages (transport.zip files)*/
  83. /* Note: The package files will be copied to core/packages but will
  84. * have to be installed manually with "Add New Package" and "Search
  85. * Locally for Packages" in Package Manager. Be aware that the
  86. * copied packages may be older versions than ones already
  87. * installed. This is necessary because Package Manager's
  88. * autoinstall of the packages is unreliable at this point.
  89. */
  90. /******************************************
  91. * Work begins here
  92. * ****************************************/
  93. /* set start time */
  94. $mtime = microtime();
  95. $mtime = explode(" ", $mtime);
  96. $mtime = $mtime[1] + $mtime[0];
  97. $tstart = $mtime;
  98. set_time_limit(0);
  99. /* define sources */
  100. $root = dirname(dirname(__FILE__)) . '/';
  101. $sources= array (
  102. 'root' => $root,
  103. 'build' => $root . '_build/',
  104. /* note that the next two must not have a trailing slash */
  105. 'source_core' => $root.'core/components/'.PKG_NAME_LOWER.'/',
  106. 'source_assets' => $root.'assets/components/'.PKG_NAME_LOWER.'/',
  107. 'resolvers' => $root . '_build/resolvers/'.PKG_NAME_LOWER.'/',
  108. 'validators'=> $root . '_build/validators/'.PKG_NAME_LOWER.'/',
  109. 'data' => $root . '_build/data/'.PKG_NAME_LOWER.'/',
  110. 'docs' => $root . 'core/components/'.PKG_NAME_LOWER.'/docs/',
  111. 'lexicon' => $root . 'core/components/'.PKG_NAME_LOWER.'/lexicon/',
  112. 'model' => $root.'core/components/'.PKG_NAME_LOWER.'/model/',
  113. 'install_options' => $root . '_build/install.options/'.PKG_NAME_LOWER.'/',
  114. 'packages'=> $root . 'core/packages',
  115. //'schema' => $root.'_build/schema/',
  116. );
  117. unset($root);
  118. /* Instantiate MODx -- if this require fails, check your
  119. * _build/build.config.php file
  120. */
  121. require_once $sources['build'].'build.config.php';
  122. require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
  123. $modx= new modX();
  124. $modx->initialize('mgr');
  125. $modx->setLogLevel(xPDO::LOG_LEVEL_INFO);
  126. $modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
  127. /* load builder */
  128. $modx->loadClass('transport.modPackageBuilder','',false, true);
  129. $builder = new modPackageBuilder($modx);
  130. $builder->createPackage(PKG_NAME_LOWER, PKG_VERSION, PKG_RELEASE);
  131. $builder->registerNamespace(PKG_NAME_LOWER,false,true,'{core_path}components/'.PKG_NAME_LOWER.'/');
  132. /* create category The category is required and will automatically
  133. * have the name of your package
  134. */
  135. $category= $modx->newObject('modCategory');
  136. $category->set('id',1);
  137. $category->set('category',PKG_CATEGORY);
  138. /* add snippets */
  139. if ($hasSnippets) {
  140. $modx->log(modX::LOG_LEVEL_INFO,'Adding in snippets.');
  141. $snippets = include $sources['data'].'transport.snippets.php';
  142. /* note: Snippets' default properties are set in transport.snippets.php */
  143. if (is_array($snippets)) {
  144. $category->addMany($snippets, 'Snippets');
  145. } else { $modx->log(modX::LOG_LEVEL_FATAL,'Adding snippets failed.'); }
  146. }
  147. if ($hasPropertySets) { /* add property sets */
  148. $modx->log(modX::LOG_LEVEL_INFO,'Adding in property sets.');
  149. $propertysets = include $sources['data'].'transport.propertysets.php';
  150. /* note: property set' properties are set in transport.propertysets.php */
  151. if (is_array($snippets)) {
  152. $category->addMany($propertysets, 'PropertySets');
  153. } else { $modx->log(modX::LOG_LEVEL_FATAL,'Adding property sets failed.'); }
  154. }
  155. if ($hasPlugins) {
  156. $modx->log(modX::LOG_LEVEL_INFO,'Adding in Plugins.');
  157. $plugins = include $sources['data'] . 'transport.plugins.php';
  158. if (is_array($plugins)) {
  159. $category->addMany($plugins);
  160. }
  161. }
  162. /* Create Category attributes array dynamically
  163. * based on which elements are present
  164. */
  165. $attr = array(xPDOTransport::UNIQUE_KEY => 'category',
  166. xPDOTransport::PRESERVE_KEYS => false,
  167. xPDOTransport::UPDATE_OBJECT => true,
  168. xPDOTransport::RELATED_OBJECTS => true,
  169. );
  170. if ($hasValidator) {
  171. $attr[xPDOTransport::ABORT_INSTALL_ON_VEHICLE_FAIL] = true;
  172. }
  173. if ($hasSnippets) {
  174. $attr[xPDOTransport::RELATED_OBJECT_ATTRIBUTES]['Snippets'] = array(
  175. xPDOTransport::PRESERVE_KEYS => false,
  176. xPDOTransport::UPDATE_OBJECT => true,
  177. xPDOTransport::UNIQUE_KEY => 'name',
  178. );
  179. }
  180. if ($hasPropertySets) {
  181. $attr[xPDOTransport::RELATED_OBJECT_ATTRIBUTES]['PropertySets'] = array(
  182. xPDOTransport::PRESERVE_KEYS => false,
  183. xPDOTransport::UPDATE_OBJECT => true,
  184. xPDOTransport::UNIQUE_KEY => 'name',
  185. );
  186. }
  187. if ($hasPlugins) {
  188. $attr[xPDOTransport::RELATED_OBJECT_ATTRIBUTES]['Plugins'] = array(
  189. xPDOTransport::PRESERVE_KEYS => false,
  190. xPDOTransport::UPDATE_OBJECT => true,
  191. xPDOTransport::UNIQUE_KEY => 'name',
  192. );
  193. }
  194. /* create a vehicle for the category and all the things
  195. * we've added to it.
  196. */
  197. $vehicle = $builder->createVehicle($category,$attr);
  198. if ($hasValidator) {
  199. $modx->log(modX::LOG_LEVEL_INFO,'Adding in Script Validator.');
  200. $vehicle->validate('php',array(
  201. 'source' => $sources['validators'] . 'preinstall.script.php',
  202. ));
  203. }
  204. /* package in script resolver if any */
  205. if ($hasResolver) {
  206. $modx->log(modX::LOG_LEVEL_INFO,'Adding in Script Resolver.');
  207. $vehicle->resolve('php',array(
  208. 'source' => $sources['resolvers'] . 'install.script.php',
  209. ));
  210. }
  211. /* This section transfers every file in the local
  212. mycomponents/mycomponent/assets directory to the
  213. target site's assets/mycomponent directory on install.
  214. If the assets dir. has been renamed or moved, they will still
  215. go to the right place.
  216. */
  217. if ($hasCore) {
  218. echo '<br>Set Core';
  219. $vehicle->resolve('file',array(
  220. 'source' => $sources['source_core'],
  221. 'target' => "return MODX_CORE_PATH . 'components/';",
  222. ));
  223. }
  224. /* This section transfers every file in the local
  225. mycomponents/mycomponent/core directory to the
  226. target site's core/mycomponent directory on install.
  227. If the core has been renamed or moved, they will still
  228. go to the right place.
  229. */
  230. if ($hasAssets) {
  231. $vehicle->resolve('file',array(
  232. 'source' => $sources['source_assets'],
  233. 'target' => "return MODX_ASSETS_PATH . 'components/';",
  234. ));
  235. }
  236. /* Add subpackages */
  237. /* The transport.zip files will be copied to core/packages
  238. * but will have to be installed manually with "Add New Package and
  239. * "Search Locally for Packages" in Package Manager
  240. */
  241. if ($hasSubPackages) {
  242. $modx->log(modX::LOG_LEVEL_INFO, 'Adding in subpackages.');
  243. $vehicle->resolve('file',array(
  244. 'source' => $sources['packages'],
  245. 'target' => "return MODX_CORE_PATH;",
  246. ));
  247. }
  248. /* package in script resolver if any */
  249. if ($hasResolver) {
  250. $modx->log(modX::LOG_LEVEL_INFO,'Adding in Tables Resolver.');
  251. $vehicle->resolve('php',array(
  252. 'source' => $sources['resolvers'] . 'tables.resolver.php',
  253. ));
  254. $modx->log(modX::LOG_LEVEL_INFO,'Adding in User input Resolver.');
  255. // handle the user input - I moved this to after files are unpacked!
  256. $vehicle->resolve('php',array(
  257. 'source' => $sources['resolvers'] . 'setupoptions.resolver.php',
  258. ));
  259. }
  260. /* Put the category vehicle (with all the stuff we added to the
  261. * category) into the package
  262. */
  263. $builder->putVehicle($vehicle);
  264. /* Transport Resources */
  265. if ($hasResources) {
  266. $resources = include $sources['data'].'transport.resources.php';
  267. if (!is_array($resources)) {
  268. $modx->log(modX::LOG_LEVEL_ERROR,'Could not package in resources.');
  269. } else {
  270. $attributes= array(
  271. xPDOTransport::PRESERVE_KEYS => false,
  272. xPDOTransport::UPDATE_OBJECT => true,
  273. xPDOTransport::UNIQUE_KEY => 'pagetitle',
  274. xPDOTransport::RELATED_OBJECTS => true,
  275. xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
  276. 'ContentType' => array(
  277. xPDOTransport::PRESERVE_KEYS => false,
  278. xPDOTransport::UPDATE_OBJECT => true,
  279. xPDOTransport::UNIQUE_KEY => 'name',
  280. ),
  281. ),
  282. );
  283. foreach ($resources as $resource) {
  284. $vehicle = $builder->createVehicle($resource,$attributes);
  285. $builder->putVehicle($vehicle);
  286. }
  287. $modx->log(modX::LOG_LEVEL_INFO,'Packaged in '.count($resources).' resources.');
  288. }
  289. unset($resources,$resource,$attributes);
  290. }
  291. /* Transport Menus */
  292. if ($hasMenu) {
  293. /* load menu */
  294. $modx->log(modX::LOG_LEVEL_INFO,'Packaging in menu...');
  295. $menu = include $sources['data'].'transport.menu.php';
  296. if (empty($menu)) {
  297. $modx->log(modX::LOG_LEVEL_ERROR,'Could not package in menu.');
  298. } else {
  299. $vehicle= $builder->createVehicle($menu,array (
  300. xPDOTransport::PRESERVE_KEYS => true,
  301. xPDOTransport::UPDATE_OBJECT => true,
  302. xPDOTransport::UNIQUE_KEY => 'text',
  303. xPDOTransport::RELATED_OBJECTS => true,
  304. xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
  305. 'Action' => array (
  306. xPDOTransport::PRESERVE_KEYS => false,
  307. xPDOTransport::UPDATE_OBJECT => true,
  308. xPDOTransport::UNIQUE_KEY => array ('namespace','controller'),
  309. ),
  310. ),
  311. ));
  312. $builder->putVehicle($vehicle);
  313. $modx->log(modX::LOG_LEVEL_INFO,'Packaged in '.count($menu).' menu items.');
  314. unset($vehicle,$menu);
  315. }
  316. }
  317. /* load system settings */
  318. if ($hasSettings) {
  319. $settings = include $sources['data'].'transport.settings.php';
  320. if (!is_array($settings)) {
  321. $modx->log(modX::LOG_LEVEL_ERROR,'Could not package in settings.');
  322. } else {
  323. $attributes= array(
  324. xPDOTransport::UNIQUE_KEY => 'key',
  325. xPDOTransport::PRESERVE_KEYS => true,
  326. xPDOTransport::UPDATE_OBJECT => false,
  327. );
  328. foreach ($settings as $setting) {
  329. $vehicle = $builder->createVehicle($setting,$attributes);
  330. $builder->putVehicle($vehicle);
  331. }
  332. $modx->log(modX::LOG_LEVEL_INFO,'Packaged in '.count($settings).' System Settings.');
  333. unset($settings,$setting,$attributes);
  334. }
  335. }
  336. /* Next-to-last step - pack in the license file, readme.txt, changelog,
  337. * and setup options
  338. */
  339. $builder->setPackageAttributes(array(
  340. 'license' => file_get_contents($sources['docs'] . 'license.txt'),
  341. 'readme' => file_get_contents($sources['docs'] . 'readme.txt'),
  342. 'changelog' => file_get_contents($sources['docs'] . 'changelog.txt'),
  343. // 'setup-options' => array( 'source' => $sources['install_options'].'user.input.php',),
  344. ));
  345. /* Last step - zip up the package */
  346. $builder->pack();
  347. /* report how long it took */
  348. $mtime= microtime();
  349. $mtime= explode(" ", $mtime);
  350. $mtime= $mtime[1] + $mtime[0];
  351. $tend= $mtime;
  352. $totalTime= ($tend - $tstart);
  353. $totalTime= sprintf("%2.4f s", $totalTime);
  354. $modx->log(xPDO::LOG_LEVEL_INFO, "Package Built.");
  355. $modx->log(xPDO::LOG_LEVEL_INFO, "Execution time: {$totalTime}");
  356. exit();