<commit_before><?php

/**
 * Created by PhpStorm.
 * User: godson
 * Date: 26.03.14
 * Time: 14:32
 */
class Wordpress extends CApplicationComponent
{
    private $url = '';
    private $login = '';
    private $password = '';

    public function __construct()
    {
        $this->url      = Yii::app()->params['wp']['url'];
        $this->login    = Yii::app()->params['wp']['login'];
        $this->password = Yii::app()->params['wp']['password'];
    }

    protected function makeRequest($url, $params = array(), $post = false)
    {
        if ($url && !empty($params)) {
            $result  = '';
//            $request = http_build_query($params);
            if ($post) {
                $result = PageLoader::load($url, $params);
            } else {
                $request = http_build_query($params);

                $result = PageLoader::load($url . "?" . $request);
            }
            if ("?" == substr(trim($result), 0, 1)) {
                $result = preg_replace('/.+?({.+}).+/', '$1', $result);
            }
            return json_decode($result);
        } else {
            return false;
        }
    }

    public function createPost(
        $title,
        $content,
        $source,
        $status = "draft",
        $attachment = false,
        $categories = array(),
        $tags = array()
    ) {
        $nonce = $this->getNonce("posts", "create_post");
        if (!$nonce) {
            return false;
        }
        $params = array(
            "nonce"   => $nonce->nonce,
            "status" => $status,
            "title"   => $title,
            "content" => $content,
            "author"  => "admin",
            "cookie" => $this->getAuthCookie(),
//            "custom[source]" => $source,
        );
        foreach ($source as $key => $val) {
            $params["custom[$key]"] = $val;
        }
        if ($attachment) {
//            echo "\n ATT \n";
            $params["attachment"] = "@{$attachment}";
            $params["is_featured_image"] = true;
//            echo $params["attachment"] . "\n";
        }
        if (!empty($categories)) {
            $params['categories'] = implode(",", $categories);
        }
        if (!empty($tags)) {
            $params['tags'] = implode(",", $tags);
        }
        $result = $this->makeRequest($this->url . "posts/create_post/", $params, true);
//        $result = $this->makeRequest("http://na.loc/uploadCheck.php", $params, true);
        if ($result && ("ok" == $result->status)) {
            return true;
        } else {
            return false;
        }
    }

    protected function getNonce($controller, $method)
    {
        $params = array(
            "controller" => $controller,
            "method"     => $method,
            "callback"   => "?"
        );
        return $this->makeRequest($this->url . "core/get_nonce/", $params);
    }

    protected function getAuthCookie()
    {
        $nonce  = $this->getNonce("auth", "generate_auth_cookie");
        $params = array(
            "nonce"    => $nonce->nonce,
            "username" => $this->login,
            "password" => $<PASSWORD>
        );

        $result = $this->makeRequest($this->url . "auth/generate_auth_cookie/", $params);
        return $result->cookie;

    }

    public function getCategories()
    {
        return $this->makeRequest($this->url . "get_category_index/");
    }
} <commit_msg>fix parralel publishing<commit_after><?php

/**
 * Created by PhpStorm.
 * User: godson
 * Date: 26.03.14
 * Time: 14:32
 */
class Wordpress extends CApplicationComponent
{
    private $url = '';
    private $login = '';
    private $password = '';

    public function __construct()
    {
        $this->url      = Yii::app()->params['wp']['url'];
        $this->login    = Yii::app()->params['wp']['login'];
        $this->password = Yii::app()->params['wp']['password'];
    }

    protected function makeRequest($url, $params = array(), $post = false)
    {
        if ($url && !empty($params)) {
            $result  = '';
//            $request = http_build_query($params);
            if ($post) {
                $result = PageLoader::load($url, $params);
            } else {
                $request = http_build_query($params);

                $result = PageLoader::load($url . "?" . $request);
            }
            if ("?" == substr(trim($result), 0, 1)) {
                $result = preg_replace('/.+?({.+}).+/', '$1', $result);
            }
            return json_decode($result);
        } else {
            return false;
        }
    }

    public function createPost(
        $title,
        $content,
        $source,
        $status = "draft",
        $attachment = false,
        $categories = array(),
        $tags = array()
    ) {
        $nonce = $this->getNonce("posts", "create_post");
        if (!$nonce) {
            return false;
        }
        $params = array(
            "nonce"   => $nonce->nonce,
            "status" => $status,
            "title"   => $title,
            "content" => $content,
            "author"  => "admin",
            "cookie" => $this->getAuthCookie(),
//            "custom[source]" => $source,
        );
        foreach ($source as $key => $val) {
            $params["custom[$key]"] = $val;
        }
        if ($attachment) {
//            echo "\n ATT \n";
            $params["attachment"] = "@{$attachment}";
            $params["is_featured_image"] = true;
//            echo $params["attachment"] . "\n";
        }
        if (!empty($categories)) {
            $params['categories'] = implode(",", $categories);
        }
        if (!empty($tags)) {
            $params['tags'] = implode(",", $tags);
        }
        $result = $this->makeRequest($this->url . "posts/create_post/", $params, true);
//        $result = $this->makeRequest("http://na.loc/uploadCheck.php", $params, true);
        if ($result && ("ok" == $result->status)) {
            return true;
        } else {
            return false;
        }
    }

    protected function getNonce($controller, $method)
    {
        $params = array(
            "controller" => $controller,
            "method"     => $method,
            "callback"   => "?"
        );
        return $this->makeRequest($this->url . "core/get_nonce/", $params);
    }

    protected function getAuthCookie()
    {
        $nonce  = $this->getNonce("auth", "generate_auth_cookie");
        if (!$nonce) {
            return false;
        }
        $params = array(
            "nonce"    => $nonce->nonce,
            "username" => $this->login,
            "password" => $<PASSWORD>
        );

        $result = $this->makeRequest($this->url . "auth/generate_auth_cookie/", $params);
        return $result->cookie;

    }

    public function getCategories()
    {
        return $this->makeRequest($this->url . "get_category_index/");
    }
} 
{'fraction_non_alphanumeric': 0.18270207517051226, 'fraction_numerical': 0.003773037295022493, 'mean_word_length': 1.6974559686888453, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2131892', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1996, 'n_words': 390}