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