]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-importer.php
WordPress 3.8.1-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-importer.php
1 <?php
2 /**
3  * WP_Importer base class
4  */
5 class WP_Importer {
6         /**
7          * Class Constructor
8          *
9          * @return void
10          */
11         function __construct() {}
12
13         /**
14          * Returns array with imported permalinks from WordPress database
15          *
16          * @param string $bid
17          * @return array
18          */
19         function get_imported_posts( $importer_name, $bid ) {
20                 global $wpdb;
21
22                 $hashtable = array();
23
24                 $limit = 100;
25                 $offset = 0;
26
27                 // Grab all posts in chunks
28                 do {
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 );
32
33                         // Increment offset
34                         $offset = ( $limit + $offset );
35
36                         if ( !empty( $results ) ) {
37                                 foreach ( $results as $r ) {
38                                         // Set permalinks into array
39                                         $hashtable[$r->meta_value] = intval( $r->post_id );
40                                 }
41                         }
42                 } while ( count( $results ) == $limit );
43
44                 // unset to save memory
45                 unset( $results, $r );
46
47                 return $hashtable;
48         }
49
50         /**
51          * Return count of imported permalinks from WordPress database
52          *
53          * @param string $bid
54          * @return int
55          */
56         function count_imported_posts( $importer_name, $bid ) {
57                 global $wpdb;
58
59                 $count = 0;
60
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 );
64
65                 $result = $wpdb->get_results( $sql );
66
67                 if ( !empty( $result ) )
68                         $count = intval( $result[0]->cnt );
69
70                 // unset to save memory
71                 unset( $results );
72
73                 return $count;
74         }
75
76         /**
77          * Set array with imported comments from WordPress database
78          *
79          * @param string $bid
80          * @return array
81          */
82         function get_imported_comments( $bid ) {
83                 global $wpdb;
84
85                 $hashtable = array();
86
87                 $limit = 100;
88                 $offset = 0;
89
90                 // Grab all comments in chunks
91                 do {
92                         $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
93                         $results = $wpdb->get_results( $sql );
94
95                         // Increment offset
96                         $offset = ( $limit + $offset );
97
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 );
103
104                                         // Check if this comment came from this blog
105                                         if ( $bid == $ca_bid ) {
106                                                 $hashtable[$source_comment_id] = intval( $r->comment_ID );
107                                         }
108                                 }
109                         }
110                 } while ( count( $results ) == $limit );
111
112                 // unset to save memory
113                 unset( $results, $r );
114
115                 return $hashtable;
116         }
117
118         function set_blog( $blog_id ) {
119                 if ( is_numeric( $blog_id ) ) {
120                         $blog_id = (int) $blog_id;
121                 } else {
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" );
125                                 exit();
126                         }
127                         if ( empty( $parsed['path'] ) )
128                                 $parsed['path'] = '/';
129                         $blog = get_blog_details( array( 'domain' => $parsed['host'], 'path' => $parsed['path'] ) );
130                         if ( !$blog ) {
131                                 fwrite( STDERR, "Error: Could not find blog\n" );
132                                 exit();
133                         }
134                         $blog_id = (int) $blog->blog_id;
135                 }
136
137                 if ( function_exists( 'is_multisite' ) ) {
138                         if ( is_multisite() )
139                                 switch_to_blog( $blog_id );
140                 }
141
142                 return $blog_id;
143         }
144
145         function set_user( $user_id ) {
146                 if ( is_numeric( $user_id ) ) {
147                         $user_id = (int) $user_id;
148                 } else {
149                         $user_id = (int) username_exists( $user_id );
150                 }
151
152                 if ( !$user_id || !wp_set_current_user( $user_id ) ) {
153                         fwrite( STDERR, "Error: can not find user\n" );
154                         exit();
155                 }
156
157                 return $user_id;
158         }
159
160         /**
161          * Sort by strlen, longest string first
162          *
163          * @param string $a
164          * @param string $b
165          * @return int
166          */
167         function cmpr_strlen( $a, $b ) {
168                 return strlen( $b ) - strlen( $a );
169         }
170
171         /**
172          * GET URL
173          *
174          * @param string $url
175          * @param string $username
176          * @param string $password
177          * @param bool $head
178          * @return array
179          */
180         function get_page( $url, $username = '', $password = '', $head = false ) {
181                 // Increase the timeout
182                 add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
183
184                 $headers = array();
185                 $args = array();
186                 if ( true === $head )
187                         $args['method'] = 'HEAD';
188                 if ( !empty( $username ) && !empty( $password ) )
189                         $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
190
191                 $args['headers'] = $headers;
192
193                 return wp_safe_remote_request( $url, $args );
194         }
195
196         /**
197          * Bump up the request timeout for http requests
198          *
199          * @param int $val
200          * @return int
201          */
202         function bump_request_timeout( $val ) {
203                 return 60;
204         }
205
206         /**
207          * Check if user has exceeded disk quota
208          *
209          * @return bool
210          */
211         function is_user_over_quota() {
212                 if ( function_exists( 'upload_is_user_over_quota' ) ) {
213                         if ( upload_is_user_over_quota( 1 ) ) {
214                                 echo "Sorry, you have used your upload quota.\n";
215                                 return true;
216                         }
217                 }
218
219                 return false;
220         }
221
222         /**
223          * Replace newlines, tabs, and multiple spaces with a single space
224          *
225          * @param string $string
226          * @return string
227          */
228         function min_whitespace( $string ) {
229                 return preg_replace( '|[\r\n\t ]+|', ' ', $string );
230         }
231
232         /**
233          * Reset global variables that grow out of control during imports
234          *
235          * @return void
236          */
237         function stop_the_insanity() {
238                 global $wpdb, $wp_actions;
239                 // Or define( 'WP_IMPORTING', true );
240                 $wpdb->queries = array();
241                 // Reset $wp_actions to keep it from growing out of control
242                 $wp_actions = array();
243         }
244 }
245
246 /**
247  * Returns value of command line params.
248  * Exits when a required param is not set.
249  *
250  * @param string $param
251  * @param bool $required
252  * @return mixed
253  */
254 function get_cli_args( $param, $required = false ) {
255         $args = $_SERVER['argv'];
256
257         $out = array();
258
259         $last_arg = null;
260         $return = null;
261
262         $il = sizeof( $args );
263
264         for ( $i = 1, $il; $i < $il; $i++ ) {
265                 if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) {
266                         $parts = explode( "=", $match[1] );
267                         $key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] );
268
269                         if ( isset( $parts[1] ) ) {
270                                 $out[$key] = $parts[1];
271                         } else {
272                                 $out[$key] = true;
273                         }
274
275                         $last_arg = $key;
276                 } else if ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) {
277                         for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
278                                 $key = $match[1]{$j};
279                                 $out[$key] = true;
280                         }
281
282                         $last_arg = $key;
283                 } else if ( $last_arg !== null ) {
284                         $out[$last_arg] = $args[$i];
285                 }
286         }
287
288         // Check array for specified param
289         if ( isset( $out[$param] ) ) {
290                 // Set return value
291                 $return = $out[$param];
292         }
293
294         // Check for missing required param
295         if ( !isset( $out[$param] ) && $required ) {
296                 // Display message and exit
297                 echo "\"$param\" parameter is required but was not specified\n";
298                 exit();
299         }
300
301         return $return;
302 }