migrate-news-joomla2dolibarr.php 7.2 KB

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