Archive Extractor – A Perch Hack / Plugin

Edit : Changes made as per Drew’s advice. Now deals with ’shared’ content and custom table prefix.

For sometime now I have been meaning to post this little hack I came up with — however I completely forgot. This is a hack for the brilliant ‘little’ CMS , Perch .
It was made through necessity as I wanted to use Perch but I needed to be able to have news items from other pages on the front page. To my surprise it was nice and easy, and although I’m sure it will be inefficient, it seems pretty fast.

It is an incredibly simple plugin that pulls the JSON out of the Perch database and returns it. It doesn’t try to do anything else — no formatting, no nothing — just the way I like it.

Standard disclaimer : I am not a server-side kinda guy, I can not take responsibility for anything this plugin does to your installation of Perch, server security or general happiness. That said, I can’t see how these lazy few lines of code could screw up much.

Installation

Download the file and put it in your Perch “plugins” folder. This isn’t mandatory for it to work but just seems like a logical place to put it. Watch out when you next need to update Perch — Back up!!

Usage

You must include the file at the top of the page you wish to use Archive Extractor. If your Perch directory is called “perch” then the include could be something like this:

	 include('perch/plugins/archiveExtractor/archiveExtractor.php');

To use just call the function with the 2 variables, the Perch Content name (e.g “News Item”) and the path to the page it is on (e.g “/news/index.php”). With the data returned you can iterate through the items and pull from it the data you want.
Maybe best seen as an example:

	// Call function with variables.
	$newsArchive = archiveExtractor('News Item','/news/index.php');
	// Check to see if there are any results
	if($newsArchive) {
		$limit = count($newsArchive);
		// Loop through all results
		for ($i=0; $i < $limit; $i++) {
			// In the example the word 'title' refers to the id of a content type 
			// from the 'News Item' content template
			echo '
  • '.$newsArchive[$i]->title.' Trip
    on '.$newsArchive[$i]->information.'.
  • '; } }

    If you want to extract data from shared regions, simply leave out the content path. It took me a while to understand the point of this (as this was recommended by Drew) but obviously it would be handy if you only wanted to extract part of the data (i.e. title and date).

    $newsArchive = archiveExtractor('News Item');
    
    Download Click to view code
    /*
    	Archive Extractor - A Perch Hack
    	By Ad Taylor - http://www.iamadtaylor.com
    	For usage see :
    	Creative Commons Attribution-Share Alike 2.0 UK: England & Wales Licence
    */
    
    function archiveExtractor($contentKey,$contentPage = '*')
    {	
    	$query = queryDB($contentKey,$contentPage,$limit);
    	if ($query) {
    		foreach ($query as $key => $value) {
    			$json = $value;
    		}
    
    		return PerchUtil::json_safe_decode($json);
    	} else {
    		return NULL;
    	}
    
    }
    
    
    function queryDB($contentKey,$contentPage) {
    	
    	require_once $_SERVER['DOCUMENT_ROOT'].'/edit/config/config.php';
    	
    	$link = @mysql_connect(PERCH_DB_SERVER, PERCH_DB_USERNAME, PERCH_DB_PASSWORD);
    	
    	if (!$link) {
    		return NULL;
    	}
    	else {
    		mysql_select_db(PERCH_DB_DATABASE,$link);
    		$sql = "SELECT `contentJSON` FROM `".PERCH_DB_PREFIX."contentItems` WHERE `contentKey` LIKE '$contentKey' AND `contentPage` LIKE CONVERT(_utf8 '$contentPage' USING latin1) OR '*' COLLATE latin1_swedish_ci";
    		$result = mysql_query($sql, $link);
    		return mysql_fetch_assoc($result);
    		mysql_close($link);
    		
    	}
    }