<?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>Ciarpame {.com} &#187; Tricks</title>
	<atom:link href="http://www.ciarpame.com/category/tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ciarpame.com</link>
	<description>Useful stuffs for the masses. Useless things for few.</description>
	<lastBuildDate>Tue, 01 Dec 2009 13:03:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>SSHKeychain: errore &#8220;The following install step failed: run postinstall script for SSHKeychain&#8221;</title>
		<link>http://www.ciarpame.com/2009/05/29/sshkeychain-errore-the-following-install-step-failed-run-postinstall-script-for-sshkeychain/</link>
		<comments>http://www.ciarpame.com/2009/05/29/sshkeychain-errore-the-following-install-step-failed-run-postinstall-script-for-sshkeychain/#comments</comments>
		<pubDate>Fri, 29 May 2009 10:15:35 +0000</pubDate>
		<dc:creator>Giovanni Savastano</dc:creator>
				<category><![CDATA[Linux&Co.]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Usefulware]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=625</guid>
		<description><![CDATA[Se volete installare SSHKeychain 0.8.2 su Mac e viene visualizzato l&#8217;errore &#8220;The following install step failed: run postinstall script for SSHKeychain&#8221; c&#8217;è un modo molto semplice per risolvere il problema. Copiate il pkg dell&#8217;installazione sul Desktop (o nel percorso che preferite) Fate tasto destro sul pkg-&#62;mostra contenuto pacchetto Entrate nella directory Contents -&#62; Resources Editate [...]]]></description>
			<content:encoded><![CDATA[<p>Se volete installare SSHKeychain 0.8.2 su Mac e viene visualizzato l&#8217;errore &#8220;The following install step failed: run postinstall script for SSHKeychain&#8221; c&#8217;è un modo molto semplice per risolvere il problema.</p>
<p><span id="more-625"></span></p>
<ol>
<li>Copiate il pkg dell&#8217;installazione sul Desktop (o nel percorso che preferite)</li>
<li>Fate tasto destro sul pkg-&gt;mostra contenuto pacchetto</li>
<li>Entrate nella directory Contents -&gt; Resources</li>
<li>Editate con Textedit o quello che preferite (MA NON FATE doppio click) il file <strong>postinstall</strong></li>
<li>modificate la riga 2:
<ul>
<li>da: chown -R $USER:$USER &#8220;$2/SSHKeychain.app&#8221;</li>
<li>a: chown -R $USER:`id -gn` &#8220;$2/SSHKeychain.app&#8221;</li>
</ul>
</li>
<li>salvate il file ed uscite</li>
<li>cliccate due volte sul file pkg e fate ripartire l&#8217;installazione</li>
</ol>
<p>voilà! fatto.</p>
<p>Gio</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/05/29/sshkeychain-errore-the-following-install-step-failed-run-postinstall-script-for-sshkeychain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Far Future Expires Header: come e perchè (IIS, Apache, Tomcat, PHP, ASP, JSP)</title>
		<link>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/</link>
		<comments>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 11:42:12 +0000</pubDate>
		<dc:creator>Giovanni Savastano</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Web Servers]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=415</guid>
		<description><![CDATA[Sempre più spesso, nel mondo dello sviluppo web, si sente parlare di aumento delle performance, dimunuzione delle richieste HTTP, caching di contenuti dinamici, compressione di immagini e altre tecniche orientate all&#8217;allegerimento delle pagine di un sito e all&#8217;aumento della visualizzazione sul computer del nostro caro visitatore. Sicuramente una condizione ottimale per ottenere prestazioni migliori della [...]]]></description>
			<content:encoded><![CDATA[<p>Sempre più spesso, nel mondo dello sviluppo web, si sente parlare di aumento delle performance, dimunuzione delle richieste HTTP, caching di contenuti dinamici, compressione di immagini e altre tecniche orientate all&#8217;allegerimento delle pagine di un sito e all&#8217;aumento della visualizzazione sul computer del nostro caro visitatore.</p>
<p>Sicuramente una condizione ottimale per ottenere prestazioni migliori della propria web app o del proprio sito web è impostare il cosidetto <span style="text-decoration: underline;">Far Future Expire Header</span> degli elementi prensenti nelle pagine e delle pagine stesse.</p>
<p>In questo post cercherò di descrivere alcune tecniche per impostare tale condizione.</p>
<p><span id="more-415"></span></p>
<p>Cito dal sito di <a title="YSlow for Firebug" href="http://developer.yahoo.com/yslow/" target="_blank">YSlow</a>:</p>
<blockquote><p>Web page designs are getting richer and richer, which means more scripts, stylesheets, images, and Flash in the page. A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on <em>all</em> components including scripts, stylesheets, and Flash components.</p></blockquote>
<p>Quindi in pratica che fa?:-)</p>
<p>L&#8217;Expire Header definisce la scadenza dell&#8217;oggetto che il browser preleva dal server e deposita nella cache del client. Impostando la &#8220;data di scadenza&#8221; ad un periodo molto lontano (Far Future per l&#8217;appunto) diciamo al browser di non richiamare più tale componente durante le succesive visite alla pagina che lo conteneva, rendendo di fatto la navigazione più veloce. Inoltre, applicando questa tecnica ad elementi comuni a tutte le pagine (es: background o file javascript o file css) l&#8217;utente usufruirà dei benefici di questa tecnica fin dalla seconda pagina che andrà a visualizzare sul nostro sito.</p>
<p>Vediamo come fare.</p>
<p><strong>Tramite l&#8217;uso di .htaccess (Apache)</strong></p>

<div class="wp_codebox"><table><tr id="p41511"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p415code11"><pre class="shell" style="font-family:monospace;">&lt;FilesMatch &quot;\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|php|html|htm|php4|php5)$&quot;&gt;
Header set Expires &quot;Thu, 2 Feb 2010 20:00:00 GMT&quot;
&lt;/FilesMatch&gt;</pre></td></tr></table></div>

<p>con l&#8217;istruzione FileMatch indichiamo a quale tipo di file applicare le regole all&#8217;interno del blocco di codice e con &#8220;Header set Expires&#8221; andremo a dire ad Apache di <i>servire</i> il contenuto indicato con una data di scadenza molto lontana. Nell&#8217;esempio ho usato il 2 Febbraio 2010, alle 8 di sera.</p>
<p><strong>Tramite le impostazioni di IIS 6.0</strong></p>
<ol>
<li>Aprite il pannello di controllo di Internet Information Services (IIS).</li>
<li>Espandete &#8220;LocalMachine&#8221; sul pannelo di sinistra e succesivamente cliccate su Web Sites</li>
<li>Click con il tasto destro sul sito interessato e selezionate Properties.</li>
<li>Nel tab HTTP Headers, attivate la checkbox &#8220;Enable Content Expiration&#8221; e succesivamente selezionate l&#8217;opzione &#8220;Expire On&#8221;. e impostare il 2 Febbraio 2010 alle 8 di sera</li>
<li>Riavviate tutti i servizi IIS.</li>
</ol>
<p><strong>Con PHP</strong><br />
File far_future_loader.php</p>

<div class="wp_codebox"><table><tr id="p41512"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p415code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/javascript; charset: UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/scripts.js'</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>

<p>con il codice sopra potete, ad esempio, raggruppare tutti i vostri file javascript in un unico grande file .php, modificare l&#8217;header Content-type in modo che venga interpretato correttamente dal browser come text/javascript e poi modificarne a piaciemento la deata di scandenza.<br />
Successivamente nell&#8217;header delle vostre pagine, anzichè includere i singoli file javascript con l&#8217;istruzione:</p>

<div class="wp_codebox"><table><tr id="p41513"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p415code13"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;js/scripts.js&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>userete</p>

<div class="wp_codebox"><table><tr id="p41514"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p415code14"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;js/far_future_loader.php&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<p>Potete usare il file far_future_loader.php per ogni tipo di file, semplicemente cambiano il Content-type dei file che andrete ad includere, ad esempio, se volete averne uno anche per i file .css dovrete scrivere:</p>

<div class="wp_codebox"><table><tr id="p41515"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p415code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/css; charset: UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/style.css'</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>

<p>e successivamente nell&#8217;header delle vostre pagine, esattamente come avete fatto per il file javascript, sostituirete l&#8217;istruzione:</p>

<div class="wp_codebox"><table><tr id="p41516"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p415code16"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot; type=&quot;text/css&quot; /&gt;</pre></td></tr></table></div>

<p>con</p>

<div class="wp_codebox"><table><tr id="p41517"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p415code17"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/far_future_loader.php&quot; type=&quot;text/css&quot; /&gt;</pre></td></tr></table></div>

<p><strong>Con ASP</strong><br />
la procedura è identica a quella per PHP con la sola differenza che in ASP gli header si setttano con i seguenti comandi:</p>

<div class="wp_codebox"><table><tr id="p41518"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p415code18"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">CacheControl</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;must-revalidate&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Expires</span> <span style="color: #006600; font-weight: bold;">=</span> -<span style="color: #800000;">1</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></td></tr></table></div>

<p><strong>Con JSP (Tomcat)</strong><br />
Grazie a <a href="http://www.jguru.com/guru/viewfaqs.jsp?EID=1168738" target="_blank">Byron Tymvios</a> su <a target="_blank" href="http://www.jguru.com/faq/view.jsp?EID=1311010">jGuru</a><br />
E&#8217; possibile ottenere l&#8217;identico risultato avuto con PHP o ASP tramite i filtri. Ad esempio:</p>

<div class="wp_codebox"><table><tr id="p41519"><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
</pre></td><td class="code" id="p415code19"><pre class="jsp" style="font-family:monospace;">package com.xyz
&nbsp;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
&nbsp;
public class CacheFilter implements javax.servlet.Filter {
  FilterConfig filterConfig = null;
&nbsp;
  public void init(FilterConfig filterConfig){
    this.filterConfig = filterConfig;
  }
&nbsp;
  public void doFilter(ServletRequest req,
     ServletResponse res,
     FilterChain chain)
     throws IOException, ServletException {
    String sCache = filterConfig.getInitParameter(&quot;cache&quot;);
&nbsp;
    if(sCache != null){       ((HttpServletResponse)res).setHeader(&quot;Cache-Control&quot;, sCache);
&nbsp;
    }
&nbsp;
    chain.doFilter(req, res);
  }
&nbsp;
  public void destroy(){
    this.filterConfig = null;
  }
}</pre></td></tr></table></div>

<p>e poi impostare nel file web.xml il filtro per ogni contenuto desiderato. Ecco l&#8217;esempio per i file .jpg:</p>

<div class="wp_codebox"><table><tr id="p41520"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p415code20"><pre class="jsp" style="font-family:monospace;">&lt;filter&gt;
  &lt;filter-name&gt;Cache&lt;/filter-name&gt;
  &lt;filter-class&gt;com.xyz.CacheFilter&lt;/filter-class&gt;
  &lt;init-param&gt;
    &lt;param-name&gt;cache&lt;/param-name&gt;
    &lt;param-value&gt;public, max-age=2592000&lt;/param-value&gt;
  &lt;/init-param&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
  &lt;filter-name&gt;Cache&lt;/filter-name&gt;
  &lt;url-pattern&gt;*.jpg&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre></td></tr></table></div>

<p>è tutto <img src='http://www.ciarpame.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
nella prossimo post parleremo degli ETag.<br />
Giovanni</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ActiveRecord: da Rails a PHP, programmare agilmente (2)</title>
		<link>http://www.ciarpame.com/2009/01/12/activerecord-da-rails-a-php-programmare-agilmente-2/</link>
		<comments>http://www.ciarpame.com/2009/01/12/activerecord-da-rails-a-php-programmare-agilmente-2/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 11:04:52 +0000</pubDate>
		<dc:creator>Daniele Veratti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=459</guid>
		<description><![CDATA[Rieccoci di nuovo a parlare di active record. Nella scorsa puntata avevamo visto una implementazione base del pattern. Essa però è orfana di qualsiasi metodo per la ricerca dei record. Vediamo ora come è possibile farlo. Rails mette a disposizione diversi metodi per recuperare record dalle tabelle di un database. Il primo è quello di [...]]]></description>
			<content:encoded><![CDATA[<p>Rieccoci di nuovo a parlare di active record. Nella scorsa puntata avevamo visto una implementazione base del pattern. Essa però è orfana di qualsiasi metodo per la ricerca dei record. Vediamo ora come è possibile farlo.<br />
<span id="more-459"></span><br />
Rails mette a disposizione diversi metodi per recuperare record dalle tabelle di un database. Il primo è quello di utilizzare il costruttore della classe passando come parametro l&#8217;id del record che vogliamo leggere. In molti linguaggi di programmazione Object-Oriented è possibile effettuare l&#8217;overload del costruttore, ovvero definire più costruttori per una stessa classe, dove in ogni costruttore cambiano i parametri per numero e/o tipo. In PHP non esiste tale meccanismo (e neanche in Ruby da quanto mi risulta!). Abbiamo visto nella puntata precedente come il costruttore della classe php ActiveRecordBase accetti come unico parametro un array contenente i campi che vogliamo dare ad un nuovo record. Simuliamo ora l&#8217;overloading del costruttore in modo che accetti sia l&#8217;array che un numero che identifichi l&#8217;ID del record.</p>

<div class="wp_codebox"><table><tr id="p45926"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p459code26"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> ActiveRecordBase <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># qui ci va la dichiarazione degli attributi della classe che tralasciamo
</span>  <span style="color: #666666; font-style: italic;"># .....
</span>  <span style="color: #666666; font-style: italic;">#
</span>  <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;">$data</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: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>MDB2<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>DB_DSN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// DB_DSN è una stringa che contiene di dati accesso al DB</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</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: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
                  <span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</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: #009900;">&#125;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</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: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</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;"># Qui prosegue il resto del codice della classe
</span><span style="color: #666666; font-style: italic;"># ....
</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Con questa modifica la nostra classe continua a funzionare accettando 0 o 1 parametro di tipo numerico o array/hash.<br />
Vediamo ora l&#8217;implementazione di un metodo find() che permetta di passare come parametro l&#8217;ID del record che vogliamo ottenere. Per semplicità d&#8217;ora in avanti verranno omesse le dichiarazioni di classe quando non strettamente necessarie.</p>

<div class="wp_codebox"><table><tr id="p45927"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p459code27"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> find<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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span>
                  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</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;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</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: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
             <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</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></pre></td></tr></table></div>

<p>Come potete vedere, l&#8217;implementazione di find() è molto simile a quella del costruttore. Ricordiamoci il principio DRY (= Don&#8217;t Repeat Yourself). Modifichiamo il costruttore e aggiungiamo un metodo privato che assegni a partire da un array i valori corrispondenti agli attributi della classe.<br />
__assignData sarà un metodo privato che assegna i valori agli attributi:</p>

<div class="wp_codebox"><table><tr id="p45928"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p459code28"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __assignData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</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;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</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: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
             <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</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></pre></td></tr></table></div>

<p>Possiamo semplificare il metodo find():</p>

<div class="wp_codebox"><table><tr id="p45929"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p459code29"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> find<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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span>
                  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__assignData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>E ora rivediamo il costruttore:</p>

<div class="wp_codebox"><table><tr id="p45930"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p459code30"><pre class="php" style="font-family:monospace;">  <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;">$data</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: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>MDB2<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>DB_DSN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// DB_DSN è una stringa che contiene di dati accesso al DB</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</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: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</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;">find</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__assignData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/01/12/activerecord-da-rails-a-php-programmare-agilmente-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord: da Rails a PHP, programmare agilmente (1)</title>
		<link>http://www.ciarpame.com/2008/12/10/activerecord-da-rails-a-php-programmare-agilmente-1/</link>
		<comments>http://www.ciarpame.com/2008/12/10/activerecord-da-rails-a-php-programmare-agilmente-1/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 12:42:19 +0000</pubDate>
		<dc:creator>Daniele Veratti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=274</guid>
		<description><![CDATA[Una delle feature del framework Ruby On Rails più potenti è l&#8217;implementazione del pattern ActiveRecord. Esso permette di &#8220;vedere&#8221; ogni record di una tabella del database come istanze di una classe permettendo di eseguire le normali operazioni di CRUD (Create, Read, Update, Delete) con poche righe di codice. L&#8217;implementazione di ActiveRecord in Rails è molto [...]]]></description>
			<content:encoded><![CDATA[<p>Una delle feature del framework Ruby On Rails più potenti è l&#8217;implementazione del pattern ActiveRecord. Esso permette di &#8220;vedere&#8221; ogni record di una tabella del database come istanze di una classe permettendo di eseguire le normali operazioni di CRUD (Create, Read, Update, Delete) con poche righe di codice.</p>
<p>L&#8217;implementazione di ActiveRecord in Rails è molto complessa, permette di effettuare validazioni e avere riferimenti a chiavi esterne. Vediamo un&#8217;implementazione più semplice in PHP, ma comunque efficace.<br />
Cercheremo di arrivare ad un&#8217;implementazione molto potente in qualche puntata.<br />
<span id="more-274"></span><br />
Il concetto che sta alla base di active record è di trasformare l&#8217;accesso al database in accesso a una classe.<br />
Cominciamo a definire una classe base che abbia l&#8217;accesso al database. Per semplificare utilizzeremo la classe PEAR::MDB2.</p>

<div class="wp_codebox"><table><tr id="p27433"><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
</pre></td><td class="code" id="p274code33"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'MDB2.php'</span><span style="color: #339933;">;</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> ActiveRecordBase <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__db</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Dichiariamo un oggetto &quot;protected&quot; per l'accesso al DB</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__primary_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Definiamo una chiave primaria per la tabella. Può essere poi</span>
                                           <span style="color: #666666; font-style: italic;">// modificata per ogni singola classe che estende questa.</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__fields</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// In questo attributo di tipo array andremo a salvare i campi della nostra tabella (escluso il campo primary key)</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$__table</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// In questo attributo salviamo il nome della tabella</span>
  <span style="color: #009933; font-style: italic;">/**
   * Il costruttore istanzia una connessione con il db e se sono presenti dei dati
   * li salva nell'oggetto (non nel database!)
   */</span>
  <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;">$data</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: #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;">db</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>MDB2<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span>DB_DSN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// DB_DSN è una stringa che contiene di dati accesso al DB</span>
     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</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: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</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: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Definiamo un metodo per create un nuovo Record.
   * Esso è privato perché verrà utilizzato dal metodo save()
   */</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> create<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;">// Creaimo l'istruzione SQL a partire dai campi disponibili</span>
     <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; (&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$data</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>
     <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__fields <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</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: #339933;">!</span><span style="color: #000088;">$first</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$field</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;) VALUES (&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;?&quot;</span> <span style="color: #b1b100;">else</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;,?&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$sql</span><span style="color: #339933;">:=</span><span style="color: #0000ff;">&quot;);&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$prep</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<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>
     <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$prep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertID</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</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<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; SET &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$data</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>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__fields <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</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: #339933;">!</span><span style="color: #000088;">$first</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$field</span> = ?&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$field</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$prep</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<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>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$prep</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__table<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
             <span style="color: #0000ff;">&quot; WHERE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteIdentifier</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</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: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</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> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$pkey_field</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__primary_key<span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$pkey_field</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">else</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;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In questa implementazione, ancora in stato &#8220;embrionale&#8221;, è possibile creare un nuovo record.<br />
Un uso molto semplice potrebbe essere creare una classe che estenda la base e istanziarne un oggetto:</p>

<div class="wp_codebox"><table><tr id="p27434"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p274code34"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'ActiveRecordBase.class.php'</span>
<span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecordBase <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$dog</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Pluto'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'7'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Il precedente script inserisce un nuovo record in una tabella &#8216;dog&#8217; (stessa nome della classe che estende ActiveRecordBase) assegnando gli attributi &#8216;name&#8217; e &#8216;age&#8217;.<br />
La primary key viene implicitamente impostata come &#8220;id&#8221; (ovviamente se il campo ha l&#8217;attributo auto_increment) e il valore inserito automaticamente.<br />
Il tutto in 3 righe di codice, molto molto semplici&#8230;non è una bella comodità?<br />
Più avanti vedremo come effettuare ricerche, e quindi come implementare il metodo &#8220;find&#8221; e migliorare ulteriormente la classe base in modo da gestire meglio errori, validazioni e chiavi esterne.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/12/10/activerecord-da-rails-a-php-programmare-agilmente-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cambiare facilmente il docroot di suexec su Debian (sarge/lenny/etch).</title>
		<link>http://www.ciarpame.com/2008/10/27/cambiare-facilmente-il-docroot-di-suexec-su-debian-sargelennyetch/</link>
		<comments>http://www.ciarpame.com/2008/10/27/cambiare-facilmente-il-docroot-di-suexec-su-debian-sargelennyetch/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 07:00:05 +0000</pubDate>
		<dc:creator>Giovanni Savastano</dc:creator>
				<category><![CDATA[Linux&Co.]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[suexec]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=264</guid>
		<description><![CDATA[Una delle cose più fastidiose di apache è che, se compilato senza attenzione, si porta dietro una serie di configurazioni non modificabili tramite semplici file di configurazione. Alcune di queste sono sicuramente i parametri di suexec. Suexec è una funzionalità presente in apache 1.2 che permette di eseguire CGI e applicazioni SSI con i privilegi [...]]]></description>
			<content:encoded><![CDATA[<p>Una delle cose più fastidiose di apache è che, se compilato senza attenzione, si porta dietro una serie di configurazioni non modificabili tramite semplici file di configurazione.<br />
Alcune di queste sono sicuramente i parametri di suexec.<br />
Suexec è una funzionalità presente in apache 1.2 che permette di eseguire CGI e applicazioni SSI con i privilegi diversi da quelli dell&#8217;utente apache di sistema. In questo modo ogni utente può eseguire scripts complessi senza poter uscire dal proprio spazio web.</p>
<p>Purtoppo a volte è necessario modificare i path di default.<br />
Vediamo come</p>
<p><span id="more-264"></span></p>
<p>Innanzitutto loggatevi sulla vostra macchina con privilegi di root</p>

<div class="wp_codebox"><table><tr id="p26444"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p264code44"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># su -</span></pre></td></tr></table></div>

<p>controlliamo i parametri del nostro suexec:</p>

<div class="wp_codebox"><table><tr id="p26445"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p264code45"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /usr/lib/apache2/suexec -V</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_DOC_ROOT</span>=<span style="color: #ff0000;">&quot;/var/www&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_GID_MIN</span>=<span style="color: #000000;">100</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_HTTPD_USER</span>=<span style="color: #ff0000;">&quot;www-data&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_LOG_EXEC</span>=<span style="color: #ff0000;">&quot;/var/log/apache2/suexec.log&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_SAFE_PATH</span>=<span style="color: #ff0000;">&quot;/usr/local/bin:/usr/bin:/bin&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_UID_MIN</span>=<span style="color: #000000;">100</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_USERDIR_SUFFIX</span>=<span style="color: #ff0000;">&quot;public_html&quot;</span></pre></td></tr></table></div>

<p>come potete vedere dal risultato, i parametri cablati al momento della compilazione sono molti.<br />
spostiamoci in /usr/src e scarichiamo i sorgenti necessari al nostro scopo</p>

<div class="wp_codebox"><table><tr id="p26446"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p264code46"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># cd /usr/src</span>
<span style="color: #666666; font-style: italic;"># apt-get update</span>
<span style="color: #666666; font-style: italic;"># apt-get build-dep apache2</span>
<span style="color: #666666; font-style: italic;"># apt-get source apache2</span></pre></td></tr></table></div>

<p>ok, adesso spostiamoci nella directory con i sorgenti e configuriamo apache con i parametri che ci interessano:</p>

<div class="wp_codebox"><table><tr id="p26447"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p264code47"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># cd apache2-2.2.3</span>
<span style="color: #666666; font-style: italic;"># nano debian/rules</span></pre></td></tr></table></div>

<p>all&#8217;interno del file rules troverete un sacco di parametri. quelli ci interessano in questo momento sono quelli sotto la voce AP2_COMMON_CONFARGS.<br />
quindi modifichiamo quanto ci serve andando a cambiare quanto presente alla riga</p>

<div class="wp_codebox"><table><tr id="p26448"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p264code48"><pre class="bash" style="font-family:monospace;">      <span style="color: #660033;">--with-suexec-docroot</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>hosting \</pre></td></tr></table></div>

<p>e salviamo.<br />
ora, semplicemente, dovremmo ricompilare il file. prima di farlo però, è necessario modificare la versione di apache in modo tale che il nostro cambiamento non venga sovrascritto ad ogni upgrade.<br />
quindi</p>

<div class="wp_codebox"><table><tr id="p26449"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p264code49"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># dch -i</span></pre></td></tr></table></div>

<p>e ci troviamo all&#8217;interno del changelog di apache.<br />
UN CONSIGLIO: se non volete dover reinstallare tutti i pacchetti di apache, eliminate la versione che dch vi aggiunge in automatico e andate a lavorare sull&#8217;ultima. aggiungete un bel * e inserite il vostro commento, tipo: &#8220;fixed suexec docroot to: /home&#8221; e salvate.<br />
in questo modo la versione rimarrà inalterata e apache installerà solo la &#8220;patch&#8221; da voi applicata</p>
<p>a questo punto dobbiamo solo compilare e pacchettizzare</p>

<div class="wp_codebox"><table><tr id="p26450"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p264code50"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># dpkg-buildpackage</span></pre></td></tr></table></div>

<p>aspettate la fine delle operazioni di compilazione, tornate in /usr/src e installate i pacchetti modificati (nel nostro caso apache2.2-common e apache2.2-utils)</p>

<div class="wp_codebox"><table><tr id="p26451"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p264code51"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># cd /usr/src //o se volete cd ../</span>
<span style="color: #666666; font-style: italic;"># dpkg -i apache2.2-common_2.2.3-4+etch6_amd64.deb </span>
<span style="color: #666666; font-style: italic;"># dpkg -i apache2-utils_2.2.3-4+etch6_amd64.deb</span></pre></td></tr></table></div>

<p>fatto questo restartate apache, verificate che tutto sia andato a posto ed eventualmente controllate i conflitti con apt</p>

<div class="wp_codebox"><table><tr id="p26452"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p264code52"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /etc/init.d/apache2 restart</span>
<span style="color: #666666; font-style: italic;"># /usr/lib/apache2/suexec -V</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_DOC_ROOT</span>=<span style="color: #ff0000;">&quot;/home&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_GID_MIN</span>=<span style="color: #000000;">100</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_HTTPD_USER</span>=<span style="color: #ff0000;">&quot;www-data&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_LOG_EXEC</span>=<span style="color: #ff0000;">&quot;/var/log/apache2/suexec.log&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_SAFE_PATH</span>=<span style="color: #ff0000;">&quot;/usr/local/bin:/usr/bin:/bin&quot;</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_UID_MIN</span>=<span style="color: #000000;">100</span>
     <span style="color: #660033;">-D</span> <span style="color: #007800;">AP_USERDIR_SUFFIX</span>=<span style="color: #ff0000;">&quot;public_html&quot;</span> 
<span style="color: #666666; font-style: italic;"># apt-get install -f</span></pre></td></tr></table></div>

<p>alla prossima!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/10/27/cambiare-facilmente-il-docroot-di-suexec-su-debian-sargelennyetch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accorciare e velocizzare le query SQL&#8230;si può!</title>
		<link>http://www.ciarpame.com/2008/10/23/accorciare-e-velocizzare-le-query-sqlsi-puo/</link>
		<comments>http://www.ciarpame.com/2008/10/23/accorciare-e-velocizzare-le-query-sqlsi-puo/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 09:00:01 +0000</pubDate>
		<dc:creator>Daniele Veratti</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[INNER JOIN]]></category>
		<category><![CDATA[JOIN]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[NATURAL JOIN]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=250</guid>
		<description><![CDATA[Spesso nello sviluppo di applicazioni, sia web che non, &#232; necessario appoggiarsi ad una base di dati. Una buona progettazione del database è talvolta fondamentale per ottenere buone prestazioni, sia per quanto riguarda la scrittura del codice, sia per quanto riguarda l&#8217;applicazione in sè. Prendiamo per esempio la tabella impiegati(id,cognome,nome,eta,dipartimento_id) e la tabella dipartimenti(id, nome_dipartimento,sede). [...]]]></description>
			<content:encoded><![CDATA[<p>Spesso nello sviluppo di applicazioni, sia web che non, &egrave; necessario appoggiarsi ad una base di dati. Una buona progettazione del database è talvolta fondamentale per ottenere buone prestazioni, sia per quanto riguarda la scrittura del codice, sia per quanto riguarda l&#8217;applicazione in sè.<br />
<span id="more-250"></span><br />
Prendiamo per esempio la tabella impiegati(id,cognome,nome,eta,dipartimento_id) e la tabella dipartimenti(id, nome_dipartimento,sede).</p>
<p>Se volessimo ottenere la lista degli impiegati il cui dipartimento ha, per esempio, sede a Roma, potremmo scrivere diverse query SQL che ci fanno ottenere lo stesso risultato:</p>

<div class="wp_codebox"><table><tr id="p25058"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p250code58"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> i<span style="color: #66cc66;">.*</span> <span style="color: #993333; font-weight: bold;">FROM</span> impiegati i<span style="color: #66cc66;">,</span>dipartimenti d 
<span style="color: #993333; font-weight: bold;">WHERE</span> i<span style="color: #66cc66;">.</span>dipartimento_id <span style="color: #66cc66;">=</span> d<span style="color: #66cc66;">.</span>id <span style="color: #993333; font-weight: bold;">AND</span> d<span style="color: #66cc66;">.</span>sede <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Roma'</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> impiegati 
<span style="color: #993333; font-weight: bold;">WHERE</span> dipartimento_id <span style="color: #993333; font-weight: bold;">IN</span> 
<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> dipartimenti <span style="color: #993333; font-weight: bold;">WHERE</span> sede <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Roma'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> i<span style="color: #66cc66;">.*</span> <span style="color: #993333; font-weight: bold;">FROM</span> impiegati i 
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> dipartimenti d <span style="color: #993333; font-weight: bold;">ON</span> i<span style="color: #66cc66;">.</span>dipartimento_id <span style="color: #66cc66;">=</span> d<span style="color: #66cc66;">.</span>id 
<span style="color: #993333; font-weight: bold;">WHERE</span> d<span style="color: #66cc66;">.</span>sede <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Roma'</span>;</pre></td></tr></table></div>

<p>Tra le precedenti query l&#8217;ultima ha prestazioni decisamente migliori anche se è la più lunga da scrivere:<br />
nel primo caso il DBMS deve prima effettuare un prodotto cartesiano tra le due tabelle ed estrarre i record dove corrispondono gli ID dei dipartimenti e la sede è Roma;<br />
nel secondo caso il DBMS prima leggere tutti i dipartimenti con sede = &#8216;Roma&#8217; e per ogni impiegato verificare che dipartimento_id sia in questa lista.<br />
Nel terzo caso invece il DBMS automaticamente associa ogni impiegato con il dipartimento relativo e la ricerca sarà effettuata solo sul campo &#8220;sede&#8221;.<br />
L&#8217;uso di INNER JOIN velocizza le vostre applicazioni, ma usatelo con cautela: i valori nulli sui campi su cui si effettua il JOIN non sono ammessi.<br />
Se ve n&#8217;é la necessità si possono utilizzare LEFT e RIGHT JOIN.<br />
Se per esempio vogliamo sapere i cognomi degli impiegati che lavorano a Roma o che non lavorano (= dipartimento_id IS NULL) dovremmo scrivere:</p>

<div class="wp_codebox"><table><tr id="p25059"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p250code59"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> i<span style="color: #66cc66;">.</span>cognome <span style="color: #993333; font-weight: bold;">FROM</span> impiegati i 
<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> dipartimenti d <span style="color: #993333; font-weight: bold;">ON</span> i<span style="color: #66cc66;">.</span>dipartimento_id <span style="color: #66cc66;">=</span> d<span style="color: #66cc66;">.</span>id 
<span style="color: #993333; font-weight: bold;">WHERE</span> sede <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Roma'</span> <span style="color: #993333; font-weight: bold;">OR</span> i<span style="color: #66cc66;">.</span>dipartimento_id <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span>;</pre></td></tr></table></div>

<p>La scrittura delle INNER JOIN può diventare molto ripetitiva, soprattutto se vengono usate spesso. L&#8217;uso per di convenzioni però ci può dare una mano. Ridefiniamo le tabelle di prima in questo modo:</p>

<div class="wp_codebox"><table><tr id="p25060"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p250code60"><pre class="sql" style="font-family:monospace;">impiegati <span style="color: #66cc66;">&#40;</span>impiegato_id<span style="color: #66cc66;">,</span> cognome<span style="color: #66cc66;">,</span> nome<span style="color: #66cc66;">,</span> eta<span style="color: #66cc66;">,</span> dipartimento_id<span style="color: #66cc66;">&#41;</span>
dipartimenti <span style="color: #66cc66;">&#40;</span>dipartimento_id<span style="color: #66cc66;">,</span> sede<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Con le tabelle definite in questa guisa possiamo sfoderare un ulteriore tipo di JOIN: il NATURAL JOIN.<br />
Il natural join utilizza campi con lo stesso nome per effettuare la combinazione dei record delle tabelle. Quindi se volessimo sapere quali impiegati lavorano a Roma, la nostra query SQL diventa:</p>

<div class="wp_codebox"><table><tr id="p25061"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p250code61"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> i<span style="color: #66cc66;">.*</span> <span style="color: #993333; font-weight: bold;">FROM</span> impiegati i <span style="color: #993333; font-weight: bold;">NATURAL</span> <span style="color: #993333; font-weight: bold;">JOIN</span> dipartimenti d <span style="color: #993333; font-weight: bold;">WHERE</span> d<span style="color: #66cc66;">.</span>sede <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Roma'</span>;</pre></td></tr></table></div>

<p>Praticamente il NATURAL JOIN funziona esattamente come l&#8217;INNER JOIN, solo che ci fa risparmiare la scrittura di ON i.dipartimento_id = d.dipartimento_id, mantenendo inalterate le prestazioni.<br />
Quindi scegliendo bene i nomi dei campi possiamo scrivere query più veloci e più velocemente.</p>
<p>Ovviamente le prestazione delle JOIN aumentano in presenza di INDICI. Il miglior tipo di indice per effettuare un join è HASH, non implementato purtroppo da MySQL (almeno per quanto riguarda InnoDB e MyISAM).<br />
Infatti un indice implementato tramite HASH ha prestazioni migliori rispetto a BTREE se la ricerca viene effettuata in base a delle uguaglianze.</p>
<p>Gli indici BTREE invece hanno migliori prestazione in ricerche basate su range, come ad esempio nella query:</p>

<div class="wp_codebox"><table><tr id="p25062"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p250code62"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> impiegati <span style="color: #993333; font-weight: bold;">WHERE</span> eta <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">30</span></pre></td></tr></table></div>

<p>Un indice BTREE sul campo &#8220;eta&#8221; migliorerebbe le prestazione della vostra query.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/10/23/accorciare-e-velocizzare-le-query-sqlsi-puo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto focus al caricamento e backspace funzionante? si può!</title>
		<link>http://www.ciarpame.com/2008/10/20/auto-focus-al-caricamento-e-backspace-funzionante-si-puo/</link>
		<comments>http://www.ciarpame.com/2008/10/20/auto-focus-al-caricamento-e-backspace-funzionante-si-puo/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 07:00:16 +0000</pubDate>
		<dc:creator>Giovanni Savastano</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=242</guid>
		<description><![CDATA[A volte piccoli accorgimenti rendono la vita dei navigatori molto più semplice. Ad esempio, impostare il cursore della tastiera di default nei box di ricerca è un&#8217;operazione semplice da realizzare e utile per chi arriva sulle vostre pagine. Vediamo come farlo in javascript, grazie a Mootools. Immaginiamo di avere un form di ricerca di questo [...]]]></description>
			<content:encoded><![CDATA[<p>A volte piccoli accorgimenti rendono la vita dei navigatori molto più semplice.<br />
Ad esempio, impostare il cursore della tastiera di default nei box di ricerca è un&#8217;operazione semplice da realizzare e utile per chi arriva sulle vostre pagine.</p>
<p>Vediamo come farlo in javascript, grazie a <a href="http://mootools.net" title="Mootools" alt="mootools" target="_blank">Mootools</a>.<br />
<span id="more-242"></span></p>
<p>Immaginiamo di avere un form di ricerca di questo tipo:<code></p>

<div class="wp_codebox"><table><tr id="p24268"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p242code68"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;/?action=cerca&quot; name=&quot;ricerca&quot; id=&quot;ricerca&quot; method=&quot;get&quot;&gt;
       &lt;input name=&quot;q&quot; id=&quot;q&quot; size=&quot;20&quot; type=&quot;text&quot; /&gt;
       &lt;input type=&quot;submit&quot; value=&quot;Cerca&quot; /&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Semplice ed essenziale.<br />
Per portare il cursore sul campo "q" il codice necessario è davvero corto:</p>

<div class="wp_codebox"><table><tr id="p24269"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p242code69"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'q'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'q'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>L'IF iniziale ci è utile per poter far partire l'azione solo ed esclusivamente quando il form di ricerca è presente nella pagina che si sta visitando ed evitare che venga generato un errore di tipo NOT FOUND o NOT DEFINED</p>
<p>Aggiungiamo ora l'azione al caricamento della pagina:</p>

<div class="wp_codebox"><table><tr id="p24270"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p242code70"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'domready'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'q'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
              $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'q'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>L'unico inconveniente di questo trucchetto è che se l'utente si accorge di aver sbagliato pagina e preme il tasto Backspace sulla tastiera per tornare alla pagina precedente, il focus nell'input ne impedirà il funzionamento. E' molto fastidioso!!</p>
<p>Quindi?<br />
quindi dobbiamo fare in modo che alla pressione del Backspace il nostro javascript capisca se stiamo cancellando un testo o stiamo tentando di tornare indietro.</p>

<div class="wp_codebox"><table><tr id="p24271"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p242code71"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keydown'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #003366; font-weight: bold;">var</span> event <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Event<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">key</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'backspace'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>q.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
              history.<span style="color: #000066;">back</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>fatto!</p>
<p>NOTA: se volete evitare di caricare un framework come mootools nel vostro sito solo per effettuare un'operazione del genere, potete tranquillamente ricorrere al javascript tradizionale in questo modo:</p>

<div class="wp_codebox"><table><tr id="p24272"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p242code72"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'q'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
              <span style="color: #003366; font-weight: bold;">var</span> campo <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'q'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              campo.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
       document.<span style="color: #660066;">onkeydown</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #003366; font-weight: bold;">var</span> event <span style="color: #339933;">=</span> window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span>
              <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">8</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>q.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                      history.<span style="color: #000066;">back</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span>
       <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>alla prossima!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/10/20/auto-focus-al-caricamento-e-backspace-funzionante-si-puo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>true mysql &#8220;natural order by&#8221; trick</title>
		<link>http://www.ciarpame.com/2008/06/28/true-mysql-natural-order-by-trick/</link>
		<comments>http://www.ciarpame.com/2008/06/28/true-mysql-natural-order-by-trick/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 15:06:27 +0000</pubDate>
		<dc:creator>Giovanni Savastano</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=128</guid>
		<description><![CDATA[when you develop some kind of web app (or sites containing tabular data too) and you need to order &#8220;naturally&#8221; mysql results, problems starts to come out! googling a lot I&#8217;ve found a simple trick that I&#8217;m here to share. go to: Drupal ViewVC site point to / contributions / modules / natsort / natsort.install.mysql. [...]]]></description>
			<content:encoded><![CDATA[<p>when you develop some kind of web app (or sites containing tabular data too) and you need to order &#8220;naturally&#8221; mysql results, problems starts to come out! googling a lot I&#8217;ve found a simple trick that I&#8217;m here to share.</p>
<ol>
<li>go to: <a href="http://cvs.drupal.org/viewvc.py/drupal/" target="_blank">Drupal ViewVC</a> site</li>
<li>point to <a href="http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/natsort/natsort.install.mysql?view=log&amp;pathrev=DRUPAL-5" target="_blank">/ contributions / modules / natsort / natsort.install.mysql</a>.</li>
<li>download the latest version of the script</li>
<li>login to your ssh enviroment</li>
<li>type: mysql &#8211; u [username] -p [databasename] &lt; natsort.install.mysql</li>
<li>insert your corresponding password</li>
<li>look at your db, now you have 3 new stored procedures, 2 stored functions and 2 new tables:
<ol>
<li>Stored Procedures:
<ul>
<li>natsort_benchmark</li>
<li>natsort_finalize</li>
<li>natsort_initialize</li>
</ul>
</li>
</ol>
<ul>
<li>Stored Functions:
<ul>
<li>natsort_canon</li>
<li>natsort_canon_save</li>
</ul>
</li>
<li>Tables:
<ul>
<li>natsort_lookup</li>
<li>natsort_lookup_pending</li>
</ul>
</li>
</ul>
</li>
<li>you can delete the natsort_initialize() stored procedure. it is used only by drupal, so we don&#8217;t need this</li>
<li>go to <a href="http://cvs.drupal.org/viewvc.py/drupal/" target="_blank">Drupal ViewVC</a> site</li>
<li>Point to <a href="http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/natsort/README.txt?view=log&amp;pathrev=DRUPAL-5" target="_blank">/ contributions / modules / natsort / README.txt </a></li>
<li>follow the simple pass to start using this good workaround.</li>
</ol>
<p>Don&#8217;t forget to call the natsort_finalize() procedure at the end of any query you do. This will speed-up all you next queries.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2008/06/28/true-mysql-natural-order-by-trick/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
