PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/include/admin/importers/textpattern.inc.php

http://github.com/s9y/Serendipity
PHP | 251 lines | 190 code | 46 blank | 15 comment | 34 complexity | d5a8181eaa22b70112c0ae350f03ddb1 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. * textpattern Importer, by Garvin Hicking *
  6. * ****************************************************************/
  7. class Serendipity_Import_textpattern extends Serendipity_Import {
  8. var $info = array('software' => 'Textpattern 1.0rc1');
  9. var $data = array();
  10. var $inputFields = array();
  11. var $categories = array();
  12. function getImportNotes() {
  13. return '<p>Textpattern uses MySQLs native PASSWORD() function to save passwords. Thus, those passwords are incompatible with the MD5 hashing of Serendipity. The passwords for all users have been set to "txp". <strong>You need to modify the passwords manually for each user</strong>, we are sorry for that inconvenience.</p>';
  14. }
  15. function __construct($data) {
  16. $this->data = $data;
  17. $this->inputFields = array(array('text' => INSTALL_DBHOST,
  18. 'type' => 'input',
  19. 'name' => 'host'),
  20. array('text' => INSTALL_DBUSER,
  21. 'type' => 'input',
  22. 'name' => 'user'),
  23. array('text' => INSTALL_DBPASS,
  24. 'type' => 'protected',
  25. 'name' => 'pass'),
  26. array('text' => INSTALL_DBNAME,
  27. 'type' => 'input',
  28. 'name' => 'name'),
  29. array('text' => INSTALL_DBPREFIX,
  30. 'type' => 'input',
  31. 'name' => 'prefix',
  32. 'default' => ''),
  33. array('text' => CHARSET,
  34. 'type' => 'list',
  35. 'name' => 'charset',
  36. 'value' => 'UTF-8',
  37. 'default' => $this->getCharsets(true)),
  38. array('text' => CONVERT_HTMLENTITIES,
  39. 'type' => 'bool',
  40. 'name' => 'use_strtr',
  41. 'default' => 'true'),
  42. array('text' => ACTIVATE_AUTODISCOVERY,
  43. 'type' => 'bool',
  44. 'name' => 'autodiscovery',
  45. 'default' => 'false')
  46. );
  47. }
  48. function validateData() {
  49. return sizeof($this->data);
  50. }
  51. function getInputFields() {
  52. return $this->inputFields;
  53. }
  54. function import() {
  55. global $serendipity;
  56. // Save this so we can return it to its original value at the end of this method.
  57. $noautodiscovery = isset($serendipity['noautodiscovery']) ? $serendipity['noautodiscovery'] : false;
  58. if ($this->data['autodiscovery'] == 'false') {
  59. $serendipity['noautodiscovery'] = 1;
  60. }
  61. $this->getTransTable();
  62. $this->data['prefix'] = serendipity_db_escape_string($this->data['prefix']);
  63. $users = array();
  64. $entries = array();
  65. if (!extension_loaded('mysqli')) {
  66. return MYSQL_REQUIRED;
  67. }
  68. $txpdb = @mysqli_connect($this->data['host'], $this->data['user'], $this->data['pass']);
  69. if (!$txpdb || mysqli_connect_error()) {
  70. return sprintf(COULDNT_CONNECT, serendipity_specialchars($this->data['host']));
  71. }
  72. if (!@mysqli_select_db($txpdb, $this->data['name'])) {
  73. return sprintf(COULDNT_SELECT_DB, mysqli_error($txpdb));
  74. }
  75. /* Users */
  76. $res = @$this->nativeQuery("SELECT user_id AS ID,
  77. name AS user_login,
  78. `pass` AS user_pass,
  79. email AS user_email,
  80. privs AS user_level
  81. FROM {$this->data['prefix']}txp_users", $txpdb);
  82. if (!$res) {
  83. return sprintf(COULDNT_SELECT_USER_INFO, mysqli_error($txpdb));
  84. }
  85. for ($x=0, $max_x = mysqli_num_rows($res); $x < $max_x ; $x++ ) {
  86. $users[$x] = mysqli_fetch_assoc($res);
  87. $data = array('right_publish' => ($users[$x]['user_level'] <= 4) ? 1 : 0,
  88. 'realname' => $users[$x]['user_login'],
  89. 'username' => $users[$x]['user_login'],
  90. 'email' => $users[$x]['user_email'],
  91. 'password' => md5('txp')); // blame TXP for using PASSWORD().
  92. if ( $users[$x]['user_level'] == 1 ) {
  93. $data['userlevel'] = USERLEVEL_EDITOR;
  94. } elseif ($users[$x]['user_level'] == 2) {
  95. $data['userlevel'] = USERLEVEL_CHIEF;
  96. } else {
  97. $data['userlevel'] = USERLEVEL_ADMIN;
  98. }
  99. if ($serendipity['serendipityUserlevel'] < $data['userlevel']) {
  100. $data['userlevel'] = $serendipity['serendipityUserlevel'];
  101. }
  102. serendipity_db_insert('authors', $this->strtrRecursive($data));
  103. $users[$x]['authorid'] = serendipity_db_insert_id('authors', 'authorid');
  104. }
  105. /* Categories */
  106. if (!$this->importCategories('root', 0, $txpdb)) {
  107. return sprintf(COULDNT_SELECT_CATEGORY_INFO, mysqli_error($txpdb));
  108. }
  109. serendipity_rebuildCategoryTree();
  110. /* Entries */
  111. // Notice: Textpattern doesn't honor the prefix for this table. Wicked system.
  112. $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}textpattern ORDER BY Posted;", $txpdb);
  113. if (!$res) {
  114. return sprintf(COULDNT_SELECT_ENTRY_INFO, mysqli_error($txpdb));
  115. }
  116. for ($x=0, $max_x = mysqli_num_rows($res) ; $x < $max_x ; $x++ ) {
  117. $entries[$x] = mysqli_fetch_assoc($res);
  118. $entry = array('title' => $this->decode($entries[$x]['Title']),
  119. 'isdraft' => ($entries[$x]['Status'] == '4') ? 'false' : 'true',
  120. 'allow_comments' => ($entries[$x]['Annotate'] == '1' ) ? 'true' : 'false',
  121. 'timestamp' => strtotime($entries[$x]['Posted']),
  122. 'extended' => $this->strtr($entries[$x]['Body_html']),
  123. 'body' => $this->strtr($entries[$x]['Excerpt']));
  124. $entry['authorid'] = '';
  125. $entry['author'] = '';
  126. foreach ($users as $user) {
  127. if ($user['user_login'] == $entries[$x]['AuthorID']) {
  128. $entry['authorid'] = $user['authorid'];
  129. $entry['author'] = $user['user_login'];
  130. break;
  131. }
  132. }
  133. if (!is_int($entries[$x]['entryid'] = serendipity_updertEntry($entry))) {
  134. return $entries[$x]['entryid'];
  135. }
  136. /* Entry/category */
  137. foreach ($this->categories as $category) {
  138. if ($category['name'] == $entries[$x]['Category1'] || $category['name'] == $entries[$x]['Category2']) {
  139. $data = array('entryid' => $entries[$x]['entryid'],
  140. 'categoryid' => $category['categoryid']);
  141. serendipity_db_insert('entrycat', $this->strtrRecursive($data));
  142. break;
  143. }
  144. }
  145. }
  146. /* Comments */
  147. $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}txp_discuss;", $txpdb);
  148. if (!$res) {
  149. return sprintf(COULDNT_SELECT_COMMENT_INFO, mysqli_error($txpdb));
  150. }
  151. while ($a = mysqli_fetch_assoc($res)) {
  152. foreach ($entries as $entry) {
  153. if ($entry['ID'] == $a['parentid'] ) {
  154. $author = $a['name'];
  155. $mail = $a['email'];
  156. $url = $a['web'];
  157. $comment = array('entry_id ' => $entry['entryid'],
  158. 'parent_id' => 0,
  159. 'timestamp' => strtotime($a['posted']),
  160. 'author' => $author,
  161. 'email' => $mail,
  162. 'url' => $url,
  163. 'ip' => $a['ip'],
  164. 'status' => ($a['visible'] == '1' ? 'approved' : 'pending'),
  165. 'body' => $a['message'],
  166. 'subscribed'=> 'false',
  167. 'type' => 'NORMAL');
  168. serendipity_db_insert('comments', $this->strtrRecursive($comment));
  169. if ($a['visible'] == '1') {
  170. $cid = serendipity_db_insert_id('comments', 'id');
  171. serendipity_approveComment($cid, $entry['entryid'], true);
  172. }
  173. }
  174. }
  175. }
  176. $serendipity['noautodiscovery'] = $noautodiscovery;
  177. // That was fun.
  178. return true;
  179. }
  180. function importCategories($parentname = 'root', $parentid = 0, $txpdb = null) {
  181. $res = $this->nativeQuery("SELECT * FROM {$this->data['prefix']}txp_category
  182. WHERE parent = '" . mysqli_escape_string($parentname) . "' AND type = 'article'", $txpdb);
  183. if (!$res) {
  184. echo mysqli_error();
  185. return false;
  186. }
  187. // Get all the info we need
  188. for ($x=0, $max_x = mysqli_num_rows($res) ; $x < $max_x ; $x++) {
  189. $row = mysqli_fetch_assoc($res);
  190. $cat = array('category_name' => $row['name'],
  191. 'category_description' => $row['name'],
  192. 'parentid' => $parentid,
  193. 'category_left' => 0,
  194. 'category_right' => 0);
  195. serendipity_db_insert('category', $this->strtrRecursive($cat));
  196. $row['categoryid'] = serendipity_db_insert_id('category', 'categoryid');
  197. $this->categories[] = $row;
  198. $this->importCategories($row['name'], $row['categoryid'], $txpdb);
  199. }
  200. return true;
  201. }
  202. }
  203. return 'Serendipity_Import_textpattern';
  204. /* vim: set sts=4 ts=4 expandtab : */
  205. ?>