<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Giacomo&#039;s</title>
	<atom:link href="http://giacomo.graziosi.info/feed/" rel="self" type="application/rss+xml" />
	<link>http://giacomo.graziosi.info</link>
	<description>that&#039;s my spot!</description>
	<lastBuildDate>Mon, 13 Feb 2012 15:14:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>NAS with Ruby, NILFS2 and Samba</title>
		<link>http://giacomo.graziosi.info/2011/02/07/nas-with-ruby-nilfs2-and-samba/</link>
		<comments>http://giacomo.graziosi.info/2011/02/07/nas-with-ruby-nilfs2-and-samba/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 21:10:31 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[nas]]></category>
		<category><![CDATA[nilfs]]></category>
		<category><![CDATA[nilfs2]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[samba]]></category>

		<guid isPermaLink="false">http://giacomo.graziosi.info/?p=36</guid>
		<description><![CDATA[Here comes a simple script I wrote to setup a couple of file servers using Linux and NILFS2 with daily incremental snapshots, sharing of the snapshots via Samba and optional sync on an external NTFS hard disk on USB: nilfs2_nas_0.1_alpha. As usual be extremely careful with this code as it did not receive proper testing and should [...]]]></description>
			<content:encoded><![CDATA[<p>Here comes a simple script I wrote to setup a couple of file servers using Linux and NILFS2 with daily incremental snapshots, sharing of the snapshots via Samba and optional sync on an external NTFS hard disk on USB: <a href="http://giacomo.graziosi.info/files/2011/02/nilfs2_nas_0.1_alpha.zip">nilfs2_nas_0.1_alpha</a>.</p>
<p>As usual be extremely careful with this code as it did not receive proper testing and should be seen as a starting point for your own setups rather than a ready to use solution.</p>
<p>Take a look on the class I wrote to run NILFS2 commands, it isn&#8217;t exactly fail proof <img src='http://giacomo.graziosi.info/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> NILFS2
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>device<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">IOError</span>, <span style="color:#996600;">&quot;can't find device file&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>device<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@device</span> = device
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> get_checkpoints<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        t = <span style="color:#996600;">`lscp #{@device}`</span>
        r = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
        t.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>l<span style="color:#006600; font-weight:bold;">|</span>
            a = l.<span style="color:#CC0066; font-weight:bold;">split</span>
            time = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{a[1]} #{a[2]}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
            r.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:CNO =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:MODE</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:FLG</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
            <span style="color:#ff3333; font-weight:bold;">:NBLKINC</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:ICNT</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; a<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:time</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; time<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> r
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> get_snapshots<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        r = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
        get_checkpoints.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> r.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> c<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:MODE</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">&quot;ss&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> r
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> ss_to_mount<span style="color:#006600; font-weight:bold;">&#40;</span>snapshot, path<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:dev =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#0066ff; font-weight:bold;">@device</span>, <span style="color:#ff3333; font-weight:bold;">:path</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; path, <span style="color:#ff3333; font-weight:bold;">:fs</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;nilfs2&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:opts</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;ro&quot;</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;nil, <span style="color:#996600;">&quot;cp&quot;</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; snapshot<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:CNO</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> make_checkpoint<span style="color:#006600; font-weight:bold;">&#40;</span>snapshot=<span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        snapshot ? <span style="color:#996600;">`mkcp -s #{@device}`</span> : <span style="color:#996600;">`mkcp #{@device}`</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> make_snapshot<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        make_checkpoint<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> remove_checkpoint<span style="color:#006600; font-weight:bold;">&#40;</span>checkpoint<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#996600;">`chcp cp #{@device} #{checkpoint[:CNO]}`</span> <span style="color:#9966CC; font-weight:bold;">if</span> checkpoint<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:MODE</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">&quot;ss&quot;</span>
        <span style="color:#996600;">`rmcp #{@device} #{checkpoint[:CNO]}`</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> get_total_space<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#996600;">`df -Pk #{@device} |grep ^/ | awk '{print $2;}'`</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1024</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> get_used_space<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#996600;">`df -Pk #{@device} |grep ^/ | awk '{print $3;}'`</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1024</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> get_free_space<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#996600;">`df -Pk #{@device} |grep ^/ | awk '{print $4;}'`</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1024</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> get_free_space_percent<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        get_free_space<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">/</span>get_total_space<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://giacomo.graziosi.info/2011/02/07/nas-with-ruby-nilfs2-and-samba/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WoW</title>
		<link>http://giacomo.graziosi.info/2011/02/06/wow/</link>
		<comments>http://giacomo.graziosi.info/2011/02/06/wow/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 11:00:52 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[wtf]]></category>

		<guid isPermaLink="false">http://giacomo.graziosi.info/?p=5</guid>
		<description><![CDATA[&#8230;is no longer.]]></description>
			<content:encoded><![CDATA[<p>&#8230;is no longer.</p>
]]></content:encoded>
			<wfw:commentRss>http://giacomo.graziosi.info/2011/02/06/wow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Throwing some sources</title>
		<link>http://giacomo.graziosi.info/2008/10/11/throwing-some-sources/</link>
		<comments>http://giacomo.graziosi.info/2008/10/11/throwing-some-sources/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 11:06:38 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[file system]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[user space]]></category>

		<guid isPermaLink="false">http://giacomo.graziosi.info/?p=10</guid>
		<description><![CDATA[I just made public under the terms of the GPLv3 a couple of old university projects hoping they will help someone. Here they are: ufs (micro file system, a fat-alike user space file system with some unix extensions) and Vespasiano (small movie theater manager, done when learning sockets in Java). Please note that they both are so bugged to [...]]]></description>
			<content:encoded><![CDATA[<p>I just made public under the terms of the GPLv3 a couple of old university projects hoping they will help someone. Here they are: <a href="http://www.assembla.com/wiki/show/ufs" target="_blank">ufs</a> (micro file system, a fat-alike user space file system with some unix extensions) and <a href="http://www.assembla.com/wiki/show/vespasiano" target="_blank">Vespasiano</a> (small movie theater manager, done when learning sockets in Java).</p>
<p>Please note that they both are <strong>so bugged to be broken</strong>. Be careful <img src='http://giacomo.graziosi.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://giacomo.graziosi.info/2008/10/11/throwing-some-sources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cerberus Project</title>
		<link>http://giacomo.graziosi.info/2008/10/01/cerberus-project/</link>
		<comments>http://giacomo.graziosi.info/2008/10/01/cerberus-project/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 19:55:32 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[cctv]]></category>
		<category><![CDATA[cerberus]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[motion]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[surveillance]]></category>
		<category><![CDATA[video surveillance]]></category>
		<category><![CDATA[vigilante]]></category>

		<guid isPermaLink="false">http://giacomo.graziosi.info/?p=29</guid>
		<description><![CDATA[What is this cool drooling puppy doing on my blog? His name is Cerberus, he came from Greek mithology and he is nothing less than the guardian of the gate to Hades, also known as Hell. No, I haven’t joined satanism, this is just a mascotte, the mascotte for a new project I’ve been thinking [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-30" src="http://giacomo.graziosi.info/files/2011/02/cerberus.png" alt="" width="560" height="358" /><br />
What is this cool drooling puppy doing on my blog? His name is Cerberus, he came from Greek mithology and he is nothing less than the guardian of the gate to Hades, also known as Hell.</p>
<p>No, I haven’t joined satanism, this is just a mascotte, the mascotte for a new project I’ve been thinking on lately: a Debian based Linux distribution specialized on video surveillace, its name will obviously be Cerberus <img src='http://giacomo.graziosi.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>Why am I building another Linux distribution? Well, the answer is very simple: in the last two years I’ve been selling video surveillance systems build on top of Debian or Ubuntu with Motion and Vigilante. Assembling such systems requires to repeat each time a set of very specific steps such as installing the distribution with the needed packages, configuring the modprobe options for driver of the video capture card (often based on the bt878 chip), setup Motion and Vigilante, linking it all together on the user’s desktop. Most of these actions (excluding hardware probing/configuration) could be automatized, or better, saved prebuild in a proper environment.<br />
This is where Cerberus Linux enters the game: it is meant to act like firmwares on embedded Linux devices, a main read-only partition containing the distribution with a small overlay read-write (freezable to read-only) partition containing the configuration files.<br />
Why am I following the “embedded-firmware”-like way in place of a more common standard installation? First reason is robustness: making the system read-only prevents damage to the file system when doing hard resets (which are very common on this kind of systems, often seen as vhs recorders or table dvd players by the inexperienced users who will use them). Second reason is easy of use: you can (re)install and update the base system by rewriting the partition without caring about backups or installation process, is it just a call to dd, automatizable. Third reason: users are idiots, give them a writeable home and they will find a way to mess up the entire system, give them a writeable root file system and they will bring back the system as an ash heap claiming you sold them a broken thing.</p>
<p>So the roadmap so far is to build the distribution with the listed features, put a new Vigilante (based on Cluttermm/Gstreamermm) on it and maybe develop a remote web configuration system (maybe with ExtJS and Rails or Zend Framework or Webtoolkit).</p>
<p>Of course if you have any advice or proposal for Cerberus please contact me, any good idea will be welcomed.</p>
]]></content:encoded>
			<wfw:commentRss>http://giacomo.graziosi.info/2008/10/01/cerberus-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLite3 support for Motion</title>
		<link>http://giacomo.graziosi.info/2008/09/30/sqlite3-support-for-motion/</link>
		<comments>http://giacomo.graziosi.info/2008/09/30/sqlite3-support-for-motion/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 14:49:17 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[motion]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[sqlite]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://giacomo.graziosi.info/?p=13</guid>
		<description><![CDATA[﻿One of the first problem occurred when developing the specialized video surveillance Linux distribution I’ve been working on these days is the database engine to be used to archive the list of recording files saved by Motion. As every write operation will be placed on a read-write file system layer on top of the main [...]]]></description>
			<content:encoded><![CDATA[<p>﻿One of the first problem occurred when developing the specialized video surveillance Linux distribution I’ve been working on these days is the database engine to be used to archive the list of recording files saved by Motion. As every write operation will be placed on a read-write file system layer on top of the main read-only layer (containing the whole “monolithic” distribution, something like the firmware on an embedded device) I have the need to store the whole database data in a compact manner, something like only one file and not similar to the /var/lib/mysql/ directory with its many entries.<br />
The problem with this is that Motion only supported MySQL and PostgreSQL so I had to add the SQLite layer by myself. As a result I have published a <a href="http://www.lavrsen.dk/twiki/bin/view/Motion/SQLite3Patch" target="_blank">patch on the Motion’s wiki</a>, test it please <img src='http://giacomo.graziosi.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://giacomo.graziosi.info/2008/09/30/sqlite3-support-for-motion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What I don’t like in Ruby</title>
		<link>http://giacomo.graziosi.info/2008/04/07/what-i-don%e2%80%99t-like-in-ruby/</link>
		<comments>http://giacomo.graziosi.info/2008/04/07/what-i-don%e2%80%99t-like-in-ruby/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 11:02:34 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[gil]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://giacomo.graziosi.info/?p=7</guid>
		<description><![CDATA[I’m beginning to learn the well hyped Ruby language, in many ways it looks useful, elegant and pleasant, still it has something that, at least at a first glance, looks odd to me. I will use this post to take track of those odd things that I will meet when studying the language. So, here [...]]]></description>
			<content:encoded><![CDATA[<p>I’m beginning to learn the well hyped Ruby language, in many ways it looks useful, elegant and pleasant, still it has something that, at least at a first glance, looks odd to me. I will use this post to take track of those odd things that I will meet when studying the language.</p>
<p>So, here we go:</p>
<p><strong>1) Ends</strong>, take a look on this snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> !r.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#9966CC; font-weight:bold;">then</span>
  rr = r.<span style="color:#9900CC;">right</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> !rr.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#9966CC; font-weight:bold;">then</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>r.<span style="color:#9900CC;">color</span> == <span style="color:#6666ff; font-weight:bold;">RedBlackTree::RED</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#9966CC; font-weight:bold;">and</span><span style="color:#006600; font-weight:bold;">&#40;</span>rr.<span style="color:#9900CC;">color</span> == <span style="color:#6666ff; font-weight:bold;">RedBlackTree::RED</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">then</span>
      x = rot_left<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      copy<span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> x.<span style="color:#0000FF; font-weight:bold;">nil</span>?
      <span style="color:#0066ff; font-weight:bold;">@color</span> = <span style="color:#6666ff; font-weight:bold;">RedBlackTree::BLACK</span>
      l = <span style="color:#0066ff; font-weight:bold;">@left</span>
      l.<span style="color:#9900CC;">color</span> = <span style="color:#6666ff; font-weight:bold;">RedBlackTree::RED</span> <span style="color:#9966CC; font-weight:bold;">unless</span> l.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>and here comes the Python equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>right <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    rr = right.__right
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>rr <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>right.__color == RedBlackTree.<span style="color: black;">red</span><span style="color: black;">&#41;</span><span style="color: #ff7700;font-weight:bold;">and</span><span style="color: black;">&#40;</span>rr.__color == RedBlackTree.<span style="color: black;">red</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            x = <span style="color: #008000;">self</span>.__rotLeft<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.__copy<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.__color = RedBlackTree.<span style="color: black;">black</span>
            left = <span style="color: #008000;">self</span>.__left
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>left <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
                left.__color = RedBlackTree.<span style="color: black;">red</span></pre></div></div>

<p>The Python way looks just the best way in my opinion. What’s the reason for the ends when you’ll indent the code anyway (and if you don’t then you deserve nothing else than suffering and pain)? Even the <strong>{</strong> C/C++/Java approach <strong>}</strong> appears way better or at least less intrusive.</p>
<p><strong>2) “Fuzzy” expressions terminators.</strong> Try to compile and execute the following C++ code:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &amp;lt;iostream&amp;gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Foo <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">void</span> bar<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">int</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">int</span> y <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> x
        <span style="color: #000040;">+</span>y<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    Foo <span style="color: #000040;">*</span>f <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Foo<span style="color: #008080;">;</span>
    f<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>bar<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">delete</span><span style="color: #008000;">&#40;</span>f<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>What do you expect it to print? Right, it will correctly print <em>5</em>. Now Ruby’s turn:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> bar
    x,y = <span style="color:#006666;">3</span>,<span style="color:#006666;">2</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> x
    <span style="color:#006600; font-weight:bold;">+</span>y
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
f = Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
f.<span style="color:#9900CC;">bar</span></pre></div></div>

<p>Now what output do you expect? The answer is <em>3</em>. This is because Ruby doesn’t use semicolons as C, C++, Java, PHP and many other languages do to terminate statements. It is still the better way to go, semicolons are redundants useless things almost everytime, still you have to pay attention: note that the above Ruby code isn’t wrong so the interpreter will not warn you, it will just print <em>3</em> instead of <em>5</em> and the method <em>bar</em> will return the value<em>2</em> (remember that in Ruby the last expression in a method or function is the actual return value of it so if you replace <em>f.bar</em> with <em>puts f.bar</em> it will print the “missing” <em>2</em>).</p>
<p><strong>3) No method overloading.</strong> The following code is self-explanatory:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
    <span style="color:#9966CC; font-weight:bold;">def</span> bar<span style="color:#006600; font-weight:bold;">&#40;</span>a, b<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span> b
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> bar<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">puts</span> a
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
gh = Foo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
gh.<span style="color:#9900CC;">bar</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;hello&quot;</span>, <span style="color:#996600;">&quot;world&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>if you try to execute it then you will get something like this:</p>
<blockquote><p>asd.rb:12:in `bar’: wrong number of arguments (2 for 1) (ArgumentError)<br />
from asd.rb:12</p></blockquote>
<p><strong>4) GIL (global interpreter lock).</strong> To me it’s nothing else than an alternative reading for “fake threading”. If you are on a multicore/multicpu system try to execute this snippet while monitoring the cpu activity, it will start 8 threads and compute fibonacci(40) on each thread:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Fibonacci
    <span style="color:#9966CC; font-weight:bold;">def</span> fib<span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>i <span style="color:#006600; font-weight:bold;">&amp;</span>lt;= <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
            <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
            <span style="color:#0000FF; font-weight:bold;">return</span> fib<span style="color:#006600; font-weight:bold;">&#40;</span>i <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> fib<span style="color:#006600; font-weight:bold;">&#40;</span>i <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
threads = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006666;">8</span>.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span> threads<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span>Fibonacci.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">fib</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">30</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
threads.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span> t.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>If you come from C/C++ or Java you may expect this code to use up to 8 cores/cpus on your system at the same time… wrong! It will execute the code using only one core/cpu at once because the GIL prevents the threads from taking control of the interpreter at the same time. Cool, isn’t it?</p>
<p><strong>5) Method invocation hooking.</strong> The following is the best way I found to intercept whenever a method is called:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> bar1<span style="color:#006600; font-weight:bold;">&#40;</span>gh1<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;bar1 #{gh1}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Foo
  <span style="color:#9966CC; font-weight:bold;">def</span> bar1_hook<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;bar1 hooked!&quot;</span>
    bar1_asd<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  alias_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bar1_asd</span>, <span style="color:#ff3333; font-weight:bold;">:bar1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  alias_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bar1</span>, <span style="color:#ff3333; font-weight:bold;">:bar1_hook</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
a = Foo.<span style="color:#9900CC;">new</span>
a.<span style="color:#9900CC;">bar1</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foobar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>So <em>bar1</em> is the method to be hooked: you have to monkey patch the <em>Foo</em> class defining the hooking method and hack it all together with alias_method. Doing this you are ripping the original class apart: if someone will edit the<em>bar1</em> method after your hook he/she will actually be editing the <em>bar1_hook</em> method without even noticing it (well, he will notice the unexpected behaviour and maybe try to debug his/her own code… have fun <img src='http://giacomo.graziosi.info/wp-includes/images/smilies/icon_neutral.gif' alt=':-|' class='wp-smiley' /> ), I really hope to be wrong about this point…</p>
]]></content:encoded>
			<wfw:commentRss>http://giacomo.graziosi.info/2008/04/07/what-i-don%e2%80%99t-like-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML importer from Danea Easyfatt to Cubecart</title>
		<link>http://giacomo.graziosi.info/2007/07/30/xml-importer-from-danea-easyfatt-to-cubecart/</link>
		<comments>http://giacomo.graziosi.info/2007/07/30/xml-importer-from-danea-easyfatt-to-cubecart/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 14:56:58 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[cubecart]]></category>
		<category><![CDATA[danea]]></category>
		<category><![CDATA[danea easyfatt]]></category>
		<category><![CDATA[easyfatt]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://giacomo.graziosi.info/?p=16</guid>
		<description><![CDATA[This is ugly, bugged, wrote in PHP (I’m not a PHP programmer) and will probably send death threats emails to all of your customers (really, I didn’t have the time to test it). If you still want it, click to download or just read it online: 1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<p>This is ugly, bugged, wrote in PHP (I’m not a PHP programmer) and will probably send death threats emails to all of your customers (really, I didn’t have the time to test it).<br />
If you still want it, <a href="../files/2007/07/cube_importer-01.zip">click to download</a> or just read it online:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * ----------------------------------------------------------------------------
 * 
 *  Copyright (C) 2007 Giacomo Graziosi (g.graziosi@gmail.com)
 * 
 * ----------------------------------------------------------------------------
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 *
 * ----------------------------------------------------------------------------
**/</span>
&nbsp;
&nbsp;
<span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Ugly importer interface abstraction</span>
<span style="color: #000000; font-weight: bold;">class</span> Outputter
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$file_handle</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> write<span style="color: #009900;">&#40;</span><span style="color: #000088;">$buffer</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file_handle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>##################################<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Importer implementation for the Cubecart</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Importer
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$logger</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$xml_products</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$import_str</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$op</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logger</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$op</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysql:host='</span><span style="color: #339933;">.</span><span style="color: #000088;">$host</span><span style="color: #339933;">.</span><span style="color: #0000ff;">';dbname='</span><span style="color: #339933;">.</span><span style="color: #000088;">$dbname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//$xml = simplexml_load_file($xml_file);</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">import_data</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$import_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update_and_delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insert_new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//print_r($this-&gt;cats);</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> query_has_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;cat_id&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//echo &quot;$sql_query\n&quot;;</span>
        <span style="color: #666666; font-style: italic;">//$s = $this-&gt;dbh-&gt;query($sql_query);</span>
        <span style="color: #666666; font-style: italic;">//if ($row = $s-&gt;fetch())</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_query</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$str_id</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    abstract <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> import_data<span style="color: #009900;">&#40;</span><span style="color: #000088;">$import_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    abstract <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> update_and_delete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    abstract <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> insert_new<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CubeImporter <span style="color: #000000; font-weight: bold;">extends</span> Importer
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$cats</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> import_data<span style="color: #009900;">&#40;</span><span style="color: #000088;">$import_str</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$import_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_products</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Products</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Product</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> update_and_delete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT * from CubeCart_inventory'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_products</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$product</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'productId'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">InternalID</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//print($row['productId'].&quot; update\n&quot;);</span>
                    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$product</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">continue</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">//print($row['productId'].&quot; delete\n&quot;);</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'productId'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> insert_new<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml_products</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$product</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT * from CubeCart_inventory'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'productId'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">InternalID</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">continue</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$product</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> add_cat<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cat_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cat_fat_id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO CubeCart_category (cat_name, cat_father_id)&quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;values ('<span style="color: #006699; font-weight: bold;">$cat_name</span>', <span style="color: #006699; font-weight: bold;">$cat_fat_id</span>)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//echo &quot;ho inserito &quot;.$this-&gt;dbh-&gt;lastInsertId().&quot;\n&quot;;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> check_categories<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//needs some refactoring</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Subcategory&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_search</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subcategory</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cats</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_search</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cats</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_has_row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM CubeCart_category WHERE cat_name = '<span style="color: #006699; font-weight: bold;">$xml_product-&gt;Category</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">//inserire cat</span>
                        <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_cat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #666666; font-style: italic;">//$this-&gt;cats[&quot;$xml_product-&gt;Category&quot;] = $a;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #666666; font-style: italic;">//$a deve contenere id di cat</span>
                <span style="color: #000088;">$fa</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_has_row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM CubeCart_category&quot;</span>
                <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; WHERE cat_name = '<span style="color: #006699; font-weight: bold;">$xml_product-&gt;Subcategory</span>' AND cat_father_id = <span style="color: #006699; font-weight: bold;">$fa</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_cat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subcategory</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fa</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #666666; font-style: italic;">//$this-&gt;cats[&quot;$xml_product-&gt;Subcategory&quot;] = $a;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//If Subcategory doesn't exist then there must be at least a Category</span>
            <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_search</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cats</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_has_row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM CubeCart_category WHERE cat_name = '<span style="color: #006699; font-weight: bold;">$xml_product-&gt;Category</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_cat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Category</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #666666; font-style: italic;">//$this-&gt;cats[&quot;$xml_product-&gt;Category&quot;] = $a;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> add_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$cat_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check_categories</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$tax_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_has_row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM CubeCart_taxes WHERE taxName = &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'IVA'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
        <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO CubeCart_inventory (productID, productCode,&quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;price, name, cat_id, sale_price, stock_level, taxType)&quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; VALUES (:productID, :productCode, :price, :name, :cat_id, &quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;:sale_price, :stock_level, :taxType)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':productID'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">InternalID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':productCode'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':price'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GrossPrice3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':cat_id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cat_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':sale_price'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GrossPrice3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':stock_level'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AvailableQty</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':taxType'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tax_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO CubeCart_cats_idx (cat_id, productId)&quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; VALUES (<span style="color: #006699; font-weight: bold;">$cat_id</span>, <span style="color: #006699; font-weight: bold;">$xml_product-&gt;InternalID</span>)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE CubeCart_category SET noProducts = noProducts + 1 &quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;WHERE cat_id = <span style="color: #006699; font-weight: bold;">$cat_id</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> update_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$cat_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check_categories</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_product</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$tax_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_has_row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM CubeCart_taxes WHERE taxName = &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'IVA'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
        <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE CubeCart_inventory SET productCode = :productCode, price = :price, name = :name, cat_id = :cat_id, &quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;sale_price = :sale_price, stock_level = :stock_level, taxType = :taxType WHERE productID = :productID&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':productID'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">InternalID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':productCode'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':price'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GrossPrice3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':cat_id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cat_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':sale_price'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GrossPrice3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':stock_level'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml_product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AvailableQty</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':taxType'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tax_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logger</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> delete_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//echo(&quot;$xml_product-&gt;Description\n&quot;);</span>
        <span style="color: #000088;">$cat_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query_has_row</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT cat_id FROM CubeCart_inventory WHERE productId = <span style="color: #006699; font-weight: bold;">$id</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;cat_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM CubeCart_inventory WHERE productId = <span style="color: #006699; font-weight: bold;">$id</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE CubeCart_category SET noProducts = noProducts - 1 &quot;</span>
        <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;WHERE productId = <span style="color: #006699; font-weight: bold;">$cat_id</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$op</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Outputter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/path/to/log/asd.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;file.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$xmlstr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$op</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlstr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ci</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CubeImporter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlstr</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'database_name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$op</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$op</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$op</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;OK&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://giacomo.graziosi.info/2007/07/30/xml-importer-from-danea-easyfatt-to-cubecart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

