25if (!defined(
'NOSESSION')) {
26 define(
'NOSESSION',
'1');
29$sapi_type = php_sapi_name();
30$script_file = basename(__FILE__);
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";
40define(
'EVEN_IF_ONLY_LOGIN_ALLOWED', 1);
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';
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]);
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";
57 print
"Load joomla news and create them into Dolibarr database (if they don't alreay exist).\n";
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';
73if (!empty($dolibarr_main_db_readonly)) {
74 print
"Error: instance in read-onyl mode\n";
78$joomlaserverinfoarray = preg_split(
'/(:|@|\/)/', $joomlaserverinfo);
79$joomlalogin = $joomlaserverinfoarray[0];
80$joomlapass = $joomlaserverinfoarray[1];
81$joomlahost = $joomlaserverinfoarray[2];
82$joomlaprefix = $joomlaserverinfoarray[3];
83$joomladatabase = $joomlaserverinfoarray[4];
88$result = $website->fetch(0, $websiteref);
90 print
'Error, web site '.$websiteref.
' not found'.
"\n";
93$websiteid = $website->id;
96$dbjoomla =
getDoliDBInstance(
'mysqli', $joomlahost, $joomlalogin, $joomlapass, $joomladatabase, $joomlaport);
97if ($dbjoomla->error) {
98 dol_print_error($dbjoomla,
"host=".$joomlahost.
", port=".$joomlaport.
", user=".$joomlalogin.
", databasename=".$joomladatabase.
", ".$dbjoomla->error);
102$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';
103$sql .=
' LEFT JOIN '.$joomlaprefix.
'_users as u ON u.id = c.created_by';
104$sql .=
' WHERE featured = 1';
105$sql .=
' AND c.id NOT IN ('.$excludeid.
')';
106$sql .=
' ORDER BY publish_up ASC';
107$resql = $dbjoomla->query($sql);
114$blogpostheader = file_get_contents($path.
'blogpost-header.txt');
115if ($blogpostheader ===
false) {
116 print
"Error: Failed to load file content of 'blogpost-header.txt'\n";
119$blogpostfooter = file_get_contents($path.
'blogpost-footer.txt');
120if ($blogpostfooter ===
false) {
121 print
"Error: Failed to load file content of 'blogpost-footer.txt'\n";
129$i = 0; $nbimported = 0; $nbalreadyexists = 0;
130while ($obj = $dbjoomla->fetch_object($resql)) {
134 $alias = $obj->alias;
135 $title = $obj->title;
138 if (empty($description)) {
143 if ($blogpostheader) {
144 $htmltext .= $blogpostheader.
"\n";
146 $htmltext .=
'<section id="mysectionnewsintro" contenteditable="true">'.
"\n";
147 $htmltext .= $obj->introtext;
148 $htmltext .=
'</section>'.
"\n";
149 if ($obj->fulltext) {
150 $htmltext .=
'<section id="mysectionnewscontent" contenteditable="true">'.
"\n";
151 $htmltext .=
'<br>'.
"\n".
'<hr>'.
"\n".
'<br>'.
"\n";
152 $htmltext .= $obj->fulltext;
153 $htmltext .=
"</section>";
155 if ($blogpostfooter) {
156 $htmltext .=
"\n".$blogpostfooter;
159 $language = ($forcelang ? $forcelang : ($obj->language && $obj->language !=
'*' ? $obj->language :
'en'));
160 $keywords = $obj->metakey;
161 $author_alias = $obj->username;
163 $date_creation = $dbjoomla->jdate($obj->publish_up);
165 print
'#'.$i.
' id='.$id.
' '.$title.
' lang='.$language.
' keywords='.$keywords.
' importid='.$importid.
"\n";
167 $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)';
168 $sqlinsert .=
" VALUES(".$websiteid.
", '".$db->escape($alias).
"', '', '".$db->escape($title).
"', '".$db->escape($description).
"', '".$db->escape($keywords).
"', ";
169 $sqlinsert .=
" '".$db->escape($htmltext).
"', '1', 'blogpost', '".$db->escape($language).
"', '".$db->escape($importid).
"', '".$db->escape($image).
"', '".$db->idate($date_creation).
"', '".$db->escape($author_alias).
"')";
170 print $sqlinsert.
"\n";
172 $result = $db->query($sqlinsert);
174 print
'ERROR: '.$db->lasterrno.
": ".$sqlinsert.
"\n";
175 if ($db->lasterrno !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
181 $pageid = $db->last_insert_id(MAIN_DB_PREFIX.
'website_page');
184 global $dolibarr_main_data_root;
185 $pathofwebsite = $dolibarr_main_data_root.
'/website/'.$websiteref;
186 $filetpl = $pathofwebsite.
'/page'.$pageid.
'.tpl.php';
188 $websitepage->fetch($pageid);
192 print
"Insert done - pageid = ".$pageid.
"\n";
196 if ($max && $i >= $max) {
197 print
'Nb max of record ('.$max.
') reached. We stop now.'.
"\n";
203if ($mode ==
'confirm' && !$error) {
205 print $nbalreadyexists.
" page(s) already exists.\n";
206 print $nbimported.
" page(s) imported with importid=".$importid.
"\n";
209 print
"Rollback (mode=test)\n";
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
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).