<commit_before><?php
// vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker syntax=php:
/**
 * Container for {@link tgif_file}
 *
 * @package tgiframework
 * @subpackage utilities
 * @copyright 2010 <NAME>
 * @license GNU Lesser General Public License <http://www.gnu.org/licenses/lgpl.html>
 */
// {{{ tgif_http_client
// docs {{{
/**
 * curl utilities
 *
 * @package tgiframework
 * @subpackage utilities
 * @author <NAME> <<EMAIL>> refactored from {@link tgif_diagnostics}
 */
// }}}
class tgif_http_client
{
    // {{{ + fetch($url,$post)
    /**
     * cURL fetch a url return body as string
     *
     * @param string $url where to grab data
     * @param array|string $post post variables. If string it is raw post data
     * @param integer|false $chmod octal number for chmod of file with set.
     * @return boolean success or failure
     */
    static function fetch($url, $post=array())
    {
        $isRunning = self::_diag_start('fetch_into', array(
                'url'       => $url,
                'post'      => $post,
            ));

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, 1); 
            if ( is_string($post) ) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            } else {
                curl_setopt($ch, CURLOPT_POSTFIELDS, tgif_http::url_encode($post));
            }
        }
        $return = curl_exec($ch);
        curl_close($ch); //must close curl handle first

        return $return;
    }
    // }}}
    // {{{ + fetch_into($url,$destFile,$chmod=false)
    /**
     * cURL fetch a url into a file (in an atomic manner).
     *
     * @param string $url where to grab data
     * @param string $destFile where to copy to
     * @param integer|false $chmod octal number for chmod of file with set.
     * @return boolean success or failure
     */
    static function fetch_into($url, $destFile, $chmod=false)
    {
        $isRunning = self::_diag_start('fetch_into', array(
                'url'       => $url,
                'dest_file' => $destFile,
                'chmod'     => $chmod,
            ));

        $base_path = dirname($destFile);
        if ( !file_exists($base_path) ) {
            $dir_chmod = ( $chmod!==false ) ? tgif_file::dir_chmod($chmod) : 0777;
            if ( !mkdir($base_path, $dir_chmod, true) ) {
                $success = false;<commit_msg>diagnostics bug fixed<commit_after><?php
// vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker syntax=php:
/**
 * Container for {@link tgif_file}
 *
 * @package tgiframework
 * @subpackage utilities
 * @copyright 2010 <NAME>
 * @license GNU Lesser General Public License <http://www.gnu.org/licenses/lgpl.html>
 */
// {{{ tgif_http_client
// docs {{{
/**
 * curl utilities
 *
 * @package tgiframework
 * @subpackage utilities
 * @author <NAME> <<EMAIL>> refactored from {@link tgif_diagnostics}
 */
// }}}
class tgif_http_client
{
    // {{{ + fetch($url,$post)
    /**
     * cURL fetch a url return body as string
     *
     * @param string $url where to grab data
     * @param array|string $post post variables. If string it is raw post data
     * @param integer|false $chmod octal number for chmod of file with set.
     * @return boolean success or failure
     */
    static function fetch($url, $post=array())
    {
        $isRunning = self::_diag_start('fetch', array(
                'url'       => $url,
                'post'      => $post,
            ));

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($post) {
            curl_setopt($ch, CURLOPT_POST, 1); 
            if ( is_string($post) ) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            } else {
                curl_setopt($ch, CURLOPT_POSTFIELDS, tgif_http::url_encode($post));
            }
        }
        $return = curl_exec($ch);
        curl_close($ch); //must close curl handle first
        self::_diag_stop($isRunning, array());

        return $return;
    }
    // }}}
    // {{{ + fetch_into($url,$destFile,$chmod=false)
    /**
     * cURL fetch a url into a file (in an atomic manner).
     *
     * @param string $url where to grab data
     * @param string $destFile where to copy to
     * @param integer|false $chmod octal number for chmod of file with set.
     * @return boolean success or failure
     */
    static function fetch_into($url, $destFile, $chmod=false)
    {
        $isRunning = self::_diag_start('fetch_into', array(
                'url'       => $url,
                'dest_file' => $destFile,
                'chmod'     => $chmod,
            ));

        $base_path = dirname($destFile);
        if ( !file_exists($base_path) ) {
            $dir_chmod = ( $chmod!==false ) ? tgif_file::dir_chmod($chmod) : 0777;
            if ( !mkdir($base_path, $dir_chmod, true) ) {
                $success = false;
{'fraction_non_alphanumeric': 0.13641826923076922, 'fraction_numerical': 0.005208333333333333, 'mean_word_length': 2.202694034637588, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7374056', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1548, 'n_words': 397}