3 * WP_Importer base class
10 public function __construct() {}
13 * Returns array with imported permalinks from WordPress database
15 * @global wpdb $wpdb WordPress database abstraction object.
17 * @param string $importer_name
21 public function get_imported_posts( $importer_name, $bid ) {
29 // Grab all posts in chunks
31 $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 );
33 $results = $wpdb->get_results( $sql );
36 $offset = ( $limit + $offset );
38 if ( !empty( $results ) ) {
39 foreach ( $results as $r ) {
40 // Set permalinks into array
41 $hashtable[$r->meta_value] = intval( $r->post_id );
44 } while ( count( $results ) == $limit );
46 // Unset to save memory.
47 unset( $results, $r );
53 * Return count of imported permalinks from WordPress database
55 * @global wpdb $wpdb WordPress database abstraction object.
57 * @param string $importer_name
61 public function count_imported_posts( $importer_name, $bid ) {
66 // Get count of permalinks
67 $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 );
70 $result = $wpdb->get_results( $sql );
72 if ( !empty( $result ) )
73 $count = intval( $result[0]->cnt );
75 // Unset to save memory.
82 * Set array with imported comments from WordPress database
84 * @global wpdb $wpdb WordPress database abstraction object.
89 public function get_imported_comments( $bid ) {
97 // Grab all comments in chunks
99 $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
100 $results = $wpdb->get_results( $sql );
103 $offset = ( $limit + $offset );
105 if ( !empty( $results ) ) {
106 foreach ( $results as $r ) {
107 // Explode comment_agent key
108 list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent );
109 $source_comment_id = intval( $source_comment_id );
111 // Check if this comment came from this blog
112 if ( $bid == $ca_bid ) {
113 $hashtable[$source_comment_id] = intval( $r->comment_ID );
117 } while ( count( $results ) == $limit );
119 // Unset to save memory.
120 unset( $results, $r );
127 * @param int $blog_id
130 public function set_blog( $blog_id ) {
131 if ( is_numeric( $blog_id ) ) {
132 $blog_id = (int) $blog_id;
134 $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
135 if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
136 fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
139 if ( empty( $parsed['path'] ) ) {
140 $parsed['path'] = '/';
142 $blogs = get_sites( array( 'domain' => $parsed['host'], 'number' => 1, 'path' => $parsed['path'] ) );
144 fwrite( STDERR, "Error: Could not find blog\n" );
147 $blog = array_shift( $blogs );
148 $blog_id = (int) $blog->blog_id;
151 if ( function_exists( 'is_multisite' ) ) {
152 if ( is_multisite() )
153 switch_to_blog( $blog_id );
161 * @param int $user_id
164 public function set_user( $user_id ) {
165 if ( is_numeric( $user_id ) ) {
166 $user_id = (int) $user_id;
168 $user_id = (int) username_exists( $user_id );
171 if ( !$user_id || !wp_set_current_user( $user_id ) ) {
172 fwrite( STDERR, "Error: can not find user\n" );
180 * Sort by strlen, longest string first
186 public function cmpr_strlen( $a, $b ) {
187 return strlen( $b ) - strlen( $a );
194 * @param string $username
195 * @param string $password
199 public function get_page( $url, $username = '', $password = '', $head = false ) {
200 // Increase the timeout
201 add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
205 if ( true === $head )
206 $args['method'] = 'HEAD';
207 if ( !empty( $username ) && !empty( $password ) )
208 $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
210 $args['headers'] = $headers;
212 return wp_safe_remote_request( $url, $args );
216 * Bump up the request timeout for http requests
221 public function bump_request_timeout( $val ) {
226 * Check if user has exceeded disk quota
230 public function is_user_over_quota() {
231 if ( function_exists( 'upload_is_user_over_quota' ) ) {
232 if ( upload_is_user_over_quota() ) {
241 * Replace newlines, tabs, and multiple spaces with a single space
243 * @param string $string
246 public function min_whitespace( $string ) {
247 return preg_replace( '|[\r\n\t ]+|', ' ', $string );
251 * Reset global variables that grow out of control during imports
254 * @global array $wp_actions
256 public function stop_the_insanity() {
257 global $wpdb, $wp_actions;
258 // Or define( 'WP_IMPORTING', true );
259 $wpdb->queries = array();
260 // Reset $wp_actions to keep it from growing out of control
261 $wp_actions = array();
266 * Returns value of command line params.
267 * Exits when a required param is not set.
269 * @param string $param
270 * @param bool $required
273 function get_cli_args( $param, $required = false ) {
274 $args = $_SERVER['argv'];
281 $il = sizeof( $args );
283 for ( $i = 1, $il; $i < $il; $i++ ) {
284 if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
285 $parts = explode( "=", $match[1] );
286 $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
288 if ( isset( $parts[1] ) ) {
289 $out[$key] = $parts[1];
295 } elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
296 for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
297 $key = $match[1]{$j};
302 } elseif ( $last_arg !== null ) {
303 $out[$last_arg] = $args[$i];
307 // Check array for specified param
308 if ( isset( $out[$param] ) ) {
310 $return = $out[$param];
313 // Check for missing required param
314 if ( !isset( $out[$param] ) && $required ) {
315 // Display message and exit
316 echo "\"$param\" parameter is required but was not specified\n";