[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress 3.0.1

Provided by Yoast

title

Body

[close]

/wp-admin/includes/ -> class-wp-importer.php (source)

   1  <?php
   2  /**
   3   * WP_Importer base class
   4   */
   5  class WP_Importer {
   6      /**
   7       * Class Constructor
   8       *
   9       * @return void
  10       */
  11  	function __construct() {}
  12  
  13  	function WP_Importer() {
  14          $this->__construct();
  15      }
  16  
  17      /**
  18       * Returns array with imported permalinks from WordPress database
  19       *
  20       * @param string $bid
  21       * @return array
  22       */
  23  	function get_imported_posts( $importer_name, $bid ) {
  24          global $wpdb;
  25  
  26          $hashtable = array();
  27  
  28          $limit = 100;
  29          $offset = 0;
  30  
  31          // Grab all posts in chunks
  32          do {
  33              $meta_key = $importer_name . '_' . $bid . '_permalink';
  34              $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '%s' LIMIT %d,%d", $meta_key, $offset, $limit );
  35              $results = $wpdb->get_results( $sql );
  36  
  37              // Increment offset
  38              $offset = ( $limit + $offset );
  39  
  40              if ( !empty( $results ) ) {
  41                  foreach ( $results as $r ) {
  42                      // Set permalinks into array
  43                      $hashtable[$r->meta_value] = intval( $r->post_id );
  44                  }
  45              }
  46          } while ( count( $results ) == $limit );
  47  
  48          // unset to save memory
  49          unset( $results, $r );
  50  
  51          return $hashtable;
  52      }
  53  
  54      /**
  55       * Return count of imported permalinks from WordPress database
  56       *
  57       * @param string $bid
  58       * @return int
  59       */
  60  	function count_imported_posts( $importer_name, $bid ) {
  61          global $wpdb;
  62  
  63          $count = 0;
  64  
  65          // Get count of permalinks
  66          $meta_key = $importer_name . '_' . $bid . '_permalink';
  67          $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key );
  68  
  69          $result = $wpdb->get_results( $sql );
  70  
  71          if ( !empty( $result ) )
  72              $count = intval( $result[0]->cnt );
  73  
  74          // unset to save memory
  75          unset( $results );
  76  
  77          return $count;
  78      }
  79  
  80      /**
  81       * Set array with imported comments from WordPress database
  82       *
  83       * @param string $bid
  84       * @return array
  85       */
  86  	function get_imported_comments( $bid ) {
  87          global $wpdb;
  88  
  89          $hashtable = array();
  90  
  91          $limit = 100;
  92          $offset = 0;
  93  
  94          // Grab all comments in chunks
  95          do {
  96              $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
  97              $results = $wpdb->get_results( $sql );
  98  
  99              // Increment offset
 100              $offset = ( $limit + $offset );
 101  
 102              if ( !empty( $results ) ) {
 103                  foreach ( $results as $r ) {
 104                      // Explode comment_agent key
 105                      list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent );
 106                      $source_comment_id = intval( $source_comment_id );
 107  
 108                      // Check if this comment came from this blog
 109                      if ( $bid == $ca_bid ) {
 110                          $hashtable[$source_comment_id] = intval( $r->comment_ID );
 111                      }
 112                  }
 113              }
 114          } while ( count( $results ) == $limit );
 115  
 116          // unset to save memory
 117          unset( $results, $r );
 118  
 119          return $hashtable;
 120      }
 121  
 122  	function set_blog( $blog_id ) {
 123          if ( is_numeric( $blog_id ) ) {
 124              $blog_id = (int) $blog_id;
 125          } else {
 126              $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
 127              if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
 128                  fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
 129                  exit();
 130              }
 131              if ( empty( $parsed['path'] ) )
 132                  $parsed['path'] = '/';
 133              $blog = get_blog_details( array( 'domain' => $parsed['host'], 'path' => $parsed['path'] ) );
 134              if ( !$blog ) {
 135                  fwrite( STDERR, "Error: Could not find blog\n" );
 136                  exit();
 137              }
 138              $blog_id = (int) $blog->blog_id;
 139              // Restore global $current_blog
 140              global $current_blog;
 141              $current_blog = $blog;
 142          }
 143  
 144          if ( function_exists( 'is_multisite' ) ) {
 145              if ( is_multisite() )
 146                  switch_to_blog( $blog_id );
 147          }
 148  
 149          return $blog_id;
 150      }
 151  
 152  	function set_user( $user_id ) {
 153          if ( is_numeric( $user_id ) ) {
 154              $user_id = (int) $user_id;
 155          } else {
 156              $user_id = (int) username_exists( $user_id );
 157          }
 158  
 159          if ( !$user_id || !wp_set_current_user( $user_id ) ) {
 160              fwrite( STDERR, "Error: can not find user\n" );
 161              exit();
 162          }
 163  
 164          return $user_id;
 165      }
 166  
 167      /**
 168       * Sort by strlen, longest string first
 169       *
 170       * @param string $a
 171       * @param string $b
 172       * @return int
 173       */
 174  	function cmpr_strlen( $a, $b ) {
 175          return strlen( $b ) - strlen( $a );
 176      }
 177  
 178      /**
 179       * GET URL
 180       *
 181       * @param string $url
 182       * @param string $username
 183       * @param string $password
 184       * @param bool $head
 185       * @return array
 186       */
 187  	function get_page( $url, $username = '', $password = '', $head = false ) {
 188          // Increase the timeout
 189          add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
 190  
 191          $headers = array();
 192          $args = array();
 193          if ( true === $head )
 194              $args['method'] = 'HEAD';
 195          if ( !empty( $username ) && !empty( $password ) )
 196              $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
 197  
 198          $args['headers'] = $headers;
 199  
 200          return wp_remote_request( $url, $args );
 201      }
 202  
 203      /**
 204       * Bump up the request timeout for http requests
 205       *
 206       * @param int $val
 207       * @return int
 208       */
 209  	function bump_request_timeout( $val ) {
 210          return 60;
 211      }
 212  
 213      /**
 214       * Check if user has exceeded disk quota
 215       *
 216       * @return bool
 217       */
 218  	function is_user_over_quota() {
 219          global $current_blog;
 220  
 221          if ( function_exists( 'upload_is_user_over_quota' ) ) {
 222              if ( upload_is_user_over_quota( 1 ) ) {
 223                  echo "Sorry, you have used your upload quota.\n";
 224                  return true;
 225              }
 226          }
 227  
 228          return false;
 229      }
 230  
 231      /**
 232       * Replace newlines, tabs, and multiple spaces with a single space
 233       *
 234       * @param string $string
 235       * @return string
 236       */
 237  	function min_whitespace( $string ) {
 238          return preg_replace( '|[\r\n\t ]+|', ' ', $string );
 239      }
 240  
 241      /**
 242       * Reset global variables that grow out of control during imports
 243       *
 244       * @return void
 245       */
 246  	function stop_the_insanity() {
 247          global $wpdb, $wp_actions;
 248          // Or define( 'WP_IMPORTING', true );
 249          $wpdb->queries = array();
 250          // Reset $wp_actions to keep it from growing out of control
 251          $wp_actions = array();
 252      }
 253  }
 254  
 255  /**
 256   * Returns value of command line params.
 257   * Exits when a required param is not set.
 258   *
 259   * @param string $param
 260   * @param bool $required
 261   * @return mixed
 262   */
 263  function get_cli_args( $param, $required = false ) {
 264      $args = $_SERVER['argv'];
 265  
 266      $out = array();
 267  
 268      $last_arg = null;
 269      $return = null;
 270  
 271      $il = sizeof( $args );
 272  
 273      for ( $i = 1, $il; $i < $il; $i++ ) {
 274          if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
 275              $parts = explode( "=", $match[1] );
 276              $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
 277  
 278              if ( isset( $parts[1] ) ) {
 279                  $out[$key] = $parts[1];
 280              } else {
 281                  $out[$key] = true;
 282              }
 283  
 284              $last_arg = $key;
 285          } else if ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
 286              for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
 287                  $key = $match[1]{$j};
 288                  $out[$key] = true;
 289              }
 290  
 291              $last_arg = $key;
 292          } else if ( $last_arg !== null ) {
 293              $out[$last_arg] = $args[$i];
 294          }
 295      }
 296  
 297      // Check array for specified param
 298      if ( isset( $out[$param] ) ) {
 299          // Set return value
 300          $return = $out[$param];
 301      }
 302  
 303      // Check for missing required param
 304      if ( !isset( $out[$param] ) && $required ) {
 305          // Display message and exit
 306          echo "\"$param\" parameter is required but was not specified\n";
 307          exit();
 308      }
 309  
 310      return $return;
 311  }


Generated: Thu Oct 14 05:12:05 2010 Cross-referenced by PHPXref 0.7