3 * WP_Importer base class
11 function __construct() {}
14 * Returns array with imported permalinks from WordPress database
19 function get_imported_posts( $importer_name, $bid ) {
27 // Grab all posts in chunks
29 $meta_key = $importer_name . '_' . $bid . '_permalink';
30 $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '%s' LIMIT %d,%d", $meta_key, $offset, $limit );
31 $results = $wpdb->get_results( $sql );
34 $offset = ( $limit + $offset );
36 if ( !empty( $results ) ) {
37 foreach ( $results as $r ) {
38 // Set permalinks into array
39 $hashtable[$r->meta_value] = intval( $r->post_id );
42 } while ( count( $results ) == $limit );
44 // unset to save memory
45 unset( $results, $r );
51 * Return count of imported permalinks from WordPress database
56 function count_imported_posts( $importer_name, $bid ) {
61 // Get count of permalinks
62 $meta_key = $importer_name . '_' . $bid . '_permalink';
63 $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key );
65 $result = $wpdb->get_results( $sql );
67 if ( !empty( $result ) )
68 $count = intval( $result[0]->cnt );
70 // unset to save memory
77 * Set array with imported comments from WordPress database
82 function get_imported_comments( $bid ) {
90 // Grab all comments in chunks
92 $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
93 $results = $wpdb->get_results( $sql );
96 $offset = ( $limit + $offset );
98 if ( !empty( $results ) ) {
99 foreach ( $results as $r ) {
100 // Explode comment_agent key
101 list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent );
102 $source_comment_id = intval( $source_comment_id );
104 // Check if this comment came from this blog
105 if ( $bid == $ca_bid ) {
106 $hashtable[$source_comment_id] = intval( $r->comment_ID );
110 } while ( count( $results ) == $limit );
112 // unset to save memory
113 unset( $results, $r );
118 function set_blog( $blog_id ) {
119 if ( is_numeric( $blog_id ) ) {
120 $blog_id = (int) $blog_id;
122 $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
123 if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
124 fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
127 if ( empty( $parsed['path'] ) )
128 $parsed['path'] = '/';
129 $blog = get_blog_details( array( 'domain' => $parsed['host'], 'path' => $parsed['path'] ) );
131 fwrite( STDERR, "Error: Could not find blog\n" );
134 $blog_id = (int) $blog->blog_id;
137 if ( function_exists( 'is_multisite' ) ) {
138 if ( is_multisite() )
139 switch_to_blog( $blog_id );
145 function set_user( $user_id ) {
146 if ( is_numeric( $user_id ) ) {
147 $user_id = (int) $user_id;
149 $user_id = (int) username_exists( $user_id );
152 if ( !$user_id || !wp_set_current_user( $user_id ) ) {
153 fwrite( STDERR, "Error: can not find user\n" );
161 * Sort by strlen, longest string first
167 function cmpr_strlen( $a, $b ) {
168 return strlen( $b ) - strlen( $a );
175 * @param string $username
176 * @param string $password
180 function get_page( $url, $username = '', $password = '', $head = false ) {
181 // Increase the timeout
182 add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
186 $args['reject_unsafe_urls'] = true;
187 if ( true === $head )
188 $args['method'] = 'HEAD';
189 if ( !empty( $username ) && !empty( $password ) )
190 $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
192 $args['headers'] = $headers;
194 return wp_remote_request( $url, $args );
198 * Bump up the request timeout for http requests
203 function bump_request_timeout( $val ) {
208 * Check if user has exceeded disk quota
212 function is_user_over_quota() {
213 if ( function_exists( 'upload_is_user_over_quota' ) ) {
214 if ( upload_is_user_over_quota( 1 ) ) {
215 echo "Sorry, you have used your upload quota.\n";
224 * Replace newlines, tabs, and multiple spaces with a single space
226 * @param string $string
229 function min_whitespace( $string ) {
230 return preg_replace( '|[\r\n\t ]+|', ' ', $string );
234 * Reset global variables that grow out of control during imports
238 function stop_the_insanity() {
239 global $wpdb, $wp_actions;
240 // Or define( 'WP_IMPORTING', true );
241 $wpdb->queries = array();
242 // Reset $wp_actions to keep it from growing out of control
243 $wp_actions = array();
248 * Returns value of command line params.
249 * Exits when a required param is not set.
251 * @param string $param
252 * @param bool $required
255 function get_cli_args( $param, $required = false ) {
256 $args = $_SERVER['argv'];
263 $il = sizeof( $args );
265 for ( $i = 1, $il; $i < $il; $i++ ) {
266 if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
267 $parts = explode( "=", $match[1] );
268 $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
270 if ( isset( $parts[1] ) ) {
271 $out[$key] = $parts[1];
277 } else if ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
278 for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
279 $key = $match[1]{$j};
284 } else if ( $last_arg !== null ) {
285 $out[$last_arg] = $args[$i];
289 // Check array for specified param
290 if ( isset( $out[$param] ) ) {
292 $return = $out[$param];
295 // Check for missing required param
296 if ( !isset( $out[$param] ) && $required ) {
297 // Display message and exit
298 echo "\"$param\" parameter is required but was not specified\n";