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