migrate-news-joomla2dolibarr.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file scripts/website/migrate_news_joomla2dolibarr.php
  20. * \ingroup scripts
  21. * \brief Migrate news from a Joomla databse into a Dolibarr website
  22. */
  23. $sapi_type = php_sapi_name();
  24. $script_file = basename(__FILE__);
  25. $path = __DIR__.'/';
  26. // Test if batch mode
  27. if (substr($sapi_type, 0, 3) == 'cgi') {
  28. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  29. exit(-1);
  30. }
  31. @set_time_limit(0); // No timeout for this script
  32. define('EVEN_IF_ONLY_LOGIN_ALLOWED', 1); // Set this define to 0 if you want to lock your script when dolibarr setup is "locked to admin user only".
  33. $error = 0;
  34. $mode = empty($argv[1])?'':$argv[1];
  35. $websiteref = empty($argv[2])?'':$argv[2];
  36. $joomlaserverinfo = empty($argv[3])?'':$argv[3];
  37. $image = 'image/__WEBSITE_KEY__/images/stories/dolibarr.png';
  38. $max = (!isset($argv[4]) || (empty($argv[4]) && $argv[4] !== '0'))?'10':$argv[4];
  39. if (empty($argv[3]) || !in_array($argv[1], array('test', 'confirm')) || empty($websiteref)) {
  40. print '***** '.$script_file.' *****'."\n";
  41. print "Usage: $script_file (test|confirm) website login:pass@serverjoomla/tableprefix/databasejoomla [nbmaxrecord]\n";
  42. print "\n";
  43. print "Load joomla news and create them into Dolibarr database (if they don't alreay exist).\n";
  44. exit(-1);
  45. }
  46. require $path."../../htdocs/master.inc.php";
  47. include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
  48. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  49. include_once DOL_DOCUMENT_ROOT.'/core/lib/website2.lib.php';
  50. $langs->load('main');
  51. $joomlaserverinfoarray = preg_split('/(:|@|\/)/', $joomlaserverinfo);
  52. $joomlalogin = $joomlaserverinfoarray[0];
  53. $joomlapass = $joomlaserverinfoarray[1];
  54. $joomlahost = $joomlaserverinfoarray[2];
  55. $joomlaprefix = $joomlaserverinfoarray[3];
  56. $joomladatabase = $joomlaserverinfoarray[4];
  57. $joomlaport = 3306;
  58. $website = new Website($db);
  59. $result = $website->fetch(0, $websiteref);
  60. if ($result <= 0) {
  61. print 'Error, web site '.$websiteref.' not found'."\n";
  62. exit(-1);
  63. }
  64. $websiteid = $website->id;
  65. $importid = dol_print_date(dol_now(), 'dayhourlog');
  66. $dbjoomla=getDoliDBInstance('mysqli', $joomlahost, $joomlalogin, $joomlapass, $joomladatabase, $joomlaport);
  67. if ($dbjoomla->error)
  68. {
  69. dol_print_error($dbjoomla, "host=".$joomlahost.", port=".$joomlaport.", user=".$joomlalogin.", databasename=".$joomladatabase.", ".$dbjoomla->error);
  70. exit(-1);
  71. }
  72. $sql = 'SELECT c.id, c.title, c.alias, c.created, c.introtext, `fulltext`, c.metadesc, c.metakey, c.language, c.created, c.publish_up, u.username FROM '.$joomlaprefix.'_content as c';
  73. $sql.= ' LEFT JOIN '.$joomlaprefix.'_users as u ON u.id = c.created_by';
  74. $sql.= ' WHERE featured = 1';
  75. $sql.= ' ORDER BY publish_up ASC';
  76. $resql = $dbjoomla->query($sql);
  77. if (! $resql) {
  78. dol_print_error($dbjoomla);
  79. exit;
  80. }
  81. $blogpostheader = file_get_contents($path.'blogpost-header.txt');
  82. if ($blogpostheader === false) {
  83. print "Error: Failed to load file content of 'blogpost-header.txt'\n";
  84. exit(-1);
  85. }
  86. $blogpostfooter = file_get_contents($path.'blogpost-footer.txt');
  87. if ($blogpostfooter === false) {
  88. print "Error: Failed to load file content of 'blogpost-footer.txt'\n";
  89. exit(-1);
  90. }
  91. $db->begin();
  92. $i = 0; $nbimported = 0; $nbalreadyexists = 0;
  93. while ($obj = $dbjoomla->fetch_object($resql)) {
  94. if ($obj) {
  95. $i++;
  96. $id = $obj->id;
  97. $alias = $obj->alias;
  98. $title = $obj->title;
  99. //$description = dol_string_nohtmltag($obj->introtext);
  100. $description = trim(dol_trunc(dol_string_nohtmltag($obj->metadesc), 250));
  101. if (empty($description)) $description = trim(dol_trunc(dol_string_nohtmltag($obj->introtext), 250));
  102. $htmltext = "";
  103. if ($blogpostheader) $htmltext .= $blogpostheader."\n";
  104. $htmltext .= '<section id="mysectionnewsintro" contenteditable="true">'."\n";
  105. $htmltext .= $obj->introtext;
  106. $htmltext .= '</section>'."\n";
  107. if ($obj->fulltext) {
  108. $htmltext .= '<section id="mysectionnewscontent" contenteditable="true">'."\n";
  109. $htmltext .= '<br>'."\n".'<hr>'."\n".'<br>'."\n";
  110. $htmltext .= $obj->fulltext;
  111. $htmltext .= "</section>";
  112. }
  113. if ($blogpostfooter) $htmltext .= "\n".$blogpostfooter;
  114. $language = ($obj->language && $obj->language != '*' ? $obj->language : 'en');
  115. $keywords = $obj->metakey;
  116. $author_alias = $obj->username;
  117. $date_creation = $dbjoomla->jdate($obj->publish_up);
  118. print '#'.$i.' id='.$id.' '.$title.' lang='.$language.' keywords='.$keywords.' importid='.$importid."\n";
  119. $sqlinsert = 'INSERT INTO '.MAIN_DB_PREFIX.'website_page(fk_website, pageurl, aliasalt, title, description, keywords, content, status, type_container, lang, import_key, image, date_creation, author_alias)';
  120. $sqlinsert .= " VALUES(".$websiteid.", '".$db->escape($alias)."', '', '".$db->escape($title)."', '".$db->escape($description)."', '".$db->escape($keywords)."', ";
  121. $sqlinsert .= " '".$db->escape($htmltext)."', '1', 'blogpost', '".$db->escape($language)."', '".$db->escape($importid)."', '".$db->escape($image)."', '".$db->idate($date_creation)."', '".$db->escape($author_alias)."')";
  122. print $sqlinsert."\n";
  123. $result = $db->query($sqlinsert);
  124. if ($result <= 0) {
  125. print 'ERROR: '.$db->lasterrno.": ".$sqlinsert."\n";
  126. if ($db->lasterrno != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  127. $error++;
  128. } else {
  129. $nbalreadyexists++;
  130. }
  131. } else {
  132. $pageid = $db->last_insert_id(MAIN_DB_PREFIX.'website_page');
  133. if ($pageid > 0) { // We must also regenerate page on disk
  134. global $dolibarr_main_data_root;
  135. $pathofwebsite = $dolibarr_main_data_root.'/website/'.$websiteref;
  136. $filetpl = $pathofwebsite.'/page'.$pageid.'.tpl.php';
  137. $websitepage = new WebsitePage($db);
  138. $websitepage->fetch($pageid);
  139. dolSavePageContent($filetpl, $website, $websitepage);
  140. }
  141. print "Insert done - pageid = ".$pageid."\n";
  142. $nbimported++;
  143. }
  144. if ($max && $i >= $max) {
  145. print 'Nb max of record ('.$max.') reached. We stop now.'."\n";
  146. break;
  147. }
  148. }
  149. }
  150. if ($mode == 'confirm' && ! $error) {
  151. print "Commit\n";
  152. print $nbalreadyexists." page(s) already exists.\n";
  153. print $nbimported." page(s) imported with importid=".$importid."\n";
  154. $db->commit();
  155. } else {
  156. print "Rollback\n";
  157. $db->rollback();
  158. }
  159. exit($error);