<commit_before><?php
/**
 * ownCloud - ocsms
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @contributor: stagprom <https://github.com/stagprom/>
 * @copyright <NAME> 2014-2016
 */

namespace OCA\OcSms\Lib;

use \OCP\Contacts\IManager as IContactsManager;

use \OCA\OcSms\Db\ConfigMapper;

class ContactCache {
	/**
	* @var array used to cache the parsed contacts for every request
	*/
	private $contacts;
	private $contactsInverted;
	private $contactPhotos;

	private $cfgMapper;
	private $contactsManager;

	public function __construct (ConfigMapper $cfgMapper, IContactsManager $contactsManager) {
		$this->contacts = array();
		$this->contactPhotos = array();
		$this->contactsInverted = array();

		$this->cfgMapper = $cfgMapper;
		$this->contactsManager = $contactsManager;
	}

	public function getContacts() {
		// Only load contacts if they aren't in the buffer
		if(count($this->contacts) == 0) {
			$this->loadContacts();
		}
		return $this->contacts;
	}

	public function getInvertedContacts() {
		// Only load contacts if they aren't in the buffer
		if(count($this->contactsInverted) == 0) {
			$this->loadContacts();
		}
		return $this->contactsInverted;
	}

	public function getContactPhotos() {
		// Only load contacts if they aren't in the buffer
		if(count($this->contactPhotos) == 0) {
			$this->loadContacts();
		}
		return $this->contactPhotos;
	}

	/**
	 * Partially importe this function from owncloud Chat app
	 * https://github.com/owncloud/chat/blob/master/app/chat.php
	 */
	private function loadContacts() {
		$this->contacts = array();
		$this->contactsInverted = array();

		// Cache country because of loops
                $configuredCountry = $this->cfgMapper->getCountry();

		$cm = $this->contactsManager;
		if ($cm == null) {
			return;
		}

		$result = $cm->search('',array('FN'));

		foreach ($result as $r) {
			if (isset ($r["TEL"])) {
				$phoneIds = $r["TEL"];
				if (is_array($phoneIds)) {
					$countPhone = count($phoneIds);
					for ($i=0; $i < $countPhone; $i++) {
						$this->pushPhoneNumberToCache($phoneIds[$i], $r["FN"], $configuredCountry);
					}
				}
				else {
					$this->pushPhoneNumberToCache($phoneIds, $r["FN"], $configuredCountry);
				}
				
				if (isset ($r["PHOTO"])) {
					// Remove useless prefix
					$ocversion = \OCP\Util::getVersion();
					$photoURL = preg_replace("#^VALUE=uri:#","",$r["PHOTO"], 1);
					$this->contactPhotos[$r["FN"]] = $photoURL;
				}
			}
		}
	}

	private function pushPhoneNumberToCache($rawPhone, $contactName, $country) {

		$phoneNb = PhoneNumberFormatter::format($country, $rawPhone);
		$this->contacts[$phoneNb] = $contactName;
		// Inverted contacts
		if (!isset($this->contactsInverted[$contactName])) {
			$this->contactsInverted[$contactName] = array();<commit_msg>Drop spaces from contactcache to dedup some more contacts<commit_after><?php
/**
 * ownCloud - ocsms
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @contributor: stagprom <https://github.com/stagprom/>
 * @copyright <NAME> 2014-2016
 */

namespace OCA\OcSms\Lib;

use \OCP\Contacts\IManager as IContactsManager;

use \OCA\OcSms\Db\ConfigMapper;

class ContactCache {
	/**
	* @var array used to cache the parsed contacts for every request
	*/
	private $contacts;
	private $contactsInverted;
	private $contactPhotos;

	private $cfgMapper;
	private $contactsManager;

	public function __construct (ConfigMapper $cfgMapper, IContactsManager $contactsManager) {
		$this->contacts = array();
		$this->contactPhotos = array();
		$this->contactsInverted = array();

		$this->cfgMapper = $cfgMapper;
		$this->contactsManager = $contactsManager;
	}

	public function getContacts() {
		// Only load contacts if they aren't in the buffer
		if(count($this->contacts) == 0) {
			$this->loadContacts();
		}
		return $this->contacts;
	}

	public function getInvertedContacts() {
		// Only load contacts if they aren't in the buffer
		if(count($this->contactsInverted) == 0) {
			$this->loadContacts();
		}
		return $this->contactsInverted;
	}

	public function getContactPhotos() {
		// Only load contacts if they aren't in the buffer
		if(count($this->contactPhotos) == 0) {
			$this->loadContacts();
		}
		return $this->contactPhotos;
	}

	/**
	 * Partially importe this function from owncloud Chat app
	 * https://github.com/owncloud/chat/blob/master/app/chat.php
	 */
	private function loadContacts() {
		$this->contacts = array();
		$this->contactsInverted = array();

		// Cache country because of loops
                $configuredCountry = $this->cfgMapper->getCountry();

		$cm = $this->contactsManager;
		if ($cm == null) {
			return;
		}

		$result = $cm->search('',array('FN'));

		foreach ($result as $r) {
			if (isset ($r["TEL"])) {
				$phoneIds = $r["TEL"];
				if (is_array($phoneIds)) {
					$countPhone = count($phoneIds);
					for ($i=0; $i < $countPhone; $i++) {
						$phoneNumber = preg_replace("#[ ]#","", $phoneIds[$i]);
						$this->pushPhoneNumberToCache($phoneNumber, $r["FN"], $configuredCountry);
					}
				}
				else {
					$phoneNumber = preg_replace("#[ ]#","", $phoneIds);
					$this->pushPhoneNumberToCache($phoneNumber, $r["FN"], $configuredCountry);
				}
				
				if (isset ($r["PHOTO"])) {
					// Remove useless prefix
					$ocversion = \OCP\Util::getVersion();
					$photoURL = preg_replace("#^VALUE=uri:#","",$r["PHOTO"], 1);
					$this->contactPhotos[$r["FN"]] = $photoURL;
				}
			}
		}
	}

	private function pushPhoneNumberToCache($rawPhone, $contactName, $country) {
		$phoneNb = PhoneNumberFormatter::format($country, $rawPhone);
		$this->contacts[$phoneNb] = $contactName;
		// Inverted contacts
		if (!isset($this->contactsInverted[$contactName])) {
			$this->contactsInverted[$contactName] = array();
{'fraction_non_alphanumeric': 0.16384563303994584, 'fraction_numerical': 0.004739336492890996, 'mean_word_length': 3.8474159146841673, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 65, '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': '7516944', 'n_tokens_mistral': 2235, 'n_tokens_neox': 1932, 'n_words': 441}