b2evolution PHP Cross Reference Blogging Systems

Source: /inc/tools/model/_wp.funcs.php - 1287 lines - 45388 bytes - Summary - Text - Print

Description: This file implements the functions to work with WordPress importer. b2evolution - {@link http://b2evolution.net/} Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   1  <?php
   2  /**

   3   * This file implements the functions to work with WordPress importer.

   4   *

   5   * b2evolution - {@link http://b2evolution.net/}

   6   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}

   9   *

  10   * {@internal Open Source relicensing agreement:

  11   * }}

  12   *

  13   * @package admin

  14   *

  15   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  16   * @author fplanque: Francois PLANQUE.

  17   *

  18   * @version $Id: _wp.funcs.php 78 2011-10-26 16:29:49Z fplanque $

  19   */
  20  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  21  
  22  
  23  /**

  24   * Import WordPress data from XML file into b2evolution database

  25   */
  26  function wpxml_import()
  27  {
  28      global $DB, $tableprefix;
  29  
  30      // Load classes:

  31      load_class( 'regional/model/_country.class.php', 'Country' );
  32      load_class( 'regional/model/_region.class.php', 'Region' );
  33      load_class( 'regional/model/_subregion.class.php', 'Subregion' );
  34      load_class( 'regional/model/_city.class.php', 'City' );
  35  
  36      // Set Blog from request blog ID

  37      $wp_blog_ID = param( 'wp_blog_ID', 'integer', 0 );
  38      $BlogCache = & get_BlogCache();
  39      $wp_Blog = & $BlogCache->get_by_ID( $wp_blog_ID );
  40  
  41      // The import type ( replace | append )

  42      $import_type = param( 'import_type', 'string', 'replace' );
  43  
  44      $XML_file_path = get_param( 'wp_file' );
  45      $XML_file_name = basename( $XML_file_path );
  46  
  47      if( preg_match( '/\.(xml|txt)$/i', $XML_file_name ) )
  48      { // XML format
  49          // Check WordPress XML file

  50          if( ! wpxml_check_xml_file( $XML_file_path ) )
  51          { // Errors are in XML file
  52              return;
  53          }
  54  
  55          // Use this folder to upload files if they exist in subfolder "/b2evolution_export_files"

  56          $attached_files_path = dirname( $XML_file_path );
  57      }
  58      else if( preg_match( '/\.zip$/i', $XML_file_name ) )
  59      { // ZIP format
  60          // Extract ZIP and check WordPress XML file

  61          global $media_path;
  62  
  63          $ZIP_folder_path = $media_path.'import/temp-'.md5( rand() );
  64  
  65          if( ! unpack_archive( $XML_file_path, $ZIP_folder_path, true, $XML_file_name ) )
  66          { // Errors on unpack ZIP file
  67              return;
  68          }
  69  
  70          // Find valid XML file in ZIP package

  71          $ZIP_files_list = scandir( $ZIP_folder_path );
  72          $xml_exists_in_zip = false;
  73          foreach( $ZIP_files_list as $ZIP_file )
  74          {
  75              if( preg_match( '/\.(xml|txt)$/i', $ZIP_file ) )
  76              { // XML file is found in ZIP package
  77                  if( wpxml_check_xml_file( $ZIP_folder_path.'/'.$ZIP_file ) )
  78                  { // XML file is valid
  79                      $XML_file_path = $ZIP_folder_path.'/'.$ZIP_file;
  80                      $xml_exists_in_zip = true;
  81                      break;
  82                  }
  83              }
  84          }
  85  
  86          if( ! $xml_exists_in_zip )
  87          { // No XML is detected in ZIP package
  88              echo '<p style="color:red">'.T_( 'XML file is not detected in your ZIP package.' ).'</p>';
  89              // Delete temporary folder that contains the files from extracted ZIP package

  90              rmdir_r( $ZIP_folder_path );
  91              return;
  92          }
  93  
  94          // Use this folder to upload files, $ZIP_folder_path must be deleted after import

  95          $attached_files_path = $ZIP_folder_path;
  96      }
  97      else
  98      { // Unrecognized extension
  99          echo '<p style="color:red">'.sprintf( T_( '%s has an unrecognized extension.' ), '<b>'.$xml_file['name'].'</b>' ).'</p>';
 100          return;
 101      }
 102  
 103      // Parse WordPress XML file into array

 104      $xml_data = wpxml_parser( $XML_file_path );
 105  
 106  
 107      $DB->begin();
 108  
 109      if( $import_type == 'replace' )
 110      {    // Remove data from selected blog
 111  
 112          // Get existing categories

 113          $SQL = new SQL();
 114          $SQL->SELECT( 'cat_ID' );
 115          $SQL->FROM( 'T_categories' );
 116          $SQL->WHERE( 'cat_blog_ID = '.$DB->quote( $wp_blog_ID ) );
 117          $old_categories = $DB->get_col( $SQL->get() );
 118          if( !empty( $old_categories ) )
 119          {    // Get existing posts
 120              $SQL = new SQL();
 121              $SQL->SELECT( 'post_ID' );
 122              $SQL->FROM( 'T_items__item' );
 123              $SQL->WHERE( 'post_main_cat_ID IN ( '.implode( ', ', $old_categories ).' )' );
 124              $old_posts = $DB->get_col( $SQL->get() );
 125          }
 126  
 127          echo T_('Removing the comments... ');
 128          evo_flush();
 129          if( !empty( $old_posts ) )
 130          {
 131              $SQL = new SQL();
 132              $SQL->SELECT( 'comment_ID' );
 133              $SQL->FROM( 'T_comments' );
 134              $SQL->WHERE( 'comment_post_ID IN ( '.implode( ', ', $old_posts ).' )' );
 135              $old_comments = $DB->get_col( $SQL->get() );
 136              $DB->query( 'DELETE FROM T_comments WHERE comment_post_ID IN ( '.implode( ', ', $old_posts ).' )' );
 137              if( !empty( $old_comments ) )
 138              {
 139                  $DB->query( 'DELETE FROM T_comments__votes WHERE cmvt_cmt_ID IN ( '.implode( ', ', $old_comments ).' )' );
 140                  $DB->query( 'DELETE FROM T_links WHERE link_cmt_ID IN ( '.implode( ', ', $old_comments ).' )' );
 141              }
 142          }
 143          echo T_('OK').'<br />';
 144  
 145          echo T_('Removing the posts... ');
 146          evo_flush();
 147          if( !empty( $old_categories ) )
 148          {
 149              $DB->query( 'DELETE FROM T_items__item WHERE post_main_cat_ID IN ( '.implode( ', ', $old_categories ).' )' );
 150              if( !empty( $old_posts ) )
 151              {    // Remove the post's data from related tables
 152                  $DB->query( 'DELETE FROM T_items__item_settings WHERE iset_item_ID IN ( '.implode( ', ', $old_posts ).' )' );
 153                  $DB->query( 'DELETE FROM T_items__prerendering WHERE itpr_itm_ID IN ( '.implode( ', ', $old_posts ).' )' );
 154                  $DB->query( 'DELETE FROM T_items__subscriptions WHERE isub_item_ID IN ( '.implode( ', ', $old_posts ).' )' );
 155                  $DB->query( 'DELETE FROM T_items__version WHERE iver_itm_ID IN ( '.implode( ', ', $old_posts ).' )' );
 156                  $DB->query( 'DELETE FROM T_postcats WHERE postcat_post_ID IN ( '.implode( ', ', $old_posts ).' )' );
 157                  $DB->query( 'DELETE FROM T_slug WHERE slug_itm_ID IN ( '.implode( ', ', $old_posts ).' )' );
 158                  $DB->query( 'DELETE FROM T_links WHERE link_itm_ID IN ( '.implode( ', ', $old_posts ).' )' );
 159              }
 160          }
 161          echo T_('OK').'<br />';
 162      
 163          echo T_('Removing the categories... ');
 164          evo_flush();
 165          $DB->query( 'DELETE FROM T_categories WHERE cat_blog_ID = '.$DB->quote( $wp_blog_ID ) );
 166          echo T_('OK').'<br />';
 167  
 168          echo T_('Removing the tags that are no longer used... ');
 169          evo_flush();
 170          if( !empty( $old_posts ) )
 171          {    // Remove the tags
 172  
 173              // Get tags from selected blog

 174              $SQL = new SQL();
 175              $SQL->SELECT( 'itag_tag_ID' );
 176              $SQL->FROM( 'T_items__itemtag' );
 177              $SQL->WHERE( 'itag_itm_ID IN ( '.implode( ', ', $old_posts ).' )' );
 178              $old_tags_this_blog = array_unique( $DB->get_col( $SQL->get() ) );
 179  
 180              if( !empty( $old_tags_this_blog ) )
 181              {
 182                  // Get tags from other blogs

 183                  $SQL = new SQL();
 184                  $SQL->SELECT( 'itag_tag_ID' );
 185                  $SQL->FROM( 'T_items__itemtag' );
 186                  $SQL->WHERE( 'itag_itm_ID NOT IN ( '.implode( ', ', $old_posts ).' )' );
 187                  $old_tags_other_blogs = array_unique( $DB->get_col( $SQL->get() ) );
 188                  $old_tags_other_blogs_sql = !empty( $old_tags_other_blogs ) ? ' AND tag_ID NOT IN ( '.implode( ', ', $old_tags_other_blogs ).' )': '';
 189  
 190                  // Remove the tags that are no longer used

 191                  $DB->query( 'DELETE FROM T_items__tag
 192                      WHERE tag_ID IN ( '.implode( ', ', $old_tags_this_blog ).' )'.
 193                      $old_tags_other_blogs_sql );
 194              }
 195  
 196              // Remove the links of tags with posts

 197              $DB->query( 'DELETE FROM T_items__itemtag WHERE itag_itm_ID IN ( '.implode( ', ', $old_posts ).' )' );
 198          }
 199          echo T_('OK').'<br /><br />';
 200      }
 201  
 202  
 203      /* Import authors */

 204      $authors = array();
 205      $authors_IDs = array();
 206      if( isset( $xml_data['authors'] ) && count( $xml_data['authors'] ) > 0 )
 207      {
 208          global $Settings, $UserSettings;
 209  
 210          echo T_('Importing the users... ');
 211          evo_flush();
 212  
 213          // Get existing users

 214          $SQL = new SQL();
 215          $SQL->SELECT( 'user_login, user_ID' );
 216          $SQL->FROM( 'T_users' );
 217          $existing_users = $DB->get_assoc( $SQL->get() );
 218  
 219          $authors_count = 0;
 220          foreach( $xml_data['authors'] as $author )
 221          {
 222              if( empty( $existing_users[ (string) $author['author_login'] ] ) )
 223              {    // Insert new user into DB if User doesn't exist with current login name
 224              
 225                  $GroupCache = & get_GroupCache();
 226                  if( !empty( $author['author_group'] ) )
 227                  {    // Set user group from xml data
 228                      if( ( $UserGroup = & $GroupCache->get_by_name( $author['author_group'], false ) ) === false )
 229                      {    // If user's group doesn't exist yet, we should create new
 230                          $UserGroup = new Group();
 231                          $UserGroup->set( 'name', $author['author_group'] );
 232                          $UserGroup->dbinsert();
 233                      }
 234                  }
 235                  else
 236                  {    // Set default user group is it is not defined in xml
 237                      if( ( $UserGroup = & $GroupCache->get_by_name( 'Normal Users', false ) ) === false )
 238                      {    // Exit from import of users, because we cannot set default user group
 239                          break;
 240                      }
 241                  }
 242  
 243                  unset( $author_created_from_country );
 244                  if( !empty( $author['author_created_from_country'] ) )
 245                  {    // Get country ID from DB by code
 246                      $CountryCache = & get_CountryCache();
 247                      if( ( $Country = & $CountryCache->get_by_name( $author['author_created_from_country'], false ) ) !== false )
 248                      {
 249                          $author_created_from_country = $Country->ID;
 250                      }
 251                  }
 252  
 253                  // Get regional IDs by their names

 254                  $author_regions = wp_get_regional_data( $author['author_country'], $author['author_region'], $author['author_subregion'], $author['author_city'] );
 255  
 256                  $User = new User();
 257                  $User->set( 'login', $author['author_login'] );
 258                  $User->set( 'email', $author['author_email'] );
 259                  $User->set( 'firstname', $author['author_first_name'] );
 260                  $User->set( 'lastname', $author['author_last_name'] );
 261                  $User->set( 'pass', $author['author_pass'] );
 262                  $User->set_Group( $UserGroup );
 263                  $User->set( 'status', !empty( $author['author_status'] ) ? $author['author_status'] : 'autoactivated' );
 264                  $User->set( 'nickname', $author['author_nickname'] );
 265                  $User->set( 'url', $author['author_url'] );
 266                  $User->set( 'level', $author['author_level'] );
 267                  $User->set( 'locale', $author['author_locale'] );
 268                  $User->set( 'gender', ( $author['author_gender'] == 'female' ? 'F' : ( $author['author_gender'] == 'male' ? 'M' : '' ) ) );
 269                  if( $author['author_age_min'] > 0 )
 270                  {
 271                      $User->set( 'age_min', $author['author_age_min'] );
 272                  }
 273                  if( $author['author_age_max'] > 0 )
 274                  {
 275                      $User->set( 'age_max', $author['author_age_max'] );
 276                  }
 277                  if( isset( $author_created_from_country ) )
 278                  {    // User was created from this country
 279                      $User->set( 'reg_ctry_ID', $author_created_from_country );
 280                  }
 281                  if( !empty( $author_regions['country'] ) )
 282                  {    // Country
 283                      $User->set( 'ctry_ID', $author_regions['country'] );
 284                      if( !empty( $author_regions['region'] ) )
 285                      {    // Region
 286                          $User->set( 'rgn_ID', $author_regions['region'] );
 287                          if( !empty( $author_regions['subregion'] ) )
 288                          {    // Subregion
 289                              $User->set( 'subrg_ID', $author_regions['subregion'] );
 290                          }
 291                          if( !empty( $author_regions['city'] ) )
 292                          {    // City
 293                              $User->set( 'city_ID', $author_regions['city'] );
 294                          }
 295                      }
 296                  }
 297                  $User->set( 'source', $author['author_source'] );
 298                  $User->set_datecreated( empty( $author['author_created_ts'] ) ? mktime() : intval( $author['author_created_ts'] ) );
 299                  $User->set( 'lastseen_ts', ( empty( $author['author_lastseen_ts'] ) ? NULL : $author['author_lastseen_ts'] ), true );
 300                  $User->set( 'profileupdate_date', empty( $author['author_profileupdate_date'] ) ? date( 'Y-m-d', mktime() ): $author['author_profileupdate_date'] );
 301                  $User->dbinsert();
 302                  $user_ID = $User->ID;
 303                  if( !empty( $user_ID ) && !empty( $author['author_created_fromIPv4'] ) )
 304                  {
 305                      $UserSettings->set( 'created_fromIPv4', ip2int( $author['author_created_fromIPv4'] ), $user_ID );
 306                  }
 307                  $authors_count++;
 308              }
 309              else
 310              {    // Get ID of existing user
 311                  $user_ID = $existing_users[ (string) $author['author_login'] ];
 312              }
 313              // Save user ID of current author

 314              $authors[ $author['author_login'] ] = (string) $user_ID;
 315              $authors_IDs[ $author['author_id'] ] = (string) $user_ID;
 316          }
 317  
 318          $UserSettings->dbupdate();
 319  
 320          echo sprintf( T_('%d records'), $authors_count ).'<br />';
 321      }
 322  
 323      /* Import files, Copy them all to media folder */

 324      if( isset( $xml_data['files'] ) && count( $xml_data['files'] ) > 0 )
 325      {
 326          echo T_('Importing the files... ');
 327          evo_flush();
 328  
 329          if( ! file_exists( $attached_files_path.'/b2evolution_export_files' ) )
 330          { // Display an error if files are attached but folder doesn't exist
 331              echo '<p class="red">'.sprintf( T_('No folder %s found. It must exists to import the attached files properly.'), '<b>'.$attached_files_path.'/b2evolution_export_files'.'</b>' ).'</p>';
 332          }
 333          else
 334          { // The attached files are located in this subfolder
 335              $subfolder_path = '/b2evolution_export_files';
 336  
 337              $files_count = 0;
 338              $files = array();
 339  
 340              foreach( $xml_data['files'] as $file )
 341              {
 342                  switch( $file['file_root_type'] )
 343                  {
 344                      case 'shared':
 345                          // Shared files

 346                          $file_root_ID = 0;
 347                          break;
 348  
 349                      case 'user':
 350                          // User's files

 351                          if( isset( $authors_IDs[ $file['file_root_ID'] ] ) )
 352                          { // If owner of this file exists in our DB
 353                              $file_root_ID = $authors_IDs[ $file['file_root_ID'] ];
 354                              break;
 355                          }
 356                          // Otherwise we should upload this file into blog's folder:

 357  
 358                      default: // 'collection', 'absolute', 'skins'
 359                          // The files from other blogs and from other places must be moved in the folder of the current blog

 360                          $file['file_root_type'] = 'collection';
 361                          $file_root_ID = $wp_blog_ID;
 362                          break;
 363                  }
 364  
 365                  // Get FileRoot by type and ID

 366                  $FileRoot = new FileRoot( $file['file_root_type'], $file_root_ID );
 367                  if( is_dir( $attached_files_path.$subfolder_path.'/'.$file['zip_path'].$file['file_path'] ) )
 368                  { // Folder
 369                      $file_destination_path = $FileRoot->ads_path;
 370                  }
 371                  else
 372                  { // File
 373                      $file_destination_path = $FileRoot->ads_path.$file['file_path'];
 374                  }
 375  
 376                  if( ! file_exists( $attached_files_path.$subfolder_path.'/'.$file['zip_path'].$file['file_path'] ) )
 377                  { // File doesn't exist
 378                      echo '<p class="orange">'.sprintf( T_('Unable to copy file %s, because it does not exist.'), '<b>'.$file['zip_path'].$file['file_path'].'</b>' ).'</p>';
 379                      // Skip it

 380                      continue;
 381                  }
 382                  else if( ! copy_r( $attached_files_path.$subfolder_path.'/'.$file['zip_path'].$file['file_path'], $file_destination_path ) )
 383                  { // No permission to copy to this folder
 384                      if( is_dir( $attached_files_path.$subfolder_path.'/'.$file['zip_path'].$file['file_path'] ) )
 385                      { // Folder
 386                          echo '<p class="orange">'.sprintf( T_('Unable to copy folder %s to %s. Please, check the permissions assigned to this folder.'), '<b>'.$file['zip_path'].$file['file_path'].'</b>', '<b>'.$file_destination_path.'</b>' ).'</p>';
 387                      }
 388                      else
 389                      { // File
 390                          echo '<p class="orange">'.sprintf( T_('Unable to copy file %s to %s. Please, check the permissions assigned to this folder.'), '<b>'.$file['zip_path'].$file['file_path'].'</b>', '<b>'.$file_destination_path.'</b>' ).'</p>';
 391                      }
 392                      // Skip it

 393                      continue;
 394                  }
 395  
 396                  // Create new File object, It will be linked to the items below

 397                  $File = new File( $file['file_root_type'], $file_root_ID, $file['file_path'] );
 398                  $File->set( 'title', $file['file_title'] );
 399                  $File->set( 'alt', $file['file_alt'] );
 400                  $File->set( 'desc', $file['file_desc'] );
 401                  $files[ $file['file_ID'] ] = $File;
 402  
 403                  $files_count++;
 404              }
 405  
 406              echo sprintf( T_('%d records'), $files_count ).'<br />';
 407  
 408              if( isset( $ZIP_folder_path ) && file_exists( $ZIP_folder_path ) )
 409              { // This folder was created only to extract files from ZIP package, Remove it now
 410                  rmdir_r( $ZIP_folder_path );
 411              }
 412          }
 413      }
 414  
 415      /* Import categories */

 416      $category_default = 0;
 417  
 418      // Get existing categories

 419      $SQL = new SQL();
 420      $SQL->SELECT( 'cat_urlname, cat_ID' );
 421      $SQL->FROM( 'T_categories' );
 422      $SQL->WHERE( 'cat_blog_ID = '.$DB->quote( $wp_blog_ID ) );
 423      $categories = $DB->get_assoc( $SQL->get() );
 424  
 425      if( isset( $xml_data['categories'] ) && count( $xml_data['categories'] ) > 0 )
 426      {
 427          echo T_('Importing the categories... ');
 428          evo_flush();
 429  
 430          load_class( 'chapters/model/_chapter.class.php', 'Chapter' );
 431          load_funcs( 'locales/_charset.funcs.php' );
 432  
 433          $categories_count = 0;
 434          foreach( $xml_data['categories'] as $cat )
 435          {
 436              if( empty( $categories[ (string) $cat['category_nicename'] ] ) )
 437              {
 438                  $Chapter = new Chapter( NULL, $wp_blog_ID );
 439                  $Chapter->set( 'name', $cat['cat_name'] );
 440                  $Chapter->set( 'urlname', $cat['category_nicename'] );
 441                  $Chapter->set( 'description', $cat['category_description'] );
 442                  if( !empty( $cat['category_parent'] ) && isset( $categories[ (string) $cat['category_parent'] ] ) )
 443                  {    // Set category parent ID
 444                      $Chapter->set( 'parent_ID', $categories[ (string) $cat['category_parent'] ] );
 445                  }
 446                  $Chapter->dbinsert();
 447  
 448                  // Save new category

 449                  $categories[ $cat['category_nicename'] ] = $Chapter->ID;
 450                  if( empty( $category_default ) )
 451                  {    // Set first category as default
 452                      $category_default = $Chapter->ID;
 453                  }
 454                  $categories_count++;
 455              }
 456          }
 457  
 458          if( empty( $category_default ) )
 459          {    // Set first category as default
 460              foreach( $categories as $category_name => $category_ID )
 461              {
 462                  $category_default = $category_ID;
 463                  break;
 464              }
 465          }
 466  
 467          echo sprintf( T_('%d records'), $categories_count ).'<br />';
 468      }
 469  
 470      /* Import tags */

 471      $tags = array();
 472      if( isset( $xml_data['tags'] ) && count( $xml_data['tags'] ) > 0 )
 473      {
 474          echo T_('Importing the tags... ');
 475          evo_flush();
 476  
 477          // Get existing tags

 478          $SQL = new SQL();
 479          $SQL->SELECT( 'tag_name, tag_ID' );
 480          $SQL->FROM( 'T_items__tag' );
 481          $tags = $DB->get_assoc( $SQL->get() );
 482  
 483          $tags_count = 0;
 484          foreach( $xml_data['tags'] as $tag )
 485          {
 486              if( empty( $tags[ (string) $tag['tag_name'] ] ) )
 487              {    // Insert new tag into DB if tag doesn't exist with current name
 488                  mysql_query( 'INSERT INTO '.$tableprefix.'items__tag ( tag_name )
 489                      VALUES ( '.$DB->quote( $tag['tag_name'] ).' )',
 490                      $DB->dbhandle );
 491                  $tag_ID = mysql_insert_id( $DB->dbhandle );
 492                  // Save new tag

 493                  $tags[ $tag['tag_name'] ] = (string) $tag_ID;
 494                  $tags_count++;
 495              }
 496          }
 497          echo sprintf( T_('%d records'), $tags_count ).'<br />';
 498      }
 499  
 500  
 501      /* Import posts */

 502      $posts = array();
 503      $comments = array();
 504      if( isset( $xml_data['posts'] ) && count( $xml_data['posts'] ) > 0 )
 505      {
 506          load_class( 'items/model/_item.class.php', 'Item' );
 507  
 508          // Set status's links between WP and b2evo names

 509          $post_statuses = array(
 510              'publish'    => 'published',
 511              'pending'    => 'deprecated',
 512              'deprecated' => 'deprecated',
 513              'protected'  => 'protected',
 514              'private'    => 'private',
 515              'redirected' => 'redirected',
 516              'draft'      => 'draft',
 517          );
 518  
 519          // Get post types

 520          $SQL = new SQL();
 521          $SQL->SELECT( 'LOWER( ptyp_name ), ptyp_ID' );
 522          $SQL->FROM( 'T_items__type' );
 523          $post_types = $DB->get_assoc( $SQL->get() );
 524  
 525          echo T_('Importing the posts... ');
 526          evo_flush();
 527  
 528          foreach( $xml_data['posts'] as $post )
 529          {
 530              $author_ID = isset( $authors[ (string) $post['post_author'] ] ) ? $authors[ (string) $post['post_author'] ] : 1;
 531              $last_edit_user_ID = isset( $authors[ (string) $post['post_lastedit_user'] ] ) ? $authors[ (string) $post['post_lastedit_user'] ] : $author_ID;
 532  
 533              $post_main_cat_ID = $category_default;
 534              $post_extra_cat_IDs = array( $post_main_cat_ID );
 535              $post_tags = array();
 536              if( !empty( $post['terms'] ) )
 537              {    // Set categories and tags
 538                  foreach( $post['terms'] as $term )
 539                  {
 540                      switch( $term['domain'] )
 541                      {
 542                          case 'category':
 543                              if( isset( $categories[ (string) $term['slug'] ] ) )
 544                              {
 545                                  if( $post_main_cat_ID == $category_default )
 546                                  {    // Set main category
 547                                      $post_main_cat_ID = $categories[ (string) $term['slug'] ];
 548                                  }
 549                                  else
 550                                  {    // Set extra categories
 551                                      $post_extra_cat_IDs[] = $categories[ (string) $term['slug'] ];
 552                                  }
 553                              }
 554                              break;
 555  
 556                          case 'post_tag':
 557                              if( isset( $tags[ (string) $term['slug'] ] ) )
 558                              {    // Set tag
 559                                  $post_tags[] = $term['slug'];
 560                              }
 561                              break;
 562                      }
 563                  }
 564              }
 565  
 566              // Set post type ID

 567              $post_type_ID = isset( $post_types[ strtolower( $post['post_type'] ) ] ) ? $post_types[ strtolower( $post['post_type'] ) ] : '1';
 568  
 569              // Get regional IDs by their names

 570              $item_regions = wp_get_regional_data( $post['post_country'], $post['post_region'], $post['post_subregion'], $post['post_city'] );
 571  
 572              $Item = new Item();
 573              $Item->set( 'main_cat_ID', $post_main_cat_ID );
 574              $Item->set( 'creator_user_ID', $author_ID );
 575              $Item->set( 'lastedit_user_ID', $last_edit_user_ID );
 576              $Item->set( 'title', $post['post_title'] );
 577              $Item->set( 'content', $post['post_content'] );
 578              $Item->set( 'excerpt', $post['post_excerpt'] );
 579              $Item->set( 'datestart', $post['post_date'] );
 580              $Item->set( 'datecreated', !empty( $post['post_datecreated'] ) ? $post['post_datecreated'] : $post['post_date'] );
 581              $Item->set( 'datemodified', !empty( $post['post_datemodified'] ) ? $post['post_datemodified'] : $post['post_date'] );
 582              $Item->set( 'urltitle', !empty( $post['post_urltitle'] ) ? $post['post_urltitle'] : $post['post_title'] );
 583              $Item->set( 'url', $post['post_url'] );
 584              $Item->set( 'status', isset( $post_statuses[ (string) $post['status'] ] ) ? $post_statuses[ (string) $post['status'] ] : 'draft' );
 585              // If 'comment_status' has the unappropriate value set it to 'open'

 586              $Item->set( 'comment_status', ( in_array( $post['comment_status'], array( 'open', 'closed', 'disabled' ) ) ? $post['comment_status'] : 'open' ) );
 587              $Item->set( 'ptyp_ID', $post_type_ID );
 588              if( empty( $post['post_excerpt'] ) && !empty( $post['post_content'] ) )
 589              {    // Generate excerpt
 590                  $Item->set( 'excerpt', wp_generate_excerpt( $post['post_content'] ) );
 591                  $Item->set( 'excerpt_autogenerated', '1' );
 592              }
 593              $Item->set( 'extra_cat_IDs', $post_extra_cat_IDs );
 594              $Item->set( 'dateset', $post['post_date_mode'] == 'set' ? 1 : 0 );
 595              if( isset( $authors[ (string) $post['post_assigned_user'] ] ) )
 596              {
 597                  $Item->set( 'assigned_user', $authors[ (string) $post['post_assigned_user'] ] );
 598              }
 599              $Item->set( 'datedeadline', $post['post_datedeadline'] );
 600              $Item->set( 'locale', $post['post_locale'] );
 601              $Item->set( 'excerpt_autogenerated', $post['post_excerpt_autogenerated'] );
 602              $Item->set( 'titletag', $post['post_titletag'] );
 603              $Item->set( 'notifications_status', empty( $post['post_notifications_status'] ) ? 'noreq' : $post['post_notifications_status'] );
 604              $Item->set( 'views', $post['post_views'] );
 605              $Item->set( 'renderers', array( $post['post_renderers'] ) );
 606              $Item->set( 'priority', $post['post_priority'] );
 607              $Item->set( 'featured', $post['post_featured'] );
 608              $Item->set( 'order', $post['post_order'] );
 609              if( !empty( $item_regions['country'] ) )
 610              {    // Country
 611                  $Item->set( 'ctry_ID', $item_regions['country'] );
 612                  if( !empty( $item_regions['region'] ) )
 613                  {    // Region
 614                      $Item->set( 'rgn_ID', $item_regions['region'] );
 615                      if( !empty( $item_regions['subregion'] ) )
 616                      {    // Subregion
 617                          $Item->set( 'subrg_ID', $item_regions['subregion'] );
 618                      }
 619                      if( !empty( $item_regions['city'] ) )
 620                      {    // City
 621                          $Item->set( 'city_ID', $item_regions['city'] );
 622                      }
 623                  }
 624              }
 625  
 626              if( count( $post_tags ) > 0 )
 627              {
 628                  $Item->tags = $post_tags;
 629              }
 630  
 631              $Item->dbinsert();
 632              $posts[ $post['post_id'] ] = $Item->ID;
 633  
 634              if( ! empty( $files ) && ! empty( $post['links'] ) )
 635              { // Link the files to the Item if it has them
 636                  foreach( $post['links'] as $link )
 637                  {
 638                      if( isset( $files[ $link['link_file_ID'] ] ) )
 639                      { // Link a file to Item
 640                          $File = $files[ $link['link_file_ID'] ];
 641                          $LinkOwner = new LinkItem( $Item );
 642                          $File->link_to_Object( $LinkOwner, $link['link_order'], $link['link_position'] );
 643                      }
 644                  }
 645              }
 646  
 647              if( !empty( $post['comments'] ) )
 648              { // Set comments
 649                  $comments[ $Item->ID ] = $post['comments'];
 650              }
 651          }
 652  
 653          foreach( $xml_data['posts'] as $post )
 654          {    // Set post parents
 655              if( !empty( $post['post_parent'] ) && isset( $posts[ (string) $post['post_parent'] ] ) )
 656              {
 657                  mysql_query( 'UPDATE '.$tableprefix.'items__item
 658                            SET post_parent_ID = '.$DB->quote( $posts[ (string) $post['post_parent'] ] ).'
 659                          WHERE post_ID = '.$DB->quote( $posts[ (string) $post['post_id'] ] ),
 660                      $DB->dbhandle );
 661              }
 662          }
 663  
 664          echo sprintf( T_('%d records'), count( $xml_data['posts'] ) ).'<br />';
 665      }
 666  
 667  
 668      /* Import comments */

 669      if( !empty( $comments ) )
 670      {
 671          echo T_('Importing the comments... ');
 672          evo_flush();
 673  
 674          $comments_count = 0;
 675          $comments_IDs = array();
 676          foreach( $comments as $post_ID => $comments )
 677          {
 678              if( empty( $comments ) )
 679              {    // Skip if no comments
 680                  continue;
 681              }
 682  
 683              foreach( $comments as $comment )
 684              {
 685                  $comment_author_ID = 0;
 686                  if( !empty( $comment['comment_user_id'] ) && isset( $authors_IDs[ (string) $comment['comment_user_id'] ] ) )
 687                  {    // Author ID
 688                      $comment_author_ID = $authors_IDs[ (string) $comment['comment_user_id'] ];
 689                  }
 690  
 691                  $comment_parent_ID = 0;
 692                  if( !empty( $comment['comment_parent'] ) && isset( $comments_IDs[ (string) $comment['comment_parent'] ] ) )
 693                  {    // Parent comment ID
 694                      $comment_parent_ID = $comments_IDs[ (string) $comment['comment_parent'] ];
 695                  }
 696  
 697                  unset( $comment_IP_country );
 698                  if( !empty( $comment['comment_IP_country'] ) )
 699                  {    // Get country ID by code
 700                      $CountryCache = & get_CountryCache();
 701                      if( $Country = & $CountryCache->get_by_name( $comment['comment_IP_country'], false ) )
 702                      {
 703                          $comment_IP_country = $Country->ID;
 704                      }
 705                  }
 706  
 707                  $Comment = new Comment();
 708                  $Comment->item_ID = $post_ID;
 709                  $Comment->set( 'post_ID', $post_ID );
 710                  if( !empty( $comment_parent_ID ) )
 711                  {
 712                      $Comment->set( 'in_reply_to_cmt_ID', $comment_parent_ID );
 713                  }
 714                  $Comment->set( 'date', $comment['comment_date'] );
 715                  if( !empty( $comment_author_ID ) )
 716                  {
 717                      $Comment->set( 'author_ID', $comment_author_ID );
 718                  }
 719                  $Comment->set( 'author', evo_substr( $comment['comment_author'], 0, 100 ) );
 720                  $Comment->set( 'author_IP', $comment['comment_author_IP'] );
 721                  $Comment->set( 'author_email', $comment['comment_author_email'] );
 722                  $Comment->set( 'content', $comment['comment_content'] );
 723                  if( empty( $comment['comment_status'] ) )
 724                  {    // If comment status is empty (the export of wordpress doesn't provide this field)
 725                      $Comment->set( 'status', $comment['comment_approved'] == '1' ? 'published' : 'draft' );
 726                  }
 727                  else
 728                  {    // Set status when we have predefined value
 729                      $Comment->set( 'status', $comment['comment_status'] );
 730                  }
 731                  if( !empty( $comment_IP_country ) )
 732                  {    // Country
 733                      $Comment->set( 'IP_ctry_ID', $comment_IP_country );
 734                  }
 735                  $Comment->set( 'rating', $comment['comment_rating'] );
 736                  $Comment->set( 'featured', $comment['comment_featured'] );
 737                  $Comment->set( 'nofollow', $comment['comment_nofollow'] );
 738                  $Comment->set( 'helpful_addvotes', $comment['comment_helpful_addvotes'] );
 739                  $Comment->set( 'helpful_countvotes', $comment['comment_helpful_countvotes'] );
 740                  $Comment->set( 'spam_addvotes', $comment['comment_spam_addvotes'] );
 741                  $Comment->set( 'spam_countvotes', $comment['comment_spam_countvotes'] );
 742                  $Comment->set( 'karma', $comment['comment_karma'] );
 743                  $Comment->set( 'spam_karma', $comment['comment_spam_karma'] );
 744                  $Comment->set( 'allow_msgform', $comment['comment_allow_msgform'] );
 745                  $Comment->set( 'notif_status', empty( $comment['comment_notif_status'] ) ? 'noreq' : $comment['comment_notif_status'] );
 746                  $Comment->dbinsert();
 747  
 748                  $comments_IDs[ $comment['comment_id'] ] = $Comment->ID;
 749                  $comments_count++;
 750              }
 751          }
 752  
 753          echo sprintf( T_('%d records'), $comments_count ).'<br />';
 754      }
 755  
 756      $DB->commit();
 757  }
 758  
 759  
 760  /**

 761   * Parse WordPress XML file into array

 762   *

 763   * @param string File path

 764   * @return array XML data:

 765   *          authors

 766   *          posts

 767   *          categories

 768   *          tags

 769   *          terms

 770   *          base_url

 771   *          wxr_version

 772   */
 773  function wpxml_parser( $file )
 774  {
 775      $authors = array();
 776      $posts = array();
 777      $categories = array();
 778      $tags = array();
 779      $terms = array();
 780      $files = array();
 781  
 782      $xml = simplexml_load_file( $file );
 783  
 784      // Get WXR version

 785      $wxr_version = $xml->xpath( '/rss/channel/wp:wxr_version' );
 786      $wxr_version = (string) trim( $wxr_version[0] );
 787  
 788      $base_url = $xml->xpath( '/rss/channel/wp:base_site_url' );
 789      $base_url = (string) trim( $base_url[0] );
 790  
 791      $namespaces = $xml->getDocNamespaces();
 792      if( !isset( $namespaces['wp'] ) )
 793      {
 794          $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
 795      }
 796      if( !isset( $namespaces['evo'] ) )
 797      {
 798          $namespaces['evo'] = 'http://b2evolution.net/export/1.0/';
 799      }
 800      if( !isset( $namespaces['excerpt'] ) )
 801      {
 802          $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
 803      }
 804  
 805      // Get authors

 806      foreach( $xml->xpath('/rss/channel/wp:author') as $author_arr )
 807      {
 808          $a = $author_arr->children( $namespaces['wp'] );
 809          $ae = $author_arr->children( $namespaces['evo'] );
 810          $login = (string) $a->author_login;
 811          $authors[$login] = array(
 812              'author_id'                   => (int) $a->author_id,
 813              'author_login'                => $login,
 814              'author_email'                => (string) $a->author_email,
 815              'author_display_name'         => (string) $a->author_display_name,
 816              'author_first_name'           => (string) $a->author_first_name,
 817              'author_last_name'            => (string) $a->author_last_name,
 818              'author_pass'                 => (string) $ae->author_pass,
 819              'author_group'                => (string) $ae->author_group,
 820              'author_status'               => (string) $ae->author_status,
 821              'author_nickname'             => (string) $ae->author_nickname,
 822              'author_url'                  => (string) $ae->author_url,
 823              'author_level'                => (int) $ae->author_level,
 824              'author_locale'               => (string) $ae->author_locale,
 825              'author_gender'               => (string) $ae->author_gender,
 826              'author_age_min'              => (int) $ae->author_age_min,
 827              'author_age_max'              => (int) $ae->author_age_max,
 828              'author_created_from_country' => (string) $ae->author_created_from_country,
 829              'author_country'              => (string) $ae->author_country,
 830              'author_region'               => (string) $ae->author_region,
 831              'author_subregion'            => (string) $ae->author_subregion,
 832              'author_city'                 => (string) $ae->author_city,
 833              'author_source'               => (string) $ae->author_source,
 834              'author_created_ts'           => (string) $ae->author_created_ts,
 835              'author_lastseen_ts'          => (string) $ae->author_lastseen_ts,
 836              'author_created_fromIPv4'     => (string) $ae->author_created_fromIPv4,
 837              'author_profileupdate_date'   => (string) $ae->author_profileupdate_date,
 838          );
 839      }
 840  
 841      // Get files

 842      foreach( $xml->xpath('/rss/channel/file') as $file_arr )
 843      {
 844          $t = $file_arr->children( $namespaces['evo'] );
 845          $files[] = array(
 846              'file_ID'        => (int) $t->file_ID,
 847              'file_root_type' => (string) $t->file_root_type,
 848              'file_root_ID'   => (int) $t->file_root_ID,
 849              'file_path'      => (string) $t->file_path,
 850              'file_title'     => (string) $t->file_title,
 851              'file_alt'       => (string) $t->file_alt,
 852              'file_desc'      => (string) $t->file_desc,
 853              'zip_path'       => (string) $t->zip_path,
 854          );
 855      }
 856  
 857      // Get categories

 858      foreach( $xml->xpath('/rss/channel/wp:category') as $term_arr )
 859      {
 860          $t = $term_arr->children( $namespaces['wp'] );
 861          $categories[] = array(
 862              'term_id'              => (int) $t->term_id,
 863              'category_nicename'    => (string) $t->category_nicename,
 864              'category_parent'      => (string) $t->category_parent,
 865              'cat_name'             => (string) $t->cat_name,
 866              'category_description' => (string) $t->category_description
 867          );
 868      }
 869  
 870      // Get tags

 871      foreach( $xml->xpath('/rss/channel/wp:tag') as $term_arr )
 872      {
 873          $t = $term_arr->children( $namespaces['wp'] );
 874          $tags[] = array(
 875              'term_id'         => (int) $t->term_id,
 876              'tag_slug'        => (string) $t->tag_slug,
 877              'tag_name'        => (string) $t->tag_name,
 878              'tag_description' => (string) $t->tag_description
 879          );
 880      }
 881  
 882      // Get terms

 883      foreach( $xml->xpath('/rss/channel/wp:term') as $term_arr )
 884      {
 885          $t = $term_arr->children( $namespaces['wp'] );
 886          $terms[] = array(
 887              'term_id'          => (int) $t->term_id,
 888              'term_taxonomy'    => (string) $t->term_taxonomy,
 889              'slug'             => (string) $t->term_slug,
 890              'term_parent'      => (string) $t->term_parent,
 891              'term_name'        => (string) $t->term_name,
 892              'term_description' => (string) $t->term_description
 893          );
 894      }
 895  
 896      // Get posts

 897      foreach( $xml->channel->item as $item )
 898      {
 899          $post = array(
 900              'post_title' => (string) $item->title,
 901              'guid'       => (string) $item->guid,
 902          );
 903  
 904          $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
 905          $post['post_author'] = (string) $dc->creator;
 906  
 907          $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
 908          $excerpt = $item->children( $namespaces['excerpt'] );
 909          $post['post_content'] = (string) $content->encoded;
 910          $post['post_excerpt'] = (string) $excerpt->encoded;
 911  
 912          $wp = $item->children( $namespaces['wp'] );
 913          $evo = $item->children( $namespaces['evo'] );
 914  
 915          $post['post_id']        = (int) $wp->post_id;
 916          $post['post_date']      = (string) $wp->post_date;
 917          $post['post_date_gmt']  = (string) $wp->post_date_gmt;
 918          $post['comment_status'] = (string) $wp->comment_status;
 919          $post['ping_status']    = (string) $wp->ping_status;
 920          $post['post_name']      = (string) $wp->post_name;
 921          $post['status']         = (string) $wp->status;
 922          $post['post_parent']    = (int) $wp->post_parent;
 923          $post['menu_order']     = (int) $wp->menu_order;
 924          $post['post_type']      = (string) $wp->post_type;
 925          $post['post_password']  = (string) $wp->post_password;
 926          $post['is_sticky']      = (int) $wp->is_sticky;
 927          $post['post_date_mode']     = (string) $evo->post_date_mode;
 928          $post['post_lastedit_user'] = (string) $evo->post_lastedit_user;
 929          $post['post_assigned_user'] = (string) $evo->post_assigned_user;
 930          $post['post_datedeadline']  = (string) $evo->post_datedeadline;
 931          $post['post_datecreated']   = (string) $evo->post_datecreated;
 932          $post['post_datemodified']  = (string) $evo->post_datemodified;
 933          $post['post_locale']        = (string) $evo->post_locale;
 934          $post['post_excerpt_autogenerated'] = (int) $evo->post_excerpt_autogenerated;
 935          $post['post_urltitle']      = (string) $evo->post_urltitle;
 936          $post['post_titletag']      = (string) $evo->post_titletag;
 937          $post['post_url']           = (string) $evo->post_url;
 938          $post['post_notifications_status'] = (string) $evo->post_notifications_status;
 939          $post['post_views']         = (int) $evo->post_views;
 940          $post['post_renderers']     = (string) $evo->post_renderers;
 941          $post['post_priority']      = (int) $evo->post_priority;
 942          $post['post_featured']      = (int) $evo->post_featured;
 943          $post['post_order']         = (int) $evo->post_order;
 944          $post['post_country']       = (string) $evo->post_country;
 945          $post['post_region']        = (string) $evo->post_region;
 946          $post['post_subregion']     = (string) $evo->post_subregion;
 947          $post['post_city']          = (string) $evo->post_city;
 948  
 949          if( isset( $wp->attachment_url ) )
 950          {
 951              $post['attachment_url'] = (string) $wp->attachment_url;
 952          }
 953  
 954          foreach ( $item->category as $c )
 955          {
 956              $att = $c->attributes();
 957              if( isset( $att['nicename'] ) )
 958              {
 959                  $post['terms'][] = array(
 960                      'name'   => (string) $c,
 961                      'slug'   => (string) $att['nicename'],
 962                      'domain' => (string) $att['domain']
 963                  );
 964              }
 965          }
 966  
 967          foreach( $wp->postmeta as $meta )
 968          {
 969              $post['postmeta'][] = array(
 970                  'key'   => (string) $meta->meta_key,
 971                  'value' => (string) $meta->meta_value
 972              );
 973          }
 974  
 975          foreach( $wp->comment as $comment )
 976          {
 977              $evo_comment = $comment->children( $namespaces['evo'] );
 978  
 979              $meta = array();
 980              if( isset( $comment->commentmeta ) )
 981              {
 982                  foreach( $comment->commentmeta as $m )
 983                  {
 984                      $meta[] = array(
 985                          'key'   => (string) $m->meta_key,
 986                          'value' => (string) $m->meta_value
 987                      );
 988                  }
 989              }
 990  
 991              $post['comments'][] = array(
 992                  'comment_id'           => (int) $comment->comment_id,
 993                  'comment_author'       => (string) $comment->comment_author,
 994                  'comment_author_email' => (string) $comment->comment_author_email,
 995                  'comment_author_IP'    => (string) $comment->comment_author_IP,
 996                  'comment_author_url'   => (string) $comment->comment_author_url,
 997                  'comment_date'         => (string) $comment->comment_date,
 998                  'comment_date_gmt'     => (string) $comment->comment_date_gmt,
 999                  'comment_content'      => (string) $comment->comment_content,
1000                  'comment_approved'     => (string) $comment->comment_approved,
1001                  'comment_type'         => (string) $comment->comment_type,
1002                  'comment_parent'       => (string) $comment->comment_parent,
1003                  'comment_user_id'      => (int) $comment->comment_user_id,
1004                  'comment_status'             => (string) $evo_comment->comment_status,
1005                  'comment_IP_country'         => (string) $evo_comment->comment_IP_country,
1006                  'comment_rating'             => (int) $evo_comment->comment_rating,
1007                  'comment_featured'           => (int) $evo_comment->comment_featured,
1008                  'comment_nofollow'           => (int) $evo_comment->comment_nofollow,
1009                  'comment_helpful_addvotes'   => (int) $evo_comment->comment_helpful_addvotes,
1010                  'comment_helpful_countvotes' => (int) $evo_comment->comment_helpful_countvotes,
1011                  'comment_spam_addvotes'      => (int) $evo_comment->comment_spam_addvotes,
1012                  'comment_spam_countvotes'    => (int) $evo_comment->comment_spam_countvotes,
1013                  'comment_karma'              => (int) $evo_comment->comment_comment_karma,
1014                  'comment_spam_karma'         => (int) $evo_comment->comment_spam_karma,
1015                  'comment_allow_msgform'      => (int) $evo_comment->comment_allow_msgform,
1016                  'comment_notif_status'       => (string) $evo_comment->comment_notif_status,
1017                  'commentmeta'                => $meta,
1018              );
1019          }
1020  
1021          foreach( $evo->link as $link )
1022          { // Get the links
1023              $evo_link = $link->children( $namespaces['evo'] );
1024  
1025              $post['links'][] = array(
1026                  'link_ID'               => (int) $link->link_ID,
1027                  'link_datecreated'      => (string) $link->link_datecreated,
1028                  'link_datemodified'     => (string) $link->link_datemodified,
1029                  'link_creator_user_ID'  => (int) $link->link_creator_user_ID,
1030                  'link_lastedit_user_ID' => (int) $link->link_lastedit_user_ID,
1031                  'link_itm_ID'           => (int) $link->link_itm_ID,
1032                  'link_cmt_ID'           => (int) $link->link_cmt_ID,
1033                  'link_usr_ID'           => (int) $link->link_usr_ID,
1034                  'link_file_ID'          => (int) $link->link_file_ID,
1035                  'link_ltype_ID'         => (int) $link->link_ltype_ID,
1036                  'link_position'         => (string) $link->link_position,
1037                  'link_order'            => (int) $link->link_order,
1038              );
1039          }
1040  
1041          $posts[] = $post;
1042      }
1043  
1044      return array(
1045          'authors'    => $authors,
1046          'files'      => $files,
1047          'posts'      => $posts,
1048          'categories' => $categories,
1049          'tags'       => $tags,
1050          'terms'      => $terms,
1051          'base_url'   => $base_url,
1052          'version'    => $wxr_version
1053      );
1054  }
1055  
1056  
1057  /**

1058   * Check WordPress XML file for correct format

1059   *

1060   * @param string File path

1061   * @param boolean TRUE to halt process of error, FALSE to print out error

1062   * @return boolean TRUE on success, FALSE or HALT on errors

1063   */
1064  function wpxml_check_xml_file( $file, $halt = false )
1065  {
1066      $internal_errors = libxml_use_internal_errors( true );
1067      $xml = simplexml_load_file( $file );
1068      if( !$xml )
1069      { // halt/display if loading produces an error
1070          if( $halt )
1071          {
1072              debug_die( 'There was an error when reading this WXR file.' );
1073          }
1074          else
1075          {
1076              echo '<p style="color:red">'.T_('There was an error when reading this WXR file.').'</p>';
1077              return false;
1078          }
1079      }
1080  
1081      $wxr_version = $xml->xpath( '/rss/channel/wp:wxr_version' );
1082      if( !$wxr_version )
1083      {
1084          if( $halt )
1085          {
1086              debug_die( 'This does not appear to be a WXR file, missing/invalid WXR version number.' );
1087          }
1088          else
1089          {
1090              echo '<p style="color:red">'.T_('This does not appear to be a WXR file, missing/invalid WXR version number.').'</p>';
1091              return false;
1092          }
1093      }
1094  
1095      $wxr_version = (string) trim( $wxr_version[0] );
1096      if( !preg_match( '/^\d+\.\d+$/', $wxr_version ) )
1097      { // confirm that we are dealing with the correct file format
1098          if( $halt )
1099          {
1100              debug_die( 'This does not appear to be a WXR file, missing/invalid WXR version number.' );
1101          }
1102          else
1103          {
1104              echo '<p style="color:red">'.T_('This does not appear to be a WXR file, missing/invalid WXR version number.').'</p>';
1105              return false;
1106          }
1107      }
1108  
1109      return true;
1110  }
1111  
1112  
1113  /**

1114   * Get the unique url name

1115   *

1116   * @param string Source text

1117   * @param string Table name

1118   * @param string Field name

1119   * @return string category's url name

1120   */
1121  function wp_unique_urlname( $source, $table, $field )
1122  {
1123      global $DB;
1124  
1125      // Replace special chars/umlauts, if we can convert charsets:

1126      load_funcs( 'locales/_charset.funcs.php' );
1127      $url_name = strtolower( replace_special_chars( $source ) );
1128  
1129      $url_number = 1;
1130      $url_name_correct = $url_name;
1131      do
1132      {    // Check for unique url name in DB
1133          $SQL = new SQL();
1134          $SQL->SELECT( $field );
1135          $SQL->FROM( $table );
1136          $SQL->WHERE( $field.' = '.$DB->quote( $url_name_correct ) );
1137          $category = $DB->get_var( $SQL->get() );
1138          if( $category )
1139          {    // Category already exists with such url name; Change it
1140              $url_name_correct = $url_name.'-'.$url_number;
1141              $url_number++;
1142          }
1143      }
1144      while( !empty( $category ) );
1145  
1146      return $url_name_correct;
1147  }
1148  
1149  
1150  /**

1151   * Generate the excerpt for post

1152   *

1153   * @param string

1154   * @return string

1155   */
1156  function wp_generate_excerpt( $content )
1157  {
1158      $content = trim( strip_tags( $content ) );
1159      return strmaxlen( $content, 254, '&hellip;' );
1160  }
1161  
1162  
1163  /**

1164   * Get regional data (Used to get regional IDs for user & item by regional names)

1165   *

1166   * @param string Country code

1167   * @param string Region name

1168   * @param string Subregion name

1169   * @param string City name

1170   * @return array Regional data

1171   */
1172  function wp_get_regional_data( $country_code, $region, $subregion, $city )
1173  {
1174      $data = array(
1175              'country' => 0,
1176              'region' => 0,
1177              'subregion' => 0,
1178              'city' => 0,
1179          );
1180  
1181      if( !empty( $country_code ) )
1182      {    // Get country ID from DB by code
1183          $CountryCache = & get_CountryCache();
1184          if( $Country = & $CountryCache->get_by_name( $country_code, false ) )
1185          {
1186              $data['country'] = $Country->ID;
1187  
1188              if( !empty( $region ) )
1189              {    // Get region ID from DB by name
1190                  $RegionCache = & get_RegionCache();
1191                  if( $Region = & $RegionCache->get_by_name( $region, false ) )
1192                  {
1193                      if( $Region->ctry_ID == $data['country'] )
1194                      {
1195                          $data['region'] = $Region->ID;
1196  
1197                          if( !empty( $subregion ) )
1198                          {    // Get subregion ID from DB by name
1199                              $SubregionCache = & get_SubregionCache();
1200                              if( $Subregion = & $SubregionCache->get_by_name( $subregion, false ) )
1201                              {
1202                                  if( $Subregion->rgn_ID == $data['region'] )
1203                                  {
1204                                      $data['subregion'] = $Subregion->ID;
1205                                  }
1206                              }
1207                          }
1208  
1209                          if( !empty( $city ) )
1210                          {    // Get city ID from DB by name
1211                              $CityCache = & get_CityCache();
1212                              if( $City = & $CityCache->get_by_name( $city, false ) )
1213                              {
1214                                  if( $City->rgn_ID == $data['region'] )
1215                                  {
1216                                      $data['city'] = $City->ID;
1217                                  }
1218                              }
1219                          }
1220                      }
1221                  }
1222              }
1223          }
1224      }
1225  
1226      return $data;
1227  }
1228  
1229  
1230  /**

1231   * Get available files to import from the folder /media/import/

1232   * 

1233   * @return array Files

1234   */
1235  function wpxml_get_import_files()
1236  {
1237      global $media_path;
1238  
1239      // Get all files from the import folder

1240      $files = get_filenames( $media_path.'import/', array(
1241              'flat' => false
1242          ) );
1243  
1244      $import_files = array();
1245      foreach( $files as $file )
1246      {
1247          $file_paths = array();
1248          $file_type = '';
1249          if( is_array( $file ) )
1250          { // It is a folder, Find xml file inside
1251              foreach( $file as $key => $sub_file )
1252              {
1253                  if( $key == 'b2evolution_export_files' && is_array( $sub_file ) )
1254                  { // Probably it is folder with the attached files
1255                      $file_type = T_('attached files');
1256                  }
1257                  elseif( is_string( $sub_file ) && preg_match( '/\.(xml|txt)$/i', $sub_file ) )
1258                  { // Probably it is a file with import data
1259                      $file_paths[] = $sub_file;
1260                  }
1261              }
1262          }
1263          elseif( is_string( $file ) )
1264          { // File in the root, Single XML file
1265              $file_paths[] = $file;
1266          }
1267  
1268          foreach( $file_paths as $file_path )
1269          {
1270              if( ! empty( $file_path ) && preg_match( '/\.(xml|txt|zip)$/i', $file_path, $file_matches ) )
1271              { // This file can be a file with import data
1272                  if( empty( $file_type ) )
1273                  { // Set type from file extension
1274                      $file_type = $file_matches[1] == 'zip' ? T_('archive') : T_('single');
1275                  }
1276                  $import_files[] = array(
1277                          'path' => $file_path,
1278                          'type' => $file_type,
1279                      );
1280              }
1281          }
1282      }
1283  
1284      return $import_files;
1285  }
1286  
1287  ?>

title

Description

title

Description

title

Description

title

title

Body