<?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>corprewland &#187; database hacking</title>
	<atom:link href="http://www.corprew.org/blog/tag/database-hacking/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.corprew.org</link>
	<description>(dis)information organization</description>
	<lastBuildDate>Thu, 04 Feb 2010 20:48:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>drupal and taxonomy</title>
		<link>http://www.corprew.org/blog/2008/04/17/drupal-and-taxonomy/</link>
		<comments>http://www.corprew.org/blog/2008/04/17/drupal-and-taxonomy/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 07:38:39 +0000</pubDate>
		<dc:creator>corprew</dc:creator>
				<category><![CDATA[computer programming]]></category>
		<category><![CDATA[acls]]></category>
		<category><![CDATA[bees]]></category>
		<category><![CDATA[cmses]]></category>
		<category><![CDATA[database hacking]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[drupal5]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[node-based-cms]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[organic systems]]></category>
		<category><![CDATA[simple solutions to complex problems]]></category>
		<category><![CDATA[taxonomy]]></category>
		<category><![CDATA[taxos]]></category>

		<guid isPermaLink="false">http://www.corprew.org/blog/2008/04/17/drupal-and-taxonomy/</guid>
		<description><![CDATA[Drupal 5 has a few problems in its security layer, as I&#8217;ve mentioned other places, and some of them stem from the sort of &#8216;it-works-for-me&#8217; philosophy of open source. This is particularly a problem in a complex system like Drupal, which in most installations is made up of a few dozen modules in addition to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.drupal.org/">Drupal 5</a> has a few problems in its security layer, as I&#8217;ve mentioned other places, and some of them stem from the sort of &#8216;it-works-for-me&#8217; philosophy of open source.   This is particularly a problem in a complex system like Drupal, which in most installations is made up of a few dozen modules in addition to the core.</p>
<p>The current issue I&#8217;m having is that nodes created by the aggregation module get their taxonomy stripped when they&#8217;re updated because of how another module uses the security functionality, which is just <em>hilarious</em> in a site that&#8217;s largely organized organically by taxonomy. So, after talking with the people I&#8217;m working for on the site, I ended up creating a simple PHP script to run through cron that fixes the issues &#8216;the hard way.&#8217;</p>
<p>If you check out this query&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> fix_object<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sqlcon</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT term_data.name name, term_data.tid termid, node.nid nodeid, node.title title FROM node LEFT JOIN term_node  ON ( term_node.nid = node.nid ) LEFT JOIN term_data ON ( term_data.tid = term_node.tid ) WHERE node.type = 'aggregation_item ' AND node.title LIKE 'Xxxxx &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;%'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Perform Query</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// ... and so on...</span></pre></div></div>

<p>You can see that this is a fairly normal sql query that looks for all the nodes of type aggregation_item and titled a particular pattern.  Because of the way the joins are structured, that means that any nodes that have lost their taxonomies will have NULL for termname and termid.  Those nodeids with NULL termids can then have the proper taxonomy entries stuffed back into them&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> insert_taxo_4_node<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxo_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO term_node (nid, tid) VALUES (&quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$node_id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$taxo_id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Check result</span>
  <span style="color: #666666; font-style: italic;">// This shows the actual query sent to MySQL, and the error. Useful for debugging.</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$message</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Invalid query: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Whole query: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;m largely posting this up in case people run into the same problem &#8212; this is a hilariously simple fix for a difficult to fix problem in drupal, but it&#8217;s a generic information architecture issue of what to do when the system that you&#8217;re working on is unreliable.  I should probably mention that the issues with security in drupal aren&#8217;t related to authentication, but instead are related to item ACLs denying access to things for strange reasons, and are not crucial security bugs in the OMG MUST PATCH NOW sense.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.corprew.org/blog/2008/04/17/drupal-and-taxonomy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
