<commit_before><?php

namespace App;
use ActiveRecord\SQLBuilder;
use HtmlGenerator\HtmlTag;
use Peertopark\UriBuilder;

/**
 * Class for writing out complex pagination HTML
 *  derived from http://codereview.stackexchange.com/a/10292/21877
 */
class Pagination {
	public $maxPages, $page, $itemsPerPage;
	public $_context = 2, $_wrap, $basePath;
	/**
	 * @var string
	 */
	private $_noconflict;

	/**
	 * Creates an instance of the class and return the generated HTML
	 *
	 * @param string $basePath     The starting path of ech paginated page without the page number
	 * @param int    $ItemsPerPage Number of items to display on a single page
	 * @param int    $EntryCount   Number of available entries
	 * @param string $noconflict   Specify a name to use for the query string parameter
	 */
	public function __construct(string $basePath, int $ItemsPerPage, ?int $EntryCount = null, string $noconflict = ''){
		$this->itemsPerPage = $ItemsPerPage;
		$this->basePath = $basePath;
		$this->_noconflict = $noconflict;
		$this->page = 1;
		$this->guessPage();

		if ($EntryCount !== null)
			$this->calcMaxPages($EntryCount);
	}

	private function guessPage(){
		$page = $_GET["{$this->_noconflict}page"] ?? null;
		if ($page === null){
			$uri = explode('/', $_SERVER['REQUEST_URI']);
			$page = array_pop($uri);
		}
		if (is_numeric($page))
			$this->page = max((int) $page, 1);
	}

	/**
	 * Set a specific page as the currrent
	 *
	 * @param int $page
	 *
	 * @return self
	 */
	public function forcePage(int $page){
		$this->page = max($page, 1);

		return $this;
	}

	/**
	 * Calculate the number of maximum possible pages
	 *
	 * @param int $EntryCount
	 *
	 * @return self
	 */
	public function calcMaxPages(int $EntryCount){
		$this->maxPages = (int) max(1, ceil($EntryCount/$this->itemsPerPage));
		if ($this->page > $this->maxPages)
			$this->page = $this->maxPages;

		return $this;
	}

	/**
	 * Collect page numbers for pagination
	 *
	 * @return array
	 * @throws \RuntimeException
	 */
	private function _getLinks(){
		if ($this->maxPages === null)
			throw new \RuntimeException('$this->maxPages must be defined');

		return array_unique(
			array_merge(
				[1],
				range(
					max($this->page - $this->_context, 1),
					min($this->page + $this->_context, $this->maxPages)
				),
				[$this->maxPages]
			)
		);
	}

	private function _makeLink($i){
		$href = new UriBuilder("/{$this->basePath}");
		$href->append_query_raw($this->getPageQueryString($i));
		return $href->build_http_string();
	}

	private function _makeItem(int $i, &$currentIndex = null, $nr = null){
		$current = $i === (int) $this->page;
		if ($currentIndex !== null && $current)
			$currentIndex = $nr;
		return '<li>'.(
			!$current
			? HtmlTag::createElement('a')->set('href', $this->_makeLink($i))->text($i)
			: "<strong>$i</strong>"
		).'</li>';<commit_msg>Fix pagination links throwing GET params away<commit_after><?php

namespace App;
use ActiveRecord\SQLBuilder;
use HtmlGenerator\HtmlTag;
use Peertopark\UriBuilder;

/**
 * Class for writing out complex pagination HTML
 *  derived from http://codereview.stackexchange.com/a/10292/21877
 */
class Pagination {
	public $maxPages, $page, $itemsPerPage;
	public $_context = 2, $_wrap, $basePath;
	/**
	 * @var string
	 */
	private $_noconflict;

	/**
	 * Creates an instance of the class and return the generated HTML
	 *
	 * @param string $basePath     The starting path of ech paginated page without the page number
	 * @param int    $ItemsPerPage Number of items to display on a single page
	 * @param int    $EntryCount   Number of available entries
	 * @param string $noconflict   Specify a name to use for the query string parameter
	 */
	public function __construct(string $basePath, int $ItemsPerPage, ?int $EntryCount = null, string $noconflict = ''){
		$this->itemsPerPage = $ItemsPerPage;
		$this->basePath = $basePath;
		$this->_noconflict = $noconflict;
		$this->page = 1;
		$this->guessPage();

		if ($EntryCount !== null)
			$this->calcMaxPages($EntryCount);
	}

	private function guessPage(){
		$page = $_GET["{$this->_noconflict}page"] ?? null;
		if ($page === null){
			$uri = explode('/', $_SERVER['REQUEST_URI']);
			$page = array_pop($uri);
		}
		if (is_numeric($page))
			$this->page = max((int) $page, 1);
	}

	/**
	 * Set a specific page as the currrent
	 *
	 * @param int $page
	 *
	 * @return self
	 */
	public function forcePage(int $page){
		$this->page = max($page, 1);

		return $this;
	}

	/**
	 * Calculate the number of maximum possible pages
	 *
	 * @param int $EntryCount
	 *
	 * @return self
	 */
	public function calcMaxPages(int $EntryCount){
		$this->maxPages = (int) max(1, ceil($EntryCount/$this->itemsPerPage));
		if ($this->page > $this->maxPages)
			$this->page = $this->maxPages;

		return $this;
	}

	/**
	 * Collect page numbers for pagination
	 *
	 * @return array
	 * @throws \RuntimeException
	 */
	private function _getLinks(){
		if ($this->maxPages === null)
			throw new \RuntimeException('$this->maxPages must be defined');

		return array_unique(
			array_merge(
				[1],
				range(
					max($this->page - $this->_context, 1),
					min($this->page + $this->_context, $this->maxPages)
				),
				[$this->maxPages]
			)
		);
	}

	private function _makeLink($i){
		$href = new UriBuilder("/{$this->basePath}");
		$href->append_query_raw($this->getPageQueryString($i));
		$get = $_GET;
		if (isset($get["{$this->_noconflict}page"]))
			unset($get["{$this->_noconflict}page"]);
		$href->append_query_array($get);
		return $href->build_http_string();
	}

	private function _makeItem(int $i, &$currentIndex = null, $nr = null){
		$current = $i === (int) $this->page;
		if ($currentIndex !== null && $current)
			$currentIndex = $nr;
		return '<li>'.(
			!$current
			? HtmlTag::createElement('a')->set('href', $this->_makeLink($i))->text($i)
			: "<strong>$i</strong>"
		).'</li>';
{'fraction_non_alphanumeric': 0.1658154579502894, 'fraction_numerical': 0.005788219271365339, 'mean_word_length': 3.745557350565428, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 78, 'https://': 0, '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': '6622648', 'n_tokens_mistral': 2185, 'n_tokens_neox': 1933, 'n_words': 511}