[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-admin/ -> export.php (source)

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


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1