<commit_before><?php

/**
 *
 * Shared methods to build Facebook OpenGraph
 *  https://developers.facebook.com/docs/sharing/webmasters#markup
 * and Twitter Cards meta-tags
 *  https://dev.twitter.com/cards/overview
 */

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

trait SharingBuilderTrait
{
    /*
     * transforms en -> en_US and de -> de_DE
     *
     */
    protected function buildOgLocale($request)
    {
        $locale = $request->getLocale();

        switch ($locale) {
            case 'en':
                $append = 'US';
                break;

            default:
                $append = strtoupper($locale);

        }

        return implode('_', [ $locale, $append ]);
    }

    /**
     * Build og:* meta-tags for sharing on FB
     *
     * Debug through https://developers.facebook.com/tools/debug/sharing/
     *
     */
    public function buildOg($entity, $request, $routeName, $routeParams = [])
    {
        $translator = $this->get('translator');
        $twig = $this->get('twig');
        $globals = $twig->getGlobals();

        if (empty($routeParams)) {
            $routeParams = [ 'id' => $entity->getId() ];
        }

        $og = [
            'og:site_name' => /** @Ignore */ $translator->trans($globals['siteName']),
            'og:locale' => $this->buildOgLocale($request),
            'og:url' => $this->generateUrl($routeName, $routeParams, true),
        ];

        /*
        foreach ($app['app_allowed_locales'] as $locale) {
            $locale_full = $this->buildOgLocale($request, $locale);
            if ($locale_full != $og['og:locale']) {
                if (!isset($og['og:locale:alternate'])) {
                    $og['og:locale:alternate'] = array();
                }
                $og['og:locale:alternate'][] = $locale_full;
            }
        }
        */

        $baseUri = $request->getUriForPath('/');

        if ($entity instanceof \AppBundle\Entity\OgSerializable) {
            $ogEntity = $entity->ogSerialize($request->getLocale(), $baseUri);
            if (isset($ogEntity)) {
                $og = array_merge($og, $ogEntity);
                if (array_key_exists('article:section', $og)) {
                    $og['article:section'] = /** @Ignore */ $translator->trans($og['article:section']);
                }
            }
        }

        if (empty($og['og:image'])) {
            // this one is required
            if ($entity instanceof \AppBundle\Entity\Person) {
                $og['og:image'] = $baseUri . 'img/icon/placeholder_person.png';
            }<commit_msg>Fix generation of absolute urls<commit_after><?php

/**
 *
 * Shared methods to build Facebook OpenGraph
 *  https://developers.facebook.com/docs/sharing/webmasters#markup
 * and Twitter Cards meta-tags
 *  https://dev.twitter.com/cards/overview
 */

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

trait SharingBuilderTrait
{
    /*
     * transforms en -> en_US and de -> de_DE
     *
     */
    protected function buildOgLocale($request)
    {
        $locale = $request->getLocale();

        switch ($locale) {
            case 'en':
                $append = 'US';
                break;

            default:
                $append = strtoupper($locale);

        }

        return implode('_', [ $locale, $append ]);
    }

    /**
     * Build og:* meta-tags for sharing on FB
     *
     * Debug through https://developers.facebook.com/tools/debug/sharing/
     *
     */
    public function buildOg($entity, $request, $routeName, $routeParams = [])
    {
        $translator = $this->get('translator');
        $twig = $this->get('twig');
        $globals = $twig->getGlobals();

        if (empty($routeParams)) {
            $routeParams = [ 'id' => $entity->getId() ];
        }

        $og = [
            'og:site_name' => /** @Ignore */ $translator->trans($globals['siteName']),
            'og:locale' => $this->buildOgLocale($request),
            'og:url' => $this->generateUrl($routeName, $routeParams,
                                           \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL),
        ];

        /*
        foreach ($app['app_allowed_locales'] as $locale) {
            $locale_full = $this->buildOgLocale($request, $locale);
            if ($locale_full != $og['og:locale']) {
                if (!isset($og['og:locale:alternate'])) {
                    $og['og:locale:alternate'] = array();
                }
                $og['og:locale:alternate'][] = $locale_full;
            }
        }
        */

        $baseUri = $request->getUriForPath('/');

        if ($entity instanceof \AppBundle\Entity\OgSerializable) {
            $ogEntity = $entity->ogSerialize($request->getLocale(), $baseUri);
            if (isset($ogEntity)) {
                $og = array_merge($og, $ogEntity);
                if (array_key_exists('article:section', $og)) {
                    $og['article:section'] = /** @Ignore */ $translator->trans($og['article:section']);
                }
            }
        }

        if (empty($og['og:image'])) {
            // this one is required
            if ($entity instanceof \AppBundle\Entity\Person) {
                $og['og:image'] = $baseUri . 'img/icon/placeholder_person.png';
            }
{'fraction_non_alphanumeric': 0.16126615471061997, 'fraction_numerical': 0.0, 'mean_word_length': 1.8880475932936722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 6, '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': '4222701', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1526, 'n_words': 264}