PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/include/admin/importers/pmachine.inc.php

http://github.com/s9y/Serendipity
PHP | 252 lines | 191 code | 46 blank | 15 comment | 33 complexity | 8b6d1e346bac05b565b72829f85c4c3b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
  3. # All rights reserved. See LICENSE file for licensing details
  4. /*****************************************************************
  5. * pMachine Importer, by Garvin Hicking *
  6. * ****************************************************************/
  7. class Serendipity_Import_pMachine extends Serendipity_Import {
  8. var $info = array('software' => 'pMachine Pro 2.4');
  9. var $data = array();
  10. var $inputFields = array();
  11. function __construct($data) {
  12. $this->data = $data;
  13. $this->inputFields = array(array('text' => INSTALL_DBHOST,
  14. 'type' => 'input',
  15. 'name' => 'host'),
  16. array('text' => INSTALL_DBUSER,
  17. 'type' => 'input',
  18. 'name' => 'user'),
  19. array('text' => INSTALL_DBPASS,
  20. 'type' => 'protected',
  21. 'name' => 'pass'),
  22. array('text' => INSTALL_DBNAME,
  23. 'type' => 'input',
  24. 'name' => 'name'),
  25. array('text' => INSTALL_DBPREFIX,
  26. 'type' => 'input',
  27. 'name' => 'prefix',
  28. 'default' => 'pm_'),
  29. array('text' => CHARSET,
  30. 'type' => 'list',
  31. 'name' => 'charset',
  32. 'value' => 'native',
  33. 'default' => $this->getCharsets()),
  34. array('text' => CONVERT_HTMLENTITIES,
  35. 'type' => 'bool',
  36. 'name' => 'use_strtr',
  37. 'default' => 'true'),
  38. array('text' => ACTIVATE_AUTODISCOVERY,
  39. 'type' => 'bool',
  40. 'name' => 'autodiscovery',
  41. 'default' => 'false')
  42. );
  43. }
  44. function validateData() {
  45. return sizeof($this->data);
  46. }
  47. function getInputFields() {
  48. return $this->inputFields;
  49. }
  50. function import() {
  51. global $serendipity;
  52. // Save this so we can return it to its original value at the end of this method.
  53. $noautodiscovery = isset($serendipity['noautodiscovery']) ? $serendipity['noautodiscovery'] : false;
  54. if ($this->data['autodiscovery'] == 'false') {
  55. $serendipity['noautodiscovery'] = 1;
  56. }
  57. $this->getTransTable();
  58. $this->data['prefix'] = serendipity_db_escape_string($this->data['prefix']);
  59. $users = array();
  60. $categories = array();
  61. $entries = array();
  62. if (!extension_loaded('mysqli')) {
  63. return MYSQL_REQUIRED;
  64. }
  65. $pmdb = @mysqli_connect($this->data['host'], $this->data['user'], $this->data['pass']);
  66. if (!$pmdb || mysqli_connect_error()) {
  67. return sprintf(COULDNT_CONNECT, serendipity_specialchars($this->data['host']));
  68. }
  69. if (!@mysqli_select_db($pmdb, $this->data['name'])) {
  70. return sprintf(COULDNT_SELECT_DB, mysqli_error($pmdb));
  71. }
  72. /* Users */
  73. $res = @$this->nativeQuery("SELECT id AS ID,
  74. username AS user_login,
  75. `password` AS user_pass,
  76. email AS user_email,
  77. status AS user_level,
  78. url AS url
  79. FROM {$this->data['prefix']}members", $pmdb);
  80. if (!$res) {
  81. return sprintf(COULDNT_SELECT_USER_INFO, mysqli_error($pmdb));
  82. }
  83. for ($x=0, $max_x = mysqli_num_rows($res); $x < $max_x ; $x++ ) {
  84. $users[$x] = mysqli_fetch_assoc($res);
  85. $data = array('right_publish' => ($users[$x]['user_level'] >= 3) ? 1 : 0,
  86. 'realname' => $users[$x]['user_login'],
  87. 'username' => $users[$x]['user_login'],
  88. 'email' => $users[$x]['user_email'],
  89. 'password' => $users[$x]['user_pass']); // pMachine uses md5, too.
  90. if ( $users[$x]['user_level'] < 12 ) {
  91. $data['userlevel'] = USERLEVEL_EDITOR;
  92. } else {
  93. $data['userlevel'] = USERLEVEL_ADMIN;
  94. }
  95. if ($serendipity['serendipityUserlevel'] < $data['userlevel']) {
  96. $data['userlevel'] = $serendipity['serendipityUserlevel'];
  97. }
  98. serendipity_db_insert('authors', $this->strtrRecursive($data));
  99. $users[$x]['authorid'] = serendipity_db_insert_id('authors', 'authorid');
  100. }
  101. /* Categories */
  102. $res = @$this->nativeQuery("SELECT id AS cat_ID,
  103. category AS cat_name,
  104. category AS category_description
  105. FROM {$this->data['prefix']}categories ORDER BY id", $pmdb);
  106. if (!$res) {
  107. return sprintf(COULDNT_SELECT_CATEGORY_INFO, mysqli_error($pmdb));
  108. }
  109. // Get all the info we need
  110. for ($x=0, $max_x = mysqli_num_rows($res) ; $x < $max_x ; $x++) {
  111. $categories[] = mysqli_fetch_assoc($res);
  112. }
  113. // Insert all categories as top level (we need to know everyone's ID before we can represent the hierarchy).
  114. for ($x=0, $max_x = sizeof($categories) ; $x < $max_x ; $x++ ) {
  115. $cat = array('category_name' => $categories[$x]['cat_name'],
  116. 'category_description' => $categories[$x]['category_description'],
  117. 'parentid' => 0, // <---
  118. 'category_left' => 0,
  119. 'category_right' => 0);
  120. serendipity_db_insert('category', $this->strtrRecursive($cat));
  121. $categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid');
  122. }
  123. serendipity_rebuildCategoryTree();
  124. /* Entries */
  125. $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}weblog ORDER BY t_stamp;", $pmdb);
  126. if (!$res) {
  127. return sprintf(COULDNT_SELECT_ENTRY_INFO, mysqli_error($pmdb));
  128. }
  129. for ($x=0, $max_x = mysqli_num_rows($res) ; $x < $max_x ; $x++ ) {
  130. $entries[$x] = mysqli_fetch_assoc($res);
  131. $entry = array('title' => $this->decode($entries[$x]['title']),
  132. 'isdraft' => ($entries[$x]['status'] == 'open') ? 'false' : 'true',
  133. 'allow_comments' => ($entries[$x]['showcomments'] == '1' ) ? 'true' : 'false',
  134. 'timestamp' => $entries[$x]['t_stamp'],
  135. 'extended' => $this->strtr($entries[$x]['more']),
  136. 'body' => $this->strtr($entries[$x]['body']));
  137. $entry['authorid'] = '';
  138. $entry['author'] = '';
  139. foreach ($users as $user) {
  140. if ($user['ID'] == $entries[$x]['member_id']) {
  141. $entry['authorid'] = $user['authorid'];
  142. $entry['author'] = $user['username'];
  143. break;
  144. }
  145. }
  146. if (!is_int($entries[$x]['entryid'] = serendipity_updertEntry($entry))) {
  147. return $entries[$x]['entryid'];
  148. }
  149. /* Entry/category */
  150. foreach ($categories as $category) {
  151. if ($category['cat_ID'] == $entries[$x]['category'] ) {
  152. $data = array('entryid' => $entries[$x]['entryid'],
  153. 'categoryid' => $category['categoryid']);
  154. serendipity_db_insert('entrycat', $this->strtrRecursive($data));
  155. break;
  156. }
  157. }
  158. }
  159. /* Comments */
  160. $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}comments;", $pmdb);
  161. if (!$res) {
  162. return sprintf(COULDNT_SELECT_COMMENT_INFO, mysqli_error($pmdb));
  163. }
  164. while ($a = mysqli_fetch_assoc($res)) {
  165. foreach ($entries as $entry) {
  166. if ($entry['post_id'] == $a['post_id'] ) {
  167. $author = '';
  168. $mail = '';
  169. $url = '';
  170. if (!empty($a['member_id'])) {
  171. foreach($users AS $user) {
  172. if ($user['ID'] == $a['member_id']) {
  173. $author = $user['user_login'];
  174. $mail = $user['user_email'];
  175. $url = $user['url'];
  176. break;
  177. }
  178. }
  179. }
  180. $comment = array('entry_id ' => $entry['entryid'],
  181. 'parent_id' => 0,
  182. 'timestamp' => $a['t_stamp'],
  183. 'author' => $author,
  184. 'email' => $mail,
  185. 'url' => $url,
  186. 'ip' => $a['comment_ip'],
  187. 'status' => ($a['status'] == 'open' ? 'approved' : 'pending'),
  188. 'body' => $a['body'],
  189. 'subscribed'=> 'false',
  190. 'type' => 'NORMAL');
  191. serendipity_db_insert('comments', $this->strtrRecursive($comment));
  192. if ($a['status'] == 'open') {
  193. $cid = serendipity_db_insert_id('comments', 'id');
  194. serendipity_approveComment($cid, $entry['entryid'], true);
  195. }
  196. }
  197. }
  198. }
  199. $serendipity['noautodiscovery'] = $noautodiscovery;
  200. // That was fun.
  201. return true;
  202. }
  203. }
  204. return 'Serendipity_Import_pMachine';
  205. /* vim: set sts=4 ts=4 expandtab : */
  206. ?>