Zend_Gdata_Youtube and disallowing commenting/embedding of video

Even in the latest ZF stable release 1.11.4 there is still no support of new Youtube API accessControl element. The framework is still using yt:noembed option and has no support for disallowing commenting at all. As I tried yt:noembed – it wasn’t working, so we need to add support to ZF of this feature. First we need to understand how Youtube feed needs to be formed in order to prevent commenting and/or embedding. It’s done using element and its options.

and

Since ZF Gdata API has support of noembed option it has the class and methods forming RSS DOM that check for noembed element and append it where necessary, but there is no nocomment support, so we need the same extension class + DOM append this element. So first let’s create Extension_NoComment class similar to other extension classes:

 
/**
 * @see Zend_Gdata_Extension
 */
require_once 'Zend/Gdata/Extension.php';
 
 
class Zend_Gdata_YouTube_Extension_NoComment extends Zend_Gdata_Extension
{
 
    protected $_rootNamespace = 'yt';
    protected $_rootElement = 'accessControl';
    protected $_extensionAttributes = array(
                                            array('name'  => 'permission',
                                                  'value' => 'denied'),
                                            array('name'  => 'action',
                                                  'value' => 'comment'));
 
 
    public function __construct($enabled = null)
    {
        $this->registerAllNamespaces(Zend_Gdata_YouTube::$namespaces);
        parent::__construct();
    }
 
}


I skipped long phpDoc comments, but you’d rather use them. As you see we added protected element and described its attributes for yt:accessControl (no comment option). Since it extends Zend_Gdata_Extension it will be serialized to xml string when necessary. Now we need either hack the NoEmbed extension class in the framework or create our own (same story as with NoComment) and extend if from Extension. Actually, you’d better create your own classes in your project structure and extend them from ZF classes than hack the framework itself even though it uses deprecated API version. That’s just a good practice so you can see where your code is and where ZF code is. So you’ll end up with something like:

/**
 * @see Zend_Gdata_Extension
 */
require_once 'Zend/Gdata/Extension.php';
 
class Zend_Gdata_YouTube_Extension_NoEmbed extends Zend_Gdata_Extension
{
 
    protected $_rootNamespace = 'yt';
    protected $_rootElement = 'accessControl';
    protected $_extensionAttributes = array(
                                            array('name'  => 'permission',
                                                  'value' => 'denied'),
                                            array('name'  => 'action',
                                                  'value' => 'embed'));
 
 
    public function __construct($enabled = null)
    {
        $this->registerAllNamespaces(Zend_Gdata_YouTube::$namespaces);
        parent::__construct();
    }
 
}

pretty much the same as NoComment one. Now as there was no NoComment extension we should find the right place to append it to DOM. This can be done by extending VideoEntry class with your own one, overriding getDom() and adding several methods for NoComment feature:

class Zend_Gdata_YouTube_VideoEntry_AD extends Zend_Gdata_YouTube_VideoEntry
{
    protected $_noComment = null;
 
    public function setNoComment($noComment = null)
    {
        $this->_noComment = $noComment;
        return $this;
    }
 
    public function getNoComment()
    {
        return $this->_noComment;
    }
 
    public function isVideoCommentable()
    {
        if ($this->getNoComment() == null) {
            return true;
        } else {
            return false;
        }
    }
 
    public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
    {
        $element = parent::getDOM($doc, $majorVersion, $minorVersion);
 
        if ($this->_noComment != null) {
            $element->appendChild($this->_noComment->getDOM(
                $element->ownerDocument));
        }
 
        return $element;
    }
 
 
}

As you see we call parent method + append our own NoComment xml.

That’s all. It should be working. Here is the test code which really works on 1.11.4:

<?php
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.realpath('./'));
 
require_once('Zend/Loader/Autoloader.php');
 
$loader = Zend_Loader_Autoloader::getInstance();
 
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$client = Zend_Gdata_ClientLogin::getHttpClient(
                                          $username = 'your.email@gmail.com',
                                          $password = 'xxxxx',
                                          $service = 'youtube',
                                          $client = null,
                                          $source = 'MySource', // a short string identifying your application
                                          $loginToken = null,
                                          $loginCaptcha = null,
                                          $authenticationURL);
 
$yt = new Zend_Gdata_YouTube($client,
   'YOUR',
   'Youtube API',
   'KEYS');
 
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry_AD();
$filesource = $yt->newMediaFileSource('file.avi');
$filesource->setContentType('video/mp4');
$filesource->setSlug('file.avi');
$myVideoEntry->setMediaSource($filesource);
$myVideoEntry->setVideoTitle('TEST');
$myVideoEntry->setVideoDescription('how to test');
$myVideoEntry->setVideoCategory('Howto');
$myVideoEntry->SetVideoTags('test');
$myVideoEntry->setNoEmbed(new Zend_Gdata_YouTube_Extension_NoEmbed());
$myVideoEntry->setNoComment(new Zend_Gdata_YouTube_Extension_NoComment());
 
$newEntry = $yt->insertEntry($myVideoEntry,
            'https://uploads.gdata.youtube.com/feeds/users/default/uploads',
            'Zend_Gdata_YouTube_VideoEntry'
);

Attached is the code of all three files explained above

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.