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;
135 // Restore global $current_blog
136 global $current_blog;
137 $current_blog = $blog;
140 if ( function_exists( 'is_multisite' ) ) {
141 if ( is_multisite() )
142 switch_to_blog( $blog_id );
148 function set_user( $user_id ) {
149 if ( is_numeric( $user_id ) ) {
150 $user_id = (int) $user_id;
152 $user_id = (int) username_exists( $user_id );
155 if ( !$user_id || !wp_set_current_user( $user_id ) ) {
156 fwrite( STDERR, "Error: can not find user\n" );
164 * Sort by strlen, longest string first
170 function cmpr_strlen( $a, $b ) {
171 return strlen( $b ) - strlen( $a );
178 * @param string $username
179 * @param string $password
183 function get_page( $url, $username = '', $password = '', $head = false ) {
184 // Increase the timeout
185 add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
189 if ( true === $head )
190 $args['method'] = 'HEAD';
191 if ( !empty( $username ) && !empty( $password ) )
192 $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
194 $args['headers'] = $headers;
196 return wp_remote_request( $url, $args );
200 * Bump up the request timeout for http requests
205 function bump_request_timeout( $val ) {
210 * Check if user has exceeded disk quota
214 function is_user_over_quota() {
215 global $current_blog;
217 if ( function_exists( 'upload_is_user_over_quota' ) ) {
218 if ( upload_is_user_over_quota( 1 ) ) {
219 echo "Sorry, you have used your upload quota.\n";
228 * Replace newlines, tabs, and multiple spaces with a single space
230 * @param string $string
233 function min_whitespace( $string ) {
234 return preg_replace( '|[\r\n\t ]+|', ' ', $string );
238 * Reset global variables that grow out of control during imports
242 function stop_the_insanity() {
243 global $wpdb, $wp_actions;
244 // Or define( 'WP_IMPORTING', true );
245 $wpdb->queries = array();
246 // Reset $wp_actions to keep it from growing out of control
247 $wp_actions = array();
252 * Returns value of command line params.
253 * Exits when a required param is not set.
255 * @param string $param
256 * @param bool $required
259 function get_cli_args( $param, $required = false ) {
260 $args = $_SERVER['argv'];
267 $il = sizeof( $args );
269 for ( $i = 1, $il; $i < $il; $i++ ) {
270 if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
271 $parts = explode( "=", $match[1] );
272 $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
274 if ( isset( $parts[1] ) ) {
275 $out[$key] = $parts[1];
281 } else if ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
282 for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
283 $key = $match[1]{$j};
288 } else if ( $last_arg !== null ) {
289 $out[$last_arg] = $args[$i];
293 // Check array for specified param
294 if ( isset( $out[$param] ) ) {
296 $return = $out[$param];
299 // Check for missing required param
300 if ( !isset( $out[$param] ) && $required ) {
301 // Display message and exit
302 echo "\"$param\" parameter is required but was not specified\n";