<commit_before><?php

class Manager {

	private static $instance;
	protected $feeds = array();
	protected $links = array();
	protected $done = array();

	public function __construct() {
		global $config;
		$this->feeds = Text::unhash(get_file(FILE_FEEDS));
		$this->links = Text::unhash(get_file(FILE_LINKS));
	}

	public static function getInstance($project = NULL) {
		if (!isset(self::$instance)) {
			self::$instance = new Manager();
		}
		return self::$instance;
	}

	protected function save() {
		update_file(FILE_FEEDS, Text::hash($this->feeds));
		update_file(FILE_LINKS, Text::hash($this->links));
	}

	protected function createNewFeed($url = '') {
		$id = Manager::newKey($this->feeds);
		$this->feeds[$id] = array(
			'title' => '',
			'url' => $url,
			'link' => '',
			'unread' => array(),
			'read' => array(),
			'archived' => array(),
			'deleted' => array()
		);
		return $id;
	}

	public function getFeeds() {
		return $this->feeds;
	}

	public function getFeed($id) {
		if (isset($this->feeds[$id])) { return $this->feeds[$id]; }
		return false;
	}

	public function getLinks($filter = array(), $idStart = NULL, $limit = NULL) {
		global $config;
		if (!$limit) { $limit = $config['links_per_page']; }
		$links = array();
		if (isset($filter['feed']) && isset($this->feeds[$filter['feed']])) {
			$feed = $this->feeds[$filter['feed']];
			foreach ($feed['unread'] as $v) { $links[$v] = $this->links[$v]; }
			foreach ($feed['read'] as $v) { $links[$v] = $this->links[$v]; }
			foreach ($feed['archived'] as $v) { $links[$v] = $this->links[$v]; }
		}
		else {
			$links = $this->links;
		}
		if (isset($filter['type'])) {
			foreach ($links as $id => $l) {
				if ($l['type'] != $filter['type']) { unset($links[$id]); }
			}
		}
		$links = Manager::sort($links);
		if ($idStart && isset($links[$idStart])) {
			$keys = array_keys($links);
			$start = array_search($idStart, $keys)+1;
		}
		else {
			$start = 0;
		}
		return array_slice($links, $start, $limit);
	}

	public function getLink($id) {
		if (isset($this->links[$id])) { return $this->links[$id]; }
		return false;
	}

	public function addFeed($post) {
		if (!isset($post['url'])
			|| !filter_var($post['url'], FILTER_VALIDATE_URL)
		) {
			return Trad::$settings['validate_url'];
		}
		foreach ($this->feeds as $f) {
			if ($f['url'] == $post['url']) {
				return Trad::A_ERROR_EXISTING_FEED;
			}
		}
		$id = $this->createNewFeed($post['url']);
		$this->update(array($id));
		if (!isset($this->done[$id]) || $this->done[$id] === false) {
			unset($this->feeds[$id]);
			$this->done = array();
			return Trad::A_ERROR_BAD_FEED;
		}
		$this->feeds[$id]['title'] = $this->done[$id]['title'];
		$this->feeds[$id]['url'] = $this->done[$id]['url'];
		$this->feeds[$id]['link'] = $this->done[$id]['link'];
		$this->save();
		$this->done = array();
		return true;
	}

	public function editFeed($post, $id) {
		if (!isset($this->feeds[$id])
			|| !isset($post['title'])
			|| !isset($post['url'])
			|| !isset($post['link'])
		) {
			return Trad::A_ERROR_FORM;
		}
		if (!filter_var($post['url'], FILTER_VALIDATE_URL)
			|| !filter_var($post['link'], FILTER_VALIDATE_URL)
		) {
			return Trad::$settings['validate_url'];
		}
		$oldfeed = $this->feeds[$id];
		$this->feeds[$id]['title'] =
			htmlspecialchars($post['title'], ENT_QUOTES, 'UTF-8');
		$this->feeds[$id]['url'] = $post['url'];
		$this->feeds[$id]['link'] = $post['link'];
		$this->update(array($id));
		if (!isset($this->done[$id]) || $this->done[$id] === false) {
			$this->feeds[$id] = $oldfeed;
			$this->done = array();
			return Trad::A_ERROR_BAD_FEED;
		}
		$this->save();
		$this->done = array();
		return true;
	}

	public function refreshFeed($feed = NULL) {
		$added = array();
		$ids = array($feed);
		if ($feed === NULL || !isset($this->feeds[$feed])) {
			$ids = array_keys($this->feeds);
		}
		$this->update($ids);
		foreach ($this->done as $d) {
			if ($d !== false) {
				foreach ($d['added'] as $id) {
					$added[] = $this->links[$id];
				}
			}
		}
		$this->save();
		$this->done = array();
		return $added;
	}

	public function clearFeed($id) {
		if (!isset($this->feeds[$id])) { return Trad::A_ERROR_UNKNOWN_FEED; }
		foreach ($this->feeds[$id]['unread'] as $l) {
			unset($this->links[$l]);
			$this->feeds[$id]['deleted'][] = $l;
		}
		$this->feeds[$id]['unread'] = array();
		foreach ($this->feeds[$id]['read'] as $l) {
			unset($this->links[$l]);
			$this->feeds[$id]['deleted'][] = $l;
		}
		$this->feeds[$id]['read'] = array();
		$this->save();
		return true;
	}
<commit_msg>Résoud bug mauvais ids de liens lors d'une actualisation.<commit_after><?php

class Manager {

	private static $instance;
	protected $feeds = array();
	protected $links = array();
	protected $done = array();

	public function __construct() {
		global $config;
		$this->feeds = Text::unhash(get_file(FILE_FEEDS));
		$this->links = Text::unhash(get_file(FILE_LINKS));
	}

	public static function getInstance($project = NULL) {
		if (!isset(self::$instance)) {
			self::$instance = new Manager();
		}
		return self::$instance;
	}

	protected function save() {
		update_file(FILE_FEEDS, Text::hash($this->feeds));
		update_file(FILE_LINKS, Text::hash($this->links));
	}

	protected function createNewFeed($url = '') {
		$id = Manager::newKey($this->feeds);
		$this->feeds[$id] = array(
			'title' => '',
			'url' => $url,
			'link' => '',
			'unread' => array(),
			'read' => array(),
			'archived' => array(),
			'deleted' => array()
		);
		return $id;
	}

	public function getFeeds() {
		return $this->feeds;
	}

	public function getFeed($id) {
		if (isset($this->feeds[$id])) { return $this->feeds[$id]; }
		return false;
	}

	public function getLinks($filter = array(), $idStart = NULL, $limit = NULL) {
		global $config;
		if (!$limit) { $limit = $config['links_per_page']; }
		$links = array();
		if (isset($filter['feed']) && isset($this->feeds[$filter['feed']])) {
			$feed = $this->feeds[$filter['feed']];
			foreach ($feed['unread'] as $v) { $links[$v] = $this->links[$v]; }
			foreach ($feed['read'] as $v) { $links[$v] = $this->links[$v]; }
			foreach ($feed['archived'] as $v) { $links[$v] = $this->links[$v]; }
		}
		else {
			$links = $this->links;
		}
		if (isset($filter['type'])) {
			foreach ($links as $id => $l) {
				if ($l['type'] != $filter['type']) { unset($links[$id]); }
			}
		}
		$links = Manager::sort($links);
		if ($idStart && isset($links[$idStart])) {
			$keys = array_keys($links);
			$start = array_search($idStart, $keys)+1;
		}
		else {
			$start = 0;
		}
		return array_slice($links, $start, $limit);
	}

	public function getLink($id) {
		if (isset($this->links[$id])) { return $this->links[$id]; }
		return false;
	}

	public function addFeed($post) {
		if (!isset($post['url'])
			|| !filter_var($post['url'], FILTER_VALIDATE_URL)
		) {
			return Trad::$settings['validate_url'];
		}
		foreach ($this->feeds as $f) {
			if ($f['url'] == $post['url']) {
				return Trad::A_ERROR_EXISTING_FEED;
			}
		}
		$id = $this->createNewFeed($post['url']);
		$this->update(array($id));
		if (!isset($this->done[$id]) || $this->done[$id] === false) {
			unset($this->feeds[$id]);
			$this->done = array();
			return Trad::A_ERROR_BAD_FEED;
		}
		$this->feeds[$id]['title'] = $this->done[$id]['title'];
		$this->feeds[$id]['url'] = $this->done[$id]['url'];
		$this->feeds[$id]['link'] = $this->done[$id]['link'];
		$this->save();
		$this->done = array();
		return true;
	}

	public function editFeed($post, $id) {
		if (!isset($this->feeds[$id])
			|| !isset($post['title'])
			|| !isset($post['url'])
			|| !isset($post['link'])
		) {
			return Trad::A_ERROR_FORM;
		}
		if (!filter_var($post['url'], FILTER_VALIDATE_URL)
			|| !filter_var($post['link'], FILTER_VALIDATE_URL)
		) {
			return Trad::$settings['validate_url'];
		}
		$oldfeed = $this->feeds[$id];
		$this->feeds[$id]['title'] =
			htmlspecialchars($post['title'], ENT_QUOTES, 'UTF-8');
		$this->feeds[$id]['url'] = $post['url'];
		$this->feeds[$id]['link'] = $post['link'];
		$this->update(array($id));
		if (!isset($this->done[$id]) || $this->done[$id] === false) {
			$this->feeds[$id] = $oldfeed;
			$this->done = array();
			return Trad::A_ERROR_BAD_FEED;
		}
		$this->save();
		$this->done = array();
		return true;
	}

	public function refreshFeed($feed = NULL) {
		$added = array();
		$ids = array($feed);
		if ($feed === NULL || !isset($this->feeds[$feed])) {
			$ids = array_keys($this->feeds);
		}
		$this->update($ids);
		foreach ($this->done as $d) {
			if ($d !== false) {
				foreach ($d['added'] as $id) {
					$added[$id] = $this->links[$id];
				}
			}
		}
		$this->save();
		$this->done = array();
		return $added;
	}

	public function clearFeed($id) {
		if (!isset($this->feeds[$id])) { return Trad::A_ERROR_UNKNOWN_FEED; }
		foreach ($this->feeds[$id]['unread'] as $l) {
			unset($this->links[$l]);
			$this->feeds[$id]['deleted'][] = $l;
		}
		$this->feeds[$id]['unread'] = array();
		foreach ($this->feeds[$id]['read'] as $l) {
			unset($this->links[$l]);
			$this->feeds[$id]['deleted'][] = $l;
		}
		$this->feeds[$id]['read'] = array();
		$this->save();
		return true;
	}

{'fraction_non_alphanumeric': 0.2504658555299792, 'fraction_numerical': 0.0006576783952647156, 'mean_word_length': 4.172335600907029, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 141, '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': '4407432', 'n_tokens_mistral': 3810, 'n_tokens_neox': 3453, 'n_words': 614}