[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress 3.0

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              if ( !$blog = get_blog_info( $parsed['host'], $parsed['path'] ) ) {
 134                  fwrite( STDERR, "Error: Could not find blog\n" );
 135                  exit();
 136              }
 137              $blog_id = (int) $blog->blog_id;
 138              // Restore global $current_blog
 139              global $current_blog;
 140              $current_blog = $blog;
 141          }
 142  
 143          if ( function_exists( 'is_multisite' ) ) {
 144              if ( is_multisite() )
 145                  switch_to_blog( $blog_id );
 146          }
 147  
 148          return $blog_id;
 149      }
 150  
 151  	function set_user( $user_id ) {
 152          if ( is_numeric( $user_id ) ) {
 153              $user_id = (int) $user_id;
 154          } else {
 155              $user_id = (int) username_exists( $user_id );
 156          }
 157  
 158          if ( !$user_id || !wp_set_current_user( $user_id ) ) {
 159              fwrite( STDERR, "Error: can not find user\n" );
 160              exit();
 161          }
 162  
 163          return $user_id;
 164      }
 165  
 166      /**
 167       * Sort by strlen, longest string first
 168       *
 169       * @param string $a
 170       * @param string $b
 171       * @return int
 172       */
 173  	function cmpr_strlen( $a, $b ) {
 174          return strlen( $b ) - strlen( $a );
 175      }
 176  
 177      /**
 178       * GET URL
 179       *
 180       * @param string $url
 181       * @param string $username
 182       * @param string $password
 183       * @param bool $head
 184       * @return array
 185       */
 186  	function get_page( $url, $username = '', $password = '', $head = false ) {
 187          // Increase the timeout
 188          add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
 189  
 190          $headers = array();
 191          $args = array();
 192          if ( true === $head )
 193              $args['method'] = 'HEAD';
 194          if ( !empty( $username ) && !empty( $password ) )
 195              $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
 196  
 197          $args['headers'] = $headers;
 198  
 199          return wp_remote_request( $url, $args );
 200      }
 201  
 202      /**
 203       * Bump up the request timeout for http requests
 204       *
 205       * @param int $val
 206       * @return int
 207       */
 208  	function bump_request_timeout( $val ) {
 209          return 60;
 210      }
 211  
 212      /**
 213       * Check if user has exceeded disk quota
 214       *
 215       * @return bool
 216       */
 217  	function is_user_over_quota() {
 218          global $current_user, $current_blog;
 219  
 220          if ( function_exists( 'upload_is_user_over_quota' ) ) {
 221              if ( upload_is_user_over_quota( 1 ) ) {
 222                  echo "Sorry, you have used your upload quota.\n";
 223                  return true;
 224              }
 225          }
 226  
 227          return false;
 228      }
 229  
 230      /**
 231       * Replace newlines, tabs, and multiple spaces with a single space
 232       *
 233       * @param string $string
 234       * @return string
 235       */
 236  	function min_whitespace( $string ) {
 237          return preg_replace( '|[\r\n\t ]+|', ' ', $string );
 238      }
 239  
 240      /**
 241       * Reset global variables that grow out of control during imports
 242       *
 243       * @return void
 244       */
 245  	function stop_the_insanity() {
 246          global $wpdb, $wp_actions;
 247          // Or define( 'WP_IMPORTING', true );
 248          $wpdb->queries = array();
 249          // Reset $wp_actions to keep it from growing out of control
 250          $wp_actions = array();
 251      }
 252  }
 253  
 254  /**
 255   * Returns value of command line params.
 256   * Exits when a required param is not set.
 257   *
 258   * @param string $param
 259   * @param bool $required
 260   * @return mixed
 261   */
 262  function get_cli_args( $param, $required = false ) {
 263      $args = $_SERVER['argv'];
 264  
 265      $out = array();
 266  
 267      $last_arg = null;
 268      $return = null;
 269  
 270      $il = sizeof( $args );
 271  
 272      for ( $i = 1, $il; $i < $il; $i++ ) {
 273          if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
 274              $parts = explode( "=", $match[1] );
 275              $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
 276  
 277              if ( isset( $parts[1] ) ) {
 278                  $out[$key] = $parts[1];
 279              } else {
 280                  $out[$key] = true;
 281              }
 282  
 283              $last_arg = $key;
 284          } else if ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
 285              for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
 286                  $key = $match[1]{$j};
 287                  $out[$key] = true;
 288              }
 289  
 290              $last_arg = $key;
 291          } else if ( $last_arg !== null ) {
 292              $out[$last_arg] = $args[$i];
 293          }
 294      }
 295  
 296      // Check array for specified param
 297      if ( isset( $out[$param] ) ) {
 298          // Set return value
 299          $return = $out[$param];
 300      }
 301  
 302      // Check for missing required param
 303      if ( !isset( $out[$param] ) && $required ) {
 304          // Display message and exit
 305          echo "\"$param\" parameter is required but was not specified\n";
 306          exit();
 307      }
 308  
 309      return $return;
 310  }


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