Advertisement
Guest User

Untitled

a guest
Jun 14th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.38 KB | None | 0 0
  1. <?php /* ------------------------------------------------------------------------------
  2.   ================================================================================
  3.   === Reverse Engineer Existing MySQL Database Tables to xPDO Maps and Classes ===
  4.   ================================================================================
  5.  
  6.   SYNOPSIS:
  7.   This script generates the XML schema and PHP class files that describe custom
  8.   database tables.
  9.  
  10.   This script is meant to be executed once only: after the class and schema files
  11.   have been created, the purpose of this script has been served, though you will need to run it again if you modify your schema.
  12.  
  13.   USAGE:
  14.   1. Upload this file to the root of your MODX installation
  15.   2. Set the configuration details below
  16.   3. Navigate to this script in a browser to execute it,
  17.   e.g. http://yoursite.com/thisscript.php
  18.   or, you can do this via the command line, e.g. php this-script.php
  19.  
  20.   INPUT:
  21.   Please configure the options below.
  22.  
  23.   OUTPUT:
  24.   Creates XML and PHP files:
  25.   core/components/$package_name/model/$package_name/*.class.php
  26.   core/components/$package_name/model/$package_name/mysql/*.class.php
  27.   core/components/$package_name/model/$package_name/mysql/*.inc.php
  28.   core/components/$package_name/schema/$package_name.mysql.schema.xml
  29.  
  30.   SEE ALSO:
  31.   https://forums.modx.com/index.php?topic=40174.0
  32.   https://docs.modx.org/current/en/extending-modx/tutorials/using-custom-database-tables
  33.   https://docs.modx.com/current/en/extending-modx/xpdo/class-reference/xpdogenerator/xpdogenerator.writeschema
  34.   ------------------------------------------------------------------------------ */
  35.  
  36. /* ------------------------------------------------------------------------------
  37.   CONFIGURATION
  38.   ------------------------------------------------------------------------------
  39.   Be sure to create a valid database user with permissions to the appropriate
  40.   databases and tables before you try to run this script, e.g. by running
  41.   something like the following:
  42.  
  43.   CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'y0urP@$$w0rd';
  44.   GRANT ALL ON your_db.* TO 'your_user'@'localhost';
  45.   FLUSH PRIVILEGES;
  46.  
  47.   Be sure to test that the login criteria you created actually work before
  48.   continuing. If you *can* log in, but you receive errors (e.g. SQLSTATE[42000] [1044] )
  49.   when this script runs, then you may need to grant permissions for CREATE TEMPORARY TABLES
  50.   ------------------------------------------------------------------------------ */
  51. $debug = false;         // if true, will include verbose debugging info, including SQL errors.
  52. $verbose = true;        // if true, will print status info.
  53. // The XML schema file *must* be updated each time the database is modified, either
  54. // manually or via this script. By default, the schema is regenerated.
  55. // If you have spent time adding in composite/aggregate relationships to your
  56. // XML schema file (i.e. foreign key relationships), then you may want to set this
  57. // to 'false' in order to preserve your custom modifications.
  58. $regenerate_schema = true;
  59.  
  60. // Class files are not overwritten by default
  61. $regenerate_classes = true;
  62.  
  63. // Your package shortname:
  64. $package_name = 'package_name';
  65.  
  66.  
  67. // Database Login Info can be set explicitly:
  68. //$database_server = 'localhost';          // most frequently, your database resides locally
  69. //$dbase = '';           // name of your database
  70. //$database_user = '';           // name of the user
  71. //$database_password = '';   // password for that database user
  72. // if this file is not placed side by side with the config.core.php file, add the directory path
  73. include_once 'config.core.php';
  74. // OR, use your MODX Revo connection details.  Just uncomment the next line:
  75. include(MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php');
  76. // If your tables use a prefix, this will help identify them and it ensures that
  77. // the class names appear "clean", without the prefix.
  78. $table_prefix = 'prefix_';
  79. // If you specify a table prefix, you probably want this set to 'true'. E.g. if you
  80. // have custom tables alongside the modx_xxx tables, restricting the prefix ensures
  81. // that you only generate classes/maps for the tables identified by the $table_prefix.
  82. $restrict_prefix = true;
  83.  
  84. //------------------------------------------------------------------------------
  85. //  DO NOT TOUCH BELOW THIS LINE
  86. //------------------------------------------------------------------------------
  87. if (!defined('MODX_CORE_PATH')) {
  88.     print_msg('<h1?>Reverse Engineering Error
  89.        <p>MODX_CORE_PATH not defined! Did you include the correct config file?</p>');
  90.     exit;
  91. }
  92.  
  93. $xpdo_path = strtr(MODX_CORE_PATH . 'vendor/xpdo/xpdo'. DIRECTORY_SEPARATOR, '\\', '/');//xpdo/xpdo.class.php
  94. require $xpdo_path . 'src/bootstrap.php';
  95.  
  96. // A few definitions of files/folders:
  97. $package_dir = MODX_CORE_PATH . "components/$package_name/";
  98. $model_dir = MODX_CORE_PATH . "components/$package_name/model/";
  99. $class_dir = MODX_CORE_PATH . "components/$package_name/model/$package_name";
  100. $schema_dir = MODX_CORE_PATH . "components/$package_name/model/schema";
  101. $mysql_class_dir = MODX_CORE_PATH . "components/$package_name/model/$package_name/mysql";
  102. $xml_schema_file = MODX_CORE_PATH . "components/$package_name/model/schema/$package_name.mysql.schema.xml";
  103.  
  104. // A few variables used to track execution times.
  105. $mtime = microtime();
  106. $mtime = explode(' ', $mtime);
  107. $mtime = $mtime[1] + $mtime[0];
  108. $tstart = $mtime;
  109.  
  110. // Validations
  111. if (empty($package_name)) {
  112.     print_msg('<h1>Reverse Engineering Error</h1>
  113.                <p>The $package_name cannot be empty!  Please adjust the configuration and try again.</p>');
  114.     exit;
  115. }
  116.  
  117. // Create directories if necessary
  118. $dirs = array($package_dir, $schema_dir, $mysql_class_dir, $class_dir);
  119.  
  120. foreach ($dirs as $d) {
  121.     if (!file_exists($d)) {
  122.         if (!mkdir($d, 0777, true)) {
  123.             print_msg(sprintf('<h1>Reverse Engineering Error</h1>
  124.                                <p>Error creating <code>%s</code></p>
  125.                                <p>Create the directory (and its parents) and try again.</p>'
  126.                             , $d
  127.             ));
  128.             exit;
  129.         }
  130.     }
  131.     if (!is_writable($d)) {
  132.         print_msg(sprintf('<h1>Reverse Engineering Error</h1>
  133.                        <p>The <code>%s</code> directory is not writable by PHP.</p>
  134.                        <p>Adjust the permissions and try again.</p>'
  135.                         , $d));
  136.         exit;
  137.     }
  138. }
  139.  
  140. if ($verbose) {
  141.     print_msg(sprintf('<br></br><strong>Ok:</strong> The necessary directories exist and have the correct permissions inside of <br></br>`%s`', $package_dir));
  142. }
  143.  
  144. // Delete/regenerate map files?
  145. if (file_exists($xml_schema_file) && !$regenerate_schema && $verbose) {
  146.     print_msg(sprintf('<br></br><strong>Ok:</strong> Using existing XML schema file:<br></br>`%s`', $xml_schema_file));
  147. }
  148.  
  149. use xPDO\xPDO;
  150. $xpdo = new xPDO("mysql:host=$database_server;dbname=$dbase", $database_user, $database_password, $table_prefix);
  151.  
  152. // Set the package name and root path of that package
  153. $xpdo->setPackage($package_name, $package_dir, $package_dir);
  154. $xpdo->setDebug($debug);
  155.  
  156. $manager = $xpdo->getManager();
  157. $generator = $manager->getGenerator();
  158. $time = time();
  159. //Use this to create an XML schema from an existing database
  160. if ($regenerate_schema) {
  161.     if (is_file($xml_schema_file)) {
  162.         $rename = $xml_schema_file . '-' . $time;
  163.         print_msg("<br></br>The old XML schema file: <br></br>`{$xml_schema_file}` <br></br>has been renamed to <br></br>`{$rename}`.");
  164.         rename($xml_schema_file, $rename);
  165.     }
  166.     $xml = $generator->writeSchema($xml_schema_file, $package_name, 'xPDOObject', $table_prefix, $restrict_prefix);
  167.     if ($verbose) {
  168.         print_msg(sprintf('<br></br><strong>Ok:</strong> XML schema file generated: `%s`<hr></hr>', $xml_schema_file));
  169.     }
  170. }
  171.  
  172. // Use this to generate classes and maps from your schema
  173. if ($regenerate_classes) {
  174.     if (is_dir($class_dir)) {
  175.         $rename = $class_dir . '-' . $time;
  176.         print_msg("<br></br>The old class dir: <br></br>`{$class_dir}` <br></br>has been renamed to <br></br>`{$rename}`.");
  177.         rename($class_dir, $rename);
  178.     }
  179.     $generator->parseSchema($xml_schema_file, $model_dir);
  180. }
  181.  
  182. $mtime = microtime();
  183. $mtime = explode(" ", $mtime);
  184. $mtime = $mtime[1] + $mtime[0];
  185. $tend = $mtime;
  186. $totalTime = ($tend - $tstart);
  187. $totalTime = sprintf("%2.4f s", $totalTime);
  188.  
  189. if ($verbose) {
  190.     print_msg("<br></br><br></br><strong>Finished!</strong> Execution time: {$totalTime}<br></br>");
  191.  
  192.     if ($regenerate_schema) {
  193.         print_msg("<br></br>If you need to define aggregate/composite relationships in your XML schema file, be sure to regenerate your class files.");
  194.     }
  195. }
  196.  
  197. exit();
  198.  
  199. /* ------------------------------------------------------------------------------
  200.   Formats/prints messages.  The behavior is different if the script is run
  201.   via the command line (cli).
  202.   ------------------------------------------------------------------------------ */
  203.  
  204. function print_msg($msg) {
  205.     if (php_sapi_name() == 'cli') {
  206.         $msg = preg_replace('#<br></br>#i', "\n", $msg);
  207.         $msg = preg_replace('#<h1>#i', '== ', $msg);
  208.         $msg = preg_replace('#</h1>#i', ' ==', $msg);
  209.         $msg = preg_replace('#<h2>#i', '=== ', $msg);
  210.         $msg = preg_replace('#</h2>#i', ' ===', $msg);
  211.         $msg = strip_tags($msg) . "\n";
  212.     }
  213.     print $msg;
  214. }
  215. /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement