PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/src/install/action.php

http://textmotion.googlecode.com/
PHP | 1733 lines | 1446 code | 238 blank | 49 comment | 138 complexity | 53abaa560d72181b0b2df3a99498892f MD5 | raw file
Possible License(s): MIT, CC0-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * textMotion
  4. * ---
  5. * Written by Jose Carlos Nieto <xiam@menteslibres.org>
  6. * Copyright (c) 2007-2008, Jose Carlos Nieto <xiam@menteslibres.org>
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @author Jose Carlos Nieto <xiam@menteslibres.org>
  12. * @package textMotion
  13. * @copyright Copyright (c) 2007-2008, J. Carlos Nieto <xiam@menteslibres.org>
  14. * @link http://www.textmotion.org
  15. * @version $Revision: 1507 $
  16. * @modifiedby $LastChangedBy: xiam.core $
  17. * @lastmodified $Date: 2008-11-07 12:05:50 +1100 (Fri, 07 Nov 2008) $
  18. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  19. *
  20. */
  21. class install_action extends tm_action {
  22. public function __construct($action = null) {
  23. extract(
  24. $this->using(
  25. 'session',
  26. 'env'
  27. )
  28. );
  29. $this->lock_file = TM_TEMP_DIR.'install.lock';
  30. if (preg_match('/^(test_ftp)$/', $action)) {
  31. // skip
  32. } else if (file_exists($this->lock_file)) {
  33. // skip only when the lock_key is set
  34. if (!$session->get('lock_key') || $session->get('lock_key') != $this->get_lock_key()) {
  35. // the key is incorrect
  36. $env->redirect('/');
  37. }
  38. }
  39. }
  40. private function get_lock_key() {
  41. $fh = fopen($this->lock_file, 'r');
  42. $key = trim(fread($fh, filesize($this->lock_file)));
  43. fclose($fh);
  44. return $key;
  45. }
  46. private function db_driver($driver) {
  47. $driver = preg_replace('/[^a-z0-9]/i', '', $driver);
  48. return TM_LIB_DIR.'databases/'.$driver.'.php';
  49. }
  50. private function clean_cache() {
  51. $archive =& $this->using('archive');
  52. $temp = $archive->scandir(TM_TEMP_DIR);
  53. foreach($temp as $file) {
  54. if ($file{0} != '.' && $file != 'debug.log') {
  55. $archive->clean(TM_TEMP_DIR.$file);
  56. }
  57. }
  58. }
  59. private function wp_format_fix($text) {
  60. $text = preg_replace_callback(
  61. '/(<pre[^>]*>.+?<\/pre>)|(\r?\n\r?\n)/is',
  62. create_function('$a', 'return ($a[0]{0} == "<") ? $a[0] : "<br /><br />\n";'),
  63. $text
  64. );
  65. return $text;
  66. }
  67. private function get_modules() {
  68. $archive =& $this->using('archive');
  69. $temp = $archive->scandir(TM_MODULES_DIR);
  70. $modules = array();
  71. foreach($temp as $test) {
  72. if (is_dir(TM_MODULES_DIR.$test) && $test{0} != '.') {
  73. $modules[] = $test;
  74. }
  75. }
  76. $main = array(
  77. 'conf',
  78. 'groups',
  79. 'users',
  80. 'menu',
  81. 'categories'
  82. );
  83. $modules = array_unique(array_merge($main, $modules));
  84. return $modules;
  85. }
  86. private function module_exists($module) {
  87. return (file_exists(TM_MODULES_DIR.$module) && is_dir(TM_MODULES_DIR.$module));
  88. }
  89. private function gekko_path_fix($str) {
  90. return str_replace('data/files/uploaded/', 'data/files/uploads/', $str);
  91. }
  92. private function install(&$conn = null) {
  93. extract(
  94. $this->using(
  95. 'session',
  96. 'param',
  97. 'db',
  98. 'xml',
  99. 'archive',
  100. 'env'
  101. )
  102. );
  103. if (is_object($conn)) {
  104. $db->db =& $conn->db;
  105. $db->config =& $conn->config;
  106. $db_conf = array();
  107. foreach ($db->config as $key => $val) {
  108. $db_conf[$key] = $val;
  109. }
  110. } else {
  111. $db_conf = $session->get('_db');
  112. require $this->db_driver($db_conf['driver']);
  113. $db_driver = $db_conf['driver'].'_driver';
  114. $db->db = new $db_driver;
  115. foreach ($db_conf as $key => $value) {
  116. $db->db->{$key} = $value;
  117. }
  118. $db->open(false);
  119. }
  120. $module_versions = array();
  121. $modules = $this->get_modules();
  122. foreach($modules as $module) {
  123. $module_versions[$module] = $this->install_module($db, $module);
  124. }
  125. $user = $session->get('_user');
  126. if ($user) {
  127. $db->update(
  128. 'users',
  129. array(
  130. 'username' => $user['username'],
  131. 'nickname' => $user['username'],
  132. 'email' => $user['email'],
  133. 'password' => md5($user['username'].$user['new_password']),
  134. array('date_created' => 'now()')
  135. ),
  136. 1
  137. );
  138. }
  139. $this->clean_cache();
  140. $this->using('conf');
  141. $conf =& new conf($t = 1);
  142. foreach($module_versions as $module => $version) {
  143. $conf->set($module.'/version', $version);
  144. }
  145. $conf->set('core/template', 'default', 's');
  146. $conf->set('core/template_style', 'default', 's');
  147. $conf->set('core/icon_theme', 'default', 's');
  148. $conf->set('core/emoticon_theme', 'default', 's');
  149. $conf->set('users/allow_register', false, 'b');
  150. $site = $session->get('_conf');
  151. if ($site) {
  152. foreach ($site as $class => $key) {
  153. foreach ($key as $name => $value) {
  154. $conf->set($class.'/'.$name, $value);
  155. }
  156. }
  157. }
  158. $this->save_dbconf($db);
  159. }
  160. private function save_dbconf(&$db) {
  161. $dbfile = TM_USER_DIR.'conf'.DS.'database.php';
  162. $conf =
  163. '<?php
  164. /**
  165. * Textmotion
  166. * ---
  167. * Written by Jose Carlos Nieto <xiam@menteslibres.org>
  168. * Copyright (c) 2007-2008, Jose Carlos Nieto <xiam@menteslibres.org>
  169. *
  170. * Licensed under The MIT License
  171. * Redistributions of files must retain the above copyright notice.
  172. *
  173. */
  174. class database {
  175. public $config = array(
  176. \'default\' => array (
  177. \'hostname\' => \''.str_replace("'", "\'", $db->db->hostname).'\',
  178. \'username\' => \''.str_replace("'", "\'", $db->db->username).'\',
  179. \'password\' => \''.str_replace("'", "\'", $db->db->password).'\',
  180. \'database\' => \''.str_replace("'", "\'", $db->db->database).'\',
  181. \'driver\' => \''.str_replace("'", "\'", $db->db->driver).'\',
  182. \'prefix\' => \''.str_replace("'", "\'", $db->db->prefix).'\'
  183. )
  184. );
  185. }
  186. ?>';
  187. $fh = fopen($dbfile, 'w');
  188. fwrite($fh, $conf);
  189. fclose($fh);
  190. @chmod($dbfile, 0755);
  191. }
  192. public function test_ftp() {
  193. extract(
  194. $this->using(
  195. 'param',
  196. 'session',
  197. 'json',
  198. 'net/ftp_client'
  199. )
  200. );
  201. $ftp = $param->get('ftp');
  202. $ftp_client = new ftp_client($ftp['host'], 21, $ftp['user'], $ftp['pass']);
  203. $ftp_client->open();
  204. if ($ftp_client->login()) {
  205. $this->session->set('next', $param->get('next'));
  206. $this->session->set('ftp', array(
  207. 'host' => $ftp['host'],
  208. 'user' => $ftp['user'],
  209. 'pass' => $ftp['pass']
  210. ));
  211. $json->response(
  212. array(
  213. 'successMessage' => 'Login successful.',
  214. 'execute' => 'startBrowser();'
  215. )
  216. );
  217. } else {
  218. $json->response(
  219. array(
  220. 'errorMessage' => 'Could not login into the FTP server.'
  221. )
  222. );
  223. }
  224. }
  225. private function test_db($data) {
  226. $this->using('db');
  227. $db = new db();
  228. $driver = $this->db_driver($data['driver']);
  229. if (file_exists($driver)) {
  230. require_once $driver;
  231. $copy = array(
  232. 'hostname',
  233. 'username',
  234. 'password',
  235. 'database',
  236. 'driver',
  237. 'prefix'
  238. );
  239. $class_name = $data['driver'].'_driver';
  240. $conf = array();
  241. $db->db = new $class_name;
  242. foreach($copy as $el) {
  243. $db->db->$el = $data[$el];
  244. $conf[$el] = $db->db->$el;
  245. }
  246. if ($db->db->open(false)) {
  247. return $db;
  248. } else {
  249. return $db->get_error();
  250. }
  251. }
  252. return false;
  253. }
  254. public function import() {
  255. extract(
  256. $this->using(
  257. 'db',
  258. 'param',
  259. 'session',
  260. 'json',
  261. 'env'
  262. )
  263. );
  264. $param->not_empty(
  265. 'catalog',
  266. 'db_from.driver',
  267. 'db_from.hostname',
  268. 'db_from.database',
  269. 'db_to.driver',
  270. 'db_to.hostname',
  271. 'db_to.database'
  272. );
  273. $db_from = $this->test_db($param->get('db_from'));
  274. $db_to = $this->test_db($param->get('db_to'));
  275. if (!is_object($db_from)) {
  276. $json->response(
  277. array(
  278. 'errorMessage' => __($db_from)
  279. )
  280. );
  281. }
  282. if (!is_object($db_to)) {
  283. $json->response(
  284. array(
  285. 'errorMessage' => __($db_to)
  286. )
  287. );
  288. }
  289. $arg = array();
  290. $arg['db_from'] =& $db_from;
  291. $arg['db_to'] =& $db_to;
  292. $this->install($db_to);
  293. switch($param->get('catalog')) {
  294. case 'gekko':
  295. $this->import_from_gekko($arg);
  296. break;
  297. case 'wordpress':
  298. $this->import_from_wordpress($arg);
  299. break;
  300. }
  301. $this->save_dbconf($db_to);
  302. $env->redirect('/action=install_success');
  303. }
  304. private function import_from_wordpress($vars) {
  305. extract(
  306. $this->using(
  307. 'db',
  308. 'param',
  309. 'json'
  310. )
  311. );
  312. extract($vars);
  313. $db_version = 0;
  314. $q = $db_from->find_one(
  315. array(
  316. 'option' => array(
  317. 'table' => 'options',
  318. 'fields' => array('option_value'),
  319. 'where' => $db_from->bind('option_name = ?', 'db_version')
  320. )
  321. )
  322. );
  323. if (!empty($q['option']['option_value'])) {
  324. $db_version = $q['option']['option_value'];
  325. }
  326. if ($db_version >= 3306) {
  327. $db_to->truncate('content_relations');
  328. // comments
  329. $src = $db_from->select('comments', '*', $db_from->bind('comment_approved = ?', '1'));
  330. $db_to->truncate('comments');
  331. while ($row = $db_from->fetch_array($src)) {
  332. $db_to->insert(
  333. 'comments',
  334. array(
  335. 'id' => $row['comment_id'],
  336. 'parent_id' => $row['comment_parent'],
  337. 'content' => $this->wp_format_fix($row['comment_content']),
  338. 'author_id' => $row['user_id'],
  339. 'date_created' => $row['comment_date'],
  340. 'nickname' => $row['comment_author'],
  341. 'website' => $row['comment_author_url'],
  342. 'email' => $row['comment_author_email'],
  343. 'ip_address' => $row['comment_author_ip'],
  344. 'module' => 'blog',
  345. 'thread_id' => $row['comment_post_id']
  346. )
  347. );
  348. }
  349. // importing menu
  350. $src = $db_from->select('links', '*');
  351. $db_to->truncate('menu');
  352. $db_to->truncate('menu_links');
  353. while ($row = $db_from->fetch_array($src)) {
  354. if ($db_version < 3570) {
  355. } else if ($db_version < 6039) {
  356. $src2 = $db_from->select('link2cat', 'category_id', $db_from->bind('link_id = ?', $row['link_id']));
  357. $row2 = $db_from->fetch_array($src2);
  358. $src3 = $db_from->select('categories', 'cat_name', $db_from->bind('cat_id = ?', $row2['category_id']));
  359. $row3 = $db_from->fetch_array($src3);
  360. } else {
  361. $src3 = $db_from->select('terms', 'name', $db_from->bind('term_id = ?', $row['link_category']));
  362. $row3 = $db_from->fetch_array($src3);
  363. }
  364. if (isset($row3['name'])) {
  365. $src4 = $db_to->select('menu', 'id', $db_to->bind('title = ?', $row3['name']));
  366. $row4 = $db_to->fetch_array($src4);
  367. if (!$row4) {
  368. $db_to->insert(
  369. 'menu',
  370. array(
  371. 'title' => $row3['name']
  372. )
  373. );
  374. $menu_id = $db_to->last_insert_id();
  375. $db_to->insert(
  376. 'blocks',
  377. array(
  378. 'title' => $row3['name'],
  379. 'position' => 'left',
  380. 'embedded_script' => 'menu.display',
  381. 'variable' => serialize(array('menu_id' => $menu_id)),
  382. 'status' => 1,
  383. 'author_id' => 1
  384. )
  385. );
  386. $block_id = $db_to->last_insert_id();
  387. $db_to->insert(
  388. 'blocks_groups',
  389. array(
  390. 'block_id' => $block_id,
  391. 'group_id' => TM_GROUP_ALL
  392. )
  393. );
  394. $db_to->insert(
  395. 'blocks_modules',
  396. array(
  397. 'block_id' => $block_id,
  398. 'module_name' => 'all'
  399. )
  400. );
  401. } else {
  402. $menu_id = $row4['id'];
  403. }
  404. $db_to->insert(
  405. 'menu_links',
  406. array(
  407. 'menu_id' => $menu_id,
  408. 'url' => $row['link_url'],
  409. 'tooltip' => $row['link_description'],
  410. 'title' => $row['link_name'],
  411. 'order_id' => 0,
  412. 'level' => 0
  413. )
  414. );
  415. }
  416. }
  417. // importing users
  418. $src = $db_from->select('users', '*');
  419. $db_to->truncate('users');
  420. $db_to->truncate('groups_users');
  421. while ($row = $db_from->fetch_array($src)) {
  422. if ($db_version >= 7558) {
  423. $db_to->insert(
  424. 'users',
  425. array(
  426. 'id' => $row['id'],
  427. 'username' => $row['user_login'],
  428. 'password' => $row['user_pass'],
  429. 'date_created' => $row['user_registered'],
  430. 'realname' => $row['user_nicename'],
  431. 'email' => $row['user_email'],
  432. 'nickname' => $row['user_login'],
  433. 'status' => 1
  434. )
  435. );
  436. } else {
  437. $db_to->insert(
  438. 'users',
  439. array(
  440. 'id' => $row['id'],
  441. 'username' => $row['user_login'],
  442. 'password' => '$M1'.$row['user_pass'],
  443. 'date_created' => $row['user_registered'],
  444. 'realname' => $row['user_nicename'],
  445. 'email' => $row['user_email'],
  446. 'nickname' => $row['user_login'],
  447. 'status' => 1
  448. )
  449. );
  450. }
  451. }
  452. // importing groups
  453. $src = $db_from->select('usermeta', array('user_id', 'meta_value'), $db_from->bind('meta_key = ?', 'wp_capabilities'));
  454. while ($row = $db_from->fetch_array($src)) {
  455. $db_to->insert(
  456. 'groups_users',
  457. array(
  458. 'user_id' => $row['user_id'],
  459. 'group_id' => TM_GROUP_USER
  460. )
  461. );
  462. $meta = @unserialize($row['meta_value']);
  463. if (!empty($meta['administrator'])) {
  464. $db_to->insert(
  465. 'groups_users',
  466. array(
  467. 'user_id' => $row['user_id'],
  468. 'group_id' => TM_GROUP_ADMIN
  469. )
  470. );
  471. }
  472. }
  473. if ($db_version < 6039) {
  474. // importing blog tags
  475. $src = $db_from->select('post2cat', '*');
  476. while ($row = $db_from->fetch_array($src)) {
  477. $db_to->insert(
  478. 'content_relations',
  479. array(
  480. 'module' => 'blog',
  481. 'item_id' => $row['post_id'],
  482. 'relation_id' => $row['category_id'],
  483. 'relation_type' => 'category'
  484. )
  485. );
  486. }
  487. // importing categories
  488. $src = $db_from->select('categories', '*');
  489. $db_to->truncate('categories');
  490. while ($row = $db_from->fetch_array($src)) {
  491. $db_to->insert(
  492. 'categories',
  493. array(
  494. 'id' => $row['cat_id'],
  495. 'title' => $row['cat_name'],
  496. 'text_id' => $param->textilize($row['cat_name']),
  497. 'author_id' => 1,
  498. array('date_created' => 'now()')
  499. )
  500. );
  501. }
  502. // importing blog
  503. $src = $db_from->select('posts', '*');
  504. $db_to->truncate('blog');
  505. while ($row = $db_from->fetch_array($src)) {
  506. $db_to->insert(
  507. 'blog',
  508. array(
  509. 'id' => $row['id'],
  510. 'title' => $row['post_title'],
  511. 'content' => $this->wp_format_fix($row['post_content']),
  512. 'date_created' => $row['post_date'],
  513. 'date_publish' => $row['post_date'],
  514. 'date_modified' => $row['post_modified'],
  515. 'author_id' => $row['post_author'],
  516. 'modifier_id' => 0,
  517. 'allow_comments' => intval($row['comment_status'] == 'open'),
  518. 'status' => intval($row['post_status'] == 'publish')
  519. )
  520. );
  521. }
  522. // pages
  523. $src = $db_from->select('posts', '*', $db_from->bind('post_status = ?', 'static'));
  524. $db_to->truncate('pages');
  525. while ($row = $db_from->fetch_array($src)) {
  526. $db_to->insert(
  527. 'pages',
  528. array(
  529. 'id' => $row['id'],
  530. 'text_id' => $param->textilize($row['post_title']),
  531. 'title' => $row['post_title'],
  532. 'parent_id' => $row['post_parent'],
  533. 'content' => $this->wp_format_fix($row['post_content']),
  534. 'date_created' => $row['post_date'],
  535. 'date_modified' => $row['post_modified'],
  536. 'author_id' => $row['post_author'],
  537. 'modifier_id' => 0,
  538. 'status' => intval($row['post_status'] == 'publish')
  539. )
  540. );
  541. }
  542. } else {
  543. // categories
  544. $src = $db_from->query('
  545. SELECT t.term_id, t.name FROM
  546. '.$db_from->table_name('terms').' t
  547. INNER JOIN
  548. '.$db_from->table_name('term_taxonomy').' tt
  549. ON tt.term_id = t.term_id AND tt.taxonomy = \'category\';
  550. ');
  551. $db_to->truncate('categories');
  552. while ($row = $db_from->fetch_array($src)) {
  553. $db_to->insert(
  554. 'categories',
  555. array(
  556. 'id' => $row['term_id'],
  557. 'title' => $row['name'],
  558. 'text_id' => $param->textilize($row['name']),
  559. 'author_id' => 1,
  560. array('date_created' => 'now()')
  561. )
  562. );
  563. unset($row);
  564. }
  565. // tags
  566. $src = $db_from->query('
  567. SELECT t.term_id, t.name, tt.parent, tt.count FROM
  568. '.$db_from->table_name('terms').' t
  569. INNER JOIN
  570. '.$db_from->table_name('term_taxonomy').' tt
  571. ON tt.term_id = t.term_id AND tt.taxonomy = \'post_tag\';
  572. ');
  573. $db_to->truncate('tags');
  574. while ($row = $db_from->fetch_array($src)) {
  575. $db_to->insert(
  576. 'tags',
  577. array(
  578. 'id' => $row['term_id'],
  579. 'title' => $row['name'],
  580. 'rank' => $row['count'],
  581. 'category_id' => $row['parent'],
  582. 'text_id' => $param->textilize($row['name'])
  583. )
  584. );
  585. unset($row);
  586. }
  587. // relations
  588. $src = $db_from->query('
  589. SELECT
  590. r.object_id AS item_id,
  591. t.term_id AS relation_id,
  592. t.taxonomy
  593. FROM
  594. '.$db_from->table_name('term_relationships').' r
  595. INNER JOIN
  596. '.$db_from->table_name('term_taxonomy').' t
  597. ON r.term_taxonomy_id = t.term_taxonomy_id
  598. ');
  599. while ($row = $db_from->fetch_array($src)) {
  600. $db_to->insert(
  601. 'content_relations',
  602. array(
  603. 'module' => 'blog',
  604. 'item_id' => $row['item_id'],
  605. 'relation_id' => $row['relation_id'],
  606. 'relation_type' => ($row['taxonomy'] == 'post_tag') ? 'tag' : 'category'
  607. )
  608. );
  609. }
  610. // blog
  611. $src = $db_from->select('posts', '*', $db_from->bind('post_type = ?', 'post'));
  612. $db_to->truncate('blog');
  613. while ($row = $db_from->fetch_array($src)) {
  614. $db_to->insert(
  615. 'blog',
  616. array(
  617. 'id' => $row['id'],
  618. 'title' => $row['post_title'],
  619. 'content' => $this->wp_format_fix($row['post_content']),
  620. 'date_created' => $row['post_date'],
  621. 'date_publish' => $row['post_date'],
  622. 'date_modified' => $row['post_modified'],
  623. 'author_id' => $row['post_author'],
  624. 'modifier_id' => 0,
  625. 'allow_comments' => intval($row['comment_status'] == 'open'),
  626. 'status' => intval($row['post_status'] == 'publish')
  627. )
  628. );
  629. }
  630. // pages
  631. $src = $db_from->select('posts', '*', $db_from->bind('post_type = ?', 'page'));
  632. $db_to->truncate('pages');
  633. while ($row = $db_from->fetch_array($src)) {
  634. $db_to->insert(
  635. 'pages',
  636. array(
  637. 'id' => $row['id'],
  638. 'text_id' => $param->textilize($row['post_title']),
  639. 'title' => $row['post_title'],
  640. 'parent_id' => $row['post_parent'],
  641. 'content' => $this->wp_format_fix($row['post_content']),
  642. 'date_created' => $row['post_date'],
  643. 'date_modified' => $row['post_modified'],
  644. 'author_id' => $row['post_author'],
  645. 'modifier_id' => 0,
  646. 'status' => intval($row['post_status'] == 'publish')
  647. )
  648. );
  649. }
  650. }
  651. } else {
  652. $json->response(
  653. array(
  654. 'errorMessage' => 'Your WordPress version is very old. Please upgrade to a newer version and try again.'
  655. )
  656. );
  657. }
  658. }
  659. private function import_from_gekko($vars) {
  660. extract(
  661. $this->using(
  662. 'db',
  663. 'param'
  664. )
  665. );
  666. extract($vars);
  667. $db_to->truncate('content_relations');
  668. // importing statistics
  669. $src = $db_from->select('stats_day', '*');
  670. $db_to->truncate('statistics_daily');
  671. while ($row = $db_from->fetch_array($src)) {
  672. $db_to->insert(
  673. 'statistics_daily',
  674. array(
  675. 'day' => $row['day'],
  676. 'h00' => $row['h00'],
  677. 'h01' => $row['h01'],
  678. 'h02' => $row['h02'],
  679. 'h03' => $row['h03'],
  680. 'h04' => $row['h04'],
  681. 'h05' => $row['h05'],
  682. 'h06' => $row['h06'],
  683. 'h07' => $row['h07'],
  684. 'h08' => $row['h08'],
  685. 'h09' => $row['h09'],
  686. 'h10' => $row['h10'],
  687. 'h11' => $row['h11'],
  688. 'h12' => $row['h12'],
  689. 'h13' => $row['h13'],
  690. 'h14' => $row['h14'],
  691. 'h15' => $row['h15'],
  692. 'h16' => $row['h16'],
  693. 'h17' => $row['h17'],
  694. 'h18' => $row['h18'],
  695. 'h19' => $row['h19'],
  696. 'h20' => $row['h20'],
  697. 'h21' => $row['h21'],
  698. 'h22' => $row['h22'],
  699. 'h23' => $row['h23'],
  700. 'type' => $row['variant'] ? 'total' : 'unique',
  701. 'total' => $row['total']
  702. )
  703. );
  704. }
  705. $src = $db_from->select('stats_search_keywords', '*');
  706. $db_to->truncate('statistics_search');
  707. while ($row = $db_from->fetch_array($src)) {
  708. $db_to->insert(
  709. 'statistics_search',
  710. array(
  711. 'keywords' => $row['keywords'],
  712. 'origin' => $row['engine'],
  713. 'counter' => $row['counter'],
  714. 'url' => $row['url'],
  715. 'referer' => $row['referer']
  716. )
  717. );
  718. }
  719. $src = $db_from->select('stats_uri_logs', '*', $db_from->bind('type = ?', 'request'));
  720. $db_to->truncate('statistics_urls');
  721. while ($row = $db_from->fetch_array($src)) {
  722. $db_to->insert(
  723. 'statistics_urls',
  724. array(
  725. 'url' => $row['url'],
  726. 'counter' => $row['counter'],
  727. 'ip_address' => $row['last_ip'],
  728. 'author_id' => $row['last_user_id'],
  729. 'date_created' => $row['date_access']
  730. )
  731. );
  732. }
  733. $src = $db_from->select('stats_uri_logs', '*', $db_from->bind('type = ?', 'referer'));
  734. $db_to->truncate('statistics_referers');
  735. while ($row = $db_from->fetch_array($src)) {
  736. $db_to->insert(
  737. 'statistics_referers',
  738. array(
  739. 'url' => $row['url'],
  740. 'counter' => $row['counter'],
  741. 'ip_address' => $row['last_ip'],
  742. 'author_id' => $row['last_user_id'],
  743. 'date_created' => $row['date_access']
  744. )
  745. );
  746. }
  747. $src = $db_from->select('stats_visits', '*');
  748. $db_to->truncate('statistics_global');
  749. while ($row = $db_from->fetch_array($src)) {
  750. $db_to->insert(
  751. 'statistics_global',
  752. array(
  753. 'day' => $row['day'],
  754. 'unique_hits' => $row['uniquehits'],
  755. 'total' => $row['total']
  756. )
  757. );
  758. }
  759. // importing categories
  760. $src = $db_from->select('category', '*');
  761. $db_to->truncate('categories');
  762. while ($row = $db_from->fetch_array($src)) {
  763. $db_to->insert(
  764. 'categories',
  765. array(
  766. 'id' => $row['id'],
  767. 'title' => $row['title'],
  768. 'text_id' => $param->textilize($row['title']),
  769. 'description' => $row['description']
  770. )
  771. );
  772. }
  773. // importing tags
  774. $src = $db_from->select('category_tag', '*');
  775. $db_to->truncate('tags');
  776. while ($row = $db_from->fetch_array($src)) {
  777. $db_to->insert(
  778. 'tags',
  779. array(
  780. 'id' => $row['id'],
  781. 'title' => $row['tag_name'],
  782. 'text_id' => $param->textilize($row['tag_name']),
  783. 'category_id' => $row['category_id'],
  784. 'rank' => $row['tag_rank']
  785. )
  786. );
  787. }
  788. // importing blog tags
  789. $src = $db_from->select('category_tag_relation', '*', $db_from->bind('module_name = ?', 'blog'));
  790. while ($row = $db_from->fetch_array($src)) {
  791. $db_to->insert(
  792. 'content_relations',
  793. array(
  794. 'module' => 'blog',
  795. 'relation_id' => $row['tag_id'],
  796. 'relation_type' => 'tag',
  797. 'item_id' => $row['module_item_id']
  798. )
  799. );
  800. }
  801. // importing blog
  802. $src = $db_from->select('blog', '*');
  803. $db_to->truncate('blog');
  804. while ($row = $db_from->fetch_array($src)) {
  805. $db_to->insert(
  806. 'blog',
  807. array(
  808. 'id' => $row['id'],
  809. 'title' => $row['title'],
  810. 'content' => $this->gekko_path_fix($row['content']),
  811. 'date_created' => $row['date_created'],
  812. 'date_publish' => $row['date_created'],
  813. 'date_modified' => $row['date_modified'],
  814. 'author_id' => $row['author_id'],
  815. 'modifier_id' => 0,
  816. 'status' => $row['status'],
  817. 'footer' => $row['footer'],
  818. 'page_views' => $row['page_reads']
  819. )
  820. );
  821. $db_to->insert(
  822. 'content_relations',
  823. array(
  824. 'module' => 'blog',
  825. 'relation_id' => $row['category_id'],
  826. 'relation_type' => 'category',
  827. 'item_id' => $row['id']
  828. )
  829. );
  830. }
  831. // importing pages
  832. $src = $db_from->select('page', '*');
  833. $db_to->truncate('pages');
  834. while ($row = $db_from->fetch_array($src)) {
  835. $db_to->insert(
  836. 'pages',
  837. array(
  838. 'id' => $row['id'],
  839. 'text_id' => $param->textilize($row['title']),
  840. 'title' => $row['title'],
  841. 'content' => $this->gekko_path_fix($row['content']),
  842. 'parent_id' => $row['parent_id'],
  843. 'author_id' => $row['author_id'],
  844. 'modifier_id' => $row['editor_id'],
  845. 'status' => $row['status'],
  846. 'date_created' => $row['date_created'],
  847. 'date_modified' => $row['date_modified']
  848. )
  849. );
  850. }
  851. // importing users
  852. $src = $db_from->select('user', '*');
  853. $db_to->truncate('users');
  854. $db_to->truncate('groups_users');
  855. while ($row = $db_from->fetch_array($src)) {
  856. $db_to->insert(
  857. 'users',
  858. array(
  859. 'id' => $row['id'],
  860. 'username' => $row['username'],
  861. 'password' => $row['password'],
  862. 'last_login' => $row['date_login'],
  863. 'date_created' => $row['date_registered'],
  864. 'realname' => $row['realname'],
  865. 'email' => $row['email'],
  866. 'nickname' => $row['username'],
  867. 'status' => $row['status']
  868. )
  869. );
  870. $groups = explode(',', $row['groups']);
  871. $groups[] = TM_GROUP_USER;
  872. foreach($groups as $g) {
  873. $g = trim($g);
  874. // only administrator
  875. if ($g == 1) {
  876. $db_to->insert(
  877. 'groups_users',
  878. array(
  879. 'user_id' => $row['id'],
  880. 'group_id' => $g
  881. )
  882. );
  883. }
  884. }
  885. }
  886. // importing comments
  887. $src = $db_from->select('comment', '*');
  888. $db_to->truncate('comments');
  889. while ($row = $db_from->fetch_array($src)) {
  890. $db_to->insert(
  891. 'comments',
  892. array(
  893. 'id' => $row['id'],
  894. 'parent_id' => $row['parent'],
  895. 'content' => $this->gekko_path_fix($row['content']),
  896. 'author_id' => $row['author_id'],
  897. 'date_created' => $row['date_created'],
  898. 'date_modified' => $row['date_modified'],
  899. 'nickname' => $row['nickname'],
  900. 'website' => $row['url'],
  901. 'email' => $row['email'],
  902. 'ip_address' => $row['ipaddr'],
  903. 'module' => $row['module'],
  904. 'thread_id' => $row['thread']
  905. )
  906. );
  907. }
  908. // OPTIONAL MODULES
  909. if ($this->module_exists('tagboard')) {
  910. // importing tagboard
  911. $src = $db_from->select('extension_tagboard', '*');
  912. $db_to->truncate('tagboard');
  913. while ($row = $db_from->fetch_array($src)) {
  914. $db_to->insert(
  915. 'tagboard',
  916. array(
  917. 'author_id' => $row['author_id'],
  918. 'nickname' => $row['nickname'],
  919. 'url' => $row['link'],
  920. 'message' => $row['message'],
  921. 'date_created' => $row['date_created'],
  922. 'ip_address' => $row['author_ip']
  923. )
  924. );
  925. }
  926. }
  927. if ($this->module_exists('forum')) {
  928. $db_to->truncate('forum_categories');
  929. $db_to->truncate('forum_forums');
  930. $db_to->truncate('forum_messages');
  931. $db_to->truncate('forum_topics');
  932. $db_to->truncate('forum_topics_tags');
  933. $src = $db_from->select('forum_category');
  934. while ($row = $db_from->fetch_array($src)) {
  935. $db_to->insert(
  936. 'forum_categories',
  937. array(
  938. 'id' => $row['id'],
  939. 'title' => $row['name'],
  940. 'text_id' => $param->textilize($row['name']),
  941. 'order_id' => $row['order_id']
  942. )
  943. );
  944. }
  945. $src = $db_from->select('forum_forum');
  946. while ($row = $db_from->fetch_array($src)) {
  947. $src2 = $db_from->query('
  948. SELECT
  949. t.id as last_topic_id,
  950. m.id as last_message_id,
  951. m.is_topic,
  952. m.date_created as last_message_date,
  953. m.author_id as last_author_id
  954. FROM
  955. '.$db_from->table_name('forum_forum').' f
  956. INNER JOIN '.$db_from->table_name('forum_thread').' t
  957. ON t.forum_id = f.id
  958. INNER JOIN '.$db_from->table_name('forum_message').' m
  959. ON m.topic_id = t.id
  960. WHERE f.id = \''.$row['id'].'\' ORDER BY m.date_created DESC LIMIT 1
  961. ');
  962. $row2 = $db_from->fetch_array($src2);
  963. if ($row2['is_topic']) {
  964. $row2['last_message_id'] = 0;
  965. }
  966. $db_to->insert(
  967. 'forum_forums',
  968. array(
  969. 'id' => $row['id'],
  970. 'parent_id' => $row['category_id'],
  971. 'title' => $row['name'],
  972. 'messages_count' => $row['messages_count'],
  973. 'text_id' => $param->textilize($row['name']),
  974. 'order_id' => $row['order_id'],
  975. 'description' => $row['description'],
  976. 'last_message_id' => pick($row2['last_message_id'], null),
  977. 'last_message_date' => pick($row2['last_message_date'], null),
  978. 'last_author_id' => pick($row2['last_author_id'], null),
  979. 'last_topic_id' => pick($row2['last_topic_id'], null)
  980. )
  981. );
  982. }
  983. $src = $db_from->select('forum_thread');
  984. while ($row = $db_from->fetch_array($src)) {
  985. $src2 = $db_from->select(
  986. 'forum_message',
  987. array(
  988. 'title',
  989. 'content',
  990. 'author_id',
  991. 'date_created',
  992. 'date_modified'
  993. ),
  994. $db_from->bind('is_topic = ? AND topic_id = ?', 1, $row['id']),
  995. 'ORDER BY date_created DESC LIMIT 1'
  996. );
  997. $row2 = $db_from->fetch_array($src2);
  998. $src3 = $db_from->select(
  999. 'forum_message',
  1000. array(
  1001. 'id',
  1002. 'author_id',
  1003. 'date_created'
  1004. ),
  1005. $db_from->bind('is_topic = ? AND topic_id = ?', 0, $row['id']),
  1006. 'ORDER BY date_created DESC LIMIT 1'
  1007. );
  1008. $row3 = $db_from->fetch_array($src3);
  1009. $messages = $db_from->count(
  1010. 'forum_message',
  1011. $db_from->bind('is_topic = ? AND topic_id = ?', 0, $row['id'])
  1012. );
  1013. if ($row2) {
  1014. $db_to->insert(
  1015. 'forum_topics',
  1016. array(
  1017. 'id' => $row['id'],
  1018. 'parent_id' => $row['forum_id'],
  1019. 'page_views' => $row['stat_reads'],
  1020. 'title' => $row2['title'],
  1021. 'text_id' => $param->textilize($row2['title']),
  1022. 'message' => $row2['content'],
  1023. 'author_id' => $row2['author_id'],
  1024. 'date_created' => $row2['date_created'],
  1025. 'date_modified' => $row2['date_modified'],
  1026. 'status' => 1,
  1027. 'messages_count' => $messages,
  1028. 'importance' => 'active',
  1029. 'last_message_id' => pick($row3['id'], ''),
  1030. 'last_message_date' => pick($row3['date_created'], ''),
  1031. 'last_author_id' => pick($row3['author_id'], ''),
  1032. 'type' => 'normal'
  1033. )
  1034. );
  1035. }
  1036. }
  1037. $src = $db_from->select('forum_message', '*', $db->bind('is_topic = ?', 0));
  1038. while ($row = $db_from->fetch_array($src)) {
  1039. $db_to->insert(
  1040. 'forum_messages',
  1041. array(
  1042. 'id' => $row['id'],
  1043. 'parent_id' => $row['topic_id'],
  1044. 'message' => $row['content'],
  1045. 'author_id' => $row['author_id'],
  1046. 'date_created' => $row['date_created'],
  1047. 'date_modified' => $row['date_modified']
  1048. )
  1049. );
  1050. }
  1051. }
  1052. if ($this->module_exists('gallery')) {
  1053. // importing galleries
  1054. $src = $db_from->select('gallery', '*');
  1055. $db_to->truncate('galleries');
  1056. while ($row = $db_from->fetch_array($src)) {
  1057. $db_to->insert(
  1058. 'galleries',
  1059. array(
  1060. 'id' => $row['id'],
  1061. 'parent_id' => $row['gallery_id'],
  1062. 'title' => $row['title'],
  1063. 'description' => $this->gekko_path_fix($row['description']),
  1064. 'thumbnail' => $row['thumbnail_url'],
  1065. 'author_id' => $row['author_id'],
  1066. 'date_created' => $row['date_created']
  1067. )
  1068. );
  1069. }
  1070. $src = $db_from->select('gallery_item', '*');
  1071. $db_to->truncate('gallery_items');
  1072. while ($row = $db_from->fetch_array($src)) {
  1073. $db_to->insert(
  1074. 'gallery_items',
  1075. array(
  1076. 'id' => $row['id'],
  1077. 'gallery_id' => $row['gallery_id'],
  1078. 'media_file' => $row['media_url'],
  1079. 'thumbnail' => $row['media_url'],
  1080. 'title' => $row['title'],
  1081. 'description' => $this->gekko_path_fix($row['description']),
  1082. 'author_id' => $row['author_id'],
  1083. 'date_created' => $row['date_created'],
  1084. 'date_modified' => $row['date_modified']
  1085. )
  1086. );
  1087. }
  1088. }
  1089. }
  1090. public function db_setup() {
  1091. extract(
  1092. $this->using(
  1093. 'db',
  1094. 'param',
  1095. 'session',
  1096. 'json',
  1097. 'env'
  1098. )
  1099. );
  1100. $param->not_empty(
  1101. 'db.driver',
  1102. 'db.hostname',
  1103. 'db.database'
  1104. );
  1105. $driver = $this->db_driver($param->get('db.driver'));
  1106. $session->clear('db_ok');
  1107. if (file_exists($driver)) {
  1108. require $driver;
  1109. $copy = array(
  1110. 'hostname',
  1111. 'username',
  1112. 'password',
  1113. 'database',
  1114. 'driver',
  1115. 'prefix'
  1116. );
  1117. $class_name = $param->get('db.driver').'_driver';
  1118. $conf = array();
  1119. $db->db = new $class_name;
  1120. foreach($copy as $el) {
  1121. $db->db->$el = $param->get('db.'.$el);
  1122. $conf[$el] = $db->db->$el;
  1123. }
  1124. if ($db->db->open(false)) {
  1125. $session->set('_db', $conf);
  1126. $mode = $param->get('mode');
  1127. $session->set('db_ok', true);
  1128. $env->redirect(sprintf('/action=%s', $mode));
  1129. }
  1130. } else {
  1131. $driver = false;
  1132. }
  1133. $json->response(
  1134. array(
  1135. 'errorMessage' => $driver ? $db->get_error() : 'Selected database driver does not exists.'
  1136. )
  1137. );
  1138. }
  1139. public function install_conf() {
  1140. extract(
  1141. $this->using(
  1142. 'param',
  1143. 'session',
  1144. 'json',
  1145. 'env'
  1146. )
  1147. );
  1148. $param->not_empty(
  1149. 'conf.core.site_name',
  1150. 'conf.core.site_slogan',
  1151. 'user.username',
  1152. 'user.email',
  1153. 'user.new_password',
  1154. 'user.repeat_password'
  1155. );
  1156. $this->session->set('_user', $param->get('user'));
  1157. $this->session->set('_conf', $param->get('conf'));
  1158. if ($param->get('user.new_password') == $param->get('user.repeat_password')) {
  1159. $env->redirect('/action=install_confirm');
  1160. } else {
  1161. $env->error('Password do not match.');
  1162. }
  1163. }
  1164. public function upgrade() {
  1165. extract(
  1166. $this->using(
  1167. 'db',
  1168. 'json',
  1169. 'param',
  1170. 'session'
  1171. )
  1172. );
  1173. $db_conf = $session->get('_db');
  1174. require $this->db_driver($db_conf['driver']);
  1175. $db_driver = $db_conf['driver'].'_driver';
  1176. $db->db = new $db_driver;
  1177. foreach ($db_conf as $key => $value) {
  1178. $db->db->{$key} = $value;
  1179. }
  1180. $db->open(false);
  1181. $this->using('conf');
  1182. $conf =& new conf($t = 1);
  1183. $modules = $this->get_modules();
  1184. foreach ($modules as $module) {
  1185. $version = $conf->get($module.'/version');
  1186. if ($version && ($conf->check_version('1.1', TM_VERSION) <= 0 && file_exists(TM_MODULES_DIR.$module))) {
  1187. $version = $this->upgrade_module($db, $module);
  1188. } else {
  1189. $version = $this->install_module($db, $module);
  1190. }
  1191. $conf->set($module.'/version', $version);
  1192. }
  1193. $conf->set('core/version', TM_VERSION);
  1194. $this->save_dbconf($db);
  1195. $this->clean_cache();
  1196. $json->response(
  1197. array(
  1198. 'successMessage' => 'Textmotion has been successfully upgraded.',
  1199. 'redirectTo' => $param->create('/action=upgrade_success')
  1200. )
  1201. );
  1202. }
  1203. private function install_module(&$db, $module) {
  1204. $xml =& $this->using('xml');
  1205. $version = 1;
  1206. $table_fields = array();
  1207. $data = array();
  1208. $file = TM_MODULES_DIR.$module.'/package.xml';
  1209. if (file_exists($file)) {
  1210. $xml->load($file);
  1211. $test_version = $xml->get_value('package/module/version');
  1212. if ($test_version) {
  1213. $version = $test_version;
  1214. }
  1215. // tables
  1216. $nodes = $xml->get_nodes('package/module/database/table');
  1217. foreach($nodes['_nodes'] as $i => $v) {
  1218. $table_node =& $nodes['_nodes'][$i]['table'];
  1219. $table_name = $table_node['_attr']['name'];
  1220. $table_fields[$table_name] = array();
  1221. if (isset($table_node)) {
  1222. foreach($table_node['_nodes'] as $j => $w) {
  1223. $field_node =& $table_node['_nodes'][$j]['field'];
  1224. if (isset($field_node)) {
  1225. $table_fields[$table_name][] = $field_node['_attr'];
  1226. }
  1227. }
  1228. }
  1229. }
  1230. // default data
  1231. $nodes = $xml->get_nodes('package/module/database/entries/table');
  1232. foreach($nodes['_nodes'] as $i => $v) {
  1233. $table_node =& $nodes['_nodes'][$i]['table'];
  1234. if (isset($table_node)) {
  1235. $data[$table_node['_attr']['name']] = array();
  1236. foreach($table_node['_nodes'] as $j => $w) {
  1237. $row_node =& $table_node['_nodes'][$j]['row'];
  1238. $fields = array();
  1239. if (isset($row_node)) {
  1240. foreach ($row_node['_nodes'] as $k => $x) {
  1241. $field_node =& $row_node['_nodes'][$k]['field'];
  1242. if (isset($field_node)) {
  1243. $fields[$field_node['_attr']['name']] = $field_node['_data'];
  1244. }
  1245. }
  1246. $data[$table_node['_attr']['name']][] = $fields;
  1247. }
  1248. }
  1249. }
  1250. }
  1251. foreach ($table_fields as $table => $fields) {
  1252. $sql = array();
  1253. foreach ($fields as $i => $v) {
  1254. $v = array_merge(
  1255. array(
  1256. 'empty' => true,
  1257. 'key' => false,
  1258. 'extra' => null,
  1259. 'default' => null
  1260. ),
  1261. $v
  1262. );
  1263. $v['empty'] = ($v['empty'] == 'true');
  1264. switch($db->db->driver) {
  1265. case 'mysql':
  1266. if ($v['default'] && !preg_match('/^(CURRENT_TIMESTAMP)$/', $v['default'])) {
  1267. $v['default'] = '\''.$db->escape($v['default']).'\'';
  1268. }
  1269. $fields[$i] = $db->escape($v['name']).' '.$v['type'].' '.($v['empty'] == false ? 'not null' : '').' '.($v['default'] ? 'default '.$v['default'] : '').' '.($v['extra']).' '.($v['key'] == 'PRI' ? ', PRIMARY KEY ('.$db->escape($v['name']).')' : '').' '.($v['key'] == 'MUL' ? ', KEY ('.$db->escape($v['name']).')' : '');
  1270. break;
  1271. }
  1272. }
  1273. switch($db->db->driver) {
  1274. case 'mysql':
  1275. $sql[] = 'DROP TABLE IF EXISTS '.$db->table_name($table);
  1276. $sql[] = 'CREATE TABLE '.$db->table_name($table).' ('.implode(', ', $fields).')';
  1277. break;
  1278. }
  1279. foreach($sql as $q) {
  1280. $db->query($q);
  1281. }
  1282. }
  1283. foreach ($data as $table => $inserts) {
  1284. foreach ($inserts as $insert) {
  1285. $db->insert($table, $insert);
  1286. }
  1287. }
  1288. }
  1289. return $version;
  1290. }
  1291. private function upgrade_module(&$db, $module) {
  1292. extract(
  1293. $this->using(
  1294. 'conf',
  1295. 'xml'
  1296. )
  1297. );
  1298. $version = $conf->get($module.'/version');
  1299. $file = TM_MODULES_DIR.$module.'/package.xml';
  1300. if (file_exists($file)) {
  1301. $xml->load($file);
  1302. $package_version = $xml->get_value('package/module/version');
  1303. $need_upgrade = $conf->check_version($package_version, $version);
  1304. $alter = array();
  1305. $update = array();
  1306. if ($need_upgrade > 0) {
  1307. $orig_version = $version;
  1308. $version = $package_version;
  1309. $nodes = $xml->get_nodes('package/module/database/upgrade/changeset');
  1310. if ($nodes) {
  1311. foreach($nodes['_nodes'] as $i => $v) {
  1312. $changeset_node =& $nodes['_nodes'][$i]['changeset'];
  1313. if (isset($changeset_node)) {
  1314. debug('>> '.$changeset_node['_attr']['version']);
  1315. debug('** '.$orig_version);
  1316. debug('%% '.$conf->check_version($changeset_node['_attr']['version'], $orig_version));
  1317. if ($conf->check_version($changeset_node['_attr']['version'], $orig_version) <= 0) {
  1318. continue;
  1319. }
  1320. foreach($changeset_node['_nodes'] as $j => $w) {
  1321. $table_node =& $changeset_node['_nodes'][$j]['table'];
  1322. if (isset($table_node)) {
  1323. $table_name = $table_node['_attr']['name'];
  1324. foreach($table_node['_nodes'] as $k => $x) {
  1325. $add_node =& $table_node['_nodes'][$k]['add'];
  1326. $update_node =& $table_node['_nodes'][$k]['update'];
  1327. $modify_node =& $table_node['_nodes'][$k]['modify'];
  1328. $delete_node =& $table_node['_nodes'][$k]['delete'];
  1329. $drop_node =& $table_node['_nodes'][$k]['drop'];
  1330. $copy_node =& $table_node['_nodes'][$k]['copy'];
  1331. $create_node =& $table_node['_nodes'][$k]['create'];
  1332. $rename_node =& $table_node['_nodes'][$k]['rename'];
  1333. if (isset($create_node)) {
  1334. $from_table = $copy_node['_attr']['from'];
  1335. $fields = array();
  1336. $values = array();
  1337. foreach($create_node['_nodes'] as $l => $y) {
  1338. $field_node = $create_node['_nodes'][$l]['field'];
  1339. if (isset($field_node)) {
  1340. $attr = array_merge(
  1341. array(
  1342. 'empty' => true,
  1343. 'key' => false,
  1344. 'extra' => null,
  1345. 'default' => null
  1346. ),
  1347. $field_node['_attr']
  1348. );
  1349. $fields[] = $db->escape($attr['name']).' '.$attr['type'].' '.($attr['empty'] ? '': 'NOT NULL').' '.($attr['default'] ? 'DEFAULT '.($attr['is_field'] ? $attr['default'] : $db->bind('?', $attr['default'])).'' : '').' '.$attr['extra'].' '.($attr['key'] ? ', PRIMARY KEY ('.$db->escape($attr['name']).')' : '');
  1350. }
  1351. }
  1352. $db->query('
  1353. CREATE TABLE '.$db->table_name($table_name).'
  1354. ('.implode(', ', $fields).')
  1355. ');
  1356. }
  1357. if (isset($copy_node)) {
  1358. $from_table = $copy_node['_attr']['from'];
  1359. $fields = array();
  1360. $values = array();
  1361. foreach($copy_node['_nodes'] as $l => $y) {
  1362. $field_node = $copy_node['_nodes'][$l]['field'];
  1363. if (isset($field_node)) {
  1364. $fields[] = $field_node['_attr']['name'];
  1365. if (isset($field_node['_attr']['copy'])) {
  1366. $values[] = $field_node['_attr']['copy'];
  1367. } else {
  1368. $values[] = '\''.$db->escape($field_node['_data']).'\'';
  1369. }
  1370. }
  1371. }
  1372. $db->query('
  1373. INSERT INTO '.$db->table_name($table_name).'
  1374. (`'.implode('`, `', $fields).'`)
  1375. SELECT
  1376. '.implode(', ', $values).'
  1377. FROM '.$db->table_name($from_table).'
  1378. ');
  1379. }
  1380. if (isset($drop_node)) {
  1381. $db->query('DROP TABLE '.$db->table_name($table_name).'');
  1382. }
  1383. if (isset($delete_node)) {
  1384. foreach($delete_node['_nodes'] as $l => $y) {
  1385. $field_node = $delete_node['_nodes'][$l]['field'];
  1386. if (isset($field_node)) {
  1387. $attr = $field_node['_attr'];
  1388. $db->query('
  1389. ALTER TABLE '.$db->table_name($table_name).'
  1390. DROP COLUMN '.$attr['name'].'
  1391. ');
  1392. }
  1393. }
  1394. }
  1395. if (isset($modify_node)) {
  1396. foreach($modify_node['_nodes'] as $l => $y) {
  1397. $field_node = $modify_node['_nodes'][$l]['field'];
  1398. if (isset($field_node)) {
  1399. $attr = array_merge(
  1400. array(
  1401. 'empty' => true,
  1402. 'after' => null,
  1403. 'default' => null
  1404. ),
  1405. $field_node['_attr']
  1406. );
  1407. $db->query('
  1408. ALTER TABLE '.$db->table_name($table_name).'
  1409. MODIFY '.$attr['name'].' '.$attr['type'].' '.($attr['empty'] ? '': 'NOT NULL').' '.($attr['default'] ? 'DEFAULT '.$db->bind('?', $attr['default'…

Large files files are truncated, but you can click here to view the full file