]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-importer.php
Wordpress 3.5.2-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                 $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" );
191
192                 $args['headers'] = $headers;
193
194                 return wp_remote_request( $url, $args );
195         }
196
197         /**
198          * Bump up the request timeout for http requests
199          *
200          * @param int $val
201          * @return int
202          */
203         function bump_request_timeout( $val ) {
204                 return 60;
205         }
206
207         /**
208          * Check if user has exceeded disk quota
209          *
210          * @return bool
211          */
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";
216                                 return true;
217                         }
218                 }
219
220                 return false;
221         }
222
223         /**
224          * Replace newlines, tabs, and multiple spaces with a single space
225          *
226          * @param string $string
227          * @return string
228          */
229         function min_whitespace( $string ) {
230                 return preg_replace( '|[\r\n\t ]+|', ' ', $string );
231         }
232
233         /**
234          * Reset global variables that grow out of control during imports
235          *
236          * @return void
237          */
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();
244         }
245 }
246
247 /**
248  * Returns value of command line params.
249  * Exits when a required param is not set.
250  *
251  * @param string $param
252  * @param bool $required
253  * @return mixed
254  */
255 function get_cli_args( $param, $required = false ) {
256         $args = $_SERVER['argv'];
257
258         $out = array();
259
260         $last_arg = null;
261         $return = null;
262
263         $il = sizeof( $args );
264
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] );
269
270                         if ( isset( $parts[1] ) ) {
271                                 $out[$key] = $parts[1];
272                         } else {
273                                 $out[$key] = true;
274                         }
275
276                         $last_arg = $key;
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};
280                                 $out[$key] = true;
281                         }
282
283                         $last_arg = $key;
284                 } else if ( $last_arg !== null ) {
285                         $out[$last_arg] = $args[$i];
286                 }
287         }
288
289         // Check array for specified param
290         if ( isset( $out[$param] ) ) {
291                 // Set return value
292                 $return = $out[$param];
293         }
294
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";
299                 exit();
300         }
301
302         return $return;
303 }