Advertisement
Guest User

XML PHP

a guest
Jan 8th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. <?php
  2.  
  3. // Change these
  4. $databaseHostname = "localhost";
  5. $databaseName = "musiny";
  6. $databaseUser = "root";
  7. $databasePassword = "changeme";
  8. $fileName = "export.xml";
  9. $wordpressPostType = "post";
  10.  
  11. //
  12. // Do not edit below this line
  13. // =============================================================================
  14. // =============================================================================
  15. $pdo = new \PDO("mysql:host={$databaseHostname};dbname={$databaseName};charset=utf8", $databaseUser, $databasePassword);
  16. $pdo->exec("set names utf8");
  17. $xmlFile = __DIR__ . "/" . $fileName;
  18.  
  19. $xmlBeginning = '
  20. <?xml version="1.0" encoding="UTF-8" ?>
  21.  
  22. <rss version="2.0"
  23.     xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
  24.     xmlns:content="http://purl.org/rss/1.0/modules/content/"
  25.     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  26.     xmlns:dc="http://purl.org/dc/elements/1.1/"
  27.     xmlns:wp="http://wordpress.org/export/1.2/"
  28. >
  29.  
  30.    <channel>
  31.        <wp:wxr_version>1.2</wp:wxr_version>
  32.        <wp:author></wp:author>
  33. ';
  34.  
  35. file_put_contents($xmlFile, $xmlBeginning);
  36.  
  37. $iteration = 0;
  38. $batchSize = 1000;
  39.  
  40. do {
  41.     $limit = $batchSize;
  42.     $offset = $batchSize * $iteration;
  43.  
  44.     $statement = $pdo->query("
  45.        SELECT *
  46.        FROM spodeli
  47.        WHERE cat = '69'
  48.        AND title NOT LIKE '%София – Ден и Нощ%'
  49.        GROUP by title
  50.        ORDER BY added ASC
  51.        LIMIT {$limit}
  52.        OFFSET {$offset}
  53.    ");
  54.     $songs = $statement->fetchAll(\PDO::FETCH_ASSOC);
  55.  
  56.     $xmlItems = "";
  57.     foreach ($songs as $song) {
  58.         $xmlItems .= <<<XML
  59.         <item>
  60.             <title>{$song["title"]}</title>
  61.             <description></description>
  62.             <content:encoded><![CDATA[{$song["info"]}]]></content:encoded>
  63.             <excerpt:encoded><![CDATA[]]></excerpt:encoded>
  64.             <wp:status>publish</wp:status>
  65.             <wp:post_type>{$wordpressPostType}</wp:post_type>
  66.         </item>
  67. XML;
  68.     }
  69.     file_put_contents($xmlFile, $xmlItems, FILE_APPEND);
  70.  
  71.     $iteration++;
  72. } while ($songs && !empty($songs));
  73.  
  74. $xmlEnd = '
  75.    </channel>
  76. </rss>
  77. ';
  78.  
  79. file_put_contents($xmlFile, $xmlEnd, FILE_APPEND);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement