petypen

Untitled

May 19th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.51 KB | None | 0 0
  1. //    start to count the timer
  2.  
  3. $start        = microtime( true );
  4.  
  5. $max_duration =;
  6.  
  7. //    get content of file and parse the xml
  8.  
  9. $xml = simplexml_load_file( ‘yourmxlfilesource.xml’ );
  10.  
  11. //    initiate database connection
  12.  
  13. $database_info[ 'hostname' ] = “”; //    database hostname
  14.  
  15. $database_info[ 'database' ] = “”; //    database name
  16.  
  17. $database_info[ 'username' ] = “”; //    database username
  18.  
  19. $database_info[ 'password' ] = “”; //    database password
  20.  
  21. $database_connect = mysql_pconnect( $database_info[ 'hostname' ], $database_info[ 'username' ], $database_info[ 'password' ] ) or trigger_error( mysql_error(), E_USER_ERROR );
  22.  
  23. mysql_select_db( $database_info[ 'database' ], $database_connect );
  24.  
  25. $i = 0;
  26.  
  27. //    get all the comment from xml file
  28.  
  29. $comments = get_post();
  30.  
  31. //    get all the post title array from xml file
  32.  
  33. $post_title = get_post_title_array();
  34.  
  35. $comment_result = array( );
  36.  
  37. $temp           = array( );
  38.  
  39. //    create loop to convert from xml comment into wordpress-format comment
  40.  
  41. foreach ( $comments as $comment ) {
  42.  
  43. $start_sub = microtime( true );
  44. $comment_result[ 'comment_post_ID' ]      = get_post_id( $comment->thread->attributes( ‘dsq’, TRUE )->id );
  45.  
  46. $comment_result[ 'comment_author' ]       = $comment->author->name;
  47.  
  48. $comment_result[ 'comment_author_email' ] = $comment->author->email;
  49.  
  50. $comment_result[ 'comment_author_url' ]   =;
  51.  
  52. $comment_result[ 'comment_author_IP' ]    = $comment->ipAddress;
  53.  
  54. $comment_result[ 'comment_date' ]         = sanitize_date( $comment->createdAt );
  55.  
  56. $comment_result[ 'comment_date_gmt' ]     = sanitize_date( $comment->createdAt );
  57.  
  58. $comment_result[ 'comment_content' ]      = strip_tags( mysql_real_escape_string( $comment->message ),<br><img><a>);
  59.  
  60. $comment_result[ 'comment_karma' ]        = 1;
  61. //    check if comment is spam, deleted or approved
  62. if ( $comment->isSpam ==true) {
  63.  
  64. $comment_approved = ‘spam’;
  65.  
  66. } else if ( $comment->isDeleted ==true) {
  67.  
  68. $comment_approved = ‘trash’;
  69.  
  70. } else {
  71.  
  72. $comment_approved = 1;
  73.  
  74. }
  75. $comment_result[ 'comment_approved' ] = $comment_approved;
  76.  
  77. $comment_result[ 'comment_agent' ]    =;
  78.  
  79. $comment_result[ 'comment_type' ]     =;
  80.  
  81. $comment_result[ 'comment_parent' ]   =;
  82.  
  83. $comment_result[ 'user_id' ]          =;
  84. //    store the wordpress format comment into temporary variable
  85. $temp[ $i ] = $comment_result;
  86. //    insert the wordpress format comment into wp database
  87.  
  88. insert_comment( $temp[ $i ] );
  89.  
  90. $duration[ $i ] = microtime( true )$start_sub;
  91. $i++;
  92.  
  93. }
  94.  
  95. echomax duration :. max( $duration ) .<br/>;
  96.  
  97. echomin duration :. min( $duration ) .<br/>;
  98.  
  99. echo ‘average duration :. ( array_sum( $duration ) / count( $duration ) ) .<br/>;
  100.  
  101.  
  102.  
  103. //    show the total duration of process
  104.  
  105. echo ‘total duration :. ( microtime( true )$start );
  106.  
  107. /////////    define function here
  108.  
  109. function insert_comment( $comment )
  110.  
  111. {
  112.  
  113. global $database_connect;
  114. //    function to insert the comment into wp database
  115. $field  =;
  116.  
  117. $values =;
  118. foreach ( $comment as $key => $value ) {
  119.  
  120. //    create sql query to insert the comment
  121.  
  122. $field .= ‘`’ . $key . ‘`’ .,;
  123.  
  124. $values .= ‘”‘ . $value . ‘”‘ .,;
  125.  
  126. }
  127. $field  = rtrim( $field,,);
  128.  
  129. $values = rtrim( $values,,);
  130. //    insert the comment into the database
  131. $query = “INSERT INTO `wp_comments` ($field) VALUES ($values);
  132.  
  133. $query_result = mysql_query( $query, $database_connect ) or die( mysql_error() );
  134.  
  135. }
  136.  
  137. function sanitize_date( $date )
  138.  
  139. {
  140.  
  141. //  remove the additional string from the date
  142. $date = str_replace( ‘T’, ‘ ‘, $date );
  143.  
  144. $date = str_replace( ‘Z’, ‘ ‘, $date );
  145. return $date;
  146.  
  147. }
  148.  
  149. function get_post_id( $thread )
  150.  
  151. {
  152.  
  153. global $post_title, $database_connect;
  154.  
  155. //  get wordpress post id from disqus thread id
  156. $thread_title = find_thread( ‘id’, $thread, ‘title’ ); //  get the title of the post
  157. $thread_title = explode(/, $thread_title );
  158.  
  159. $thread_title = $thread_title[ count( $thread_title ) - 1 ];
  160.  
  161. $thread_title = str_replace(-, ‘ ‘, $thread_title );
  162. $thread_title = str_replace(.html’,, $thread_title );
  163. $post_title_closest = get_closest_post_title( $thread_title, $post_title );
  164. //  get the wordpress post id from the title of the post
  165. $query = “SELECT `ID` FROM `wp_posts` WHERE `post_title` =$post_title_closest’ LIMIT 1;
  166.  
  167. $query_result = mysql_query( $query, $database_connect ) or die( mysql_error() );
  168. $query_result_row = mysql_fetch_assoc( $query_result );
  169.  
  170. return $query_result_row[ 'ID' ];
  171. }
  172.  
  173. function get_closest_post_title( $input, $words )
  174.  
  175. {
  176.  
  177. // no shortest distance found, yet
  178.  
  179. $shortest = -1;
  180. // loop through words to find the closest
  181.  
  182. foreach ( $words as $word ) {
  183. // calculate the distance between the input word,
  184.  
  185. // and the current word
  186.  
  187. $lev = levenshtein( $input, $word );
  188. // check for an exact match
  189.  
  190. if ( $lev == 0 ) {
  191. // closest word is this one (exact match)
  192.  
  193. $closest  = $word;
  194.  
  195. $shortest = 0;
  196. // break out of the loop; we’ve found an exact match
  197. break;
  198.  
  199. }
  200. // if this distance is less than the next found shortest
  201.  
  202. // distance, OR if a next shortest word has not yet been found
  203.  
  204. if ( $lev <= $shortest || $shortest < 0 ) {
  205.  
  206. // set the closest match, and shortest distance
  207.  
  208. $closest  = $word;
  209.  
  210. $shortest = $lev;
  211.  
  212. }
  213. }
  214.  
  215. return $closest;
  216.  
  217. }
  218.  
  219. function get_post_title_array( )
  220.  
  221. {
  222.  
  223. //  get wordpress post id from disqus thread id
  224. global $database_connect;
  225. //  get the wordpress post id from the title of the post
  226. $query = “SELECT DISTINCT(`post_title`) FROM `wp_posts`”;
  227.  
  228. $query_result = mysql_query( $query, $database_connect ) or die( mysql_error() );
  229. $query_result_row = mysql_fetch_assoc( $query_result );
  230. $i = 0;
  231. do {
  232.  
  233. $result[ $i ] = $query_result_row[ 'post_title' ];
  234.  
  235. $i++;
  236.  
  237. } while ( $query_result_row = mysql_fetch_assoc( $query_result ) );
  238. return $result;
  239. }
  240.  
  241. function find_thread( $category, $source_value, $return_category )
  242.  
  243. {
  244.  
  245. //    function to get thread information
  246. global $xml;
  247. foreach ( $xml->children() as $row ) {
  248. if ( (int) $row->attributes( ‘dsq’, TRUE )->id == (int) $source_value ) {
  249.  
  250. return $row->$return_category;
  251.  
  252. }
  253.  
  254. }
  255. }
  256.  
  257. function get_post( )
  258.  
  259. {
  260.  
  261. //    function to get all post from xml data
  262. global $xml;
  263. $i = 0;
  264. foreach ( $xml->children() as $key => $value ) {
  265. if ( $key == ‘post’ ) {
  266. $result[ $i ] = $value;
  267.  
  268. $i++;
  269.  
  270. }
  271.  
  272. }
  273. return $result;
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment