<commit_before><?php
/**
 * Users plugin
 * @module Users
 * @main Users
 */
/**
 * Static methods for the Users models.
 * @class Users
 * @extends Base_Users
 * @abstract
 */
abstract class Users extends Base_Users
{
	/*
	 * This is where you would place all the static methods for the models,
	 * the ones that don't strongly pertain to a particular row or table.

	 * * * */
	
	/**
	 * Determine whether a user id is that of a community
	 * @method isCommunityId
	 * @static
	 * @param {string} $userId The user id to test 
	 * @return {boolean}
	 */
	static function isCommunityId($userId)
	{
        if (in_array($userId, Q_Config::expect("Q", "plugins"))) {
			return false;
        }
		$first = mb_substr($userId, 0, 1, "UTF-8");
		return (mb_strtolower($first, "UTF-8") != $first);
	}
	
	/**
	 * Get the id of the main community from the config. Defaults to the app name.
	 * @method communityId
	 * @static
	 * @return {string} The id of the main community for the installed app.
	 */
	static function communityId()
	{
		$communityId = Q_Config::get('Users', 'community', 'id', null);
		return $communityId ? $communityId : Q::app();
	}
	
	/**
	 * Get the name of the main community from the config. Defaults to the app name.
	 * @method communityName
	 * @static
	 * @return {string} The name of the main community for the installed app.
	 */
	static function communityName()
	{
		$communityName = Q_Config::get('Users', 'community', 'name', null);
		return $communityName ? $communityName : Q::app();
	}
	
	/**
	 * Get the suffix of the main community from the config, such as "Inc." or "LLC"
	 * @return {string|null} The suffix of the main community for the installed app.
	 */
	static function communitySuffix()
	{
		return Q_Config::get('Users', 'community', 'suffix', null);
	}
	/**
	 * Get default user language from users_user table
	 * @method getLanguage
	 * @static
	 * @param {string} $userId
	 * @return {string}
	 */
	static function getLanguage($userId){
		$user = self::fetch($userId, true);

		return isset($user->preferredLanguage) ? $user->preferredLanguage : Q_Text::$language;
	}
	/**
	 * Rturn an array of the user's roles relative to a publisher
	 * @method roles
	 * @static
	 * @param string [$publisherId=Users::communityId()]
	 *  The id of the publisher relative to whom to calculate the roles.
	 *  Defaults to the community id.
	 * @param {string|array|Db_Expression} [$filter=null] 
	 *  You can pass additional criteria here for the label field
	 *  in the `Users_Contact::select`, such as an array or Db_Range
	 * @param {array} [$options=array()] Any additional options to pass to the query, such as "ignoreCache"
	 * @param {string} [$userId=null] If not passed, the logged in user is used, if any
	 * @return {array} An associative array of $roleName => $contactRow pairs
	 * @throws {Users_Exception_NotLoggedIn}
	 */
	static function roles(
		$publisherId = null,
		$filter = null,
		$options = array(),
		$userId = null)
	{
		if (empty($publisherId)) {
			$publisherId = Users::communityId();
		}
		if (!isset($userId)) {
			$user = Users::loggedInUser(false, false);
			if (!$user) {
				return array();
			}
			$userId = $user->id;
		}
		$contacts = Users_Contact::select()
			->where(array(
				'userId' => $publisherId,
				'contactUserId' => $userId
			))->andWhere($filter ? array('label' => $filter) : null)
			->options($options)
			->fetchDbRows(null, null, 'label');
		return $contacts;
	}
	/**
	 * Return an array of users_contact rows where user assigned by labels
	 * @method byRoles
	 * @static
	 * @param {string|array|Db_Expression} $filter
	 *  You can pass additional criteria here for the label field
	 *  in the `Users_Contact::select`, such as an array or Db_Range
	 * @param {array} [$options=array()] Any additional options to pass to the query, such as "ignoreCache"
	 * @param {bool} [$options.onlyCommunities=false] Set this to true if you want to get only communities rows (userId - is community).
	 * @param {string} [$userId=null] If not passed, the logged in user is used, if any
	 * @return {array} An associative array of $roleName => $contactRow pairs
	 * @throws {Users_Exception_NotLoggedIn}
	 */
	static function byRoles (
		$filter,
		$options = array(),
		$userId = null)
	{
		if (!isset($userId)) {
			$user = Users::loggedInUser(false, false);
			if (!$user) {
				return array();
			}
			$userId = $user->id;
		}

		$contacts = Users_Contact::select()
			->where(array(
				'contactUserId' => $userId
			))->andWhere(array('label' => $filter))
			->options($options)
			->fetchDbRows(null, null, 'userId');

		if (Q::ifset($options, "onlyCommunities", false)) {
			foreach ($contacts as $i => $contact) {
				if (Users::isCommunityId($contact->userId)) {
					continue;
				}

				unset($contacts[$i]);
			}
		}

		return $contacts;
	}
	/**
	 * Intelligently retrieves user by id
	 * @method fetch
	 * @static
	 * @param {string} $userId
	 * @param {boolean} [$throwIfMissing=false] If true, throws an exception if the user can't be fetched
	 * @return {Users_User|null}
	 * @throws {Users_Exception_NoSuchUser} If the URI contains an invalid "username"
	 */<commit_msg>Modified method Users::byRoles Make $filter argument optional .<commit_after><?php
/**
 * Users plugin
 * @module Users
 * @main Users
 */
/**
 * Static methods for the Users models.
 * @class Users
 * @extends Base_Users
 * @abstract
 */
abstract class Users extends Base_Users
{
	/*
	 * This is where you would place all the static methods for the models,
	 * the ones that don't strongly pertain to a particular row or table.

	 * * * */
	
	/**
	 * Determine whether a user id is that of a community
	 * @method isCommunityId
	 * @static
	 * @param {string} $userId The user id to test 
	 * @return {boolean}
	 */
	static function isCommunityId($userId)
	{
        if (in_array($userId, Q_Config::expect("Q", "plugins"))) {
			return false;
        }
		$first = mb_substr($userId, 0, 1, "UTF-8");
		return (mb_strtolower($first, "UTF-8") != $first);
	}
	
	/**
	 * Get the id of the main community from the config. Defaults to the app name.
	 * @method communityId
	 * @static
	 * @return {string} The id of the main community for the installed app.
	 */
	static function communityId()
	{
		$communityId = Q_Config::get('Users', 'community', 'id', null);
		return $communityId ? $communityId : Q::app();
	}
	
	/**
	 * Get the name of the main community from the config. Defaults to the app name.
	 * @method communityName
	 * @static
	 * @return {string} The name of the main community for the installed app.
	 */
	static function communityName()
	{
		$communityName = Q_Config::get('Users', 'community', 'name', null);
		return $communityName ? $communityName : Q::app();
	}
	
	/**
	 * Get the suffix of the main community from the config, such as "Inc." or "LLC"
	 * @return {string|null} The suffix of the main community for the installed app.
	 */
	static function communitySuffix()
	{
		return Q_Config::get('Users', 'community', 'suffix', null);
	}
	/**
	 * Get default user language from users_user table
	 * @method getLanguage
	 * @static
	 * @param {string} $userId
	 * @return {string}
	 */
	static function getLanguage($userId){
		$user = self::fetch($userId, true);

		return isset($user->preferredLanguage) ? $user->preferredLanguage : Q_Text::$language;
	}
	/**
	 * Rturn an array of the user's roles relative to a publisher
	 * @method roles
	 * @static
	 * @param string [$publisherId=Users::communityId()]
	 *  The id of the publisher relative to whom to calculate the roles.
	 *  Defaults to the community id.
	 * @param {string|array|Db_Expression} [$filter=null] 
	 *  You can pass additional criteria here for the label field
	 *  in the `Users_Contact::select`, such as an array or Db_Range
	 * @param {array} [$options=array()] Any additional options to pass to the query, such as "ignoreCache"
	 * @param {string} [$userId=null] If not passed, the logged in user is used, if any
	 * @return {array} An associative array of $roleName => $contactRow pairs
	 * @throws {Users_Exception_NotLoggedIn}
	 */
	static function roles(
		$publisherId = null,
		$filter = null,
		$options = array(),
		$userId = null)
	{
		if (empty($publisherId)) {
			$publisherId = Users::communityId();
		}
		if (!isset($userId)) {
			$user = Users::loggedInUser(false, false);
			if (!$user) {
				return array();
			}
			$userId = $user->id;
		}
		$contacts = Users_Contact::select()
			->where(array(
				'userId' => $publisherId,
				'contactUserId' => $userId
			))->andWhere($filter ? array('label' => $filter) : null)
			->options($options)
			->fetchDbRows(null, null, 'label');
		return $contacts;
	}
	/**
	 * Return an array of users_contact rows where user assigned by labels
	 * @method byRoles
	 * @static
	 * @param {string|array|Db_Expression} [$filter=null]
	 *  You can pass additional criteria here for the label field
	 *  in the `Users_Contact::select`, such as an array or Db_Range
	 * @param {array} [$options=array()] Any additional options to pass to the query, such as "ignoreCache"
	 * @param {bool} [$options.onlyCommunities=false] Set this to true if you want to get only communities rows (userId - is community).
	 * @param {string} [$userId=null] If not passed, the logged in user is used, if any
	 * @return {array} An associative array of $roleName => $contactRow pairs
	 * @throws {Users_Exception_NotLoggedIn}
	 */
	static function byRoles ($filter = null, $options = array(), $userId = null)
	{
		if (!isset($userId)) {
			$user = Users::loggedInUser(false, false);
			if (!$user) {
				return array();
			}
			$userId = $user->id;
		}

		$contacts = Users_Contact::select()
			->where(array(
				'contactUserId' => $userId
			))->andWhere($filter ? array('label' => $filter) : null)
			->options($options)
			->fetchDbRows(null, null, 'userId');

		if (Q::ifset($options, "onlyCommunities", false)) {
			foreach ($contacts as $i => $contact) {
				if (Users::isCommunityId($contact->userId)) {
					continue;
				}

				unset($contacts[$i]);
			}
		}

		return $contacts;
	}
	/**
	 * Intelligently retrieves user by id
	 * @method fetch
	 * @static
	 * @param {string} $userId
	 * @param {boolean} [$throwIfMissing=false] If true, throws an exception if the user can't be fetched
	 * @return {Users_User|null}
	 * @throws {Users_Exception_NoSuchUser} If the URI contains an invalid "username"
	 */
{'fraction_non_alphanumeric': 0.13803385917660638, 'fraction_numerical': 0.0007695267410542517, 'mean_word_length': 3.700271247739602, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '5550810', 'n_tokens_mistral': 3552, 'n_tokens_neox': 3193, 'n_words': 1100}