wp/wp-admin/includes/class-wp-importer.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     3  * WP_Importer base class
     3  * WP_Importer base class
     4  */
     4  */
     5 class WP_Importer {
     5 class WP_Importer {
     6 	/**
     6 	/**
     7 	 * Class Constructor
     7 	 * Class Constructor
     8 	 *
       
     9 	 */
     8 	 */
    10 	public function __construct() {}
     9 	public function __construct() {}
    11 
    10 
    12 	/**
    11 	/**
    13 	 * Returns array with imported permalinks from WordPress database
    12 	 * Returns array with imported permalinks from WordPress database
    21 	public function get_imported_posts( $importer_name, $bid ) {
    20 	public function get_imported_posts( $importer_name, $bid ) {
    22 		global $wpdb;
    21 		global $wpdb;
    23 
    22 
    24 		$hashtable = array();
    23 		$hashtable = array();
    25 
    24 
    26 		$limit = 100;
    25 		$limit  = 100;
    27 		$offset = 0;
    26 		$offset = 0;
    28 
    27 
    29 		// Grab all posts in chunks
    28 		// Grab all posts in chunks
    30 		do {
    29 		do {
    31 			$meta_key = $importer_name . '_' . $bid . '_permalink';
    30 			$meta_key = $importer_name . '_' . $bid . '_permalink';
    32 			$sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit );
    31 			$sql      = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit );
    33 			$results = $wpdb->get_results( $sql );
    32 			$results  = $wpdb->get_results( $sql );
    34 
    33 
    35 			// Increment offset
    34 			// Increment offset
    36 			$offset = ( $limit + $offset );
    35 			$offset = ( $limit + $offset );
    37 
    36 
    38 			if ( !empty( $results ) ) {
    37 			if ( ! empty( $results ) ) {
    39 				foreach ( $results as $r ) {
    38 				foreach ( $results as $r ) {
    40 					// Set permalinks into array
    39 					// Set permalinks into array
    41 					$hashtable[$r->meta_value] = intval( $r->post_id );
    40 					$hashtable[ $r->meta_value ] = intval( $r->post_id );
    42 				}
    41 				}
    43 			}
    42 			}
    44 		} while ( count( $results ) == $limit );
    43 		} while ( count( $results ) == $limit );
    45 
    44 
    46 		// Unset to save memory.
    45 		// Unset to save memory.
    63 
    62 
    64 		$count = 0;
    63 		$count = 0;
    65 
    64 
    66 		// Get count of permalinks
    65 		// Get count of permalinks
    67 		$meta_key = $importer_name . '_' . $bid . '_permalink';
    66 		$meta_key = $importer_name . '_' . $bid . '_permalink';
    68 		$sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key );
    67 		$sql      = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key );
    69 
    68 
    70 		$result = $wpdb->get_results( $sql );
    69 		$result = $wpdb->get_results( $sql );
    71 
    70 
    72 		if ( !empty( $result ) )
    71 		if ( ! empty( $result ) ) {
    73 			$count = intval( $result[0]->cnt );
    72 			$count = intval( $result[0]->cnt );
       
    73 		}
    74 
    74 
    75 		// Unset to save memory.
    75 		// Unset to save memory.
    76 		unset( $results );
    76 		unset( $results );
    77 
    77 
    78 		return $count;
    78 		return $count;
    89 	public function get_imported_comments( $bid ) {
    89 	public function get_imported_comments( $bid ) {
    90 		global $wpdb;
    90 		global $wpdb;
    91 
    91 
    92 		$hashtable = array();
    92 		$hashtable = array();
    93 
    93 
    94 		$limit = 100;
    94 		$limit  = 100;
    95 		$offset = 0;
    95 		$offset = 0;
    96 
    96 
    97 		// Grab all comments in chunks
    97 		// Grab all comments in chunks
    98 		do {
    98 		do {
    99 			$sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
    99 			$sql     = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
   100 			$results = $wpdb->get_results( $sql );
   100 			$results = $wpdb->get_results( $sql );
   101 
   101 
   102 			// Increment offset
   102 			// Increment offset
   103 			$offset = ( $limit + $offset );
   103 			$offset = ( $limit + $offset );
   104 
   104 
   105 			if ( !empty( $results ) ) {
   105 			if ( ! empty( $results ) ) {
   106 				foreach ( $results as $r ) {
   106 				foreach ( $results as $r ) {
   107 					// Explode comment_agent key
   107 					// Explode comment_agent key
   108 					list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent );
   108 					list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent );
   109 					$source_comment_id = intval( $source_comment_id );
   109 					$source_comment_id                   = intval( $source_comment_id );
   110 
   110 
   111 					// Check if this comment came from this blog
   111 					// Check if this comment came from this blog
   112 					if ( $bid == $ca_bid ) {
   112 					if ( $bid == $ca_bid ) {
   113 						$hashtable[$source_comment_id] = intval( $r->comment_ID );
   113 						$hashtable[ $source_comment_id ] = intval( $r->comment_ID );
   114 					}
   114 					}
   115 				}
   115 				}
   116 			}
   116 			}
   117 		} while ( count( $results ) == $limit );
   117 		} while ( count( $results ) == $limit );
   118 
   118 
   121 
   121 
   122 		return $hashtable;
   122 		return $hashtable;
   123 	}
   123 	}
   124 
   124 
   125 	/**
   125 	/**
   126 	 *
       
   127 	 * @param int $blog_id
   126 	 * @param int $blog_id
   128 	 * @return int|void
   127 	 * @return int|void
   129 	 */
   128 	 */
   130 	public function set_blog( $blog_id ) {
   129 	public function set_blog( $blog_id ) {
   131 		if ( is_numeric( $blog_id ) ) {
   130 		if ( is_numeric( $blog_id ) ) {
   132 			$blog_id = (int) $blog_id;
   131 			$blog_id = (int) $blog_id;
   133 		} else {
   132 		} else {
   134 			$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
   133 			$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
   135 			if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
   134 			if ( ( ! $parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
   136 				fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
   135 				fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
   137 				exit();
   136 				exit();
   138 			}
   137 			}
   139 			if ( empty( $parsed['path'] ) ) {
   138 			if ( empty( $parsed['path'] ) ) {
   140 				$parsed['path'] = '/';
   139 				$parsed['path'] = '/';
   141 			}
   140 			}
   142 			$blogs = get_sites( array( 'domain' => $parsed['host'], 'number' => 1, 'path' => $parsed['path'] ) );
   141 			$blogs = get_sites(
       
   142 				array(
       
   143 					'domain' => $parsed['host'],
       
   144 					'number' => 1,
       
   145 					'path'   => $parsed['path'],
       
   146 				)
       
   147 			);
   143 			if ( ! $blogs ) {
   148 			if ( ! $blogs ) {
   144 				fwrite( STDERR, "Error: Could not find blog\n" );
   149 				fwrite( STDERR, "Error: Could not find blog\n" );
   145 				exit();
   150 				exit();
   146 			}
   151 			}
   147 			$blog = array_shift( $blogs );
   152 			$blog    = array_shift( $blogs );
   148 			$blog_id = (int) $blog->blog_id;
   153 			$blog_id = (int) $blog->blog_id;
   149 		}
   154 		}
   150 
   155 
   151 		if ( function_exists( 'is_multisite' ) ) {
   156 		if ( function_exists( 'is_multisite' ) ) {
   152 			if ( is_multisite() )
   157 			if ( is_multisite() ) {
   153 				switch_to_blog( $blog_id );
   158 				switch_to_blog( $blog_id );
       
   159 			}
   154 		}
   160 		}
   155 
   161 
   156 		return $blog_id;
   162 		return $blog_id;
   157 	}
   163 	}
   158 
   164 
   159 	/**
   165 	/**
   160 	 *
       
   161 	 * @param int $user_id
   166 	 * @param int $user_id
   162 	 * @return int|void
   167 	 * @return int|void
   163 	 */
   168 	 */
   164 	public function set_user( $user_id ) {
   169 	public function set_user( $user_id ) {
   165 		if ( is_numeric( $user_id ) ) {
   170 		if ( is_numeric( $user_id ) ) {
   166 			$user_id = (int) $user_id;
   171 			$user_id = (int) $user_id;
   167 		} else {
   172 		} else {
   168 			$user_id = (int) username_exists( $user_id );
   173 			$user_id = (int) username_exists( $user_id );
   169 		}
   174 		}
   170 
   175 
   171 		if ( !$user_id || !wp_set_current_user( $user_id ) ) {
   176 		if ( ! $user_id || ! wp_set_current_user( $user_id ) ) {
   172 			fwrite( STDERR, "Error: can not find user\n" );
   177 			fwrite( STDERR, "Error: can not find user\n" );
   173 			exit();
   178 			exit();
   174 		}
   179 		}
   175 
   180 
   176 		return $user_id;
   181 		return $user_id;
   199 	public function get_page( $url, $username = '', $password = '', $head = false ) {
   204 	public function get_page( $url, $username = '', $password = '', $head = false ) {
   200 		// Increase the timeout
   205 		// Increase the timeout
   201 		add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
   206 		add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
   202 
   207 
   203 		$headers = array();
   208 		$headers = array();
   204 		$args = array();
   209 		$args    = array();
   205 		if ( true === $head )
   210 		if ( true === $head ) {
   206 			$args['method'] = 'HEAD';
   211 			$args['method'] = 'HEAD';
   207 		if ( !empty( $username ) && !empty( $password ) )
   212 		}
       
   213 		if ( ! empty( $username ) && ! empty( $password ) ) {
   208 			$headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
   214 			$headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
       
   215 		}
   209 
   216 
   210 		$args['headers'] = $headers;
   217 		$args['headers'] = $headers;
   211 
   218 
   212 		return wp_safe_remote_request( $url, $args );
   219 		return wp_safe_remote_request( $url, $args );
   213 	}
   220 	}
   272  * @param bool   $required
   279  * @param bool   $required
   273  * @return mixed
   280  * @return mixed
   274  */
   281  */
   275 function get_cli_args( $param, $required = false ) {
   282 function get_cli_args( $param, $required = false ) {
   276 	$args = $_SERVER['argv'];
   283 	$args = $_SERVER['argv'];
       
   284 	if ( ! is_array( $args ) ) {
       
   285 		$args = array();
       
   286 	}
   277 
   287 
   278 	$out = array();
   288 	$out = array();
   279 
   289 
   280 	$last_arg = null;
   290 	$last_arg = null;
   281 	$return = null;
   291 	$return   = null;
   282 
   292 
   283 	$il = sizeof( $args );
   293 	$il = sizeof( $args );
   284 
   294 
   285 	for ( $i = 1, $il; $i < $il; $i++ ) {
   295 	for ( $i = 1, $il; $i < $il; $i++ ) {
   286 		if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
   296 		if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) {
   287 			$parts = explode( "=", $match[1] );
   297 			$parts = explode( '=', $match[1] );
   288 			$key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
   298 			$key   = preg_replace( '/[^a-z0-9]+/', '', $parts[0] );
   289 
   299 
   290 			if ( isset( $parts[1] ) ) {
   300 			if ( isset( $parts[1] ) ) {
   291 				$out[$key] = $parts[1];
   301 				$out[ $key ] = $parts[1];
   292 			} else {
   302 			} else {
   293 				$out[$key] = true;
   303 				$out[ $key ] = true;
   294 			}
   304 			}
   295 
   305 
   296 			$last_arg = $key;
   306 			$last_arg = $key;
   297 		} elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
   307 		} elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) {
   298 			for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
   308 			for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
   299 				$key = $match[1]{$j};
   309 				$key         = $match[1]{$j};
   300 				$out[$key] = true;
   310 				$out[ $key ] = true;
   301 			}
   311 			}
   302 
   312 
   303 			$last_arg = $key;
   313 			$last_arg = $key;
   304 		} elseif ( $last_arg !== null ) {
   314 		} elseif ( $last_arg !== null ) {
   305 			$out[$last_arg] = $args[$i];
   315 			$out[ $last_arg ] = $args[ $i ];
   306 		}
   316 		}
   307 	}
   317 	}
   308 
   318 
   309 	// Check array for specified param
   319 	// Check array for specified param
   310 	if ( isset( $out[$param] ) ) {
   320 	if ( isset( $out[ $param ] ) ) {
   311 		// Set return value
   321 		// Set return value
   312 		$return = $out[$param];
   322 		$return = $out[ $param ];
   313 	}
   323 	}
   314 
   324 
   315 	// Check for missing required param
   325 	// Check for missing required param
   316 	if ( !isset( $out[$param] ) && $required ) {
   326 	if ( ! isset( $out[ $param ] ) && $required ) {
   317 		// Display message and exit
   327 		// Display message and exit
   318 		echo "\"$param\" parameter is required but was not specified\n";
   328 		echo "\"$param\" parameter is required but was not specified\n";
   319 		exit();
   329 		exit();
   320 	}
   330 	}
   321 
   331