XOOPS  2.6.0
schematool.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
18 require __DIR__ . '/admin_header.php';
19 
20 /* --------------------------------------------------------------- */
21 
22 use Xmf\Debug;
26 use Xoops\Core\Yaml;
30 
31 // from $_POST we use keys: op, mod_dirname
33  'post',
34  array(
35  array('op','string', 'selectmodule', true),
36  array('mod_dirname','string', '', true),
37  )
38 );
39 
40 $op = $clean_input['op'];
41 $mod_dirname = $clean_input['mod_dirname'];
42 if ($op != 'showschema' || empty($mod_dirname)) {
43  $op = 'selectmodule';
44 }
45 
46 //echo '<h2>' . _MI_SCHEMATOOL_NAME . '</h2>';
47 $indexAdmin = new \Xoops\Module\Admin();
48 $indexAdmin->displayNavigation('schematool.php');
49 
50 if ($op == 'showschema') {
51  $helper = $xoops->getModuleHelper($mod_dirname);
52  $mod_to_use = $helper->getModule();
53  $mod_to_use->loadInfo($mod_dirname, false);
54  $mod_ver = $mod_to_use->modinfo;
55  $table_list = array();
56  if (isset($mod_ver['tables'])) {
57  $table_list = $mod_ver['tables'];
58  }
59  //Debug::dump($table_list);
60  if (empty($table_list)) {
61  echo $xoops->alert(
62  'warning',
65  );
66  } else {
67 
68  // get a schema manager
69  $schemaManager = $xoops->db()->getSchemaManager();
70 
71  // create schema from the current database
72  $schema = $schemaManager->createSchema();
73 
74  // invoke our RemovePrefixes visitor with list of core tables
75  $visitor = new RemovePrefixes;
76  $visitor->setTableFilter($table_list);
77  $schema->visit($visitor);
78 
79  // Get the schema we built with the RemovePrefixes visitor.
80  // Should be just core tables with no prefix
81  $newSchema = $visitor->getNewSchema();
82 
83  // Invoke an ExportVisitor that will build a clean array version
84  // of our schema, so we can serialize it.
85  $export = new ExportVisitor;
86  $newSchema->visit($export);
87 
88  echo '<h2>' . _MI_SCHEMATOOL_EXPORT_SCHEMA . '</h2>';
89  $yamldump = Yaml::dump($export->getSchemaArray(), 5);
90  //echo '<div contenteditable><pre>' . $yamldump . '</pre></div>';
91  $schemadump = <<<EOT1
92 <section>
93  <div id="container">
94  <div contenteditable><pre>{$yamldump}</pre></div>
95  <input type="text" value="schema.yml" placeholder="schema.yml">
96  <button onclick="downloadFile()">Create file</button> <output></output>
97  </div>
98 </section>
99 EOT1;
100 
101  $script = <<<EOT2
102 <script>
103 var container = document.querySelector('#container');
104 var typer = container.querySelector('[contenteditable]');
105 var output = container.querySelector('output');
106 
107 const MIME_TYPE = 'text/plain';
108 
109 // Rockstars use event delegation!
110 document.body.addEventListener('dragstart', function(e) {
111  var a = e.target;
112  if (a.classList.contains('dragout')) {
113  e.dataTransfer.setData('DownloadURL', a.dataset.downloadurl);
114  }
115 }, false);
116 
117 document.body.addEventListener('dragend', function(e) {
118  var a = e.target;
119  if (a.classList.contains('dragout')) {
120  cleanUp(a);
121  }
122 }, false);
123 
124 document.addEventListener('keydown', function(e) {
125  if (e.keyCode == 27) { // Esc
126  document.querySelector('details').open = false;
127  } else if (e.shiftKey && e.keyCode == 191) { // shift + ?
128  document.querySelector('details').open = true;
129  }
130 }, false);
131 
132 var cleanUp = function(a) {
133  a.textContent = 'Downloaded';
134  a.dataset.disabled = true;
135 
136  // Need a small delay for the revokeObjectURL to work properly.
137  setTimeout(function() {
138  window.URL.revokeObjectURL(a.href);
139  }, 1500);
140 };
141 
142 var downloadFile = function() {
143  window.URL = window.webkitURL || window.URL;
144 
145  var prevLink = output.querySelector('a');
146  if (prevLink) {
147  window.URL.revokeObjectURL(prevLink.href);
148  output.innerHTML = '';
149  }
150 
151  var bb = new Blob([typer.textContent], {type: MIME_TYPE});
152 
153  var a = document.createElement('a');
154  a.download = container.querySelector('input[type="text"]').value;
155  a.href = window.URL.createObjectURL(bb);
156  a.textContent = 'Download ready';
157 
158  a.dataset.downloadurl = [MIME_TYPE, a.download, a.href].join(':');
159  a.draggable = false; // Don't really need, but good practice.
160  //a.classList.add('dragout');
161 
162  output.appendChild(a);
163 
164  a.onclick = function(e) {
165  if ('disabled' in this.dataset) {
166  return false;
167  }
168 
169  cleanUp(this);
170  };
171 };
172 </script>
173 EOT2;
174 
175  echo $schemadump;
176  echo $script;
177  }
178  $op = 'selectmodule';
179 }
180 
181 if ($op == 'selectmodule') {
182  $activeModules = $xoops->getActiveModules();
183  natcasesort($activeModules);
184 
185  $form = new Xoops\Form\ThemeForm('', 'schema_form', '', 'post', true, 'inline');
186 
187  $ele = new Xoops\Form\Select(_MI_SCHEMATOOL_FORM_CAPTION, 'mod_dirname', $mod_dirname);
188  foreach ($activeModules as $dirname) {
189  $mHelper = $xoops->getModuleHelper($dirname);
190  if (is_object($mHelper)) {
191  $ele->addOption($dirname, $mHelper->getModule()->getVar('name'));
192  }
193  }
194  $form->addElement($ele);
195  $form->addElement(new Xoops\Form\Hidden('op', 'showschema'));
196  $form->addElement(new Xoops\Form\Button('', 'button', XoopsLocale::A_SUBMIT, 'submit'));
197  echo $form->render();
198 }
199 
200 
201 
202 /*
203  $importer = new ImportSchema;
204  $importSchema = $importer->importSchemaArray(Yaml::load($yamldump));
205 
206  echo '<h2>Original Schema</h2>';
207  Debug::dump($schema);
208 
209  echo '<h2>Imported Schema</h2>';
210  Debug::dump($importSchema);
211 
212  $synchronizer = new SingleDatabaseSynchronizer($xoops->db());
213  $to_sql = $synchronizer->getUpdateSchema($importSchema, true);
214  //$to_sql = $synchronizer->getCreateSchema($importSchema);
215 
216  //$diff=Comparator::compareSchemas($schema, $importSchema);
217  echo '<h2>compareSchemas(original,imported)</h2>';
218  Kint::dump($to_sql);
219 
220  echo '<h1>End of Test</h1>';
221 */
222 
223 $xoops->footer();
const A_SUBMIT
Definition: en_US.php:128
static gather($source, $input_map, $require=false)
const _MI_SCHEMATOOL_FORM_CAPTION
Definition: modinfo.php:10
$form
Definition: xoops_code.php:21
$xoops
Definition: admin.php:25
const _MI_SCHEMATOOL_MSG_NO_TABLES
Definition: modinfo.php:13
$mod_dirname
Definition: schematool.php:41
$helper
const _MI_SCHEMATOOL_TITLE_NO_TABLES
Definition: modinfo.php:12
const _MI_SCHEMATOOL_EXPORT_SCHEMA
Definition: modinfo.php:11
$dirname
Definition: backend.php:38
if($op!= 'showschema'||empty($mod_dirname)) $indexAdmin
Definition: schematool.php:47
$clean_input
Definition: schematool.php:32
$op
Definition: schematool.php:40
if(!$helper)