Monday, August 25, 2008

Label Me

Of the API features that Google offers for their Blogger service the one that I was most surprised to see missing is a method to retrieve all the tags(category) for a blog. Though listed in their query parameters is the syntax for search by tag/category, the "cloud" was missing. The below code takes care of this missing feature.

::NOTE::
This class will be rolled into my helper classes soon.

class CW_Blog_Util {
const BLOGGER_URL = 'http://www.blogger.com/feeds/blogId/posts/default';

/**
* associative array holding blog tbl properties
*
* @var array
*/
private $_blogs = array();

/**
* blog title
*
* @var string
*/
private $_title = "";

private $_gDataQuery = null;

private $_gQuery = null;

public function __construct() {
$this->_gQuery = new Zend_Gdata();
}

public function getAllEntries() {

$query = $this->_getGdataQuery();
$feed = $this->_gQuery->getFeed($query);

return $feed;
}

public function getLatestBlog() {

$query = $this->_getGdataQuery();
$feed = $this->_gQuery->getFeed($query);

return $feed->current();
}

public function getBlogListByTag($tag) {

$query = $this->_getGdataQuery();
$query->setCategory($tag);

$feed = $this->_gQuery->getFeed($query);

return $feed;
}

public function getBlogLabels() {

$labels = array();

foreach ($this->getAllEntries() as $attr) {

for ($i = 0; $i < count($attr->category); $i++) {
$labels[] = $attr->category[$i]->term;
}
}

return array_count_values($labels);
}

private function _getGdataQuery() {
$this->_gDataQuery = new Zend_Gdata_Query(self::BLOGGER_URL);

return $this->_gDataQuery;
}

}

// Usage
$blogs = new CW_Blog_Util();

foreach ($blogs->getBlogLabels() as $label => $count) {
print "Label {$label}
";
}

No comments: