12 * Returns array with imported permalinks from WordPress database |
12 * Returns array with imported permalinks from WordPress database |
13 * |
13 * |
14 * @global wpdb $wpdb WordPress database abstraction object. |
14 * @global wpdb $wpdb WordPress database abstraction object. |
15 * |
15 * |
16 * @param string $importer_name |
16 * @param string $importer_name |
17 * @param string $bid |
17 * @param string $blog_id |
18 * @return array |
18 * @return array |
19 */ |
19 */ |
20 public function get_imported_posts( $importer_name, $bid ) { |
20 public function get_imported_posts( $importer_name, $blog_id ) { |
21 global $wpdb; |
21 global $wpdb; |
22 |
22 |
23 $hashtable = array(); |
23 $hashtable = array(); |
24 |
24 |
25 $limit = 100; |
25 $limit = 100; |
26 $offset = 0; |
26 $offset = 0; |
27 |
27 |
28 // Grab all posts in chunks. |
28 // Grab all posts in chunks. |
29 do { |
29 do { |
30 $meta_key = $importer_name . '_' . $bid . '_permalink'; |
30 $meta_key = $importer_name . '_' . $blog_id . '_permalink'; |
31 $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit ); |
31 $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit ); |
32 $results = $wpdb->get_results( $sql ); |
32 $results = $wpdb->get_results( $sql ); |
33 |
33 |
34 // Increment offset. |
34 // Increment offset. |
35 $offset = ( $limit + $offset ); |
35 $offset = ( $limit + $offset ); |
36 |
36 |
37 if ( ! empty( $results ) ) { |
37 if ( ! empty( $results ) ) { |
38 foreach ( $results as $r ) { |
38 foreach ( $results as $r ) { |
39 // Set permalinks into array. |
39 // Set permalinks into array. |
40 $hashtable[ $r->meta_value ] = intval( $r->post_id ); |
40 $hashtable[ $r->meta_value ] = (int) $r->post_id; |
41 } |
41 } |
42 } |
42 } |
43 } while ( count( $results ) == $limit ); |
43 } while ( count( $results ) == $limit ); |
44 |
44 |
45 // Unset to save memory. |
|
46 unset( $results, $r ); |
|
47 |
|
48 return $hashtable; |
45 return $hashtable; |
49 } |
46 } |
50 |
47 |
51 /** |
48 /** |
52 * Return count of imported permalinks from WordPress database |
49 * Return count of imported permalinks from WordPress database |
53 * |
50 * |
54 * @global wpdb $wpdb WordPress database abstraction object. |
51 * @global wpdb $wpdb WordPress database abstraction object. |
55 * |
52 * |
56 * @param string $importer_name |
53 * @param string $importer_name |
57 * @param string $bid |
54 * @param string $blog_id |
58 * @return int |
55 * @return int |
59 */ |
56 */ |
60 public function count_imported_posts( $importer_name, $bid ) { |
57 public function count_imported_posts( $importer_name, $blog_id ) { |
61 global $wpdb; |
58 global $wpdb; |
62 |
59 |
63 $count = 0; |
60 $count = 0; |
64 |
61 |
65 // Get count of permalinks. |
62 // Get count of permalinks. |
66 $meta_key = $importer_name . '_' . $bid . '_permalink'; |
63 $meta_key = $importer_name . '_' . $blog_id . '_permalink'; |
67 $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ); |
64 $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ); |
68 |
65 |
69 $result = $wpdb->get_results( $sql ); |
66 $result = $wpdb->get_results( $sql ); |
70 |
67 |
71 if ( ! empty( $result ) ) { |
68 if ( ! empty( $result ) ) { |
72 $count = intval( $result[0]->cnt ); |
69 $count = (int) $result[0]->cnt; |
73 } |
70 } |
74 |
|
75 // Unset to save memory. |
|
76 unset( $results ); |
|
77 |
71 |
78 return $count; |
72 return $count; |
79 } |
73 } |
80 |
74 |
81 /** |
75 /** |
82 * Set array with imported comments from WordPress database |
76 * Set array with imported comments from WordPress database |
83 * |
77 * |
84 * @global wpdb $wpdb WordPress database abstraction object. |
78 * @global wpdb $wpdb WordPress database abstraction object. |
85 * |
79 * |
86 * @param string $bid |
80 * @param string $blog_id |
87 * @return array |
81 * @return array |
88 */ |
82 */ |
89 public function get_imported_comments( $bid ) { |
83 public function get_imported_comments( $blog_id ) { |
90 global $wpdb; |
84 global $wpdb; |
91 |
85 |
92 $hashtable = array(); |
86 $hashtable = array(); |
93 |
87 |
94 $limit = 100; |
88 $limit = 100; |
103 $offset = ( $limit + $offset ); |
97 $offset = ( $limit + $offset ); |
104 |
98 |
105 if ( ! empty( $results ) ) { |
99 if ( ! empty( $results ) ) { |
106 foreach ( $results as $r ) { |
100 foreach ( $results as $r ) { |
107 // Explode comment_agent key. |
101 // Explode comment_agent key. |
108 list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent ); |
102 list ( $comment_agent_blog_id, $source_comment_id ) = explode( '-', $r->comment_agent ); |
109 $source_comment_id = intval( $source_comment_id ); |
103 |
|
104 $source_comment_id = (int) $source_comment_id; |
110 |
105 |
111 // Check if this comment came from this blog. |
106 // Check if this comment came from this blog. |
112 if ( $bid == $ca_bid ) { |
107 if ( $blog_id == $comment_agent_blog_id ) { |
113 $hashtable[ $source_comment_id ] = intval( $r->comment_ID ); |
108 $hashtable[ $source_comment_id ] = (int) $r->comment_ID; |
114 } |
109 } |
115 } |
110 } |
116 } |
111 } |
117 } while ( count( $results ) == $limit ); |
112 } while ( count( $results ) == $limit ); |
118 |
|
119 // Unset to save memory. |
|
120 unset( $results, $r ); |
|
121 |
113 |
122 return $hashtable; |
114 return $hashtable; |
123 } |
115 } |
124 |
116 |
125 /** |
117 /** |
259 * Resets global variables that grow out of control during imports. |
251 * Resets global variables that grow out of control during imports. |
260 * |
252 * |
261 * @since 3.0.0 |
253 * @since 3.0.0 |
262 * |
254 * |
263 * @global wpdb $wpdb WordPress database abstraction object. |
255 * @global wpdb $wpdb WordPress database abstraction object. |
264 * @global array $wp_actions |
256 * @global int[] $wp_actions |
265 */ |
257 */ |
266 public function stop_the_insanity() { |
258 public function stop_the_insanity() { |
267 global $wpdb, $wp_actions; |
259 global $wpdb, $wp_actions; |
268 // Or define( 'WP_IMPORTING', true ); |
260 // Or define( 'WP_IMPORTING', true ); |
269 $wpdb->queries = array(); |
261 $wpdb->queries = array(); |
289 $out = array(); |
281 $out = array(); |
290 |
282 |
291 $last_arg = null; |
283 $last_arg = null; |
292 $return = null; |
284 $return = null; |
293 |
285 |
294 $il = sizeof( $args ); |
286 $il = count( $args ); |
295 |
287 |
296 for ( $i = 1, $il; $i < $il; $i++ ) { |
288 for ( $i = 1, $il; $i < $il; $i++ ) { |
297 if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) { |
289 if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) { |
298 $parts = explode( '=', $match[1] ); |
290 $parts = explode( '=', $match[1] ); |
299 $key = preg_replace( '/[^a-z0-9]+/', '', $parts[0] ); |
291 $key = preg_replace( '/[^a-z0-9]+/', '', $parts[0] ); |