migrate-news-joomla2dolibarr.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. $excludeid = (empty($argv[5]) ? '' : $argv[5]);
  40. if (empty($argv[3]) || !in_array($argv[1], array('test', 'confirm')) || empty($websiteref)) {
  41. print '***** '.$script_file.' *****'."\n";
  42. print "Usage: $script_file (test|confirm) website login:pass@serverjoomla/tableprefix/databasejoomla [nbmaxrecord]\n";
  43. print "\n";
  44. print "Load joomla news and create them into Dolibarr database (if they don't alreay exist).\n";
  45. exit(-1);
  46. }
  47. require $path."../../htdocs/master.inc.php";
  48. include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
  49. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  50. include_once DOL_DOCUMENT_ROOT.'/core/lib/website2.lib.php';
  51. $langs->load('main');
  52. $joomlaserverinfoarray = preg_split('/(:|@|\/)/', $joomlaserverinfo);
  53. $joomlalogin = $joomlaserverinfoarray[0];
  54. $joomlapass = $joomlaserverinfoarray[1];
  55. $joomlahost = $joomlaserverinfoarray[2];
  56. $joomlaprefix = $joomlaserverinfoarray[3];
  57. $joomladatabase = $joomlaserverinfoarray[4];
  58. $joomlaport = 3306;
  59. $website = new Website($db);
  60. $result = $website->fetch(0, $websiteref);
  61. if ($result <= 0) {
  62. print 'Error, web site '.$websiteref.' not found'."\n";
  63. exit(-1);
  64. }
  65. $websiteid = $website->id;
  66. $importid = dol_print_date(dol_now(), 'dayhourlog');
  67. $dbjoomla = getDoliDBInstance('mysqli', $joomlahost, $joomlalogin, $joomlapass, $joomladatabase, $joomlaport);
  68. if ($dbjoomla->error)
  69. {
  70. dol_print_error($dbjoomla, "host=".$joomlahost.", port=".$joomlaport.", user=".$joomlalogin.", databasename=".$joomladatabase.", ".$dbjoomla->error);
  71. exit(-1);
  72. }
  73. $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';
  74. $sql .= ' LEFT JOIN '.$joomlaprefix.'_users as u ON u.id = c.created_by';
  75. $sql .= ' WHERE featured = 1';
  76. $sql .= ' AND c.id NOT IN ('.$excludeid.')';
  77. $sql .= ' ORDER BY publish_up ASC';
  78. $resql = $dbjoomla->query($sql);
  79. if (!$resql) {
  80. dol_print_error($dbjoomla);
  81. exit;
  82. }
  83. $blogpostheader = file_get_contents($path.'blogpost-header.txt');
  84. if ($blogpostheader === false) {
  85. print "Error: Failed to load file content of 'blogpost-header.txt'\n";
  86. exit(-1);
  87. }
  88. $blogpostfooter = file_get_contents($path.'blogpost-footer.txt');
  89. if ($blogpostfooter === false) {
  90. print "Error: Failed to load file content of 'blogpost-footer.txt'\n";
  91. exit(-1);
  92. }
  93. $db->begin();
  94. $i = 0; $nbimported = 0; $nbalreadyexists = 0;
  95. while ($obj = $dbjoomla->fetch_object($resql)) {
  96. if ($obj) {
  97. $i++;
  98. $id = $obj->id;
  99. $alias = $obj->alias;
  100. $title = $obj->title;
  101. //$description = dol_string_nohtmltag($obj->introtext);
  102. $description = trim(dol_trunc(dol_string_nohtmltag($obj->metadesc), 250));
  103. if (empty($description)) $description = trim(dol_trunc(dol_string_nohtmltag($obj->introtext), 250));
  104. $htmltext = "";
  105. if ($blogpostheader) $htmltext .= $blogpostheader."\n";
  106. $htmltext .= '<section id="mysectionnewsintro" contenteditable="true">'."\n";
  107. $htmltext .= $obj->introtext;
  108. $htmltext .= '</section>'."\n";
  109. if ($obj->fulltext) {
  110. $htmltext .= '<section id="mysectionnewscontent" contenteditable="true">'."\n";
  111. $htmltext .= '<br>'."\n".'<hr>'."\n".'<br>'."\n";
  112. $htmltext .= $obj->fulltext;
  113. $htmltext .= "</section>";
  114. }
  115. if ($blogpostfooter) $htmltext .= "\n".$blogpostfooter;
  116. $language = ($obj->language && $obj->language != '*' ? $obj->language : 'en');
  117. $keywords = $obj->metakey;
  118. $author_alias = $obj->username;
  119. $date_creation = $dbjoomla->jdate($obj->publish_up);
  120. print '#'.$i.' id='.$id.' '.$title.' lang='.$language.' keywords='.$keywords.' importid='.$importid."\n";
  121. $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)';
  122. $sqlinsert .= " VALUES(".$websiteid.", '".$db->escape($alias)."', '', '".$db->escape($title)."', '".$db->escape($description)."', '".$db->escape($keywords)."', ";
  123. $sqlinsert .= " '".$db->escape($htmltext)."', '1', 'blogpost', '".$db->escape($language)."', '".$db->escape($importid)."', '".$db->escape($image)."', '".$db->idate($date_creation)."', '".$db->escape($author_alias)."')";
  124. print $sqlinsert."\n";
  125. $result = $db->query($sqlinsert);
  126. if ($result <= 0) {
  127. print 'ERROR: '.$db->lasterrno.": ".$sqlinsert."\n";
  128. if ($db->lasterrno != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  129. $error++;
  130. } else {
  131. $nbalreadyexists++;
  132. }
  133. } else {
  134. $pageid = $db->last_insert_id(MAIN_DB_PREFIX.'website_page');
  135. if ($pageid > 0) { // We must also regenerate page on disk
  136. global $dolibarr_main_data_root;
  137. $pathofwebsite = $dolibarr_main_data_root.'/website/'.$websiteref;
  138. $filetpl = $pathofwebsite.'/page'.$pageid.'.tpl.php';
  139. $websitepage = new WebsitePage($db);
  140. $websitepage->fetch($pageid);
  141. dolSavePageContent($filetpl, $website, $websitepage);
  142. }
  143. print "Insert done - pageid = ".$pageid."\n";
  144. $nbimported++;
  145. }
  146. if ($max && $i >= $max) {
  147. print 'Nb max of record ('.$max.') reached. We stop now.'."\n";
  148. break;
  149. }
  150. }
  151. }
  152. if ($mode == 'confirm' && !$error) {
  153. print "Commit\n";
  154. print $nbalreadyexists." page(s) already exists.\n";
  155. print $nbimported." page(s) imported with importid=".$importid."\n";
  156. $db->commit();
  157. } else {
  158. print "Rollback (mode=test)\n";
  159. $db->rollback();
  160. }
  161. exit($error);