PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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