<commit_before><?php
namespace WillFarrell\AlfredPkgMan;

/*
Hex

*/

// ****************

require_once('Cache.php');

class Repo {

	private $id = 'hex';
	private $kind = 'components'; // for none found msg
	private $min_query_length = 1; // increase for slow DBs
	private $max_return = 25;

	private $cache;
	private $w;
	private $pkgs;

	function __construct() {

		$this->cache = new Cache();

		// get DB here if not dynamic search
		//$data = (array) $this->cache->get_db('.$this->id.');
	}

	// return id | url | pkgstr
	function makeArg($id, $url, $version) {
		return $id . "|" . $url . "|" . "\"$id\":\"$version\",";
	}

	/*function check($pkg, $query) {
		if (!$query) { return true; }
		if (strpos($pkg["name"], $query) !== false) {
			return true;
		} else if (strpos($pkg["description"], $query) !== false) {
			return true;
		}

		return false;
	}*/

	function search($query) {
		if ( strlen($query) < $this->min_query_length) {
			if ( strlen($query) === 0 ) { return; }
			$this->cache->w->result(
				"{$this->id}-min",
				$query,
				"Minimum query length of {$this->min_query_length} not met.",
				"",
				"icon-cache/{$this->id}.png"
			);
			return;
		}

		$this->pkgs = $this->cache->get_query_json('hex', $query, 'https://hex.pm/api/packages?search='.$query);

		foreach($this->pkgs as $pkg) {
			$url = str_replace("api/", "", $pkg->url);
			$this->cache->w->result(
				$pkg->url,
				$this->makeArg($pkg->name, $url, "*"),
				$pkg->name, $url,
				"icon-cache/{$this->id}.png"
			);

			// only search till max return reached
			if ( count ( $this->cache->w->results() ) == $this->max_return ) {
				break;
			}
		}

		if ( count( $this->cache->w->results() ) == 0) {
			$this->cache->w->result(
				"{$this->id}-search",
				"https://hex.pm/packages?search={$query}",
				"No {$this->kind} were found that matched \"{$query}\"",
				"Click to see the results for yourself",
				"icon-cache/{$this->id}.png"
			);
		}
	}

	function xml() {
		$this->cache->w->result(
			"{$this->id}-www",
			"http://hex.pm/",
			"Go to the website",
			"http://hex.pm",
			"icon-cache/{$this->id}.png"
		);

		return $this->cache->w->toxml();
	}

}

// ****************

/*$query = "leaflet";
$repo = new Repo();
$repo->search($query);
echo $repo->xml();*/

?><commit_msg>Refactor hex to use new Repo class<commit_after><?php
namespace WillFarrell\AlfredPkgMan;

require_once('Cache.php');
require_once('Repo.php');

class Hex extends Repo
{
	protected $id         = 'hex';
	protected $kind       = 'components';
	protected $url        = 'https://hex.pm';
	protected $search_url = 'https://hex.pm/api/packages?search=';

	public function search($query)
	{
		if (!$this->hasMinQueryLength($query)) {
			return $this->xml(); 
		}

		$this->pkgs = $this->cache->get_query_json(
			$this->id,
			$query,
			"{$this->search_url}{$query}"
		);

		foreach($this->pkgs as $pkg) {
			$url = str_replace('api/', '', $pkg->url);
			$this->cache->w->result(
				$pkg->url,
				$this->makeArg($pkg->name, $url),
				$pkg->name, $url,
				"icon-cache/{$this->id}.png"
			);

			// only search till max return reached
			if ( count ( $this->cache->w->results() ) == $this->max_return ) {
				break;
			}
		}

		$this->noResults($query, "{$this->search_url}{$query}");

		return $this->xml();
	}
}

// Test code, uncomment to debug this script from the command-line
// $repo = new Hex();
// echo $repo->search('test');
{'fraction_non_alphanumeric': 0.2344847775175644, 'fraction_numerical': 0.0014637002341920376, 'mean_word_length': 3.111913357400722, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 76, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 5}, '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': '45751', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1253, 'n_words': 256}