wp/wp-admin/includes/class-wp-importer.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     4  */
     4  */
     5 class WP_Importer {
     5 class WP_Importer {
     6 	/**
     6 	/**
     7 	 * Class Constructor
     7 	 * Class Constructor
     8 	 *
     8 	 *
     9 	 * @return void
     9 	 */
    10 	 */
    10 	public function __construct() {}
    11 	function __construct() {}
       
    12 
    11 
    13 	/**
    12 	/**
    14 	 * Returns array with imported permalinks from WordPress database
    13 	 * Returns array with imported permalinks from WordPress database
    15 	 *
    14 	 *
    16 	 * @param string $bid
    15 	 * @param string $bid
    17 	 * @return array
    16 	 * @return array
    18 	 */
    17 	 */
    19 	function get_imported_posts( $importer_name, $bid ) {
    18 	public function get_imported_posts( $importer_name, $bid ) {
    20 		global $wpdb;
    19 		global $wpdb;
    21 
    20 
    22 		$hashtable = array();
    21 		$hashtable = array();
    23 
    22 
    24 		$limit = 100;
    23 		$limit = 100;
    39 					$hashtable[$r->meta_value] = intval( $r->post_id );
    38 					$hashtable[$r->meta_value] = intval( $r->post_id );
    40 				}
    39 				}
    41 			}
    40 			}
    42 		} while ( count( $results ) == $limit );
    41 		} while ( count( $results ) == $limit );
    43 
    42 
    44 		// unset to save memory
    43 		// Unset to save memory.
    45 		unset( $results, $r );
    44 		unset( $results, $r );
    46 
    45 
    47 		return $hashtable;
    46 		return $hashtable;
    48 	}
    47 	}
    49 
    48 
    51 	 * Return count of imported permalinks from WordPress database
    50 	 * Return count of imported permalinks from WordPress database
    52 	 *
    51 	 *
    53 	 * @param string $bid
    52 	 * @param string $bid
    54 	 * @return int
    53 	 * @return int
    55 	 */
    54 	 */
    56 	function count_imported_posts( $importer_name, $bid ) {
    55 	public function count_imported_posts( $importer_name, $bid ) {
    57 		global $wpdb;
    56 		global $wpdb;
    58 
    57 
    59 		$count = 0;
    58 		$count = 0;
    60 
    59 
    61 		// Get count of permalinks
    60 		// Get count of permalinks
    65 		$result = $wpdb->get_results( $sql );
    64 		$result = $wpdb->get_results( $sql );
    66 
    65 
    67 		if ( !empty( $result ) )
    66 		if ( !empty( $result ) )
    68 			$count = intval( $result[0]->cnt );
    67 			$count = intval( $result[0]->cnt );
    69 
    68 
    70 		// unset to save memory
    69 		// Unset to save memory.
    71 		unset( $results );
    70 		unset( $results );
    72 
    71 
    73 		return $count;
    72 		return $count;
    74 	}
    73 	}
    75 
    74 
    77 	 * Set array with imported comments from WordPress database
    76 	 * Set array with imported comments from WordPress database
    78 	 *
    77 	 *
    79 	 * @param string $bid
    78 	 * @param string $bid
    80 	 * @return array
    79 	 * @return array
    81 	 */
    80 	 */
    82 	function get_imported_comments( $bid ) {
    81 	public function get_imported_comments( $bid ) {
    83 		global $wpdb;
    82 		global $wpdb;
    84 
    83 
    85 		$hashtable = array();
    84 		$hashtable = array();
    86 
    85 
    87 		$limit = 100;
    86 		$limit = 100;
   107 					}
   106 					}
   108 				}
   107 				}
   109 			}
   108 			}
   110 		} while ( count( $results ) == $limit );
   109 		} while ( count( $results ) == $limit );
   111 
   110 
   112 		// unset to save memory
   111 		// Unset to save memory.
   113 		unset( $results, $r );
   112 		unset( $results, $r );
   114 
   113 
   115 		return $hashtable;
   114 		return $hashtable;
   116 	}
   115 	}
   117 
   116 
   118 	function set_blog( $blog_id ) {
   117 	public function set_blog( $blog_id ) {
   119 		if ( is_numeric( $blog_id ) ) {
   118 		if ( is_numeric( $blog_id ) ) {
   120 			$blog_id = (int) $blog_id;
   119 			$blog_id = (int) $blog_id;
   121 		} else {
   120 		} else {
   122 			$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
   121 			$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
   123 			if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
   122 			if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
   140 		}
   139 		}
   141 
   140 
   142 		return $blog_id;
   141 		return $blog_id;
   143 	}
   142 	}
   144 
   143 
   145 	function set_user( $user_id ) {
   144 	public function set_user( $user_id ) {
   146 		if ( is_numeric( $user_id ) ) {
   145 		if ( is_numeric( $user_id ) ) {
   147 			$user_id = (int) $user_id;
   146 			$user_id = (int) $user_id;
   148 		} else {
   147 		} else {
   149 			$user_id = (int) username_exists( $user_id );
   148 			$user_id = (int) username_exists( $user_id );
   150 		}
   149 		}
   162 	 *
   161 	 *
   163 	 * @param string $a
   162 	 * @param string $a
   164 	 * @param string $b
   163 	 * @param string $b
   165 	 * @return int
   164 	 * @return int
   166 	 */
   165 	 */
   167 	function cmpr_strlen( $a, $b ) {
   166 	public function cmpr_strlen( $a, $b ) {
   168 		return strlen( $b ) - strlen( $a );
   167 		return strlen( $b ) - strlen( $a );
   169 	}
   168 	}
   170 
   169 
   171 	/**
   170 	/**
   172 	 * GET URL
   171 	 * GET URL
   175 	 * @param string $username
   174 	 * @param string $username
   176 	 * @param string $password
   175 	 * @param string $password
   177 	 * @param bool $head
   176 	 * @param bool $head
   178 	 * @return array
   177 	 * @return array
   179 	 */
   178 	 */
   180 	function get_page( $url, $username = '', $password = '', $head = false ) {
   179 	public function get_page( $url, $username = '', $password = '', $head = false ) {
   181 		// Increase the timeout
   180 		// Increase the timeout
   182 		add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
   181 		add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
   183 
   182 
   184 		$headers = array();
   183 		$headers = array();
   185 		$args = array();
   184 		$args = array();
   197 	 * Bump up the request timeout for http requests
   196 	 * Bump up the request timeout for http requests
   198 	 *
   197 	 *
   199 	 * @param int $val
   198 	 * @param int $val
   200 	 * @return int
   199 	 * @return int
   201 	 */
   200 	 */
   202 	function bump_request_timeout( $val ) {
   201 	public function bump_request_timeout( $val ) {
   203 		return 60;
   202 		return 60;
   204 	}
   203 	}
   205 
   204 
   206 	/**
   205 	/**
   207 	 * Check if user has exceeded disk quota
   206 	 * Check if user has exceeded disk quota
   208 	 *
   207 	 *
   209 	 * @return bool
   208 	 * @return bool
   210 	 */
   209 	 */
   211 	function is_user_over_quota() {
   210 	public function is_user_over_quota() {
   212 		if ( function_exists( 'upload_is_user_over_quota' ) ) {
   211 		if ( function_exists( 'upload_is_user_over_quota' ) ) {
   213 			if ( upload_is_user_over_quota( 1 ) ) {
   212 			if ( upload_is_user_over_quota() ) {
   214 				echo "Sorry, you have used your upload quota.\n";
   213 				echo "Sorry, you have used your upload quota.\n";
   215 				return true;
   214 				return true;
   216 			}
   215 			}
   217 		}
   216 		}
   218 
   217 
   223 	 * Replace newlines, tabs, and multiple spaces with a single space
   222 	 * Replace newlines, tabs, and multiple spaces with a single space
   224 	 *
   223 	 *
   225 	 * @param string $string
   224 	 * @param string $string
   226 	 * @return string
   225 	 * @return string
   227 	 */
   226 	 */
   228 	function min_whitespace( $string ) {
   227 	public function min_whitespace( $string ) {
   229 		return preg_replace( '|[\r\n\t ]+|', ' ', $string );
   228 		return preg_replace( '|[\r\n\t ]+|', ' ', $string );
   230 	}
   229 	}
   231 
   230 
   232 	/**
   231 	/**
   233 	 * Reset global variables that grow out of control during imports
   232 	 * Reset global variables that grow out of control during imports
   234 	 *
   233 	 *
   235 	 * @return void
   234 	 * @return void
   236 	 */
   235 	 */
   237 	function stop_the_insanity() {
   236 	public function stop_the_insanity() {
   238 		global $wpdb, $wp_actions;
   237 		global $wpdb, $wp_actions;
   239 		// Or define( 'WP_IMPORTING', true );
   238 		// Or define( 'WP_IMPORTING', true );
   240 		$wpdb->queries = array();
   239 		$wpdb->queries = array();
   241 		// Reset $wp_actions to keep it from growing out of control
   240 		// Reset $wp_actions to keep it from growing out of control
   242 		$wp_actions = array();
   241 		$wp_actions = array();
   271 			} else {
   270 			} else {
   272 				$out[$key] = true;
   271 				$out[$key] = true;
   273 			}
   272 			}
   274 
   273 
   275 			$last_arg = $key;
   274 			$last_arg = $key;
   276 		} else if ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
   275 		} elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
   277 			for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
   276 			for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
   278 				$key = $match[1]{$j};
   277 				$key = $match[1]{$j};
   279 				$out[$key] = true;
   278 				$out[$key] = true;
   280 			}
   279 			}
   281 
   280 
   282 			$last_arg = $key;
   281 			$last_arg = $key;
   283 		} else if ( $last_arg !== null ) {
   282 		} elseif ( $last_arg !== null ) {
   284 			$out[$last_arg] = $args[$i];
   283 			$out[$last_arg] = $args[$i];
   285 		}
   284 		}
   286 	}
   285 	}
   287 
   286 
   288 	// Check array for specified param
   287 	// Check array for specified param