<commit_before><?php
/**
 * FilterResponseListener for adding a rel=self Link header to a response.
 */

namespace Graviton\RestBundle\Listener;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Graviton\RestBundle\HttpFoundation\LinkHeader;
use Graviton\RestBundle\HttpFoundation\LinkHeaderItem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * FilterResponseListener for adding a rel=self Link header to a response.
 *
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     http://swisscom.ch
 */
class PagingLinkResponseListener
{
    use GetRqlUrlTrait;

    /**
     * @var Router
     */
    private $router;

    /**
     * @var \Graviton\RestBundle\HttpFoundation\LinkHeader
     */
    private $linkHeader;

    /**
     * @param Router $router router
     */
    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    /**
     * add a rel=self Link header to the response
     *
     * @param FilterResponseEvent $event response listener event
     *
     * @return void
     */
    public function onKernelResponse(FilterResponseEvent $event)
    {
        $response = $event->getResponse();
        $request = $event->getRequest();

        // extract various info from route
        $routeName = $request->get('_route');
        $routeParts = explode('.', $routeName);
        $routeType = end($routeParts);

        // always set recordcount header
        $totalCount = null;
        if ($request->attributes->has('recordCount')) {
            $totalCount = $request->attributes->get('recordCount');
        }
        // maybe have one from solr?
        if ($request->attributes->has('solr-total-count')) {
            $response->headers->set('X-Search-Source', 'solr');
            $totalCount = $request->attributes->get('solr-total-count');
        }

        if (!is_null($totalCount)) {
            $response->headers->set('X-Record-Count', (string) $totalCount);
        }

        // only collections have paging
        if ($routeType == 'all' && $request->attributes->get('paging')) {
            $rql = '';
            if ($request->attributes->get('hasRql', false)) {
                $rql = $request->attributes->get('rawRql', '');
            }

            $this->linkHeader = LinkHeader::fromResponse($response);

            $this->generateLinks(
                $routeName,
                $request->attributes->get('page'),
                $request->attributes->get('numPages'),
                $request->attributes->get('perPage'),
                $request,
                $rql
            );
            $response->headers->set(
                'Link',
                (string) $this->linkHeader
            );
            $response->headers->set(
                'X-Total-Count',
                (string) $request->attributes->get('totalCount')
            );
        }
    }

    /**
     * generate headers for all paging links
     *
     * @param string  $route    name of route
     * @param integer $page     page to link to
     * @param integer $numPages number of all pages
     * @param integer $perPage  number of records per page
     * @param Request $request  request to get rawRql from
     * @param string  $rql      rql query string
     *
     * @return void
     */
    private function generateLinks($route, $page, $numPages, $perPage, Request $request, $rql)
    {
        if ($page > 2) {
            $this->generateLink($route, 1, $perPage, 'first', $request, $rql);
        }
        if ($page > 1) {
            $this->generateLink($route, $page - 1, $perPage, 'prev', $request, $rql);
        }
        if ($page < $numPages) {
            $this->generateLink($route, $page + 1, $perPage, 'next', $request, $rql);
        }
        if ($page != $numPages) {
            $this->generateLink($route, $numPages, $perPage, 'last', $request, $rql);
        }
    }

    /**
     * generate link header passed on params and type
     *
     * @param string  $routeName use with router to generate urls
     * @param integer $page      page to link to
     * @param integer $perPage   number of items per page
     * @param string  $type      rel type of link to generate
     * @param Request $request   request to get rawRql from
     * @param string  $rql       rql query string
     *
     * @return string
     */
    private function generateLink($routeName, $page, $perPage, $type, Request $request, $rql)
    {
        $limit = '';
        if ($perPage) {
            $page = ($page - 1) * $perPage;
            $limit = sprintf('limit(%s,%s)', $perPage, $page);
        }
        if (strpos($rql, 'limit') !== false) {
            $rql = preg_replace('/limit\(.*\)/U', $limit, $rql);
        } elseif (empty($rql)) {
            $rql .= $limit;
        } else {
            $rql .= '&'.$limit;
        }

        $url = $this->getRqlUrl(
            $request,
            $this->router->generate($routeName, [], UrlGeneratorInterface::ABSOLUTE_URL) .
                '?' . strtr($rql, [',' => '%2C'])
        );

        $this->linkHeader->add(new LinkHeaderItem($url, array('rel' => $type)));
    }
}
<commit_msg>correct headers for total counts<commit_after><?php
/**
 * FilterResponseListener for adding a rel=self Link header to a response.
 */

namespace Graviton\RestBundle\Listener;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Graviton\RestBundle\HttpFoundation\LinkHeader;
use Graviton\RestBundle\HttpFoundation\LinkHeaderItem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * FilterResponseListener for adding a rel=self Link header to a response.
 *
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     http://swisscom.ch
 */
class PagingLinkResponseListener
{
    use GetRqlUrlTrait;

    /**
     * @var Router
     */
    private $router;

    /**
     * @var \Graviton\RestBundle\HttpFoundation\LinkHeader
     */
    private $linkHeader;

    /**
     * @param Router $router router
     */
    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    /**
     * add a rel=self Link header to the response
     *
     * @param FilterResponseEvent $event response listener event
     *
     * @return void
     */
    public function onKernelResponse(FilterResponseEvent $event)
    {
        $response = $event->getResponse();
        $request = $event->getRequest();

        // extract various info from route
        $routeName = $request->get('_route');
        $routeParts = explode('.', $routeName);
        $routeType = end($routeParts);

        // always set recordcount header
        $totalCount = null;
        if ($request->attributes->has('recordCount')) {
            $response->headers->set('X-Record-Count', (string) $totalCount);
        }

        // only collections have paging
        if ($routeType == 'all' && $request->attributes->get('paging')) {
            $rql = '';
            if ($request->attributes->get('hasRql', false)) {
                $rql = $request->attributes->get('rawRql', '');
            }

            $this->linkHeader = LinkHeader::fromResponse($response);

            $this->generateLinks(
                $routeName,
                $request->attributes->get('page'),
                $request->attributes->get('numPages'),
                $request->attributes->get('perPage'),
                $request,
                $rql
            );
            $response->headers->set(
                'Link',
                (string) $this->linkHeader
            );
            $response->headers->set(
                'X-Total-Count',
                (string) $request->attributes->get('totalCount')
            );
        }

        // maybe have one from solr?
        if ($request->attributes->has('solr-total-count')) {
            $response->headers->set('X-Search-Source', 'solr');
            $response->headers->set(
                'X-Total-Count',
                (string) $request->attributes->get('solr-total-count')
            );
        }
    }

    /**
     * generate headers for all paging links
     *
     * @param string  $route    name of route
     * @param integer $page     page to link to
     * @param integer $numPages number of all pages
     * @param integer $perPage  number of records per page
     * @param Request $request  request to get rawRql from
     * @param string  $rql      rql query string
     *
     * @return void
     */
    private function generateLinks($route, $page, $numPages, $perPage, Request $request, $rql)
    {
        if ($page > 2) {
            $this->generateLink($route, 1, $perPage, 'first', $request, $rql);
        }
        if ($page > 1) {
            $this->generateLink($route, $page - 1, $perPage, 'prev', $request, $rql);
        }
        if ($page < $numPages) {
            $this->generateLink($route, $page + 1, $perPage, 'next', $request, $rql);
        }
        if ($page != $numPages) {
            $this->generateLink($route, $numPages, $perPage, 'last', $request, $rql);
        }
    }

    /**
     * generate link header passed on params and type
     *
     * @param string  $routeName use with router to generate urls
     * @param integer $page      page to link to
     * @param integer $perPage   number of items per page
     * @param string  $type      rel type of link to generate
     * @param Request $request   request to get rawRql from
     * @param string  $rql       rql query string
     *
     * @return string
     */
    private function generateLink($routeName, $page, $perPage, $type, Request $request, $rql)
    {
        $limit = '';
        if ($perPage) {
            $page = ($page - 1) * $perPage;
            $limit = sprintf('limit(%s,%s)', $perPage, $page);
        }
        if (strpos($rql, 'limit') !== false) {
            $rql = preg_replace('/limit\(.*\)/U', $limit, $rql);
        } elseif (empty($rql)) {
            $rql .= $limit;
        } else {
            $rql .= '&'.$limit;
        }

        $url = $this->getRqlUrl(
            $request,
            $this->router->generate($routeName, [], UrlGeneratorInterface::ABSOLUTE_URL) .
                '?' . strtr($rql, [',' => '%2C'])
        );

        $this->linkHeader->add(new LinkHeaderItem($url, array('rel' => $type)));
    }
}

{'fraction_non_alphanumeric': 0.14308366756832386, 'fraction_numerical': 0.001305848335043373, 'mean_word_length': 2.056442417331813, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 105, 'https://': 4, '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': '531672', 'n_tokens_mistral': 3124, 'n_tokens_neox': 3039, 'n_words': 724}