<commit_before><?php
/**
 * Gengo API Client
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that came
 * with this package in the file LICENSE.txt. It is also available
 * through the world-wide-web at this URL:
 * http://gengo.com/services/api/dev-docs/gengo-code-license
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category   Gengo
 * @package    API Client Library
 * @copyright  Copyright (c) 2009-2012 Gengo, Inc. (http://gengo.com)
 * @license    http://gengo.com/services/api/dev-docs/gengo-code-license   New BSD License
 */

class Gengo_Api_Jobs extends Gengo_Api
{
    /**
     * @param string $api_key the public API key.
     * @param string $private_key the private API key.
     */
    public function __construct($api_key = null, $private_key = null)
    {
        parent::__construct($api_key, $private_key);
    }

    /**
     * translate/jobs (POST)
     *
     * Submits a job or group of jobs to translate.
     *
     * @param array $jobs An array of jobs
     * @param int $as_group Use 1 (the default) to have submitted jobs translated but 1 translator only
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function postJobs(array $jobs, $as_group = 1, $version = 'v2')
    {
        $attachments = array();
        // handle order level file attachments
        if (isset($jobs['file_attachments']))
        {
            $i = 1;
            foreach ($jobs['file_attachments'] as $attachment)
            {
                if (! is_file($attachment))
                {
                    throw new Gengo_Exception(sprintf('Order comment attachment filepath could not be found: %s', $attachment));
                }
                $attachments["order_attachment_{$i}"] = $attachment;
                $i++;
            }
            unset($jobs['file_attachments']);
        }

        // handle order level url attachments
        if (isset($jobs['url_attachments']))
        {
            $jobs['attachments'] = array();
            foreach ($jobs['url_attachments'] as $attachment)
            {
                if (! is_array($attachment))
                {
                    throw new Gengo_Exception(sprintf('Order url attachment MUST be an array'));
                }
                $jobs['attachments'][] = $attachment;
            }
            unset($jobs['url_attachments']);
        }

        // handle attachments
        foreach ($jobs as $key => $job)
        {
            // handle file attachments
            if (isset($job['file_attachments']))
            {
                $i = 1;
                foreach ($job['file_attachments'] as $attachment)
                {
                    // must be a valid filepath
                    if (! is_file($attachment))
                    {
                        throw new Gengo_Exception(sprintf('Job comment attachment filepath could not be found: %s', $attachment));
                    }
                    $attachments["{$key}_{$i}"] = $attachment;
                    $i++;
                }
                unset($jobs[$key]['file_attachments']);
            }

            // handle url attachments
            if (isset($job['url_attachments']))
            {
                $jobs[$key]['attachments'] = array();
                foreach ($job['url_attachments'] as $attachment)
                {
                    if (! is_array($attachment))
                    {
                        throw new Gengo_Exception(sprintf('Job url attachment MUST be an array'));
                    }
                    $jobs[$key]['attachments'][] = $attachment;
                }
                unset($jobs[$key]['url_attachments']);
            }
        }

        $data = array('jobs'     => $jobs,
                      'as_group' => intval($as_group),
                      'process'  => 1);

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        '_method' => 'post',
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs";

        if (! empty($attachments))
        {
            $this->response = $this->client->upload($baseurl, $attachments, $format, $params);
        }
        else {
            $this->response = $this->client->post($baseurl, $format, $params);
        }
    }

    /**
     * translate/jobs (GET)
     *
     * Retrieves a list of resources for the most recent jobs filtered
     * by the given parameters.
     *
     * @param array $ids An OPTIONAL array of ids.
     * @param string $format The OPTIONAL response format: xml or json (default).
     * @param array|string $params (DEPRECATED) If passed should contain all the
     * necessary parameters for the request including the api_key and
     * api_sig
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function getJobs($ids = null, $format = null, $params = null, $version = 'v2')
    {
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs";
        if (!is_null($ids))
        {
            $baseurl .= '/' . implode(',', $ids);
        }

        $this->response = $this->client->get($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Updates jobs to translate. returns these jobs back to the translators for revisions.
     *
     * @param array $jobs (required) An array of arrays i.e. $jobs[] = array('job_id' => 12345, 'comment' => 'A comment here')
     * Note: the comment in the above $jobs array is optional if the $comment parameters is passed to this method
     * @param string $version Version of the API to use. Defaults to 'v2'.
     * @param string $comment The comment that will be applied to all of the jobs that don't have one
     */
    public function revise(array $jobs, $version = 'v2', $comment = NULL)
    {
        // pack the jobs
        $data = array('action' => 'revise',
                      'job_ids' => $jobs);

        // add all jobs level comment if present
        if (mb_strlen($comment) > 0)
        {
            $data['comment'] = $comment;
        }

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Updates jobs to translate. Approves jobs.
     *
     * @param array $jobs (required) the payloads to identify the jobs sent back for approval:
     *  - "job_id" the job's id
     *  - "rating" (optional) - 1 (poor) to 5 (fantastic)
     *  - "for_translator" (optional) - comments for the translator
     *  - "for_mygengo" (optional) - comments for Gengo staff (private)
     *  - "public" (optional) - 1 (true) / 0 (false, default); whether Gengo can share this feedback publicly
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function approve(array $jobs, $version='v2')
    {
        $data = array('action' => 'approve',
                      'job_ids' => $jobs);

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Updates jobs to translate. returns these jobs back to the translators for rejection.
     *
     * @param array $jobs (required) the payloads to identify the jobs sent back for rejections:
     *  - reason (required) - "quality", "incomplete", "other"
     *  - comment (required)
     *  - captcha (required) - the captcha image text. Each job in a "reviewable" state will
     *  - job_id (required) - The id of the job to reject
     *  - have a captcha_url value, which is a URL to an image.  This
     *  - captcha value is required only if a job is to be rejected.
     *  - follow_up (optional) - "requeue" (default) or "cancel"
     * @param string $version Version of the API to use. Defaults to 'v2'.
     * @param string $comment The comment that will be applied to all of the jobs that don't have one
     */
    public function reject(array $jobs, $version='v2', $comment = NULL)
    {
        $data = array('action' => 'reject',
                      'job_ids' => $jobs);

        // add all jobs level comment if present
        if (mb_strlen($comment) > 0)
        {
            $data['comment'] = $comment;
        }

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true))
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Archive jobs
     *
     * @param array $jobs (required) An array containing the job ids to archive
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function archive(array $jobs, $version = 'v2')
    {
        // pack the jobs
        $data = array('action' => 'archive',
                      'job_ids' => $jobs);

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (DELETE)
     *
     * Cancels the jobs. You can only cancel a job if it has not been
     * started already by a translator.
     *
     * @param array (required) $ids Array of ids of the jobs to cancel
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function cancel($ids, $version='v2')
    {
        $data = array('job_ids' => $ids);
        $params = array('api_key' => $this->config->get('api_key', null, true), 'ts' => gmdate('U'), 'data'=> json_encode($data));
        ksort($params);
        $query = http_build_query($params);
        $hmac = hash_hmac('sha1', $query, $this->config->get('private_key', null, true));
        $params['api_sig'] = $hmac;

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->delete($baseurl, $format, $params);
    }
}
<commit_msg>[#104688106] add support for tone and purpose at the order level<commit_after><?php
/**
 * Gengo API Client
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that came
 * with this package in the file LICENSE.txt. It is also available
 * through the world-wide-web at this URL:
 * http://gengo.com/services/api/dev-docs/gengo-code-license
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category   Gengo
 * @package    API Client Library
 * @copyright  Copyright (c) 2009-2012 Gengo, Inc. (http://gengo.com)
 * @license    http://gengo.com/services/api/dev-docs/gengo-code-license   New BSD License
 */

class Gengo_Api_Jobs extends Gengo_Api
{
    /**
     * @param string $api_key the public API key.
     * @param string $private_key the private API key.
     */
    public function __construct($api_key = null, $private_key = null)
    {
        parent::__construct($api_key, $private_key);
    }

    /**
     * translate/jobs (POST)
     *
     * Submits a job or group of jobs to translate.
     *
     * @param array $jobs An array of jobs
     * @param int $as_group Use 1 (the default) to have submitted jobs translated but 1 translator only
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function postJobs(array $jobs, $as_group = 1, $version = 'v2')
    {
        $attachments = array();
        // handle order level file attachments
        if (isset($jobs['file_attachments']))
        {
            $i = 1;
            foreach ($jobs['file_attachments'] as $attachment)
            {
                if (! is_file($attachment))
                {
                    throw new Gengo_Exception(sprintf('Order comment attachment filepath could not be found: %s', $attachment));
                }
                $attachments["order_attachment_{$i}"] = $attachment;
                $i++;
            }
            unset($jobs['file_attachments']);
        }

        // handle order level url attachments
        if (isset($jobs['url_attachments']))
        {
            $jobs['attachments'] = array();
            foreach ($jobs['url_attachments'] as $attachment)
            {
                if (! is_array($attachment))
                {
                    throw new Gengo_Exception(sprintf('Order url attachment MUST be an array'));
                }
                $jobs['attachments'][] = $attachment;
            }
            unset($jobs['url_attachments']);
        }

        // handle attachments
        foreach ($jobs as $key => $job)
        {
            // handle file attachments
            if (isset($job['file_attachments']))
            {
                $i = 1;
                foreach ($job['file_attachments'] as $attachment)
                {
                    // must be a valid filepath
                    if (! is_file($attachment))
                    {
                        throw new Gengo_Exception(sprintf('Job comment attachment filepath could not be found: %s', $attachment));
                    }
                    $attachments["{$key}_{$i}"] = $attachment;
                    $i++;
                }
                unset($jobs[$key]['file_attachments']);
            }

            // handle url attachments
            if (isset($job['url_attachments']))
            {
                $jobs[$key]['attachments'] = array();
                foreach ($job['url_attachments'] as $attachment)
                {
                    if (! is_array($attachment))
                    {
                        throw new Gengo_Exception(sprintf('Job url attachment MUST be an array'));
                    }
                    $jobs[$key]['attachments'][] = $attachment;
                }
                unset($jobs[$key]['url_attachments']);
            }
        }

        // prepare data payload
        $data = array('as_group' => intval($as_group),
                      'process'  => 1);

        // handle tone and purpose at the order level
        if (isset($jobs['tone']))
        {
            $data['tone'] = $jobs['tone'];
            unset($jobs['tone']);
        }
        if (isset($jobs['purpose']))
        {
            $data['purpose'] = $jobs['purpose'];
            unset($jobs['purpose']);
        }
        // add jobs to payload
        $data['jobs'] = $jobs;

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        '_method' => 'post',
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs";

        if (! empty($attachments))
        {
            $this->response = $this->client->upload($baseurl, $attachments, $format, $params);
        }
        else {
            $this->response = $this->client->post($baseurl, $format, $params);
        }
    }

    /**
     * translate/jobs (GET)
     *
     * Retrieves a list of resources for the most recent jobs filtered
     * by the given parameters.
     *
     * @param array $ids An OPTIONAL array of ids.
     * @param string $format The OPTIONAL response format: xml or json (default).
     * @param array|string $params (DEPRECATED) If passed should contain all the
     * necessary parameters for the request including the api_key and
     * api_sig
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function getJobs($ids = null, $format = null, $params = null, $version = 'v2')
    {
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs";
        if (!is_null($ids))
        {
            $baseurl .= '/' . implode(',', $ids);
        }

        $this->response = $this->client->get($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Updates jobs to translate. returns these jobs back to the translators for revisions.
     *
     * @param array $jobs (required) An array of arrays i.e. $jobs[] = array('job_id' => 12345, 'comment' => 'A comment here')
     * Note: the comment in the above $jobs array is optional if the $comment parameters is passed to this method
     * @param string $version Version of the API to use. Defaults to 'v2'.
     * @param string $comment The comment that will be applied to all of the jobs that don't have one
     */
    public function revise(array $jobs, $version = 'v2', $comment = NULL)
    {
        // pack the jobs
        $data = array('action' => 'revise',
                      'job_ids' => $jobs);

        // add all jobs level comment if present
        if (mb_strlen($comment) > 0)
        {
            $data['comment'] = $comment;
        }

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Updates jobs to translate. Approves jobs.
     *
     * @param array $jobs (required) the payloads to identify the jobs sent back for approval:
     *  - "job_id" the job's id
     *  - "rating" (optional) - 1 (poor) to 5 (fantastic)
     *  - "for_translator" (optional) - comments for the translator
     *  - "for_mygengo" (optional) - comments for Gengo staff (private)
     *  - "public" (optional) - 1 (true) / 0 (false, default); whether Gengo can share this feedback publicly
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function approve(array $jobs, $version='v2')
    {
        $data = array('action' => 'approve',
                      'job_ids' => $jobs);

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Updates jobs to translate. returns these jobs back to the translators for rejection.
     *
     * @param array $jobs (required) the payloads to identify the jobs sent back for rejections:
     *  - reason (required) - "quality", "incomplete", "other"
     *  - comment (required)
     *  - captcha (required) - the captcha image text. Each job in a "reviewable" state will
     *  - job_id (required) - The id of the job to reject
     *  - have a captcha_url value, which is a URL to an image.  This
     *  - captcha value is required only if a job is to be rejected.
     *  - follow_up (optional) - "requeue" (default) or "cancel"
     * @param string $version Version of the API to use. Defaults to 'v2'.
     * @param string $comment The comment that will be applied to all of the jobs that don't have one
     */
    public function reject(array $jobs, $version='v2', $comment = NULL)
    {
        $data = array('action' => 'reject',
                      'job_ids' => $jobs);

        // add all jobs level comment if present
        if (mb_strlen($comment) > 0)
        {
            $data['comment'] = $comment;
        }

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true))
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (PUT)
     *
     * Archive jobs
     *
     * @param array $jobs (required) An array containing the job ids to archive
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function archive(array $jobs, $version = 'v2')
    {
        // pack the jobs
        $data = array('action' => 'archive',
                      'job_ids' => $jobs);

        $ts = gmdate('U');
        // create the query
        $params = array('api_key' => $this->config->get('api_key', null, true),
                        'ts'      => $ts,
                        'data'    => json_encode($data),
                        'api_sig' => Gengo_Crypto::sign($ts, $this->config->get('private_key', null, true)),
        );

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->put($baseurl, $format, $params);
    }

    /**
     * translate/jobs/ (DELETE)
     *
     * Cancels the jobs. You can only cancel a job if it has not been
     * started already by a translator.
     *
     * @param array (required) $ids Array of ids of the jobs to cancel
     * @param string $version Version of the API to use. Defaults to 'v2'.
     */
    public function cancel($ids, $version='v2')
    {
        $data = array('job_ids' => $ids);
        $params = array('api_key' => $this->config->get('api_key', null, true), 'ts' => gmdate('U'), 'data'=> json_encode($data));
        ksort($params);
        $query = http_build_query($params);
        $hmac = hash_hmac('sha1', $query, $this->config->get('private_key', null, true));
        $params['api_sig'] = $hmac;

        $format = $this->config->get('format', null, true);
        $this->setParamsNotId($format, $params);
        $baseurl = $this->config->get('baseurl', null, true);
        $baseurl .= "{$version}/translate/jobs/";
        $this->response = $this->client->delete($baseurl, $format, $params);
    }
}

{'fraction_non_alphanumeric': 0.14615651102813812, 'fraction_numerical': 0.003425843950883406, 'mean_word_length': 1.9250168880882683, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 246, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3064823', 'n_tokens_mistral': 7721, 'n_tokens_neox': 7338, 'n_words': 2113}