3 * WP_Importer base class
11 function __construct() {}
13 function WP_Importer() {
18 * Returns array with imported permalinks from WordPress database
23 function get_imported_posts( $importer_name, $bid ) {
31 // Grab all posts in chunks
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 );
38 $offset = ( $limit + $offset );
40 if ( !empty( $results ) ) {
41 foreach ( $results as $r ) {
42 // Set permalinks into array
43 $hashtable[$r->meta_value] = intval( $r->post_id );
46 } while ( count( $results ) == $limit );
48 // unset to save memory
49 unset( $results, $r );
55 * Return count of imported permalinks from WordPress database
60 function count_imported_posts( $importer_name, $bid ) {
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 );
69 $result = $wpdb->get_results( $sql );
71 if ( !empty( $result ) )
72 $count = intval( $result[0]->cnt );
74 // unset to save memory
81 * Set array with imported comments from WordPress database
86 function get_imported_comments( $bid ) {
94 // Grab all comments in chunks
96 $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
97 $results = $wpdb->get_results( $sql );
100 $offset = ( $limit + $offset );
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 );
108 // Check if this comment came from this blog
109 if ( $bid == $ca_bid ) {
110 $hashtable[$source_comment_id] = intval( $r->comment_ID );
114 } while ( count( $results ) == $limit );
116 // unset to save memory
117 unset( $results, $r );
122 function set_blog( $blog_id ) {
123 if ( is_numeric( $blog_id ) ) {
124 $blog_id = (int) $blog_id;
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" );
131 if ( empty( $parsed['path'] ) )
132 $parsed['path'] = '/';
133 $blog = get_blog_details( array( 'domain' => $parsed['host'], 'path' => $parsed['path'] ) );
135 fwrite( STDERR, "Error: Could not find blog\n" );
138 $blog_id = (int) $blog->blog_id;
139 // Restore global $current_blog
140 global $current_blog;
141 $current_blog = $blog;
144 if ( function_exists( 'is_multisite' ) ) {
145 if ( is_multisite() )
146 switch_to_blog( $blog_id );
152 function set_user( $user_id ) {
153 if ( is_numeric( $user_id ) ) {
154 $user_id = (int) $user_id;
156 $user_id = (int) username_exists( $user_id );
159 if ( !$user_id || !wp_set_current_user( $user_id ) ) {
160 fwrite( STDERR, "Error: can not find user\n" );
168 * Sort by strlen, longest string first
174 function cmpr_strlen( $a, $b ) {
175 return strlen( $b ) - strlen( $a );
182 * @param string $username
183 * @param string $password
187 function get_page( $url, $username = '', $password = '', $head = false ) {
188 // Increase the timeout
189 add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
193 if ( true === $head )
194 $args['method'] = 'HEAD';
195 if ( !empty( $username ) && !empty( $password ) )
196 $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
198 $args['headers'] = $headers;
200 return wp_remote_request( $url, $args );
204 * Bump up the request timeout for http requests
209 function bump_request_timeout( $val ) {
214 * Check if user has exceeded disk quota
218 function is_user_over_quota() {
219 global $current_blog;
221 if ( function_exists( 'upload_is_user_over_quota' ) ) {
222 if ( upload_is_user_over_quota( 1 ) ) {
223 echo "Sorry, you have used your upload quota.\n";
232 * Replace newlines, tabs, and multiple spaces with a single space
234 * @param string $string
237 function min_whitespace( $string ) {
238 return preg_replace( '|[\r\n\t ]+|', ' ', $string );
242 * Reset global variables that grow out of control during imports
246 function stop_the_insanity() {
247 global $wpdb, $wp_actions;
248 // Or define( 'WP_IMPORTING', true );
249 $wpdb->queries = array();
250 // Reset $wp_actions to keep it from growing out of control
251 $wp_actions = array();
256 * Returns value of command line params.
257 * Exits when a required param is not set.
259 * @param string $param
260 * @param bool $required
263 function get_cli_args( $param, $required = false ) {
264 $args = $_SERVER['argv'];
271 $il = sizeof( $args );
273 for ( $i = 1, $il; $i < $il; $i++ ) {
274 if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
275 $parts = explode( "=", $match[1] );
276 $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
278 if ( isset( $parts[1] ) ) {
279 $out[$key] = $parts[1];
285 } else if ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
286 for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
287 $key = $match[1]{$j};
292 } else if ( $last_arg !== null ) {
293 $out[$last_arg] = $args[$i];
297 // Check array for specified param
298 if ( isset( $out[$param] ) ) {
300 $return = $out[$param];
303 // Check for missing required param
304 if ( !isset( $out[$param] ) && $required ) {
305 // Display message and exit
306 echo "\"$param\" parameter is required but was not specified\n";