<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>web(cslai)</title>
	
	<link>http://cslai.coolsilon.com</link>
	<description>Findings and Notes in Web Development</description>
	<lastBuildDate>Wed, 14 Jul 2010 11:17:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/cslai" /><feedburner:info uri="cslai" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by-nc-sa/3.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><item>
		<title>New directory structure</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/Mg1VyxNVdA0/</link>
		<comments>http://cslai.coolsilon.com/2010/06/27/new-directory-structure/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 15:58:31 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[base-kite]]></category>
		<category><![CDATA[symlink]]></category>
		<category><![CDATA[theme development]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=186</guid>
		<description><![CDATA[While the previous file structure works well, I decided to tune some details before deploying the latest WordPress release. Besides that, I also started a new theme development project after my last theme which was developed more than 2 years ago. Thankfully, everything seems to work so far. The main reason behind the re-organization of [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/OkaN6kYcPrjsH2DJgk96Gkda1Eo/0/da"><img src="http://feedads.g.doubleclick.net/~a/OkaN6kYcPrjsH2DJgk96Gkda1Eo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/OkaN6kYcPrjsH2DJgk96Gkda1Eo/1/da"><img src="http://feedads.g.doubleclick.net/~a/OkaN6kYcPrjsH2DJgk96Gkda1Eo/1/di" border="0" ismap="true"></img></a></p><p>While the previous file structure works well, I decided to tune some details before deploying the latest WordPress release. Besides that, I also started a new theme development project after my last theme which was developed more than 2 years ago. Thankfully, everything seems to work so far.</p>
<p><span id="more-186"></span></p>
<p>The main reason behind the re-organization of files is to stop putting everything together within wp-content folder. As I am using <acronym title="Subversion">SVN</acronym> to manage my WordPress files, I feel more secured if I have less files changed / modified / added to my working copy. The previous structure was designed to store everything, including things are not meant to be shared among sites (installations) like attachments within wp-content folder.</p>
<p>Besides, although putting in everything within the same wp-content folder sounds like a good idea, but what if I want to create yet another new site (installation) for somebody which I don&#8217;t feel like sharing plugins and themes to this new site? After digging in the codex for some time, I found this <a href="http://codex.wordpress.org/Editing_wp-config.php">guide</a> that guides me how to further customize WordPress folder path very useful.</p>
<p>So the structure for a site now looks like this</p>
<pre>
/
|-scripts/ < - where I store my shared scripts
|-|-wordpress/ <- my wordpress, obviously
|-|-|-www/ <- my wordpress working copy
|-|-|-content/ <- new wp-content folders
|-|-|-|-site-id/ <- new wp-content folders for $site_id
|-|-|-|-|-plugins/
|-|-|-|-|-themes/
|-public_html/ <- my public folder, links to /scripts/wordpress/www
|-|-wp-site/ <- new wp-content folder, links to /scripts/wordpress/content/site-id
</pre>
<p>So now visitors of this blog should not be able to view attachment for my other blogs. </p>
<p>The deployed new theme in development is actually my current weekend project. As you can see, there's no color scheme being applied to it at the moment. I am now focusing on completing the basic features and the basic template files before being able to further customizing the color scheme.</p>
<p>Just in case if anyone is interested in this new theme, it can actually be forked via my bitbucket <a href="http://www.bitbucket.org/jeffrey04/base-kite">repository</a>. I have not actually decide what license to be used, but most probably <acronym title="General Public License">GPL</acronym> as WordPress would probably requires. If there's any problem regarding this new theme, please do not hesitate to post a bug report @ the <a href="http://www.bitbucket.org/jeffrey04/base-kite/issues?status=new&#038;status=open">issue tracker</a>.</pre>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=Mg1VyxNVdA0:BPDdTG8y_fA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Mg1VyxNVdA0:BPDdTG8y_fA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=Mg1VyxNVdA0:BPDdTG8y_fA:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Mg1VyxNVdA0:BPDdTG8y_fA:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Mg1VyxNVdA0:BPDdTG8y_fA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=Mg1VyxNVdA0:BPDdTG8y_fA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Mg1VyxNVdA0:BPDdTG8y_fA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Mg1VyxNVdA0:BPDdTG8y_fA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/Mg1VyxNVdA0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2010/06/27/new-directory-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2010/06/27/new-directory-structure/</feedburner:origLink></item>
		<item>
		<title>Linked Data Principle</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/S_U6nC9g1f8/</link>
		<comments>http://cslai.coolsilon.com/2010/06/10/linked-data-principle/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 08:35:40 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[RDF]]></category>
		<category><![CDATA[URI]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=178</guid>
		<description><![CDATA[Semantic Web is not just about putting data on the web, but also making links to allow a person as well as a machine to explore the web of data. Links are made in the web of data connects arbitrary things together as described by RDF as opposed to links in the web of hypertext, [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/NxfJegj8egqhfjXKWx_1gTVVnfk/0/da"><img src="http://feedads.g.doubleclick.net/~a/NxfJegj8egqhfjXKWx_1gTVVnfk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/NxfJegj8egqhfjXKWx_1gTVVnfk/1/da"><img src="http://feedads.g.doubleclick.net/~a/NxfJegj8egqhfjXKWx_1gTVVnfk/1/di" border="0" ismap="true"></img></a></p><p>Semantic Web is not just about putting data on the web, but also making links to allow a person as well as a machine to explore the web of data. Links are made in the web of data connects arbitrary things together as described by RDF as opposed to links in the web of hypertext, where links connects to only web-resources. Linkage of arbitrary things then allow related things to be found while performing search.</p>
<p><span id="more-178"></span></p>
<p>Besides, it is also a principle of linking data between systems and entities that allow rich self-describing inter-relations of available data across the globe on the web. Web of Data also marks a shift from publishing data in human readable HTML documents to machine readable documents that allows machines to be able to make inference out of the data published.</p>
<p>To efficiently link entities together, Sir Tim Bernes-Lee proposed four rules or expectations, as follows,</p>
<ul>
<li>Use <acronym title="Universal Resource Identifier">URI as names for things</acronym></li>
<li>Use <acronym title="Hyper-Text Transfer Protocol">HTTP</acronym> URIs to enable people to look up these names</li>
<li>Returns useful information using standard technology / format like RDF / SPARQL when someone looks up a URI</li>
<li>Allow users to discover more things by including links to other URI.</li>
</ul>
<p>However, it is important to know that breaking the rules does not neccessary destructive. It would only reduce inter-connectivity, which in turns discourages re-usability that results in making resources less valuable.</p>
<p>To use URI as names for things, Universal URI set of symbols should always be used to enable other parties to be able to process data that results in a consistent result. This also means that the risk of loosing meaning is reduced in the process. </p>
<p>It is also important to actually serve information on the web against a given URI. This allows data as well as metadata in specific standard formats such as RDF or OWL accessible. By publishing information about a resource, it enables others, especially applications and machines to properly understand the document.</p>
<p>To enable users to discover things, one of the ways is to provide inner as well as outer links information back to the user. By definition, it says that given a graph G, it is browsable if for the URI of any node in G, and if the URI is looked up, information returned that describes the node must satisfy the following conditions:</p>
<ul>
<li>Returns statements where the node is either a subject or object</li>
<li>Describes all blank nodes attached to the node by one arc.</li>
</ul>
<p>In short, it allows data to be represented in graph form and allow traversal. It is important for the query service to return RDF statements of that involves the specified node regardless it is a subject or an object. Note that the subgraph returned should be a minimum spanning tree (MSG) or known as RDF molecule.</p>
<p>However, if there is a statement that relate multiple entities, the statement should then be repeated for each of the entities. This then violates the rule where data must not store in more than one place mainly for consistency purpose. However, this becomes less of a concern if the statements are automatically generated.</p>
<p>Besides that, there may be a situation where the author of document A claims that it relates to document B, but the author of B may think otherwise. One of the reasons may be document A did not exist when B got published.</p>
<p>Multiple or expired data may also not be desirable at times. For example page visitor statistics data within a site introduction document. To solve this issue, one of the proposed way is to separate the statistics statement data out of the introduction document into an individual document.</p>
<p>In the end, links opens up the web of data to not only human beings, but also <acronym title="Artificial Intelligence">AI</acronym> processes to allow them to make inferences out of entities. Besides, it also encourages all parties to publsih data freely in an open standard format.</p>
<p>Data summarized and greatly simplified from the following sources:</p>
<ul>
<li>Sir Tim Bernes-Lee &#8211; <a href="http://www.w3.org/DesignIssues/LinkedData.html">Linked Data &#8211; Design Principle</a></li>
<li><a href="http://linkeddatatools.com/semantic-web-basics">Linked Data</a></li>
<p><!-- linkeddatatools.com -->
</ul>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=S_U6nC9g1f8:TuitCya15yU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=S_U6nC9g1f8:TuitCya15yU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=S_U6nC9g1f8:TuitCya15yU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=S_U6nC9g1f8:TuitCya15yU:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=S_U6nC9g1f8:TuitCya15yU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=S_U6nC9g1f8:TuitCya15yU:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=S_U6nC9g1f8:TuitCya15yU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=S_U6nC9g1f8:TuitCya15yU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/S_U6nC9g1f8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2010/06/10/linked-data-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2010/06/10/linked-data-principle/</feedburner:origLink></item>
		<item>
		<title>Resource Definition Framework</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/enbRgxrlxzA/</link>
		<comments>http://cslai.coolsilon.com/2010/06/04/resource-definition-framework/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 10:38:18 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Resource Description Framework]]></category>
		<category><![CDATA[RDF]]></category>
		<category><![CDATA[RDF Graph]]></category>
		<category><![CDATA[RDF Triple]]></category>
		<category><![CDATA[Semantic Network]]></category>
		<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[URI]]></category>
		<category><![CDATA[URL]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=168</guid>
		<description><![CDATA[As the name implies, Resource Definition Framework, or RDF in short, is a language to represent information about resources in world wide web. Information that can be represented is mostly metadata like title (assuming the resource is a web-page), author, last modified date etc. Besides representing resource that is network-accessible, it can be used to [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/fwgE56TbhfLKE6l8w3n6lCBTGe0/0/da"><img src="http://feedads.g.doubleclick.net/~a/fwgE56TbhfLKE6l8w3n6lCBTGe0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/fwgE56TbhfLKE6l8w3n6lCBTGe0/1/da"><img src="http://feedads.g.doubleclick.net/~a/fwgE56TbhfLKE6l8w3n6lCBTGe0/1/di" border="0" ismap="true"></img></a></p><p>As the name implies, Resource Definition Framework, or RDF in short, is a language to represent information about resources in world wide web. Information that can be represented is mostly metadata like title (assuming the resource is a web-page), author, last modified date etc. Besides representing resource that is network-accessible, it can be used to represent things that cannot be accessed through the network, as long as it can be identified using a URI.</p>
<p><span id="more-168"></span></p>
<p>The main objective of RDF is to generate information that can be processed by applications by defining a standardized approach to represent resources. The usage of standardized language also enables interchanging of information between applications without loss of meaning. This allows third party applications to use / consume information created and because the information format is standardized, tools are readily available to manipulate the information.</p>
<p>As mentioned earlier, as long as a thing can be represented in the form of Universal Resource Identifier, URI, then it can be represented / described by RDF. URI is generally used to represent not only network-accessible things, but also non-network accessible things like a arbitrary human being, corporation, or even a book in a library as well as abstract concepts that do not necessarily exist physically like creator / author / modified date. URL, which stands for Uniform Resource Locator is a subset of URI.</p>
<p>RDF is a simple language that deals with only binary relationship, which involves a Subject, a Predicate and a Object. Given an example &#8220;web(cslai) is Jeffrey04&#8242;s blog&#8221;, we can re-structure the statement into subject = <strong>web(cslai)</strong>, predicate = <strong>owner</strong>, object = <strong>Jeffrey04</strong>. Then we can put this into a graph (kinda reminds me of Semantic Network), as follows (not RDF graph):</p>
<p><img src="http://cslai.coolsilon.com/wp-site/uploads/graphviz/rdf_example.gif" tittle="rdf_example"><br>
<small>rdf_example <a href="http://ociotec.com/index.php/2008/02/25/eht-graphviz-plugin-para-wordpress/" target="_blank">&copy;</a></small>
</p>
<p>From the graph, we can see that the relationship between a subject and object is described by the predicate. Or another way of saying, <strong>subject</strong> ( web(cslai) ) has a property in the form of <strong>predicate</strong> (owner) that has a value of <strong>object</strong> ( Jeffrey04 ). </p>
<p>Before going to construct a RDF graph, it is important to know that as RDF is used to provide information on a resource, there are a set of basic rules to follow when constructing a RDF statement. Subject should always be a URI, or a blank node (will be discussed later) that denotes a resource, predicate must always be a URI and object can be another resource, a blank node or a constant represented by a character string.</p>
<p>Besides serializing a RDF graph into an XML file, the statements can also be written in the form of triples. Each statement in a graph is written as a simple triple of subject, predicate and object in exact order. Another point to note is that a graph is a primary manner of represent statement, and any other way to represent a statement is considered secondary.</p>
<p>The basic syntax of a triple requires URI to be enclosed in angle bracket or QNAME which kinda resembles XML vocabulary/namespace thingy, and literals to be enclosed in double quotes. For example</p>
<p><code>&lt;http://cslai.coolsilon.com/&gt; csterms:owner "Jeffrey04".</code></p>
<p>Blank node is introduced when a structured data presents as the object value. For example, given a triple as follows</p>
<p><code>exstaff85740 exterms:address "1501 Grant Avenue, Bedford, Massachusetts 01730"</code></p>
<p><img src="http://cslai.coolsilon.com/wp-site/uploads/graphviz/rdf_structured.gif" tittle="rdf_structured"><br>
<small>rdf_structured <a href="http://ociotec.com/index.php/2008/02/25/eht-graphviz-plugin-para-wordpress/" target="_blank">&copy;</a></small>
</p>
<p>Before discussing the graph, it is worth pointing out that each URI node is denoted by an ellipse, and literals denoted by a box. As seen on the graph, all the nodes are either a subject or an object while arcs are predicates. The respective RDF triples for the above graph are shown as follows:</p>
<pre><code>
exstaff:85740     exterms:address    exaddressid:85740 .
exaddressid:85740 exterms:street     "1501 Grant Avenue" .
exaddressid:85740 exterms:city       "Bedford" .
exaddressid:85740 exterms:state      "Massachusette" .
exaddressid:85740 exterms:postalCode "01730" .
</code></pre>
<p>As seen in both the graph as well as the RDF triples, a new node is created just to describe the concept of address. To represent the same piece of information in another way without having to create a new node, a blank node can be introduced, as follows</p>
<p><img src="http://cslai.coolsilon.com/wp-site/uploads/graphviz/rdf_structured_with_blank_node.gif" tittle="rdf_structured_with_blank_node"><br>
<small>rdf_structured_with_blank_node <a href="http://ociotec.com/index.php/2008/02/25/eht-graphviz-plugin-para-wordpress/" target="_blank">&copy;</a></small>
</p>
<p>In RDF triples form</p>
<pre><code>
exstaff:85740 exterms:address    ??? .
???           exterms:street     "1501 Grant Avenue" .
???           exterms:city       "Bedford" .
???           exterms:state      "Massachusette" .
???           exterms:postalCode "01730" .
</code></pre>
<p>As seen from the graph, address node is changed from a node with URI address into a node that doesn&#8217;t have address which is called a blank node. Then in the triplets it is written as a &#8216;???&#8217; instead of the complete URI as shown in the above example. However, besides using a &#8216;???&#8217; to denote a blank node in triples, we can also use another form of representation in case there are a lot of blank nodes that represents different things within a graph. By reusing the same example, the triples can be rephrased as follows</p>
<pre><code>
exstaff:85740 exterms:address    _:johnaddress .
_:johnaddress exterms:street     "1501 Grant Avenue" .
_:johnaddress exterms:city       "Bedford" .
_:johnaddress exterms:state      "Massachusette" .
_:johnaddress exterms:postalCode "01730" .
</code></pre>
<p>By breaking up the address into smaller structured parts, it enables external applications to manipulate the information in a more standardized way to produce a more predictable result. However, because RDF only deals with binary relationship, to properly describes N-ary relationship, it has to be broken into a list of binary relationship with the use of blank nodes. Somehow, this reminds me of something similar in prolog where you can create a kind of variable that the programmer do not need to explicitly name them.</p>
<p>Besides being used for the above situation, blank node is also often used in situation where there is no other way to properly and accurately describe a resource. For example, a person with email address johndoe@example.com, although mailto:johndoe@example.com is a valid URI, but as mailbox address is also used as an attribute, the better way of representing John Doe is by using a blank node with mailto:johndoe@example.com as object, as follows:</p>
<p><code>_:john exterms:mailbox &lt;mailto:johndoe@example.com&gt; .</code></p>
<p>An example of combining multiple RDF that is scattered around the internet is assuming there is a book that is authored by an author that uses johndoe@example.com, we can make inference by comparing the following triple</p>
<p><code>ex2terms:book78354 exterms:mailbox &lt;mailto:johndoe@example.com&gt; .</code></p>
<p>We can then deduce that The book is written by John Doe that has email address of johndoe@example.com.</p>
<p>However, because by default object allows any literals, it may make other application that consumes the information in trouble. For example, by looking at the triple below,</p>
<p><code>_:jeff exterms:age "24" .</code></p>
<p>There is no way to tell whether that 24 is a base 10 decimal, or is an octal number. Things can only go worse if the application is actually expecting a float. Type literal is introduced to solve the problem by allowing the specification of a particular datatype to be used in literals. Back to the previous example, to properly define my age, I should prepare a triple as follows:</p>
<p><code>_:jeff exterms:age "24"^^xsd:integer .</code> </p>
<p>Content is summarized / oversimplified from <a href="http://www.w3.org/TR/2004/REC-rdf-primer-20040210/">RDF Primer</a>.</p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=enbRgxrlxzA:G8IiTjX8BFs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=enbRgxrlxzA:G8IiTjX8BFs:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=enbRgxrlxzA:G8IiTjX8BFs:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=enbRgxrlxzA:G8IiTjX8BFs:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=enbRgxrlxzA:G8IiTjX8BFs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=enbRgxrlxzA:G8IiTjX8BFs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=enbRgxrlxzA:G8IiTjX8BFs:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=enbRgxrlxzA:G8IiTjX8BFs:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/enbRgxrlxzA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2010/06/04/resource-definition-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2010/06/04/resource-definition-framework/</feedburner:origLink></item>
		<item>
		<title>Semantic Web</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/a28lx56536s/</link>
		<comments>http://cslai.coolsilon.com/2010/06/03/semantic-web/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 08:46:14 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[Microformats]]></category>
		<category><![CDATA[Research Topic]]></category>
		<category><![CDATA[Semantic HTML]]></category>
		<category><![CDATA[Semantic Network]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=152</guid>
		<description><![CDATA[I am currently preparing myself in applying a postgrad programme and is looking for a research topic. At first I wanted to do something that is related to cloud computing but after some discussion with people around me, they suggest me to do something on semantic web. While posting my notes here, I realized that [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/QUetE_ewgcRDpfe3JmPXK88ESXU/0/da"><img src="http://feedads.g.doubleclick.net/~a/QUetE_ewgcRDpfe3JmPXK88ESXU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/QUetE_ewgcRDpfe3JmPXK88ESXU/1/da"><img src="http://feedads.g.doubleclick.net/~a/QUetE_ewgcRDpfe3JmPXK88ESXU/1/di" border="0" ismap="true"></img></a></p><p>I am currently preparing myself in applying a postgrad programme and is looking for a research topic. At first I wanted to do something that is related to cloud computing but after some discussion with people around me, they suggest me to do something on semantic web. While posting my notes here, I realized that I had posted something on semantic network that looks like the base of semantic web <a href="http://cslai.coolsilon.com/2008/03/20/semantic-network/">here</a> (Post still &#8220;Under construction&#8221; as of writing, will post the diagrams later tonight).</p>
<p><span id="more-152"></span></p>
<h2>Semantic Web</h2>
<p>Just thought it would be better if I start by stating a problem, imagine one day Alice is looking for the price for a piece of movie DVD, what she would do is to get to the movie store website and search for the movie DVD. However, Alice might not have been able to get the price if she is not around as the task cannot be automated easily using a computer. This is because the web page that displays the price is prepared for human being like us.</p>
<p>Therefore semantic web is a proposed solution to the problem where information should be represented not only for human being like us to read, but also for machines to be able to understand and manipulate it. In short, the definition says that Semantic Web is an extension to the World Wide Web, which information is given well-defined meaning that enable both lay person and computers to work in co-operation.</p>
<h2>Representing Information</h2>
<h3>Semantic HTML</h3>
<p>From what I remember, before Google emerge as the most popular search engine, webmasters used to include descriptive meta tags within a HTML document, as follows:</p>
<p><code class="html"><br />
&lt;meta name="keywords" content="good looking, handsome, single"&gt;<br />
&lt;meta name="description" content="About Jeffrey04"&gt;<br />
&lt;meta name="author" content="Jeffrey04"&gt;<br />
</code></p>
<p>Such that, when user search with the keywords as specified above, the document above will be returned as one of the search results. However, although metadata is displayed via meta tags as mentioned above, it is never enough to enable computers to understand that &#8220;Jeffrey04 is staying in Malaysia&#8221; or &#8220;Jeffrey04 works in Kuala Lumpur&#8221;.</p>
<p>Then CSS got popular as people around starts encouraging separation of content and presentation. Therefore, more people start using HTML tags like <code class="html">&lt;strong&gt;</code> instead of <code class="html">&lt;b&gt;</code> because <code class="html">&lt;b&gt;</code> doesn&#8217;t carry any meaning. Usage of tags with semantic meaning also enable users that relies on screen-reader to further understanding the material.</p>
<p>However, often times especially while HTML5 is still under drafting we group content into sections enclosed within a <code class="html">&lt;div&gt;</code> tag. For formatting purpose, as well as giving the block of information meaningful to machines, a class name is often given to the block, eg. <code class="html">&lt;div class="header"&gt;</code>. The usage of tag attribute to give meaning to a piece of information leads to the development of various microformats.</p>
<h3>Microformats</h3>
<p>When web-developers start using tags that accurately representing information within a document, numerous efforts are made to further mark up a document according to standard to ease machine processing. If one have ever does screen-scraping, they will feel the pain of trying to make the script to scrap the right information out of a HTML document.</p>
<p>To enable information to be read easily out of a HTML document, what a web-developer can do is to mark up the information following the specific standard. One of the standards is hCalendar, which is used to describe dated event, for example to mark up an event that is taking place at 6th June 2010, we would do:</p>
<p><code class="html"><br />
&lt;p class="vevent"&gt;John Doe is &lt;span class="summary"&gt;getting married&lt;/span&gt; on 6th June 2010 at the &lt;span class="location"&gt;Community Hall&lt;/span&gt;. The ceremony will be held from &lt;abbr class="dtstart" title="2010-06-06T14:00:00+08:00"&gt;2PM&lt;/abbr&gt; till &lt;abbr class="dtend" title="2010-06-06T16:00:00+08:00"&gt;4PM&lt;/abbr&gt;.&lt;/p&gt;<br />
</code></p>
<p>Therefore, to do screen-scraping, one would just need to search for (via either CSS or XPath) the particular block of content above.</p>
<p>(to be continued, next is on Resource Description Framework, RDF, which loosely-related to my previous post on semantic net as linked above)</p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=a28lx56536s:BqYZa2Zkf4M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=a28lx56536s:BqYZa2Zkf4M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=a28lx56536s:BqYZa2Zkf4M:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=a28lx56536s:BqYZa2Zkf4M:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=a28lx56536s:BqYZa2Zkf4M:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=a28lx56536s:BqYZa2Zkf4M:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=a28lx56536s:BqYZa2Zkf4M:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=a28lx56536s:BqYZa2Zkf4M:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/a28lx56536s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2010/06/03/semantic-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2010/06/03/semantic-web/</feedburner:origLink></item>
		<item>
		<title>Writing jQuery Plugin 101</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/Js4k2rSiBVA/</link>
		<comments>http://cslai.coolsilon.com/2010/03/04/writing-jquery-plugin-101/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 17:25:05 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=145</guid>
		<description><![CDATA[I often struggle to get my Javascript code organized, and have tried numerous ways to do so. I have tried putting relevant code into classes and instantiate as needed, then abuse jQuery&#8217;s data() method to store everything (from scalar values to functions and callbacks). Recently, after knowing (briefly) how a jQuery plugin should be written, [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/wf1nSyL9nljEPyAb_EQE_YYf8yg/0/da"><img src="http://feedads.g.doubleclick.net/~a/wf1nSyL9nljEPyAb_EQE_YYf8yg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/wf1nSyL9nljEPyAb_EQE_YYf8yg/1/da"><img src="http://feedads.g.doubleclick.net/~a/wf1nSyL9nljEPyAb_EQE_YYf8yg/1/di" border="0" ismap="true"></img></a></p><p>I often struggle to get my Javascript code organized, and have tried numerous ways to do so. I have tried putting relevant code into <a href="http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/">classes</a> and instantiate as needed, then abuse jQuery&#8217;s <code class="javascript">data()</code> method to store everything (from scalar values to functions and callbacks). Recently, after knowing (briefly) how a jQuery plugin should be written, it does greatly simplify my code.</p>
<p><span id="more-145"></span></p>
<p>While there are still a lot to learn about plugin authoring, I would only jot down what I know right now. As the code may not conform to the best practice, please let me know if there is a better way.</p>
<p>To begin, we start with an empty html page</p>
<p><code class="html"><br />
&lt;html&gt;<br />
&lt;head&gt;<br />
       &lt;title&gt;Title&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
       &lt;input type="text" id="a" /&gt;&lt;br /&gt;<br />
       &lt;input type="text" id="b" /&gt;&lt;br /&gt;<br />
       &lt;input type="text" id="c" /&gt;<br />
       &lt;script type="text/javascript" src="./jquery-1.4.1.min.js"&gt;&lt;/script&gt;<br />
       &lt;script type="text/javascript" src="./script.js"&gt;&lt;/script&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>The first included Javascript script is obviously jQuery, the second file (script.js) is my code, which is not named properly. The basic structure of the file should look like follows:</p>
<p><code class="javascript"><br />
(function($) {<br />
    var module_init = function(plugin, module) {<br />
            return function(action) {<br />
                var arg = trim_argument(arguments);<br />
                return this.each(function() {<br />
                    if($.fn[plugin][module][action]) {<br />
                        $.fn[plugin][module][action]<br />
                            .apply(this, arg);<br />
                    } else {<br />
                        throw(action + '() does not exist');<br />
                    }<br />
                })<br />
            }<br />
        },<br />
&nbsp;<br />
        module_exists = function(plugin, module) {<br />
            return $.fn[plugin][module];<br />
        },<br />
&nbsp;<br />
        shift_argument = function(item, arg) {<br />
            result = [item];<br />
&nbsp;<br />
            for(i = 0; i < arg.length; i++) {<br />
                result.push(arg[i]);<br />
            }<br />
&nbsp;<br />
            return result;<br />
        },<br />
&nbsp;<br />
        trim_argument = function(arg) {<br />
            result = [];<br />
&nbsp;<br />
            if(arg.length > 1) {<br />
                for(i = 1; i < arg.length; i++) {<br />
                    result.push(arg[i]);<br />
                }<br />
            }<br />
&nbsp;<br />
            return result;<br />
        };<br />
&nbsp;<br />
        // further code below this line<br />
})(jQuery);<br />
</code></p>
<p>Let us just ignore the functions above for now and proceed to the actual plugin code first. What we're trying to achieve here is to post whatever written to #a is posted to #b, and #b post it to #c. The function call should be as follows:</p>
<p><code class="javascript"><br />
$(selector).foo(sub_module, options);<br />
// or<br />
$(selector).foo(sub_module, action, [parameter_if_any]);<br />
</code></p>
<p><code class="javscript">foo</code> is the name of the example plugin, and <code class="javascript">sub_module</code> determines which sub-module is responsible to carry out the actual action specified by <code class="javascript">action</code>. If <code class="javascript">options</code> is passed in instead, then it would trigger <code class="javascript">init()</code> for the respective sub-module.</p>
<p>In short, <code class="javascript">foo()</code> is there just to notify the sub-module to call the right action, hence the code should be simple, as follows (add after the last comment in the basic structure code shown above):</p>
<p><code class="javascript"><br />
    (function foo() {<br />
        $.fn.foo = function(module, param) {<br />
            var arg = trim_argument(arguments);<br />
&nbsp;<br />
            return this.each(function() {<br />
                if(module_exists('foo', module)) {<br />
                    if(typeof param != 'string') {<br />
                        $.fn.foo[module]<br />
                            .apply($(this), shift_argument('init', arg));<br />
                    } else {<br />
                        $.fn.foo[module]<br />
                            .apply($(this), arg);<br />
                    }<br />
                } else {<br />
                    throw(module + ' module does not exists');<br />
                }<br />
            });<br />
        }<br />
    })();<br />
</code></p>
<p>I have a habit to group all the relevant bits together, hence the code is enclosed in an immediately executed function block. It does what it is designed to do -- to delegate the function call to the respective sub-module. <code class="javascript">trim_argument();</code> is there to remove the first argument (in this case <code class="javascript">module</code>), and <code class="javascript">shift_argument();</code> is used to insert an argument into the existing argument list. These two function functions are written to enable us to manipulate the arguments to be used in <code class="javascript">Function.apply()</code> method. Of course, if the specified sub-module does not exist, the code will throw an exception.</p>
<p>Now that we have the main plugin code written, next is to start developing the submodules. Firstly, is a plugin for the text box #a. The textbox is supposed to show textbox #b and passes the input text to #b whenever the value is not null. The code is to be added right after the main plugin, as mentioned above.</p>
<p><code class="javascript"><br />
    (function alpha() {<br />
        $.fn.foo.alpha = module_init('foo', 'alpha');<br />
&nbsp;<br />
        var alpha = $.fn.foo.alpha;<br />
&nbsp;<br />
        alpha.init = function(options) {<br />
            $(this)<br />
                .data('options', $.extend({}, options))<br />
                .keyup(alpha.alpha_update);<br />
        };<br />
&nbsp;<br />
        alpha.alpha_update = function() {<br />
            $(this)<br />
                .data('options').b<br />
                    .foo('bravo', 'alpha_update', $(this).val());<br />
        };<br />
    })();<br />
</code></p>
<p><code class="javascript">module_init</code> is there to do the actual delegation of action. Again, it will check with the remaining arguments after removing the first, which is the <code class="javascript">action</code>. The function will then attempt to call the action if available, otherwise a new exception is thrown.</p>
<p><code class="javascript"><abbr title="$.fn.foo.alpha">alpha</abbr>.init</code> is a compulsory method that initializes the sub-module. On the other hand, <code class="javascript">alpha.alpha_update</code> is the actual code that updates another textbox via another sub-module, which is named 'bravo', and coded as follows:</p>
<p><code class="javascript"><br />
    (function bravo() {<br />
        $.fn.foo.bravo = module_init('foo', 'bravo');<br />
&nbsp;<br />
        var bravo = $.fn.foo.bravo;<br />
&nbsp;<br />
        bravo.init = function(options) {<br />
            $(this)<br />
                .data('options', $.extend({}, options))<br />
                .hide();<br />
        };<br />
&nbsp;<br />
        bravo.hide = function() {<br />
            $(this).fadeOut('fast');<br />
        };<br />
&nbsp;<br />
        bravo.show = function() {<br />
            $(this).fadeIn('fast');<br />
        };<br />
&nbsp;<br />
        bravo.toggle = function(value) {<br />
            if($(this).is(':hidden') &#038;&#038; value.length > 0) {<br />
                bravo.show.call(this);<br />
            } else if($(this).is(':visible') &#038;&#038; value.length == 0) {<br />
                bravo.hide.call(this);<br />
            }<br />
        };<br />
&nbsp;<br />
        bravo.alpha_update = function(value) {<br />
            $(this)<br />
                .val('bravo: ' + value)<br />
                .data('options').c<br />
                    .foo('charlie', 'alpha_update', value);<br />
&nbsp;<br />
            bravo.toggle.call(this, value);<br />
        };<br />
    })();<br />
</code></p>
<p>The bravo code basically does what it is intended to, which is to receive update from alpha, and then toggle the display of the text box depending on the length of passed in value. After that, bravo is going to post the received update to charlie, as follows:</p>
<p><code class="javascript"><br />
    (function charlie() {<br />
        $.fn.foo.charlie = module_init('foo', 'charlie');<br />
&nbsp;<br />
        var charlie = $.fn.foo.charlie;<br />
&nbsp;<br />
        charlie.init = function(options) {<br />
            $(this)<br />
                .data('options', $.extend({}, options))<br />
                .hide();<br />
        };<br />
&nbsp;<br />
        charlie.hide = function() {<br />
            $(this).fadeOut('slow');<br />
        };<br />
&nbsp;<br />
        charlie.show = function() {<br />
            $(this).fadeIn('slow');<br />
        };<br />
&nbsp;<br />
        charlie.toggle = function(value) {<br />
            if($(this).is(':hidden') &#038;&#038; value.length > 0) {<br />
                charlie.show.call(this);<br />
            } else if($(this).is(':visible') &#038;&#038; value.length == 0) {<br />
                charlie.hide.call(this);<br />
            }<br />
        };<br />
&nbsp;<br />
        charlie.alpha_update = function(value) {<br />
            $(this).val('charlie: ' + value);<br />
&nbsp;<br />
            charlie.toggle.call(this, value);<br />
        };<br />
</code></p>
<p>After receiving data from bravo, likewise, it updates the textbox, then display or hide the textbook accordingly. For every action method call, <code class="javascript">this</code> keyword is usually similar to the jQuery callback functions, which refers to the respective html element (not wrapped with jQuery functions), hence the function call within the sub-module is often via <code class="javascript">Function.call() /* or */ Function.apply()</code>.</p>
<p>To run the code, one only need the following code:</p>
<p><code class="javascript"><br />
    $(function() {<br />
        var a = $('#a'),<br />
            b = $('#b'),<br />
            c = $('#c');<br />
&nbsp;<br />
        a.foo('alpha', { b: b, c: c }).focus();<br />
        b.foo('bravo', { a: a, c: c });<br />
        c.foo('charlie', { a: a, b: b });<br />
    });<br />
</code></p>
<p>That's all, we have done a very simple, and pointless plugin.</p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=Js4k2rSiBVA:BTT8uD7CQLU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Js4k2rSiBVA:BTT8uD7CQLU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=Js4k2rSiBVA:BTT8uD7CQLU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Js4k2rSiBVA:BTT8uD7CQLU:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Js4k2rSiBVA:BTT8uD7CQLU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=Js4k2rSiBVA:BTT8uD7CQLU:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Js4k2rSiBVA:BTT8uD7CQLU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=Js4k2rSiBVA:BTT8uD7CQLU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/Js4k2rSiBVA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2010/03/04/writing-jquery-plugin-101/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2010/03/04/writing-jquery-plugin-101/</feedburner:origLink></item>
		<item>
		<title>Mixing Non-array and Array in array_map()</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/i1rqaMvqbr0/</link>
		<comments>http://cslai.coolsilon.com/2010/01/28/mixing-non-array-and-array-in-array_map/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 03:34:07 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array_map]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=141</guid>
		<description><![CDATA[array_map function is a function that I use the most in my php scripts recently. However, there are times where I want to pass some non-array into it, therefore often times I have code like the snippet shown below: $result = array_map( 'some_callback', array_fill(0, count($some_array), 'some_string'), array_fill(0, count($some_array), 'some_other_string'), $some_array ) It doesn&#8217;t look good [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/qME5YbAXF4imHHcPijnJGmSKw1g/0/da"><img src="http://feedads.g.doubleclick.net/~a/qME5YbAXF4imHHcPijnJGmSKw1g/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/qME5YbAXF4imHHcPijnJGmSKw1g/1/da"><img src="http://feedads.g.doubleclick.net/~a/qME5YbAXF4imHHcPijnJGmSKw1g/1/di" border="0" ismap="true"></img></a></p><p><a href="http://www.php.net/manual/en/function.array-map.php">array_map</a> function is a function that I use the most in my php scripts recently. However, there are times where I want to pass some non-array into it, therefore often times I have code like the snippet shown below:</p>
<p><code class="php">$result = array_map(<br />
    'some_callback',<br />
    array_fill(0, count($some_array), 'some_string'),<br />
    array_fill(0, count($some_array), 'some_other_string'),<br />
    $some_array<br />
)</code></p>
<p>It doesn&#8217;t look good IMO, as it makes the code looks complicated. Hence, after seeing how the code may vary in all different scenarios, I created some functions to clean up the array_map call as seen above. Code snippet after the jump</p>
<p><span id="more-141"></span></p>
<p><code class="php">function array_map_mix($callback, $param) {<br />
    $main = array();</p>
<p>    $args = func_get_args();</p>
<p>    if((is_numeric($param) &#038;&#038; count($args) < 3)<br />
        || (is_array($param) &#038;&#038; count($args) < 2)) {<br />
        throw new Exception('No input available.');<br />
    } elseif(is_array($param)) {<br />
        list($main, $param) = array($param, count($param));<br />
    } elseif(is_numeric($param) === FALSE) {<br />
        throw new Exception('Invalid parameter');<br />
    }</p>
<p>    return call_user_func_array(<br />
        'array_map',<br />
        array_merge(<br />
            array($callback),<br />
            count($main) > 0 ? array($main) : array(),<br />
            count($args) - 2 > 0 ?<br />
                array_map(<br />
                    'array_create',<br />
                    array_create(count($args) - 2, $param),<br />
                    array_slice($args, 2)<br />
                )<br />
                : array()<br />
        )<br />
    );<br />
}</p>
<p>function array_create($count, $input) {<br />
    $result = array_fill(0, $count, NULL);</p>
<p>    if(is_array($input) &#038;&#038; count($input) == 1 &#038;&#038; is_array($input[0])) {<br />
        $result = array_fill(0, $count, $input[0]);<br />
    } else if(is_array($input) &#038;&#038; count($input) == $count) {<br />
        $result = $input;<br />
    } else {<br />
        $result = array_fill(0, $count, $input);<br />
    }</p>
<p>    return $result;<br />
}</code></p>
<h2>array_create</h2>
<p>Before stepping into the main array_map_mix function, we first look at the array_create function. It is basically a variant of <a href="http://www.php.net/manual/en/function.array-fill.php">array_fill</a> function, but without the initial index argument.</p>
<p>The important argument here is the <code class="php">$input</code> argument, depending on what is passed into the function, the end result can be different. If anything but array is passed in, then it will behave exactly like the array_fill call as follows:</p>
<p><code class="php">$foo = array_create(5, 'bar');<br />
// is equivalent to<br />
$foo = array_fill(0, 5, 'bar');</code></p>
<p>However, if an array is passed in, and the array element count is equivalent to the <code class="php">$count</code> argument, then array_create will return <code class="php">$input</code> without any modification.</p>
<p><code class="php">array('bar', 'baz') == array_create(2, array('bar', 'baz'));</code></p>
<p>If the array passed in has one and only one element, and the element happens to be another array, then the child array is passed into the array_fill function, as follows</p>
<p><code class="php">$foo = array_create(3, array(array('bar', 'baz')));<br />
// is equivalent to<br />
$foo = array_fill(0, 3, array('bar', 'baz'))</code></p>
<p>If the array passed in doesn&#8217;t fulfill the above two conditions, then the end result is similar like passing in a non-array argument.</p>
<p><code class="php">$foo = array_create(3, array('bar'));<br />
// is equivalent to<br />
$foo = array_fill(0, 3, array('bar'));</code></p>
<p>The weird behavior caused by the array argument is actually intended, as this function is initially created for the following function, which is the array_map_mix function.</p>
<h2>array_map_mix</h2>
<p>The first 2 mandatory arguments for array_map_mix are the <code class="php">$callback</code> as well as <code class="php">$param</code>. The <code class="php">$callback</code> is the callback function, and <code class="php">$param</code> is the parameter. The parameter can be either a number, or an array.</p>
<p>When a number is passed in as the parameter, then the code is throwing all the following arguments into array_create function as described above, with the number is sent in as <code class="php">$count</code>. This is an example showing the equivalent array_map function call.</p>
<p><code class="php">$foo = array_map_mix(<br />
    'some_callback',<br />
    2,<br />
    array('foo', 'bar'),<br />
    'baz'<br />
);<br />
// is equivalent to<br />
$foo = array_map(<br />
    'some_callback',<br />
    array('foo', 'bar'),<br />
    array_fill(0, 2, 'baz')<br />
);</code></p>
<p>What if the number passed in does not equal to the count of array elements?</p>
<p><code class="php">$foo = array_map_mix(<br />
    'some_callback',<br />
    2,<br />
    array('foo', 'bar', 'baz')<br />
);<br />
// is equivalent to<br />
$foo = array_map(<br />
    'some_callback',<br />
    array_fill(0, 2, array('foo', 'bar', 'baz'))<br />
);</code></p>
<p>What if I have 2 array, <code class="php">$foo</code> and <code class="php">$bar</code>, even both has the same amount of array elements, but I want the callback to receive <code class="php">$bar</code> as a whole, instead of just an element?</p>
<p><code class="php">// given<br />
count($foo) == count($bar);<br />
// wants this<br />
$foo = array_map(<br />
    'some_callback',<br />
    $foo,<br />
    array_fill(0, count($foo), $bar)<br />
);<br />
// equivalent array_map_mix call<br />
$foo = array_map_mix(<br />
    'some_callback',<br />
    count($foo),<br />
    $foo,<br />
    array($bar)<br />
);</code></p>
<p>Hopefully this explains why array_create is coded like above. As mentioned above, <code class="php">$param</code> can be an array as well. This is helpful when the count of array to be passed to array_map is dynamic and a fixed number is not always available.</p>
<p><code class="php">// suppose function foo() returns an array of variable size<br />
$foo = array_map_mix(<br />
    'some_callback',<br />
    foo(),<br />
    'baz'<br />
);<br />
// the equivalent code<br />
$bar = foo();<br />
$foo = array_map(<br />
    'some_callback',<br />
    $bar,<br />
    array_fill(0, count($bar), 'baz')<br />
);</code></p>
<p>Nevertheless, array_map_mix can also be used as a direct array_map alternative, although this is not advisible as the function call should take much longer time to complete because of the call_user_func_array call within array_map_mix.</p>
<p><code class="php">array_map('some_callback', array('foo', 'bar')) == array_map_mix('some_callback', array('foo', 'bar'));</code></p>
<p>The total number of arguments required is 2 when <code class="php">$param</code> is an array (means using array_map_mix as array_map) and 3 when <code class="php">$param</code> is a non-array. An exception will be thrown if expected argument is missing. Also, if anything but an array or a number is passed into array_map_mix as <code class="php">$param</code>, then another exception is thrown.</p>
<p>Hopefully these two short code snippet helps for people who uses array_map as much as I do.</p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=i1rqaMvqbr0:PL41qPfZgXs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=i1rqaMvqbr0:PL41qPfZgXs:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=i1rqaMvqbr0:PL41qPfZgXs:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=i1rqaMvqbr0:PL41qPfZgXs:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=i1rqaMvqbr0:PL41qPfZgXs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=i1rqaMvqbr0:PL41qPfZgXs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=i1rqaMvqbr0:PL41qPfZgXs:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=i1rqaMvqbr0:PL41qPfZgXs:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/i1rqaMvqbr0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2010/01/28/mixing-non-array-and-array-in-array_map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2010/01/28/mixing-non-array-and-array-in-array_map/</feedburner:origLink></item>
		<item>
		<title>Selecting Node with XPath</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/4Mdt5PcuufI/</link>
		<comments>http://cslai.coolsilon.com/2009/10/27/selecting-node-with-xpath/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 14:19:50 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=133</guid>
		<description><![CDATA[To do node selection for DOM operations, one typically uses CSS selectors as (probably) popularized by jQuery. However, there is another alternative that is as powerful if not better known as XPath. XPath may be able to do a lot more than just selecting node (which I have no time to find out for now) [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/--af8ZhnXQK4eLFTbqv74r2CikQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/--af8ZhnXQK4eLFTbqv74r2CikQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/--af8ZhnXQK4eLFTbqv74r2CikQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/--af8ZhnXQK4eLFTbqv74r2CikQ/1/di" border="0" ismap="true"></img></a></p><p>To do node selection for DOM operations, one typically uses CSS selectors as (probably) popularized by <a href="http://www.jquery.org/">jQuery</a>. However, there is another alternative that is as powerful if not better known as XPath. XPath may be able to do a lot more than just selecting node (which I have no time to find out for now) but I will just focus on how to do node selection in this blog post. </p>
<p><span id="more-133"></span> </p>
<p>The main source of inspiration of this post is from John Resig&#8217;s <a href="http://ejohn.org/blog/xpath-css-selectors/">blog post</a> comparing CSS selectors to XPath selectors. Recently I am involved in a project where I need to scrap information from a bunch of <del datetime="2009-10-27T07:02:46+00:00">badly marked up</del> HTML files. Although I am advised to use regular expression to scrap information out from the file, but I decided to give XPath a try. </p>
<p>The initial version was coded with PHP but it wouldn&#8217;t work efficiently, as the files are not correctly marked up (and also not marked up semantically) and I had to fix it by throwing the markup to Tidy before processing it. Then the markup is passed to the DOM library classes for data extraction. To sum up on the performance, it was terribly slow (mainly caused by the Tidy process).</p>
<p>Then I headed to <a href="http://www.stackoverflow.com/">stackoverflow</a> to <a href="http://stackoverflow.com/questions/1553511/extracting-info-from-html-using-phpxpath-php-pythonregexp-or-pythonxpath">look for alternatives</a>. As recommended, I use lxml and beautiful soup together to scrap information out from the source HTML file (details to be discussed in the other post).</p>
<p>Back to the topic, supposedly a HTML file is given that is marked up as follows:</p>
<p><code class="html"><br />
&lt;html&gt;<br />
    &lt;head&gt;<br />
        &lt;title&gt;Foo&lt;/title&gt;<br />
    &lt;/head&gt;<br />
    &lt;body&gt;<br />
        &lt;table&gt;<br />
            &lt;tr&gt;<br />
                &lt;td&gt;<br />
                    &lt;table&gt;<br />
                        &lt;tr&gt;<br />
                            &lt;th&gt;&lt;h1&gt;Main Content&lt;/h1&gt;&lt;/th&gt;<br />
                        &lt;/tr&gt;<br />
                        &lt;tr&gt;<br />
                            &lt;td&gt;&lt;span&gt;I am a terribly marked up HTML file&lt;/span&gt;&lt;/td&gt;<br />
                        &lt;/tr&gt;<br />
                        &lt;tr&gt;<br />
                            &lt;td&gt;&lt;span id=&quot;horrible_text&quot;&gt;Just too horrible&lt;/span&gt;&lt;/td&gt;<br />
                        &lt;/tr&gt;<br />
                    &lt;/table&gt;<br />
                &lt;/td&gt;<br />
                &lt;td&gt;<br />
                    &lt;table class=&quot;sidebar&quot;&gt;<br />
                        &lt;tr&gt;<br />
                            &lt;th colspan=&quot;2&quot;&gt;&lt;font&gt;Fancy Sidebar&lt;/font&gt;&lt;/th&gt;<br />
                        &lt;/tr&gt;<br />
                        &lt;tr&gt;<br />
                            &lt;td&gt;&lt;font&gt;Fizz&lt;/font&gt;&lt;/td&gt;<br />
                            &lt;td&gt;&lt;a href=&quot;another_broken_link.html&quot;&gt;Fancy menu 1&lt;/a&gt;&lt;/td&gt;<br />
                        &lt;/tr&gt;<br />
                        &lt;tr&gt;<br />
                            &lt;td&gt;&lt;font&gt;Buzz&lt;/font&gt;&lt;/td&gt;<br />
                            &lt;td&gt;&lt;a href=&quot;i_am_also_broken.html&quot;&gt;Fancy menu 2&lt;/a&gt;&lt;/td&gt;<br />
                        &lt;/tr&gt;<br />
                        &lt;tr&gt;<br />
                            &lt;td&gt;&lt;font&gt;Bar&lt;/font&gt;&lt;/td&gt;<br />
                            &lt;td&gt;&lt;a href=&quot;some_fancy_broken_link.html&quot;&gt;Fancy menu 3&lt;/a&gt;&lt;/td&gt;<br />
                        &lt;/tr&gt;<br />
                    &lt;/table&gt;<br />
                &lt;/td&gt;<br />
            &lt;/tr&gt;<br />
        &lt;/table&gt;<br />
        &lt;div id=&quot;footer&quot;&gt;<br />
            &lt;p&gt;Some meaningless copyright text&lt;/p&gt;<br />
        &lt;/div&gt;<br />
    &lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>As seen in the above HTML, it is not as badly marked up but it basically shows what kind of HTML files I am dealing with (except there is no single element ID defined). To quickly get started in working with XPath, I went to <a href="http://www.w3schools.com/XPath/default.asp">w3schools</a> to dig the tutorials as I am working with the file.</p>
<p>Starting from the simplest and most obvious case, which is looking for the copyright text node.</p>
<p><code>//*[@id='footer']/p</code></p>
<p>In CSS, one would probably get this done via</p>
<p><code>#footer > p</code></p>
<p>However, as I am doing this to get text information, hence in order to get the text content, I would need to do</p>
<p><code>//*[@id='footer']/p/text()</code></p>
<p>To summarize what those symbols mean, let me translate the last XPath selector into a layman friendly statement &#8212; select any node (empty tag followed by a wild card, <code>//*</code>) that has (<code>[...]</code>) an attribute named id (<code>@id</code>) which is valued &#8216;footer&#8217; (<code>@id='footer'</code>), from there, select a paragraph node (<code>/p</code>) and extract the text contained within the node (<code>/text()</code>).</p>
<p>Now that you get some idea, we shall proceed do a slightly more complicated selector. Suppose I want to extract content from the third row in the main content table, I would need to do this (as I&#8217;m new in this, this may not be the most efficient way of selecting)</p>
<p><code>//tr[*/descendant-or-self::*[contains(text(), 'Main') and contains(text(), 'Content')]]<br />
    /following-sibling::tr[2]<br />
    /td<br />
    /descendant-or-self::text()</code></p>
<p>Well, from the markup above, there is no id, no class and basically there is nothing to uniquely identify the node that I want. (I don&#8217;t know how to select the node using CSS, so assume there is none for now) Therefore, in order to select the text that I want, I select the any tr node from the document (<code>//tr</code>) that contains (<code>[...]</code>) any node (<code>*/descendant-or-self::*</code>) which in turns contains (the nested <code>[...]</code>) text &#8216;Main&#8217; and &#8216;Content&#8217; (<code>contains(text(), 'Main') and contains(text(), 'Content')</code>). From there, find the second sibling after the selected tr(s) (<code>/following-sibling::tr[2]</code>) and get the descendant which is the table data node (<code>/td</code>). Then regardless what node is wrapping the text content, just return any text-content found within the selected table data node (<code>/descendant-or-self::text()</code>).</p>
<p>You may be curious why I don&#8217;t just test <code>contains(text(), 'Main Content')</code> but to split them into two different tests. This is due to the fact that in HTML, although browsers treat all whitespace and newline characters as a space, but they do make a difference in the markup. So if it happens that my table header node is marked up as follows</p>
<p><code>&lt;th&gt;&lt;h1&gt;Main<br />
Content&lt;/h1&gt;&lt;/th&gt;</code></p>
<p>Although it is displayed identically within the browser, but if my selector tests using <code>contains(text(), 'Main Content')</code> it will fail as the correct way of doing it should be inserting a newline character instead of a space, maybe something like <code>contains(text(), "Main\nContent")</code>. There is a weakness in my approach though as the order is ignored in my test. So if there is a table within the document as follows</p>
<p><code class="html">&lt;table&gt;<br />
    &lt;tr&gt;<br />
        &lt;th&gt;&lt;h1&gt;Content not in the Main&lt;/h1&gt;&lt;/th&gt;<br />
    &lt;/tr&gt;<br />
    &lt;tr&gt;<br />
        &lt;td&gt;&lt;span&gt;I am a terribly marked up HTML file&lt;/span&gt;&lt;/td&gt;<br />
    &lt;/tr&gt;<br />
    &lt;tr&gt;<br />
        &lt;td&gt;&lt;span id=&quot;horrible_text&quot;&gt;Not supposed to be selected&lt;/span&gt;&lt;/td&gt;<br />
    &lt;/tr&gt;<br />
&lt;/table&gt;</code></p>
<p>Then the text &#8216;Not supposed to be selected&#8217; will also be selected.</p>
<p>Last example of the day, suppose I want to select all the second column of table data nodes within &#8216;Fancy Sidebar&#8217; if the first column has text content either &#8216;Fizz&#8217; or &#8216;Buzz&#8217;</p>
<p><code>//td[*/descendant-or-self::*[contains(text(), 'Fizz') or contains(text(), 'Buzz')]]<br />
    /following-sibling::td[position() = 1]</code></p>
<p>The selector basically says select any table data node (<code>//td</code>) that contains (<code>[..]</code>) regardless node type (<code>*/descendant-or-self::*</code>) as long as it contains (<code>[...]</code>) text either &#8216;Fizz&#8217; or &#8216;Buzz&#8217; (<code>contains(text(), 'Fizz') or contains(text(), 'Buzz')</code>). From there select its sibling table data nodes (<code>/following-sibling::td</code>) and pick the first among them (<code>[position() = 1]</code>).</p>
<p>If you are observant enough, you should find out that the selector may actually be simplified as follows</p>
<p><code>//td[*/descendant-or-self::*[contains(text(), 'Fizz') or contains(text(), 'Buzz')]]<br />
    /following-sibling::td</code></p>
<p>The previous selector was used because I wanted to show the <code>position()</code> function XD.</p>
<p>Lastly, if firebug is used, then <a href="https://addons.mozilla.org/en-US/firefox/addon/11900">FireXPath</a> is a great addition to the firebug that allows web developer to test XPath selectors. </p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=4Mdt5PcuufI:ICpYWh0FphA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=4Mdt5PcuufI:ICpYWh0FphA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=4Mdt5PcuufI:ICpYWh0FphA:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=4Mdt5PcuufI:ICpYWh0FphA:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=4Mdt5PcuufI:ICpYWh0FphA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=4Mdt5PcuufI:ICpYWh0FphA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=4Mdt5PcuufI:ICpYWh0FphA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=4Mdt5PcuufI:ICpYWh0FphA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/4Mdt5PcuufI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/10/27/selecting-node-with-xpath/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2009/10/27/selecting-node-with-xpath/</feedburner:origLink></item>
		<item>
		<title>Hello World from Zend MVC</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/XWtwYS9RIhw/</link>
		<comments>http://cslai.coolsilon.com/2009/09/08/hello-world-from-zend-mvc/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 14:00:22 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=125</guid>
		<description><![CDATA[This post continued from this post. Finally I have found some time to start developing my pet project using Zend Framework. After getting the controller to work the way I am more familiar (comparing to Kohana which I used at work) with, the next step is to get it to output some data. Base Action [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/HefltVxieOwPX5L5n-a_3Nj11rg/0/da"><img src="http://feedads.g.doubleclick.net/~a/HefltVxieOwPX5L5n-a_3Nj11rg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/HefltVxieOwPX5L5n-a_3Nj11rg/1/da"><img src="http://feedads.g.doubleclick.net/~a/HefltVxieOwPX5L5n-a_3Nj11rg/1/di" border="0" ismap="true"></img></a></p><p>This post continued from this <a href="http://cslai.coolsilon.com/2009/03/28/extending-zend-framework/">post</a>. Finally I have found some time to start developing my pet project using <a href="http://framework.zend.com/">Zend Framework</a>. After getting the controller to work the way I am more familiar (comparing to <a href="http://www.kohanaphp.com/">Kohana</a> which I used at work) with, the next step is to get it to output some data.</p>
<p><span id="more-125"></span></p>
<h3>Base Action Controller</h3>
<p>When I used other frameworks like Kohana or <a href="http://codeigniter.com/">CodeIgniter</a>, without extra modules installed, I would have to define view scripts specifically. However, in Zend_Controller, a view is likely to be defined properly and is waiting for data to be populated in. However, I still don&#8217;t get any output after a few attempts so I went to compare my dispatch method in the derived action controller with Zend_Controller_Action. The following code shows the updated dispatch method:</p>
<p><code class="php"><br />
    public function dispatch($actionName) {<br />
        $this->_helper->notifyPreDispatch();<br />
        $this->preDispatch();<br />
        $parameters = array();<br />
        foreach($this->_parametersMeta($actionName) as $paramMeta) {<br />
            $parameters = array_merge(<br />
                $parameters,<br />
                $this->_parameter($paramMeta, $this->_getAllParams())<br />
            );<br />
        }<br />
        call_user_func_array(array(&#038;$this, $actionName), $parameters);<br />
        $this->postDispatch();<br />
        $this->_helper->notifyPostDispatch();<br />
    }<br />
</code></p>
<p>Apparently there are some &#8216;hooks&#8217; that needs to be called throughout the dispatching process. Besides that, I want my view scripts to be loaded from another folder instead of the default ones, so I added a init method in the base action controller, as follows:</p>
<p><code class="php"><br />
    public function init() {<br />
        $this->view->addScriptPath(sprintf('%s/View', APP_PATH));<br />
    }<br />
</code></p>
<p>The script path should be defined during bootstrap phase, and I will post up a better solution once I find the exact place to configure it. Now that we have our base action controller fixed, let&#8217;s move to the actual action controller.</p>
<h3>Action Controller</h3>
<p>I am not sure whether action stack is <a href="http://en.wikipedia.org/wiki/Presentation-abstraction-control">HMVC</a>, however, I like the way it works (although it is <a href="http://www.rmauger.co.uk/2009/03/why-the-zend-framework-actionstack-is-evil/">evil</a>). I will probably stick to action stack for the time being while finding out how partial view can help in widgetizing my page. Besides having a view defined automatically, to put them into a layout I made a call in my bootstrap script, as follows:</p>
<p><code class="php"><br />
        Zend_Layout::startMvc(array(<br />
            'layoutPath' => APP_PATH . '/View/_layout'<br />
        ));<br />
</code></p>
<p>Then I did the action stack thingy as follows:</p>
<p><code class="php"><br />
class Controller_Index<br />
    extends Coolsilon_Controller_Base {<br />
    public function index() {<br />
        $this->_helper->actionStack('menu', 'index');<br />
    }<br />
    public function menu() {<br />
        $this->_helper->viewRenderer->setResponseSegment('menu');<br />
    }<br />
}<br />
</code></p>
<p>Then, in my View/_layout/layout.phtml, </p>
<p><code class="php"><br />
< ?php echo $this->layout()->menu; ?><br />
< ?php echo $this->layout()->content; ?><br />
</code></p>
<p>View/index/index.phtml</p>
<p><code>This is the main content.</code></p>
<p>View/index/menu.phtml</p>
<p><code>This is the menu.</code></p>
<p>Then by visiting the project site, then I get the following output</p>
<p><code><br />
This is the menu.<br />
This is the main content.<br />
</code></p>
<p>Phew, I can&#8217;t believe I spent so much time on this easy stuff, XD. Now I am one step closer in writting the actual website, now what kind of website should I make?</p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=XWtwYS9RIhw:Ndn_mSYgs4Y:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=XWtwYS9RIhw:Ndn_mSYgs4Y:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=XWtwYS9RIhw:Ndn_mSYgs4Y:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=XWtwYS9RIhw:Ndn_mSYgs4Y:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=XWtwYS9RIhw:Ndn_mSYgs4Y:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=XWtwYS9RIhw:Ndn_mSYgs4Y:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=XWtwYS9RIhw:Ndn_mSYgs4Y:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=XWtwYS9RIhw:Ndn_mSYgs4Y:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/XWtwYS9RIhw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/09/08/hello-world-from-zend-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2009/09/08/hello-world-from-zend-mvc/</feedburner:origLink></item>
		<item>
		<title>My Collection of Common Javascript Functions – Part 1</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/aSEwtjUQvBo/</link>
		<comments>http://cslai.coolsilon.com/2009/06/04/my-collection-of-common-javascript-functions-part-1/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 13:19:14 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=121</guid>
		<description><![CDATA[After coded enough Javascript few months back, I found that there are a couple of functions that I kept re-using in different projects. Therefore I took some time to refactor them and re-arrange them into a single file. The common code that I keep reusing even today consists of functions that does prototypical inheritance, scope [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/wNtgfKaGsWPNmnmdd2XzJTTkh6o/0/da"><img src="http://feedads.g.doubleclick.net/~a/wNtgfKaGsWPNmnmdd2XzJTTkh6o/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/wNtgfKaGsWPNmnmdd2XzJTTkh6o/1/da"><img src="http://feedads.g.doubleclick.net/~a/wNtgfKaGsWPNmnmdd2XzJTTkh6o/1/di" border="0" ismap="true"></img></a></p><p>After coded enough Javascript few months back, I found that there are a couple of functions that I kept re-using in different projects. Therefore I took some time to refactor them and re-arrange them into a single file. The common code that I keep reusing even today consists of functions that does prototypical inheritance, <a href="http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/">scope maintenance</a>, some <a href="http://www.jquery.com/">jquery</a> stuff, <a href="http://code.google.com/apis/maps/">google maps api</a> stuff and some general ajax application related code.</p>
<p><span id="more-121"></span></p>
<h3><del datetime="2010-02-18T06:44:55+00:00">Prototypical</del>Prototypal Inheritance</h3>
<p>Basically after reading through some articles across the internet, especially <a href="http://javascript.crockford.com/prototypal.html">this article</a>, then I have this piece of code in my collection.</p>
<pre class="ln-"><code class="javascript">if(typeof Object.create !== 'function') {
    Object.create = function(_o) {
        var F = function () {}
        F.prototype = _o;
        return new F();
    };
}</code></pre>
<p>It is basically the same piece of code because I find it cleaner and more efficient than my initial draft.</p>
<h3>Namespacing</h3>
<p>Everyone agrees that global variables are evil, therefore I group all my common functions and classes into one single object, as follows,</p>
<pre class="ln-"><code class="javascript">var cs = Object.create(new function() {
    // some other code
}</code></pre>
<p>And I find the way <a href="http://developer.yahoo.com/yui">YUI</a> manages the modules good enough for me, then I emulated YAHOO.namespace function by having this in my object.</p>
<pre class="ln-"><code class="javascript">    this.namespace = function(_namespace) {
        if(!this[_namespace]) {
            this[_namespace] = Object.create(new function() {});
        }

        return this[_namespace];
    }</code></pre>
<p>The code may not be clever/clean, but as long as it does the job I have no complaint.</p>
<h3>Preserving State</h3>
<p>I didn&#8217;t quite like the delegate function as I can&#8217;t pass in extra arguments, therefore I have this after some refactoring</p>
<pre class="ln-"><code class="javascript">    var __delegate = function(_method, _scope) {
        var _arguments = arguments;
        return function() {
            return _method.apply(
                _scope, compile_arguments.call(
                    this, _arguments, [], 2
            ));
        }
    };

    var compile_arguments = function(_arguments, _result, _start) {
        for(var i = _start; _arguments.length > _start &#038;&#038; i < _arguments.length; i++) {
            _result.push(_arguments[i]);
        }

        return _result;
    };

    this.delegate = __delegate;
</code></code></pre>
<h3>Emulate Functional Programming</h3>
<p>I have recently decided to learn a little bit on functional programming recently but I haven&#8217;t decide which language I am picking. I have some experience in SWI-Prolog but prolog is just a declarative functional programming and does not seem to offer closure, higher order functions and currying. Anyway, back to the topic, after spending some time in this <a href="http://eloquentjavascript.net/chapter6.html">book</a>, I modified some code in the book and result in&#8230;</p>
<pre class="ln-"><code class="javascript">    this.functional = Object.create(new function() {
        this.map = function(_function, _arguments) {
            var result = [];

            for(var i = 0; i < _arguments.length; i++) {
                result.push(_function(_arguments[i]));
            }

            return result;
        };

        this.reduce = function(_combine, _base, _arguments) {
            return _arguments.length > 0 ?
                this.reduce(
                    _combine, _combine(_base, _arguments.shift()),
                    _arguments
                ) : _base;
        };
    });</code></pre>
<p>I am not sure if I can do the similar recursion in cs.functional.map compared to cs.functional.reduce because I am rather weak in recursion.</p>
<p><del datetime="2009-08-15T14:20:38+00:00">That&#8217;s all for now, and in the second part of this article I will post some helper functions for use with some third party libraries like jQuery and Google Maps API.</del></p>
<p>EDIT 15/8/2009 There are some changes to this set of functions, will post an update soon and there will probably no part 2 any time soon.</p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=aSEwtjUQvBo:jlvYEx7DGdw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=aSEwtjUQvBo:jlvYEx7DGdw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=aSEwtjUQvBo:jlvYEx7DGdw:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=aSEwtjUQvBo:jlvYEx7DGdw:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=aSEwtjUQvBo:jlvYEx7DGdw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=aSEwtjUQvBo:jlvYEx7DGdw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=aSEwtjUQvBo:jlvYEx7DGdw:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=aSEwtjUQvBo:jlvYEx7DGdw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/aSEwtjUQvBo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/06/04/my-collection-of-common-javascript-functions-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2009/06/04/my-collection-of-common-javascript-functions-part-1/</feedburner:origLink></item>
		<item>
		<title>Maintaining State with jQuery Event</title>
		<link>http://feedproxy.google.com/~r/cslai/~3/xHWTyH7yu4w/</link>
		<comments>http://cslai.coolsilon.com/2009/05/04/maintaining-state-with-jquery-event/#comments</comments>
		<pubDate>Mon, 04 May 2009 13:17:24 +0000</pubDate>
		<dc:creator>Jeffrey04</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://cslai.coolsilon.com/?p=118</guid>
		<description><![CDATA[Maintaining state in Javascript is not too difficult once you catch the idea. However, as I am not a super brilliant programmer, it takes me some time to find a way to maintain state as YUI Event does in jQuery. I like grouping relevant functions into a class as it makes code easier to manage [...]]]></description>
			<content:encoded><![CDATA[
<p><a href="http://feedads.g.doubleclick.net/~a/xp8Yh9JpjNUEAff2McJamEjxZkc/0/da"><img src="http://feedads.g.doubleclick.net/~a/xp8Yh9JpjNUEAff2McJamEjxZkc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/xp8Yh9JpjNUEAff2McJamEjxZkc/1/da"><img src="http://feedads.g.doubleclick.net/~a/xp8Yh9JpjNUEAff2McJamEjxZkc/1/di" border="0" ismap="true"></img></a></p><p>Maintaining state in Javascript is not too difficult once you catch the idea. However, as I am not a super brilliant programmer, it takes me some time to find a way to maintain state as <a href="http://developer.yahoo.com/yui/event/">YUI Event</a> does in <a href="http://jquery.com/">jQuery</a>.</p>
<p><span id="more-118"></span></p>
<p>I like grouping relevant functions into a class as it makes code easier to manage (for me, as there may be better ways. As I just recently found out how prototyping inheritance works, it is possible that the discovery may change the way I code in future), therefore the scope of event handler functions is very important. Making the scope (value/reference of <code class="javascript">this</code>) to the DOM element listened to the event does not help in accessing the shared data so I needed a way to change that behavior.</p>
<p>To solve the problem, a small derivation of the <a href="http://cslai.coolsilon.com/2009/04/11/random-javascript-notes/">previously discussed</a> delegate function will come in useful.</p>
<pre class="ln-">
<code class="javascript">
/**
 * Simulating YUI Event with jQuery
 */
(function($) {
EventHandler = function(_method, _scope) {
    var _argument = {};

    if(arguments.length > 2) {
        _argument = arguments[2];
    }

    // jquery default event callback format
    return function(_event) {
        // this = DOM element
        return _method.call(_scope, _event, this, _argument);
    };
};
})(jQuery);

var Foo = function() {

    var bar_handler = function(_event, _element) {
        alert('bar');
    }

    var foo_handler = function(_event, _element, _extra_argument) {
        alert(_extra_argument);
    }

    (function() {
        $('#foo').click(EventHandler(foo_handler, this, 'foo'));
        $('#bar').click(EventHandler(bar_handler, this));
    }).call(this);
};
var foo = new Foo();
</code>
</pre>
<p>That basically wraps up what I found these couple of days, if I manage to dig some time, the next post should be focused on prototypical inheritance.</p>
<div style='clear:both'></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cslai?a=xHWTyH7yu4w:EB2CxivKedw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cslai?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=xHWTyH7yu4w:EB2CxivKedw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cslai?i=xHWTyH7yu4w:EB2CxivKedw:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=xHWTyH7yu4w:EB2CxivKedw:YwkR-u9nhCs"><img src="http://feeds.feedburner.com/~ff/cslai?d=YwkR-u9nhCs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=xHWTyH7yu4w:EB2CxivKedw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cslai?i=xHWTyH7yu4w:EB2CxivKedw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=xHWTyH7yu4w:EB2CxivKedw:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/cslai?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cslai?a=xHWTyH7yu4w:EB2CxivKedw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/cslai?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cslai/~4/xHWTyH7yu4w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cslai.coolsilon.com/2009/05/04/maintaining-state-with-jquery-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cslai.coolsilon.com/2009/05/04/maintaining-state-with-jquery-event/</feedburner:origLink></item>
	</channel>
</rss>
