/bp-forums/bbpress/bb-admin/export.php

  1. <?php 
  2.  
  3. require_once( '../bb-load.php' ); 
  4. require_once( BB_PATH . 'bb-admin/includes/functions.bb-admin.php' ); 
  5.  
  6. define('BB_EXPORT_USERS', 1); 
  7. define('BB_EXPORT_FORUMS', 2); 
  8. define('BB_EXPORT_TOPICS', 4); 
  9.  
  10. // Some example usage of the bitwise export levels (can be defined in bb-config.php) 
  11. //define('BB_EXPORT_LEVEL', BB_EXPORT_USERS); 
  12. //define('BB_EXPORT_LEVEL', BB_EXPORT_USERS + BB_EXPORT_FORUMS); 
  13. //define('BB_EXPORT_LEVEL', BB_EXPORT_USERS + BB_EXPORT_FORUMS + BB_EXPORT_TOPICS); 
  14.  
  15. if ( !defined('BB_EXPORT_LEVEL') ) 
  16. define('BB_EXPORT_LEVEL', 0); 
  17.  
  18. if ( !BB_EXPORT_LEVEL || !bb_current_user_can( 'import_export' ) ) 
  19. bb_die( __('Either export is disabled or you are not allowed to export.') ); 
  20.  
  21. // See bb_export_user for syntax 
  22. function _bb_export_object( $object, $properties = null, $tabs = 1 ) { 
  23. $r = ''; 
  24.  
  25. if ( !$type = $object['type'] ) 
  26. return; 
  27. unset($object['type']); 
  28.  
  29. $atts = ''; 
  30. $id = 0; 
  31. foreach ( $object as $att => $v ) { 
  32. if ( 'id' == $att ) { 
  33. $id = $v; 
  34. $v = $type . '_' . $v; 
  35. $atts .= " $att='$v'"; 
  36. unset($att, $v); 
  37.  
  38. $r .= str_repeat("\t", $tabs) . "<$type{$atts}>\n"; 
  39.  
  40. foreach ( (array) $properties as $k => $v ) { 
  41. if ( 'meta' == $k ) { 
  42. $data = ''; 
  43. foreach ( $v as $mk => $mv ) 
  44. $data .= str_repeat("\t", $tabs + 1) . "<meta key='$mk'><![CDATA[$mv]]></meta>\n"; 
  45. } else { 
  46. if ( '!' == $k{0} ) { 
  47. $k = substr($k, 1); 
  48. $v = "<![CDATA[$v]]>"; 
  49. $data = str_repeat("\t", $tabs + 1) . "<$k>$v</$k>\n"; 
  50. $r .= $data; 
  51.  
  52. $r .= apply_filters( 'in_bb_export_object_' . $type, '', $id ); 
  53.  
  54. $r .= str_repeat("\t", $tabs) . "</$type>\n\n"; 
  55.  
  56. return $r; 
  57.  
  58. // See bb_export_user for syntax 
  59. function _bb_translate_for_export( $translate, &$data ) { 
  60. $r = array(); 
  61. foreach ( $translate as $prop => $export ) { 
  62. if ( '?' == $export{0} ) { 
  63. $export = substr($export, 1); 
  64. if ( !$data[$prop] ) { 
  65. unset($data[$prop]); 
  66. continue; 
  67. if ( false === $export ) { 
  68. unset($data[$prop]); 
  69. continue; 
  70. $r[$export] = $data[$prop]; 
  71. unset($data[$prop]); 
  72. unset($export, $prop); 
  73. return $r; 
  74.  
  75. function bb_export_user( $user_id ) { 
  76. global $bbdb; 
  77. if ( !$_user = bb_get_user( $user_id ) ) 
  78. return; 
  79.  
  80. $_user = get_object_vars($_user); 
  81.  
  82. $atts = array( 
  83. 'type' => 'user',  
  84. 'id' => $_user['ID'] 
  85. ); 
  86.  
  87. // ?url means url is optional. Only include it in the export if it exists 
  88. // !title means the title should be wrapped in CDATA 
  89. // ?! is the correct order, not !? 
  90. $translate = array( 
  91. 'user_login' => 'login',  
  92. 'user_pass' => 'pass',  
  93. 'user_email' => 'email',  
  94. 'user_url' => '?url',  
  95. 'user_registered' => 'incept',  
  96. 'display_name' => '?!title',  
  97. 'user_nicename' => '?nicename',  
  98. 'user_status' => '?status',  
  99. 'ID' => false 
  100. ); 
  101.  
  102. $user = _bb_translate_for_export( $translate, $_user ); 
  103.  
  104. $meta = array(); 
  105. foreach ( $_user as $k => $v ) { 
  106. if ( 0 !== strpos($k, $bbdb->prefix) && isset($_user[$bbdb->prefix . $k]) ) 
  107. continue; 
  108. $meta[$k] = maybe_serialize($v); 
  109. unset($_user, $k, $v); 
  110.  
  111. $user['meta'] = $meta; 
  112.  
  113. return _bb_export_object( $atts, $user ); 
  114.  
  115. function bb_export_forum( $forum_id ) { 
  116. if ( !$_forum = bb_get_forum( $forum_id ) ) 
  117. return; 
  118.  
  119. $_forum = get_object_vars( $_forum ); 
  120.  
  121. $translate = array( 
  122. 'forum_name' => '!title',  
  123. 'forum_desc' => '?!content',  
  124. 'forum_parent' => '?parent' 
  125. ); 
  126.  
  127. $forum = _bb_translate_for_export( $translate, $_forum ); 
  128.  
  129. return _bb_export_object( array('type' => 'forum', 'id' => $_forum['forum_id']), $forum ); 
  130.  
  131. function bb_export_topic( $topic_id ) { 
  132. if ( !$_topic = get_topic( $topic_id ) ) 
  133. return; 
  134.  
  135. $_topic = get_object_vars( $_topic ); 
  136.  
  137. $atts = array( 
  138. 'type' => 'topic',  
  139. 'id' => $_topic['topic_id'],  
  140. 'author' => 'user_' . $_topic['topic_poster'],  
  141. 'in' => 'forum_' . $_topic['forum_id'] 
  142. ); 
  143.  
  144. $translate = array( 
  145. 'topic_title' => '!title',  
  146. 'topic_start_time' => 'incept',  
  147. 'topic_status' => '?status',  
  148. 'topic_id' => false,  
  149. 'topic_poster' => false,  
  150. 'topic_poster_name' => false,  
  151. 'topic_last_poster' => false,  
  152. 'topic_last_poster_name' => false,  
  153. 'topic_time' => false,  
  154. 'forum_id' => false,  
  155. 'topic_last_post_id' => false,  
  156. 'topic_posts' => false,  
  157. 'tag_count' => false 
  158. ); 
  159.  
  160. $topic = _bb_translate_for_export( $translate, $_topic ); 
  161.  
  162. $meta = array(); 
  163. foreach ( $_topic as $k => $v ) 
  164. $meta[$k] = maybe_serialize($v); 
  165. unset($_topic, $k, $v); 
  166.  
  167. $topic['meta'] = $meta; 
  168.  
  169. return _bb_export_object( $atts, $topic ); 
  170.  
  171. function bb_export_post( $post_id ) { 
  172. if ( !$_post = bb_get_post( $post_id ) ) 
  173. return; 
  174.  
  175. $_post = get_object_vars($_post); 
  176.  
  177. $atts = array( 
  178. 'type' => 'post',  
  179. 'id' => $_post['post_id'],  
  180. 'author' => 'user_' . $_post['poster_id'] 
  181. ); 
  182.  
  183. $translate = array( 
  184. 'post_time' => 'incept',  
  185. 'post_text' => '!content',  
  186. 'post_status' => '?status',  
  187. 'post_id' => false,  
  188. 'poster_id' => false,  
  189. 'forum_id' => false,  
  190. 'topic_id' => false,  
  191. 'post_position' => false 
  192. ); 
  193.  
  194. $post = _bb_translate_for_export( $translate, $_post ); 
  195.  
  196. $post['meta'] = $_post; 
  197.  
  198. return _bb_export_object( $atts, $post, 2 ); 
  199.  
  200. // One of these things is not like the others... 
  201. function bb_export_tag( $tag ) { 
  202. // id here is not numeric. does not currently preserve tagged_on 
  203. return "\t\t<tag author='user_$tag->user_id' id='tag_$tag->tag'><![CDATA[$tag->raw_tag]]></tag>\n"; 
  204.  
  205. function bb_export_topic_tags( $r, $topic_id ) { 
  206. global $topic_tag_cache; 
  207. if ( !get_topic( $topic_id ) ) 
  208. return; 
  209.  
  210. if ( !$tags = bb_get_topic_tags( $topic_id ) ) 
  211. return $r; 
  212.  
  213. $r .= "\n"; 
  214.  
  215. foreach ( (array) $tags as $tag ) 
  216. $r .= bb_export_tag( $tag ); 
  217. $topic_tag_cache = array(); 
  218.  
  219. return $r; 
  220.  
  221. function bb_export_topic_posts( $r, $topic_id ) { 
  222. if ( !get_topic( $topic_id ) ) 
  223. return; 
  224.  
  225. $r .= "\n"; 
  226.  
  227. $page = 1; 
  228. while ( $posts = get_thread( $topic_id, array( 'post_status' => 'all', 'page' => $page++ ) ) ) { 
  229. foreach ( $posts as $post ) 
  230. $r .= bb_export_post( $post->post_id ); 
  231.  
  232. return $r; 
  233.  
  234. function bb_export() { 
  235. global $bb; 
  236.  
  237. define( 'BB_EXPORTING', true ); 
  238. do_action( 'bb_pre_export' ); 
  239.  
  240. $bb->use_cache = false; // Turn off hard cache 
  241. $bb->page_topics = 100; 
  242.  
  243. echo "<forums-data version='0.75'>\n"; 
  244.  
  245. if (BB_EXPORT_LEVEL & BB_EXPORT_USERS) { 
  246. $page = 1; 
  247. while ( ( $users = bb_user_search( array('page' => $page++) ) ) && !is_wp_error( $users ) ) { 
  248. foreach ( $users as $user ) 
  249. echo bb_export_user( $user->ID ); 
  250. unset($users, $user, $page); 
  251.  
  252. if (BB_EXPORT_LEVEL & BB_EXPORT_FORUMS) { 
  253. $forums = bb_get_forums(); 
  254. foreach ( $forums as $forum ) 
  255. echo bb_export_forum( $forum->forum_id ); 
  256. unset($forums, $forum); 
  257.  
  258. if (BB_EXPORT_LEVEL & BB_EXPORT_TOPICS) { 
  259. $page = 1; 
  260. while ( $topics = get_latest_topics( 0, $page++ ) ) { 
  261. foreach ( $topics as $topic ) 
  262. echo bb_export_topic( $topic->topic_id ); 
  263. unset($topics, $topic, $page); 
  264.  
  265. do_action( 'bb_export' ); 
  266.  
  267. echo '</forums-data>'; 
  268.  
  269. add_filter( 'in_bb_export_object_topic', 'bb_export_topic_tags', 10, 2 ); 
  270. add_filter( 'in_bb_export_object_topic', 'bb_export_topic_posts', 10, 2 ); 
  271. add_filter( 'get_forum_where', 'bb_no_where', 9999 ); 
  272. add_filter( 'get_forums_where', 'bb_no_where', 9999 ); 
  273. add_filter( 'get_latest_topics_where', 'bb_no_where', 9999 ); 
  274. add_filter( 'get_user_where', 'bb_no_where', 9999 ); 
  275. add_filter( 'cache_users_where', 'bb_no_where', 9999 ); 
  276.  
  277. bb_export(); 
  278.  
  279. ?> 
.