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