3 * WP_Importer base class
10 public function __construct() {}
13 * Returns array with imported permalinks from WordPress database
18 public function get_imported_posts( $importer_name, $bid ) {
26 // Grab all posts in chunks
28 $meta_key = $importer_name . '_' . $bid . '_permalink';
29 $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '%s' LIMIT %d,%d", $meta_key, $offset, $limit );
30 $results = $wpdb->get_results( $sql );
33 $offset = ( $limit + $offset );
35 if ( !empty( $results ) ) {
36 foreach ( $results as $r ) {
37 // Set permalinks into array
38 $hashtable[$r->meta_value] = intval( $r->post_id );
41 } while ( count( $results ) == $limit );
43 // Unset to save memory.
44 unset( $results, $r );
50 * Return count of imported permalinks from WordPress database
55 public function count_imported_posts( $importer_name, $bid ) {
60 // Get count of permalinks
61 $meta_key = $importer_name . '_' . $bid . '_permalink';
62 $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key );
64 $result = $wpdb->get_results( $sql );
66 if ( !empty( $result ) )
67 $count = intval( $result[0]->cnt );
69 // Unset to save memory.
76 * Set array with imported comments from WordPress database
81 public function get_imported_comments( $bid ) {
89 // Grab all comments in chunks
91 $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
92 $results = $wpdb->get_results( $sql );
95 $offset = ( $limit + $offset );
97 if ( !empty( $results ) ) {
98 foreach ( $results as $r ) {
99 // Explode comment_agent key
100 list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent );
101 $source_comment_id = intval( $source_comment_id );
103 // Check if this comment came from this blog
104 if ( $bid == $ca_bid ) {
105 $hashtable[$source_comment_id] = intval( $r->comment_ID );
109 } while ( count( $results ) == $limit );
111 // Unset to save memory.
112 unset( $results, $r );
117 public function set_blog( $blog_id ) {
118 if ( is_numeric( $blog_id ) ) {
119 $blog_id = (int) $blog_id;
121 $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
122 if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) {
123 fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
126 if ( empty( $parsed['path'] ) )
127 $parsed['path'] = '/';
128 $blog = get_blog_details( array( 'domain' => $parsed['host'], 'path' => $parsed['path'] ) );
130 fwrite( STDERR, "Error: Could not find blog\n" );
133 $blog_id = (int) $blog->blog_id;
136 if ( function_exists( 'is_multisite' ) ) {
137 if ( is_multisite() )
138 switch_to_blog( $blog_id );
144 public function set_user( $user_id ) {
145 if ( is_numeric( $user_id ) ) {
146 $user_id = (int) $user_id;
148 $user_id = (int) username_exists( $user_id );
151 if ( !$user_id || !wp_set_current_user( $user_id ) ) {
152 fwrite( STDERR, "Error: can not find user\n" );
160 * Sort by strlen, longest string first
166 public function cmpr_strlen( $a, $b ) {
167 return strlen( $b ) - strlen( $a );
174 * @param string $username
175 * @param string $password
179 public function get_page( $url, $username = '', $password = '', $head = false ) {
180 // Increase the timeout
181 add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
185 if ( true === $head )
186 $args['method'] = 'HEAD';
187 if ( !empty( $username ) && !empty( $password ) )
188 $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
190 $args['headers'] = $headers;
192 return wp_safe_remote_request( $url, $args );
196 * Bump up the request timeout for http requests
201 public function bump_request_timeout( $val ) {
206 * Check if user has exceeded disk quota
210 public function is_user_over_quota() {
211 if ( function_exists( 'upload_is_user_over_quota' ) ) {
212 if ( upload_is_user_over_quota() ) {
213 echo "Sorry, you have used your upload quota.\n";
222 * Replace newlines, tabs, and multiple spaces with a single space
224 * @param string $string
227 public function min_whitespace( $string ) {
228 return preg_replace( '|[\r\n\t ]+|', ' ', $string );
232 * Reset global variables that grow out of control during imports
236 public function stop_the_insanity() {
237 global $wpdb, $wp_actions;
238 // Or define( 'WP_IMPORTING', true );
239 $wpdb->queries = array();
240 // Reset $wp_actions to keep it from growing out of control
241 $wp_actions = array();
246 * Returns value of command line params.
247 * Exits when a required param is not set.
249 * @param string $param
250 * @param bool $required
253 function get_cli_args( $param, $required = false ) {
254 $args = $_SERVER['argv'];
261 $il = sizeof( $args );
263 for ( $i = 1, $il; $i < $il; $i++ ) {
264 if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
265 $parts = explode( "=", $match[1] );
266 $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
268 if ( isset( $parts[1] ) ) {
269 $out[$key] = $parts[1];
275 } elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
276 for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
277 $key = $match[1]{$j};
282 } elseif ( $last_arg !== null ) {
283 $out[$last_arg] = $args[$i];
287 // Check array for specified param
288 if ( isset( $out[$param] ) ) {
290 $return = $out[$param];
293 // Check for missing required param
294 if ( !isset( $out[$param] ) && $required ) {
295 // Display message and exit
296 echo "\"$param\" parameter is required but was not specified\n";