PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-importer.php

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