PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

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