Guest User

Untitled

a guest
Jan 3rd, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.81 KB | None | 0 0
  1. <pre>
  2. <?php
  3. /**
  4.  * J2.5 to J4 Article Migration Script
  5.  *
  6.  * It migrates categories table, articles table, and related assets table (not the whole assets table).
  7.  * Users are not migrated. Use $admin_user_id to set the author id.
  8.  * Some article metadata, images, and attributes are ignored at the moment.
  9.  */
  10.  
  11. $hostname = "localhost";
  12. $user = "root";
  13. $pass = "";
  14.  
  15. $source = array(
  16.     "dbname" => "migration_source",
  17.     "prefix" => "hkzl8"
  18. );
  19. $target = array(
  20.     "dbname" => "migration_target",
  21.     "prefix" => "c6oeb"
  22. );
  23.  
  24. // article authors are not migrated. All articles will have the same author.
  25. $admin_user_id = "666";
  26.  
  27. // Assets will be stored here to be used in INSERT queries of Categories and Articles
  28. $ASSETS = [];
  29. // Categories will be stored here to be used in INSERT queries of Articles
  30. $CATEGORIES = [];
  31. // Articles will be stored here to be used in INSERT in workflow tables
  32. $ARTICLES = [];
  33.  
  34. try {
  35.  
  36.     $db_source = new PDO ("mysql:host=$hostname;dbname=" . $source["dbname"], $user, $pass);
  37.     $db_target = new PDO ("mysql:host=$hostname;dbname=" . $target["dbname"], $user, $pass);
  38.  
  39.  
  40.     /**
  41.      *  --------------- get and push category and article assets -----------------------------
  42.      */
  43.  
  44.     echo "source: select assets";
  45.     // skip "Uncategorised" category
  46.     $q = "SELECT * FROM `" . $source["prefix"] . "_assets` WHERE (name LIKE \"com_content.article%\" OR `name` LIKE \"com_content.category%\")  AND `name` != \"com_content.category.2\";";
  47.     $stmt = $db_source->query($q);
  48.     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  49.     foreach($rows as $row) {
  50.         $q = insertAssetQuery($row);
  51.  
  52.         $name = $row["name"];
  53.         echo "\ntarget: insert asset name " . $row["name"];
  54.         $stmt = $db_target->prepare($q);
  55.         // safe escape
  56.         $stmt->bindValue(':title', $row["title"], PDO::PARAM_STR);
  57.  
  58.         $stmt->execute();
  59.  
  60.         /**
  61.          * after insertion, get the asset from the target DB to get the asset ID
  62.          */
  63.         echo "\ntarget: select asset name" . $row["name"];
  64.         $stmt = $db_target->query("SELECT * FROM `" . $target["prefix"] . "_assets` WHERE `name` = '".$name."'");
  65.         if ($stmt->rowCount() > 1) {
  66.             throw new Exception("More than 1 asset found with name " . $name);
  67.         }
  68.         if ($stmt->rowCount() <= 0) {
  69.             throw new Exception("No asset found with name " . $name);
  70.         }
  71.  
  72.         $selected = $stmt->fetch(PDO::FETCH_ASSOC);
  73.         $ASSETS[] = $selected;
  74.     }
  75.  
  76.     echo "\nop: " . sizeof($rows) . " assets added.";
  77.  
  78.     // fetch "Uncategorised" Category asset to get the Asset Id in the target db.
  79.     $stmt = $db_target->query("SELECT * FROM `" . $target["prefix"] . "_assets` WHERE `name` LIKE \"com_content.%\" AND `title` = \"Uncategorised\"");
  80.     $uncategorised_asset = $stmt->fetch(PDO::FETCH_ASSOC);
  81.     $ASSETS[] = $uncategorised_asset;
  82.  
  83.  
  84.     /**
  85.      *  --------------- get and push categories -----------------------------
  86.      */
  87.  
  88.     echo "\nsource: select categories ";
  89.     $stmt = $db_source->query("SELECT * FROM `" . $source["prefix"] . "_categories` WHERE `extension` = \"com_content\" AND `alias` != \"uncategorised\"");
  90.     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  91.     foreach($rows as $row) {
  92.         $q = insertCatQuery($row);
  93.         echo "\ntarget: insert category id " . $row["id"];
  94.         $stmt = $db_target->prepare($q);
  95.  
  96.         // safe escape
  97.         $stmt->bindValue(':title', $row["title"], PDO::PARAM_STR);
  98.         $stmt->bindValue(':description', $row["description"], PDO::PARAM_STR);
  99.         $stmt->execute();
  100.  
  101.         /**
  102.          * after insertion, get the category from the target DB to get the category ID
  103.          */
  104.         echo "\ntarget: select category alias " . $row["alias"];
  105.         $stmt = $db_target->query("SELECT * FROM `" . $target["prefix"] . "_categories` WHERE `alias` = '". $row["alias"] ."'");
  106.         $selected = $stmt->fetch(PDO::FETCH_ASSOC);
  107.         $CATEGORIES[] = $selected;
  108.  
  109.     }
  110.     echo "\nop: " . sizeof($rows) . " categories added.";
  111.  
  112.     /**
  113.      * map "Uncategorised" ID from source db to target db.
  114.      * "Uncategorised" should have ID=2, the following code is just to be sure.
  115.      */
  116.     // fetch "Uncategorised" Category asset in source db
  117.     $stmt = $db_source->query("SELECT * FROM `" . $source["prefix"] . "_categories` WHERE `extension` = \"com_content\" AND `title` = \"Uncategorised\"");
  118.     $source_uncategorised_cat = $stmt->fetch(PDO::FETCH_ASSOC);
  119.  
  120.     // fetch "Uncategorised" Category asset to get the Cat Id in the target db.
  121.     $stmt = $db_target->query("SELECT * FROM `" . $target["prefix"] . "_categories` WHERE `extension` = \"com_content\" AND `title` = \"Uncategorised\"");
  122.     $uncategorised_cat = $stmt->fetch(PDO::FETCH_ASSOC);
  123.     $CATEGORIES[] = $uncategorised_cat;
  124.  
  125.  
  126.  
  127.     /**
  128.      * --------------- get and push articles -----------------------------
  129.      */
  130.  
  131.     echo "\nsource: select articles";
  132.     $stmt = $db_source->query("SELECT * FROM `" . $source["prefix"] . "_content`");
  133.     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  134.  
  135.     // TODO category id is wrong here! (it comes from source db while I need the id in the target)
  136.     foreach($rows as $row){
  137.         unset($row["title_alias"]);
  138.         unset($row["mask"]);
  139.         unset($row["sectionid"]);
  140.         unset($row["xreference"]);
  141.         $q = insertArticleQuery($row);
  142.         echo "\ntarget: insert article with (source) id " . $row["id"];
  143.         $stmt = $db_target->prepare($q);
  144.  
  145.         // safe escape
  146.         $stmt->bindValue(':title', $row["title"], PDO::PARAM_STR);
  147.         $stmt->bindValue(':introtext', $row["introtext"], PDO::PARAM_STR);
  148.         $stmt->bindValue(':fulltext', $row["fulltext"], PDO::PARAM_STR);
  149.  
  150.         $stmt->execute();
  151.  
  152.         /**
  153.          * after insertion, get the category from the target DB to get the category ID
  154.          */
  155.         echo "\ntarget: select article alias" . $row["alias"];
  156.         $stmt = $db_target->query("SELECT * FROM `" . $target["prefix"] . "_content` WHERE `alias` = '". $row["alias"] ."'");
  157.         $selected = $stmt->fetch(PDO::FETCH_ASSOC);
  158.         $ARTICLES[] = $selected;
  159.         $q = insertWorkflowQuery($selected);
  160.         $stmt = $db_target->prepare($q);
  161.         $stmt->execute();
  162.     }
  163.     echo "\nop: " . sizeof($rows) . " articles added.";
  164.     echo "\nop: " . sizeof($rows) . " workflow entries added.";
  165.  
  166.  
  167. } catch (PDOException $e) {
  168.     echo "Error: " . $e->getMessage();
  169.     var_dump(debug_backtrace());
  170.     die();
  171. }
  172.  
  173. function insertAssetQuery($row) {
  174.     GLOBAL $target;
  175.     return "INSERT INTO " . $target["prefix"] ."_assets(
  176.    `parent_id`,
  177.    `lft`,
  178.    `rgt`,
  179.    `level`,
  180.    `name`,
  181.    `title`,
  182.    `rules`) VALUES(
  183.                '" . $row["parent_id"] . "',
  184.                '" . $row["lft"] . "',
  185.                '" . $row["rgt"] . "',
  186.                '" . $row["level"] . "',
  187.                '" . $row["name"] . "',
  188.                :title,
  189.                '" . $row["rules"] . "')";
  190. }
  191.  
  192. function insertCatQuery($row) {
  193.     GLOBAL $target;
  194.     GLOBAL $admin_user_id;
  195.     GLOBAL $ASSETS;
  196.     $asset_id = getIdByTitle($row["title"], $ASSETS);
  197.  
  198.     return "INSERT INTO `" . $target["prefix"] ."_categories`(
  199.        `asset_id`,
  200.        `parent_id`,
  201.        `lft`,
  202.        `rgt`,
  203.        `level`,
  204.        `path`,
  205.        `extension`,
  206.        `title`,
  207.        `alias`,
  208.        `note`,
  209.        `description`,
  210.        `published`,
  211.        `checked_out`,
  212.        `checked_out_time`,
  213.        `access`,
  214.        `params`,
  215.        `metadesc`,
  216.        `metakey`,
  217.        `metadata`,
  218.        `created_user_id`,
  219.        `created_time`,
  220.        `modified_user_id`,
  221.        `modified_time`,
  222.        `hits`,
  223.        `language`,
  224.        `version`) VALUES(
  225.                '". $asset_id . "',
  226.                '" . $row["parent_id"] . "',
  227.                '" . $row["lft"] . "',
  228.                '" . $row["rgt"] . "',
  229.                '" . $row["level"] . "',
  230.                '" . $row["path"] . "',
  231.                '" . $row["extension"] . "',
  232.                :title,
  233.                '" . $row["alias"] . "',
  234.                '" . $row["note"] . "',
  235.                :description,
  236.                '" . $row["published"] . "',
  237.                '" . $row["checked_out"] . "',
  238.                '" . $row["checked_out_time"] . "',
  239.                '" . $row["access"] . "',
  240.                '" . $row["params"] . "',
  241.                '" . $row["metadesc"] . "',
  242.                '" . $row["metakey"] . "',
  243.                '" . $row["metadata"] . "',
  244.                '" . $admin_user_id . "',
  245.                '" . $row["created_time"] . "',
  246.                '" . $admin_user_id . "',
  247.                '" . $row["modified_time"] . "',
  248.                '" . $row["hits"] . "',
  249.                '" . $row["language"] . "',
  250.                '1')";
  251. }
  252.  
  253. function insertArticleQuery($row): string {
  254.     GLOBAL $target;
  255.     GLOBAL $admin_user_id;
  256.     GLOBAL $ASSETS;
  257.     GLOBAL $CATEGORIES;
  258.     GLOBAL $source_uncategorised_cat;
  259.  
  260.     $asset_id = getIdByTitle($row["title"], $ASSETS);
  261.     $category_id = getIdByTitle("Uncategorised", $CATEGORIES);
  262.  
  263.     return "INSERT INTO `" . $target["prefix"] ."_content`(
  264.        `id`,
  265.        `asset_id`,
  266.        `title`,
  267.        `alias`,
  268.        `introtext`,
  269.        `fulltext`,
  270.        `state`,
  271.        `catid`,
  272.        `created`,
  273.        `created_by`,
  274.        `created_by_alias`,
  275.        `modified`,
  276.        `modified_by`,
  277.        `checked_out`,
  278.        `checked_out_time`,
  279.        `publish_up`,
  280.        `publish_down`,
  281.        `images`,
  282.        `urls`,
  283.        `attribs`,
  284.        `version`,
  285.        `ordering`,
  286.        `metakey`,
  287.        `metadesc`,
  288.        `access`,
  289.        `hits`,
  290.        `metadata`,
  291.        `featured`,
  292.        `language`,
  293.        `note`          
  294.        ) VALUES(
  295.                NULL,
  296.                '". $asset_id . "',
  297.                :title,
  298.                '" . $row["alias"] . "',
  299.                :introtext,
  300.                :fulltext,
  301.                '" . $row["state"] . "',
  302.                '" . $row["catid"] . "',
  303.                '" . $row["created"] . "',
  304.                '" . $admin_user_id . "',
  305.                '" . $row["created_by_alias"] . "',
  306.                '" . $row["modified"] . "',
  307.                '" . $admin_user_id . "',
  308.                '" . $admin_user_id . "',
  309.                '" . $row["checked_out_time"] . "',
  310.                '" . $row["publish_up"] . "',
  311.                '" . $row["publish_down"] . "',
  312.                '{\"image_intro\":\"\",\"image_intro_alt\":\"\",\"float_intro\":\"\",\"image_intro_caption\":\"\",\"image_fulltext\":\"\",\"image_fulltext_alt\":\"\",\"float_fulltext\":\"\",\"image_fulltext_caption\":\"\"}',
  313.                '{\"urla\":\"\",\"urlatext\":\"\",\"targeta\":\"\",\"urlb\":\"\",\"urlbtext\":\"\",\"targetb\":\"\",\"urlc\":\"\",\"urlctext\":\"\",\"targetc\":\"\"}',
  314.                '{\"article_layout\":\"\",\"show_title\":\"\",\"link_titles\":\"\",\"show_tags\":\"\",\"show_intro\":\"\",\"info_block_position\":\"\",\"info_block_show_title\":\"\",\"show_category\":\"\",\"link_category\":\"\",\"show_parent_category\":\"\",\"link_parent_category\":\"\",\"show_author\":\"\",\"link_author\":\"\",\"show_create_date\":\"\",\"show_modify_date\":\"\",\"show_publish_date\":\"\",\"show_item_navigation\":\"\",\"show_hits\":\"\",\"show_noauth\":\"\",\"urls_position\":\"\",\"alternative_readmore\":\"\",\"article_page_title\":\"\",\"show_publishing_options\":\"\",\"show_article_options\":\"\",\"show_urls_images_backend\":\"\",\"show_urls_images_frontend\":\"\"}',
  315.                '1',
  316.                '" . $row["ordering"] . "',
  317.                '" . $row["metakey"] . "',
  318.                '" . $row["metadesc"] . "',
  319.                '" . $row["access"] . "',
  320.                '" . $row["hits"] . "',
  321.                '{\"robots\":\"\",\"author\":\"\",\"rights\":\"\"}',
  322.                '" . $row["featured"] . "',
  323.                '*',
  324.                ''
  325.                )";
  326. }
  327.  
  328. function insertWorkflowQuery($row): string {
  329.     GLOBAL $target;
  330.     GLOBAL $admin_user_id;
  331.     GLOBAL $ASSETS;
  332.     GLOBAL $CATEGORIES;
  333.     GLOBAL $ARTICLES;
  334.     GLOBAL $source_uncategorised_cat;
  335.  
  336.     return "INSERT INTO `" . $target["prefix"] . "_workflow_associations` (
  337.        `item_id`,
  338.        `stage_id`,
  339.        `extension`) VALUES (
  340.        '". $row["id"] . "',
  341.        '1',
  342.        'com_content.article');";
  343. }
  344.  
  345. /**
  346.  * This is used to search into $ASSETS and $CATEGORIES
  347.  *
  348.  */
  349. function getIdByTitle($title, $data) {
  350.     foreach ($data as $el) {
  351.         if ($el["title"] == $title) {
  352.             return $el["id"];
  353.         }
  354.     }
  355.     throw new Exception("Element with title " . $title . " not found.");
  356. }
  357.  
  358. /**
  359.  * escape double quote and quote
  360.  */
  361. function escape($str) {
  362.     return htmlspecialchars($str, ENT_QUOTES);
  363. }
Advertisement
Add Comment
Please, Sign In to add comment