wp/wp-admin/includes/class-wp-importer.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    23 		$hashtable = array();
    23 		$hashtable = array();
    24 
    24 
    25 		$limit  = 100;
    25 		$limit  = 100;
    26 		$offset = 0;
    26 		$offset = 0;
    27 
    27 
    28 		// Grab all posts in chunks
    28 		// Grab all posts in chunks.
    29 		do {
    29 		do {
    30 			$meta_key = $importer_name . '_' . $bid . '_permalink';
    30 			$meta_key = $importer_name . '_' . $bid . '_permalink';
    31 			$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 );
    32 			$results  = $wpdb->get_results( $sql );
    32 			$results  = $wpdb->get_results( $sql );
    33 
    33 
    34 			// Increment offset
    34 			// Increment offset.
    35 			$offset = ( $limit + $offset );
    35 			$offset = ( $limit + $offset );
    36 
    36 
    37 			if ( ! empty( $results ) ) {
    37 			if ( ! empty( $results ) ) {
    38 				foreach ( $results as $r ) {
    38 				foreach ( $results as $r ) {
    39 					// Set permalinks into array
    39 					// Set permalinks into array.
    40 					$hashtable[ $r->meta_value ] = intval( $r->post_id );
    40 					$hashtable[ $r->meta_value ] = intval( $r->post_id );
    41 				}
    41 				}
    42 			}
    42 			}
    43 		} while ( count( $results ) == $limit );
    43 		} while ( count( $results ) == $limit );
    44 
    44 
    60 	public function count_imported_posts( $importer_name, $bid ) {
    60 	public function count_imported_posts( $importer_name, $bid ) {
    61 		global $wpdb;
    61 		global $wpdb;
    62 
    62 
    63 		$count = 0;
    63 		$count = 0;
    64 
    64 
    65 		// Get count of permalinks
    65 		// Get count of permalinks.
    66 		$meta_key = $importer_name . '_' . $bid . '_permalink';
    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 );
    67 		$sql      = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key );
    68 
    68 
    69 		$result = $wpdb->get_results( $sql );
    69 		$result = $wpdb->get_results( $sql );
    70 
    70 
    71 		if ( ! empty( $result ) ) {
    71 		if ( ! empty( $result ) ) {
    72 			$count = intval( $result[0]->cnt );
    72 			$count = intval( $result[0]->cnt );
    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 			}
   128 	 */
   128 	 */
   129 	public function set_blog( $blog_id ) {
   129 	public function set_blog( $blog_id ) {
   130 		if ( is_numeric( $blog_id ) ) {
   130 		if ( is_numeric( $blog_id ) ) {
   131 			$blog_id = (int) $blog_id;
   131 			$blog_id = (int) $blog_id;
   132 		} else {
   132 		} else {
   133 			$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
   133 			$blog   = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
   134 			if ( ( ! $parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
   134 			$parsed = parse_url( $blog );
       
   135 			if ( ! $parsed || empty( $parsed['host'] ) ) {
   135 				fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
   136 				fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
   136 				exit();
   137 				exit;
   137 			}
   138 			}
   138 			if ( empty( $parsed['path'] ) ) {
   139 			if ( empty( $parsed['path'] ) ) {
   139 				$parsed['path'] = '/';
   140 				$parsed['path'] = '/';
   140 			}
   141 			}
   141 			$blogs = get_sites(
   142 			$blogs = get_sites(
   145 					'path'   => $parsed['path'],
   146 					'path'   => $parsed['path'],
   146 				)
   147 				)
   147 			);
   148 			);
   148 			if ( ! $blogs ) {
   149 			if ( ! $blogs ) {
   149 				fwrite( STDERR, "Error: Could not find blog\n" );
   150 				fwrite( STDERR, "Error: Could not find blog\n" );
   150 				exit();
   151 				exit;
   151 			}
   152 			}
   152 			$blog    = array_shift( $blogs );
   153 			$blog    = array_shift( $blogs );
   153 			$blog_id = (int) $blog->blog_id;
   154 			$blog_id = (int) $blog->blog_id;
   154 		}
   155 		}
   155 
   156 
   173 			$user_id = (int) username_exists( $user_id );
   174 			$user_id = (int) username_exists( $user_id );
   174 		}
   175 		}
   175 
   176 
   176 		if ( ! $user_id || ! wp_set_current_user( $user_id ) ) {
   177 		if ( ! $user_id || ! wp_set_current_user( $user_id ) ) {
   177 			fwrite( STDERR, "Error: can not find user\n" );
   178 			fwrite( STDERR, "Error: can not find user\n" );
   178 			exit();
   179 			exit;
   179 		}
   180 		}
   180 
   181 
   181 		return $user_id;
   182 		return $user_id;
   182 	}
   183 	}
   183 
   184 
   200 	 * @param string $password
   201 	 * @param string $password
   201 	 * @param bool   $head
   202 	 * @param bool   $head
   202 	 * @return array
   203 	 * @return array
   203 	 */
   204 	 */
   204 	public function get_page( $url, $username = '', $password = '', $head = false ) {
   205 	public function get_page( $url, $username = '', $password = '', $head = false ) {
   205 		// Increase the timeout
   206 		// Increase the timeout.
   206 		add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
   207 		add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
   207 
   208 
   208 		$headers = array();
   209 		$headers = array();
   209 		$args    = array();
   210 		$args    = array();
   210 		if ( true === $head ) {
   211 		if ( true === $head ) {
   264 	 */
   265 	 */
   265 	public function stop_the_insanity() {
   266 	public function stop_the_insanity() {
   266 		global $wpdb, $wp_actions;
   267 		global $wpdb, $wp_actions;
   267 		// Or define( 'WP_IMPORTING', true );
   268 		// Or define( 'WP_IMPORTING', true );
   268 		$wpdb->queries = array();
   269 		$wpdb->queries = array();
   269 		// Reset $wp_actions to keep it from growing out of control
   270 		// Reset $wp_actions to keep it from growing out of control.
   270 		$wp_actions = array();
   271 		$wp_actions = array();
   271 	}
   272 	}
   272 }
   273 }
   273 
   274 
   274 /**
   275 /**
   304 			}
   305 			}
   305 
   306 
   306 			$last_arg = $key;
   307 			$last_arg = $key;
   307 		} elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) {
   308 		} elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) {
   308 			for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
   309 			for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
   309 				$key         = $match[1]{$j};
   310 				$key         = $match[1][ $j ];
   310 				$out[ $key ] = true;
   311 				$out[ $key ] = true;
   311 			}
   312 			}
   312 
   313 
   313 			$last_arg = $key;
   314 			$last_arg = $key;
   314 		} elseif ( $last_arg !== null ) {
   315 		} elseif ( null !== $last_arg ) {
   315 			$out[ $last_arg ] = $args[ $i ];
   316 			$out[ $last_arg ] = $args[ $i ];
   316 		}
   317 		}
   317 	}
   318 	}
   318 
   319 
   319 	// Check array for specified param
   320 	// Check array for specified param.
   320 	if ( isset( $out[ $param ] ) ) {
   321 	if ( isset( $out[ $param ] ) ) {
   321 		// Set return value
   322 		// Set return value.
   322 		$return = $out[ $param ];
   323 		$return = $out[ $param ];
   323 	}
   324 	}
   324 
   325 
   325 	// Check for missing required param
   326 	// Check for missing required param.
   326 	if ( ! isset( $out[ $param ] ) && $required ) {
   327 	if ( ! isset( $out[ $param ] ) && $required ) {
   327 		// Display message and exit
   328 		// Display message and exit.
   328 		echo "\"$param\" parameter is required but was not specified\n";
   329 		echo "\"$param\" parameter is required but was not specified\n";
   329 		exit();
   330 		exit;
   330 	}
   331 	}
   331 
   332 
   332 	return $return;
   333 	return $return;
   333 }
   334 }