<commit_before><?php

namespace Bolt\Controllers;

use Bolt\Translation\Translator as Trans;
use Guzzle\Http\Exception\RequestException as V3RequestException;
use GuzzleHttp\Exception\RequestException;
use League\Flysystem\FileNotFoundException;
use Silex;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Async implements ControllerProviderInterface
{
    public function connect(Silex\Application $app)
    {
        /** @var $ctr \Silex\ControllerCollection */
        $ctr = $app['controllers_factory'];

        $ctr->before(array($this, 'before'));

        $ctr->get("/dashboardnews", array($this, 'dashboardnews'))
            ->bind('dashboardnews');

        $ctr->get("/latestactivity", array($this, 'latestactivity'))
            ->bind('latestactivity');

        $ctr->get("/filesautocomplete", array($this, 'filesautocomplete'));

        $ctr->get("/readme/{filename}", array($this, 'readme'))
            ->assert('filename', '.+')
            ->bind('readme');

        $ctr->get("/widget/{key}", array($this, 'widget'))
            ->bind('widget');

        $ctr->get("/makeuri", array($this, 'makeuri'));

        $ctr->get("/lastmodified/{contenttypeslug}/{contentid}", array($this, 'lastmodified'))
            ->value('contentid', '')
            ->bind('lastmodified');

        $ctr->get("/filebrowser/{contenttype}", array($this, 'filebrowser'))
            ->assert('contenttype', '.*')
            ->bind('filebrowser');

        $ctr->get("/browse/{namespace}/{path}", array($this, 'browse'))
            ->assert('path', '.*')
            ->value('namespace', 'files')
            ->value('path', '')
            ->bind('asyncbrowse');

        $ctr->post("/renamefile", array($this, 'renamefile'))
            ->bind('renamefile');

        $ctr->post("/deletefile", array($this, 'deletefile'))
            ->bind('deletefile');

        $ctr->post("/duplicatefile", array($this, 'duplicatefile'))
            ->bind('duplicatefile');

        $ctr->get("/addstack/{filename}", array($this, 'addstack'))
            ->assert('filename', '.*')
            ->bind('addstack');

        $ctr->get("/tags/{taxonomytype}", array($this, 'tags'))
            ->bind('tags');

        $ctr->get("/populartags/{taxonomytype}", array($this, 'populartags'))
            ->bind('populartags');

        $ctr->get("/showstack", array($this, 'showstack'))
            ->bind('showstack');

        $ctr->get("/omnisearch", array($this, 'omnisearch'));

        $ctr->post("/folder/rename", array($this, 'renamefolder'))
            ->bind('renamefolder');

        $ctr->post("/folder/remove", array($this, 'removefolder'))
            ->bind('removefolder');

        $ctr->post("/folder/create", array($this, 'createfolder'))
            ->bind('createfolder');

        $ctr->get('/changelog/{contenttype}/{contentid}', array($this, 'changelogRecord'))
            ->value('contenttype', '')
            ->value('contentid', '0')
            ->bind('changelogrecord');

        $ctr->get('/email/{type}/{recipient}', array($this, 'emailNotification'))
            ->assert('type', '.*')
            ->bind('emailNotification');

        return $ctr;
    }

    /**
     * News. Film at 11.
     *
     * @param \Silex\Application $app
     * @param Request            $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function dashboardnews(Silex\Application $app, Request $request)
    {
        $source = 'http://news.bolt.cm/';
        $news = $app['cache']->fetch('dashboardnews'); // Two hours.
        $hostname = $request->getHost();
        $body = '';

        // If not cached, get fresh news.
        if ($news === false) {
            $app['logger.system']->info('Fetching from remote server: ' . $source, array('event' => 'news'));

            $driver = $app['db']->getDatabasePlatform()->getName();

            $url = sprintf(
                '%s?v=%s&p=%s&db=%s&name=%s',
                $source,
                rawurlencode($app->getVersion()),
                phpversion(),
                $driver,
                base64_encode($hostname)
            );

            // Options valid if using a proxy
            if ($app['config']->get('general/httpProxy')) {
                $curlOptions = array(
                    'CURLOPT_PROXY'        => $app['config']->get('general/httpProxy/host'),
                    'CURLOPT_PROXYTYPE'    => 'CURLPROXY_HTTP',
                    'CURLOPT_PROXYUSERPWD' => $app['config']->get('general/httpProxy/user') . ':' .
                                                $app['config']->get('general/httpProxy/password')
                );
            }

            // Standard option(s)
            $curlOptions['CURLOPT_CONNECTTIMEOUT'] = 5;

            try {
                if ($app['deprecated.php']) {
                    $fetchedNewsData = $app['guzzle.client']->get($url, null, $curlOptions)->send()->getBody(true);
                } else {
                    $fetchedNewsData = $app['guzzle.client']->get($url, array(), $curlOptions)->getBody(true);
                }

                $fetchedNewsItems = json_decode($fetchedNewsData);

                if ($fetchedNewsItems) {
                    $news = array();

                    // Iterate over the items, pick the first news-item that applies and the first alert we need to show
                    $version = $app->getVersion();
                    foreach ($fetchedNewsItems as $item) {
                        $type = ($item->type === 'alert') ? 'alert' : 'information';
                        if (!isset($news[$type])
                            && (empty($item->target_version) || version_compare($item->target_version, $version, '>'))
                        ) {
                            $news[$type] = $item;
                        }
                    }
<commit_msg>fix dashboardnews 504 Gateway Time-out<commit_after><?php

namespace Bolt\Controllers;

use Bolt\Translation\Translator as Trans;
use Guzzle\Http\Exception\RequestException as V3RequestException;
use GuzzleHttp\Exception\RequestException;
use League\Flysystem\FileNotFoundException;
use Silex;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Async implements ControllerProviderInterface
{
    public function connect(Silex\Application $app)
    {
        /** @var $ctr \Silex\ControllerCollection */
        $ctr = $app['controllers_factory'];

        $ctr->before(array($this, 'before'));

        $ctr->get("/dashboardnews", array($this, 'dashboardnews'))
            ->bind('dashboardnews');

        $ctr->get("/latestactivity", array($this, 'latestactivity'))
            ->bind('latestactivity');

        $ctr->get("/filesautocomplete", array($this, 'filesautocomplete'));

        $ctr->get("/readme/{filename}", array($this, 'readme'))
            ->assert('filename', '.+')
            ->bind('readme');

        $ctr->get("/widget/{key}", array($this, 'widget'))
            ->bind('widget');

        $ctr->get("/makeuri", array($this, 'makeuri'));

        $ctr->get("/lastmodified/{contenttypeslug}/{contentid}", array($this, 'lastmodified'))
            ->value('contentid', '')
            ->bind('lastmodified');

        $ctr->get("/filebrowser/{contenttype}", array($this, 'filebrowser'))
            ->assert('contenttype', '.*')
            ->bind('filebrowser');

        $ctr->get("/browse/{namespace}/{path}", array($this, 'browse'))
            ->assert('path', '.*')
            ->value('namespace', 'files')
            ->value('path', '')
            ->bind('asyncbrowse');

        $ctr->post("/renamefile", array($this, 'renamefile'))
            ->bind('renamefile');

        $ctr->post("/deletefile", array($this, 'deletefile'))
            ->bind('deletefile');

        $ctr->post("/duplicatefile", array($this, 'duplicatefile'))
            ->bind('duplicatefile');

        $ctr->get("/addstack/{filename}", array($this, 'addstack'))
            ->assert('filename', '.*')
            ->bind('addstack');

        $ctr->get("/tags/{taxonomytype}", array($this, 'tags'))
            ->bind('tags');

        $ctr->get("/populartags/{taxonomytype}", array($this, 'populartags'))
            ->bind('populartags');

        $ctr->get("/showstack", array($this, 'showstack'))
            ->bind('showstack');

        $ctr->get("/omnisearch", array($this, 'omnisearch'));

        $ctr->post("/folder/rename", array($this, 'renamefolder'))
            ->bind('renamefolder');

        $ctr->post("/folder/remove", array($this, 'removefolder'))
            ->bind('removefolder');

        $ctr->post("/folder/create", array($this, 'createfolder'))
            ->bind('createfolder');

        $ctr->get('/changelog/{contenttype}/{contentid}', array($this, 'changelogRecord'))
            ->value('contenttype', '')
            ->value('contentid', '0')
            ->bind('changelogrecord');

        $ctr->get('/email/{type}/{recipient}', array($this, 'emailNotification'))
            ->assert('type', '.*')
            ->bind('emailNotification');

        return $ctr;
    }

    /**
     * News. Film at 11.
     *
     * @param \Silex\Application $app
     * @param Request            $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function dashboardnews(Silex\Application $app, Request $request)
    {
        $source = 'http://news.bolt.cm/';
        $news = $app['cache']->fetch('dashboardnews'); // Two hours.
        $hostname = $request->getHost();
        $body = '';

        // If not cached, get fresh news.
        if ($news === false) {
            $app['logger.system']->info('Fetching from remote server: ' . $source, array('event' => 'news'));

            $driver = $app['db']->getDatabasePlatform()->getName();

            $url = sprintf(
                '%s?v=%s&p=%s&db=%s&name=%s',
                $source,
                rawurlencode($app->getVersion()),
                phpversion(),
                $driver,
                base64_encode($hostname)
            );

            // Define guzzle options
            $guzzleOptions['config']['curl'] = array();
            $curlOptions = &$guzzleOptions['config']['curl'];

            // Options valid if using a proxy
            if ($app['config']->get('general/httpProxy')) {
                $curlOptions[CURLOPT_PROXY] = $app['config']->get('general/httpProxy/host');
                $curlOptions[CURLOPT_PROXYTYPE] = 'CURLPROXY_HTTP';
                $curlOptions[CURLOPT_PROXYUSERPWD] = $app['config']->get('general/httpProxy/user') . ':' .
                                                       $app['config']->get('general/httpProxy/password')
                ;
            }

            // Standard option(s)
            $curlOptions[CURLOPT_CONNECTTIMEOUT] = 5;

            try {
                if ($app['deprecated.php']) {
                    $fetchedNewsData = $app['guzzle.client']->get($url, $guzzleOptions)->send()->getBody(true);
                } else {
                    $fetchedNewsData = $app['guzzle.client']->get($url, $guzzleOptions)->getBody(true);
                }

                $fetchedNewsItems = json_decode($fetchedNewsData);

                if ($fetchedNewsItems) {
                    $news = array();

                    // Iterate over the items, pick the first news-item that applies and the first alert we need to show
                    $version = $app->getVersion();
                    foreach ($fetchedNewsItems as $item) {
                        $type = ($item->type === 'alert') ? 'alert' : 'information';
                        if (!isset($news[$type])
                            && (empty($item->target_version) || version_compare($item->target_version, $version, '>'))
                        ) {
                            $news[$type] = $item;
                        }
                    }

{'fraction_non_alphanumeric': 0.18142211261816688, 'fraction_numerical': 0.0013974517057131113, 'mean_word_length': 2.0652557319223988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 152, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5363850', 'n_tokens_mistral': 3413, 'n_tokens_neox': 3324, 'n_words': 541}