<commit_before><?php

class FacebookDataRetriever extends URLDataRetriever implements ItemDataRetriever, SocialDataRetriever
{
    protected $DEFAULT_PARSER_CLASS='FacebookDataParser';
    protected $cacheGroup = 'Facebook';
    protected $clientId;
    protected $clientSecret;
    protected $user;
    protected $accessToken;

    public function getServiceName() {
        return 'facebook';
    }
    
    public function getAccount() {
        return $this->user;
    }
    
    public function getUser($userID)
    {
        $this->clearInternalCache();
        $this->setBaseURL(sprintf('https://graph.facebook.com/%s/', $this->user));
        $this->setOption('action', 'user');
        return $this->getData();
    }
    
    public function canRetrieve()
    {
        return true;
    
    }
    public function canPost()
    {
        return false;
    }
    
    public function auth(array $options)
    {
    }
    
    public function getItem($id, &$response=null)
    {
        $this->clearInternalCache();
        $this->setBaseURL(sprintf('https://graph.facebook.com/%s', $id));
        return $this->getData();
    }
    
    protected function initRequest() {
        switch ($this->getOption('action'))
        {
            case 'posts':
                $this->setBaseURL(sprintf('https://graph.facebook.com/%s/posts', $this->user));
                if ($limit = $this->getOption('limit')) {
                    $this->addParameter('limit',$limit);
                }
                break;
        }
        
        if ($this->accessToken) {
            $this->addParameter('access_token', $this->accessToken);
        }
        
    }
    
    public function setUser($user)
    {
        $this->user = $user;
    }
    
    public function init($args) {
        parent::init($args);

        if (!isset($args['ACCOUNT'])) {
            throw new KurogoConfigurationException("ACCOUNT must be set for Facebook");
        }
        
        $this->setUser($args['ACCOUNT']);

        if (!isset($args['OAUTH_CONSUMER_KEY'])) {
            throw new KurogoConfigurationException("OAUTH_CONSUMER_KEY must be set for Facebook");
        }
        $this->clientId = $args['OAUTH_CONSUMER_KEY'];

        if (!isset($args['OAUTH_CONSUMER_SECRET'])) {
            throw new KurogoConfigurationException("OAUTH_CONSUMER_SECRET must be set for Facebook");
        }
        $this->clientSecret = $args['OAUTH_CONSUMER_SECRET'];
        
        $this->accessToken = $this->getAccessToken();
    }
    
    private function getAccessToken()
    {
        $this->setBaseURL('https://graph.facebook.com/oauth/access_token');
        $this->addParameter('client_id', $this->clientId);
        $this->addParameter('client_secret', $this->clientSecret);
        $this->addParameter('grant_type', 'client_credentials');
        $response = $this->getResponse();
        list($name, $token) = explode("=", $response->getResponse());
        $this->clearInternalCache();
        return $token;
    }
}

class FacebookDataParser extends DataParser
{
    private function parsePost($entry) {
        $post = new FacebookPost();
        $post->setID($entry['id']);
        $post->setAuthor($entry['from']['id']);
        $post->setCreated(new DateTime($entry['created_time']));
        if(!empty($entry['message']))
        {
            $post->setBody($entry['message']);
        }
        
        if (isset($entry['likes'])) {
            $post->setLikeCount($entry['likes']['count']);
        }
        
        switch ($entry['type'])
        {
            case 'photo':
                if (isset($entry['source'])) {
                    $post->addImage($entry['source']);
                } elseif (preg_match("/^(.*)_s\.jpg$/", $entry['picture'], $bits)) {
                    $post->addImage($bits[1].'_n.jpg');
                } else {
                    $post->addImage($entry['picture']);
                }
                break;
            case 'link':
            case 'video':
            case 'status':
                break;
            default:
                throw new KurogoDataException("Unhandled facebook type " . $entry['type']);
        }
        
        return $post;
    }

    private function parseUser($entry) {
        $user = new FacebookUser();
        $user->setUserID($entry['id']);
        $user->setName($entry['name']);
       /* if(isset($entry['picture']))
        {
            $user->setImageURL($entry['picture']);
        }
        else
        {*/
            // The below code alone causes an 800% increase in run-time.  This is the more correct way of doing it, but it's way to slow.  Maybe this + caching?
            /*$headers = get_headers('https://graph.facebook.com/'.$entry['id'].'/picture', 1);
            if(is_array($headers['Location']))
            {
                $user->setImageURL(end($headers['Location']));
            }
            else
            {
                $user->setImageURL($headers['Location']);
            }*/

            $user->setImageURL(ImageLoader::cacheImage('https://graph.facebook.com/'.$entry['id'].'/picture?type=square', array()));
        //}
        return $user;<commit_msg>don't load FB access token until necessary<commit_after><?php

class FacebookDataRetriever extends URLDataRetriever implements ItemDataRetriever, SocialDataRetriever
{
    protected $DEFAULT_PARSER_CLASS='FacebookDataParser';
    protected $cacheGroup = 'Facebook';
    protected $clientId;
    protected $clientSecret;
    protected $user;
    protected $accessToken;

    public function getServiceName() {
        return 'facebook';
    }
    
    public function getAccount() {
        return $this->user;
    }
    
    public function getUser($userID)
    {
        $this->clearInternalCache();
        $this->setBaseURL(sprintf('https://graph.facebook.com/%s/', $this->user));
        $this->setOption('action', 'user');
        return $this->getData();
    }
    
    public function canRetrieve()
    {
        return true;
    
    }
    public function canPost()
    {
        return false;
    }
    
    public function auth(array $options)
    {
    }
    
    public function getItem($id, &$response=null)
    {
        $this->clearInternalCache();
        $this->setBaseURL(sprintf('https://graph.facebook.com/%s', $id));
        return $this->getData();
    }
    
    protected function initRequest() {
        switch ($this->getOption('action'))
        {
        	case 'getAccessToken':
        		break;
            case 'posts':
                $this->setBaseURL(sprintf('https://graph.facebook.com/%s/posts', $this->user));
                if ($limit = $this->getOption('limit')) {
                    $this->addParameter('limit',$limit);
                }
            default:
            	if (!$this->accessToken) {
            		$this->accessToken = $this->getAccessToken();
            	}
				$this->addParameter('access_token', $this->accessToken);
        }
    }
    
    public function setUser($user)
    {
        $this->user = $user;
    }
    
    public function init($args) {
        parent::init($args);

        if (!isset($args['ACCOUNT'])) {
            throw new KurogoConfigurationException("ACCOUNT must be set for Facebook");
        }
        
        $this->setUser($args['ACCOUNT']);

        if (!isset($args['OAUTH_CONSUMER_KEY'])) {
            throw new KurogoConfigurationException("OAUTH_CONSUMER_KEY must be set for Facebook");
        }
        $this->clientId = $args['OAUTH_CONSUMER_KEY'];

        if (!isset($args['OAUTH_CONSUMER_SECRET'])) {
            throw new KurogoConfigurationException("OAUTH_CONSUMER_SECRET must be set for Facebook");
        }
        $this->clientSecret = $args['OAUTH_CONSUMER_SECRET'];
    }
    
    private function getAccessToken()
    {
        $this->setBaseURL('https://graph.facebook.com/oauth/access_token');
        $this->addParameter('client_id', $this->clientId);
        $this->addParameter('client_secret', $this->clientSecret);
        $this->addParameter('grant_type', 'client_credentials');
        $this->setOption('action','getAccessToken');
        $response = $this->getResponse();
        list($name, $token) = explode("=", $response->getResponse());
        $this->clearInternalCache();
        return $token;
    }
}

class FacebookDataParser extends DataParser
{
    private function parsePost($entry) {
        $post = new FacebookPost();
        $post->setID($entry['id']);
        $post->setAuthor($entry['from']['id']);
        $post->setCreated(new DateTime($entry['created_time']));
        if(!empty($entry['message']))
        {
            $post->setBody($entry['message']);
        }
        
        if (isset($entry['likes'])) {
            $post->setLikeCount($entry['likes']['count']);
        }
        
        switch ($entry['type'])
        {
            case 'photo':
                if (isset($entry['source'])) {
                    $post->addImage($entry['source']);
                } elseif (preg_match("/^(.*)_s\.jpg$/", $entry['picture'], $bits)) {
                    $post->addImage($bits[1].'_n.jpg');
                } else {
                    $post->addImage($entry['picture']);
                }
                break;
            case 'link':
            case 'video':
            case 'status':
                break;
            default:
                throw new KurogoDataException("Unhandled facebook type " . $entry['type']);
        }
        
        return $post;
    }

    private function parseUser($entry) {
        $user = new FacebookUser();
        $user->setUserID($entry['id']);
        $user->setName($entry['name']);
		$user->setImageURL(ImageLoader::cacheImage('https://graph.facebook.com/'.$entry['id'].'/picture?type=square', array()));
        return $user;
{'fraction_non_alphanumeric': 0.1566228204343836, 'fraction_numerical': 0.000611807892321811, 'mean_word_length': 2.0736446255092447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 93, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6808412', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2712, 'n_words': 464}