English
Deutsch
  

Code


About Matthias Nott
Business Objects
Picture Galleries
Code
Technical
Download





Locations of visitors to this page

I recently migrated a site (www.micro-plus.de) from Typo3 3.5 to 4.0 and at the same time migrated to Templavoila. None of the resources I found on the net gave a working example on how to migrate a content which, for a multilingual site with many pages and many paragraphs is not feasible manually.

 

So I analyzed the data model myself and wrote a short PHP script that takes a couple of parameters and migrates one page automatically; I did not make it traverse the page structure automatically to keep things clearer.

The script is very much self explaining (it outputs a help screen if you call it with no parameters). Please bear with me - it is actually the first post-hello-world code I've written in PHP, so any PHP programmer with more than 3 hours of development experience will come up with a much nicer version.

Anyway, here it is:

  1. #!/usr/local/bin/php
  2. <?php
  3. /***************************************************************
  4. *  Copyright notice
  5. *  (c) 2006 Matthias Nott (mnott@mnsoft.org)
  6. *  All rights reserved
  7. *
  8. *  This script is part of the Typo3 project. The Typo3 project is
  9. *  free software; you can redistribute it and/or modify
  10. *  it under the terms of the GNU General Public License as published by
  11. *  the Free Software Foundation; either version 2 of the License, or
  12. *  (at your option) any later version.
  13. *
  14. *  The GNU General Public License can be found at
  15. *  http://www.gnu.org/copyleft/gpl.html.
  16. *  A copy is found in the textfile GPL.txt and important notices to the license
  17. *  from the author is found in LICENSE.txt distributed with these scripts.
  18. *
  19. *
  20. *  This script is distributed in the hope that it will be useful,
  21. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. *  GNU General Public License for more details.
  24. *
  25. *  This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27.  
  28. // *******************************
  29. // Set error reporting
  30. // *******************************
  31. error_reporting (E_ALL ^ E_NOTICE);
  32.  
  33.  
  34. // *******************************
  35. // Script Classes
  36. // *******************************
  37. class Document {
  38.   var $hashVersion;
  39.   var $docid;
  40.   var $docobjects = array();
  41.   var $cmslanguages;
  42.   var $doclanguages = array();
  43.   var $container;
  44.  
  45.   public function __construct($docid, $cmslanguages, $container, $hashVersion) {
  46.     $this->docid        = $docid;
  47.     $this->cmslanguages = $cmslanguages;
  48.     $this->container    = $container;
  49.     $this->hashVersion  = $hashVersion;
  50.   }
  51.  
  52.   public function getDocid() {
  53.     return $this->docid;
  54.   }
  55.  
  56.   public function addContent($objectId, $languageId) {
  57.     $this->docobjects[$languageId][] = $objectId;
  58.     $this->doclanguages[$languageId] = null;
  59.   }
  60.  
  61.   public function getLanguageContent($languageId) {
  62.     return $this->docobjects[$languageId];
  63.   }
  64.  
  65.  
  66.   public function to_string() {
  67.     var_dump($this);
  68.   }
  69.  
  70.   public function getFlexml() {
  71.    ksort($this->doclanguages);
  72.    $flexml = "";
  73.    $flexml .= "<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"yes\" ?>\n";
  74.    $flexml .= "<T3FlexForms>\n";
  75.    $flexml .= "    <data>\n";
  76.    $flexml .= "        <sheet index=\"sDEF\">\n";
  77.    foreach($this->doclanguages as $key => $val) {
  78.    $flexml .= "            <language index=\"l".$this->cmslanguages[$key]."\">\n";
  79.    $flexml .= "                <field index=\"".$this->container."\">\n";
  80.    $flexml .= "                    <value index=\"vDEF\">".implode(',', $this->getLanguageContent($key))."</value>\n";
  81.    $flexml .= "                </field>\n";
  82.    $flexml .= "            </language>\n";
  83.    }
  84.    $flexml .= "        </sheet>\n";
  85.    $flexml .= "    </data>\n";
  86.    $flexml .= "</T3FlexForms>";
  87.  
  88.    return $flexml;
  89.   }
  90.  
  91.   /**
  92.    * Create the entries for the sys_refindex table
  93.    */
  94.   public function getRefindexRows() {
  95.     $result = array();
  96.     foreach($this->doclanguages as $key => $val) {
  97.       $content = $this->getLanguageContent($key);
  98.       $sorting = 0;
  99.       foreach($content as $ref_uid) {
  100.         $row = array();
  101.         $row[] = "'".$this->createHash("pages", $this->docid, "tx_templavoila_flex", "sDEF/l".$this->cmslanguages[$key]."/".$this->container."/vDEF/", 0, "tt_content", $ref_uid, "", 0, "", "")."'";
  102.         $row[] = "'pages'";
  103.         $row[] = $this->docid;
  104.         $row[] = "'tx_templavoila_flex'";
  105.         $row[] = "'sDEF/l".$this->cmslanguages[$key]."/".$this->container."/vDEF/'";
  106.         $row[] = "''";
  107.         $row[] = "''";
  108.         $row[] = $sorting++;
  109.         $row[] = 0;
  110.         $row[] = "'tt_content'";
  111.         $row[] = $ref_uid;
  112.         $row[] = "''";
  113.         $result[] = implode(",", $row);
  114.       }
  115.       //$hash = $this->createHash('pages', 432, 'tx_templavoila_flex', 'sDEF/lDEF/field_content/vDEF/', 0, 'tt_content', 1237, '', 0, '', '');
  116.     }
  117.     return $result;
  118.   }
  119.  
  120.  
  121.   /**
  122.    * Create the hash value as used in the sys_refindex table.
  123.    */
  124.   private function createHash($table,$uid,$field,$flexpointer,$deleted,$ref_table,$ref_uid,$ref_string='',$sort=-1,$softref_key='',$softref_id='')   {
  125.     $arr = array(
  126.       'tablename'   => $table,
  127.       'recuid'      => $uid,
  128.       'field'       => $field,
  129.       'flexpointer' => $flexpointer,
  130.       'softref_key' => $softref_key,
  131.       'softref_id'  => $softref_id,
  132.       'sorting'     => $sort,
  133.       'deleted'     => $deleted,
  134.       'ref_table'   => $ref_table,
  135.       'ref_uid'     => $ref_uid,
  136.       'ref_string'  => $ref_string,
  137.     );
  138.     return md5(implode('///',$arr).'///'.$this->hashVersion);
  139.   }
  140.  
  141.   /**
  142.    * Create the entries for the tx_templavoila_elementreference table
  143.    */
  144.   public function getElementReferencesRows() {
  145.     $result = array();
  146.     foreach($this->doclanguages as $key => $val) {
  147.       $content = $this->getLanguageContent($key);
  148.       foreach($content as $uid) {
  149.         $row = array();
  150.         $row[] = $uid;
  151.         $row[] = $this->docid;
  152.         $row[] = "'sDEF'";
  153.         $row[] = "'l".$this->cmslanguages[$key]."'";
  154.         $row[] = "'vDEF'";
  155.         $result[] = implode(",", $row);
  156.       }
  157.     }
  158.     return $result;
  159.   }
  160. }
  161.  
  162.  
  163. class MigrateContent {
  164.   var $db_host;
  165.   var $db_schema;
  166.   var $db_user;
  167.   var $db_pass;
  168.   var $db_conn;
  169.   var $hashVersion;
  170.   var $container;
  171.   var $cmslanguages;
  172.  
  173.   public function __construct($db_host, $db_schema, $db_user, $db_pass, $hashVersion, $container) {
  174.     $this->db_host      = $db_host;
  175.     $this->db_schema    = $db_schema;
  176.     $this->db_user      = $db_user;
  177.     $this->db_pass      = $db_pass;
  178.     $this->hashVersion  = $hashVersion;
  179.     $this->container    = $container;
  180.   }
  181.  
  182.   /**
  183.    * Get the language strings that are currently used in our
  184.    * CMS. These are used later for insertions in the tables.
  185.    */
  186.   private function getCMSLanguageStrings() {
  187.     $query  = "select l.uid, s.lg_iso_2 from static_languages s, sys_language l where l.static_lang_isocode = s.uid";
  188.  
  189.     $qres   = mysql_query($query);
  190.  
  191.     $result = array();
  192.  
  193.     while($row = mysql_fetch_array($qres, MYSQL_ASSOC)) {
  194.       $result[$row['uid']] = $row['lg_iso_2'];
  195.     }
  196.     $result[0] = 'DEF';
  197.     return $result;
  198.   }
  199.  
  200.  
  201.   /**
  202.    * Parse the document structure
  203.    */
  204.   private function getDocument($docid) {
  205.     $query  = "select uid, sys_language_uid from tt_content where pid = ".$docid." and deleted = 0 order by sorting";
  206.    
  207.     $qres   = mysql_query($query);
  208.  
  209.     $result = new Document($docid, $this->cmslanguages, $this->container, $this->hashVersion);
  210.  
  211.     while($row = mysql_fetch_array($qres, MYSQL_ASSOC)) {
  212.       $result->addContent($row['uid'], $row['sys_language_uid']);
  213.     }
  214.     return $result;
  215.   }
  216.  
  217.   /**
  218.    * Update the document in the database
  219.    */
  220.   private function updateDocument($doc) {
  221.     /*
  222.      * update the pages table
  223.      */
  224.     $query  = "update pages set tx_templavoila_flex = '".$doc->getFlexml()."' where uid = ".$doc->getDocid();
  225.     $qres   = mysql_query($query);
  226.  
  227.     /*
  228.      * delete existing references from sys_refindex
  229.      * and write new references to sys_refindex.
  230.      */
  231.     $query  = "delete from sys_refindex where recuid = ".$doc->getDocid();
  232.     $qres   = mysql_query($query);
  233.  
  234.     $rows   = $doc->getRefindexRows();   
  235.     foreach ($rows as $row) {
  236.       $qres  = mysql_query("insert into sys_refindex values ($row)");
  237.     }
  238.  
  239.     /*
  240.      * delete existing references from tx_templavoila_elementreferences
  241.      * and write new references to tx_templavoila_elementreferences
  242.      */
  243.     $query  = "delete from tx_templavoila_elementreferences where pid = ".$doc->getDocid();
  244.      $qres = mysql_query($query);
  245.  
  246.     $rows   = $doc->getElementReferencesRows();
  247.     foreach ($rows as $row) {
  248.       $qres = mysql_query("insert into tx_templavoila_elementreferences values ($row)");
  249.     }
  250.  
  251.   }
  252.  
  253.  
  254.   /**
  255.    * The main function
  256.    */
  257.   public function main($docid) {
  258.  
  259.     /*
  260.      * Connect to the Database
  261.      */
  262.     $dbh = mysql_pconnect($this->db_host, $this->db_user, $this->db_pass) or die ("Could not connect to database");
  263.     $this->db_conn = mysql_select_db($this->db_schema, $dbh);
  264.  
  265.  
  266.     /*
  267.      * Get the language strings that are used in our CMS.
  268.      */
  269.     $this->cmslanguages = $this->getCMSLanguageStrings();
  270.  
  271.     /*
  272.      * Get the document structure.
  273.      */
  274.  
  275.     $doc = $this->getDocument($docid);
  276.  
  277.     /*
  278.      * Update the document structure in the database
  279.      */
  280.     $this->updateDocument($doc);
  281.  
  282.     /*
  283.      * Disconnect from the Database
  284.      */
  285.     mysql_close($dbh);
  286.  
  287.   }
  288. }
  289.  
  290.  
  291. // *******************************
  292. // Startup
  293. // *******************************
  294.  
  295. if ($argc != 8 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
  296. ?>
  297. This script migrates a document from pre-TemplaVoila to TemplaVoila.
  298.  
  299.   Usage:
  300.   <?php echo $argv[0]; ?> <db_host> <db_schema> <db_user> <db_pass> <hash_version> <container> <docid>
  301.  
  302.   <db_host>      is the hostname of your mysql cms database.
  303.   <db_schema>    is the database schema name for your cms database.
  304.   <db_user>      is the database username.
  305.   <db_pass>      is the database password.
  306.   <hash_version> is the hash version (use 1 or look into class.t3lib_refindex.php).
  307.   <container>    is the name of the templavoila container where your default content goes (e.g. "field_content").
  308.   <docid>        is the id of a document that you want to migrate.
  309.  
  310.   With the --help, -help, -h,
  311.   or -? options, you can get this help.
  312. <?php
  313. }
  314.  
  315. $obj = new MigrateContent($argv[1], $argv[2], $argv[3], $argv[4], $argv[5], $argv[6]);
  316. $obj->main($argv[7]);
  317. ?>
  318.  
 


13.11.2006, 20:10 Copyright © 2005 MN Soft Industry Software, 108, route de la Fin, CH-1874 Champéry
Tel.: +41 797 844554; Fax: +41 860 797 844554, Responsible: Matthias Nott., mn(at)mnsoft.org
Top of Page