<commit_before><?php
/**
 * wslib.php - TikiWiki CMS/GroupWare
 *
 * This library enables the basic management of workspaces (WS)
 * TODO: Probably we need to modify this to adapt it to perspectives and 
 * new stuff added to Tiki in the last Tikifest.
 * 
 * @package	lib
 * @author	<NAME> (mangapower) <<EMAIL>>
 * @author	<NAME> (axold) <<EMAIL>>
 * @license	http://www.opensource.org/licenses/lgpl-2.1.php
 */

//Controlling Access
require_once 'tiki-setup.php';
if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
  header("location: index.php");
  exit;
}

//Rest of Imports
global $categlib; include_once 'lib/categories/categlib.php';

/**
 * wslib - The Workspaces Library for TikiWiki
 * TODO: Refine documentation!!!
 *
 * @category	TikiWiki
 * @package	lib/workspaces
 * @version	$Id
 */
class wslib extends CategLib 
{
    /** Stores the $prefs['ws_container'] for avoid to check it in every function */
    private $ws_container;

    /** Stores this objectype, this is WS */
    private $objectType;
	
	/** Stores the view Perm */
	private $viewPerm;

    /** Constructor, give the dbtiki to its parent, this is Categlib */
    public function __construct()
    {
		global $prefs;
		$this->ws_container = (int) $prefs['ws_container'];
		$this->objectType = 'ws';
		$this->viewPerm = 'tiki_p_ws_view';
    }

    /** Initialize the Workspaces in TikiWiki setting a container in the category table and return its ID
     *
     * @return The ws_container ID if it wasn't set before the ws_container, if not, null
     */
    public function init_ws()
    {
	if (!$this->ws_container)
	{
	    global $prefs, $tikilib;
	    $id = parent::add_category(0, '', 'Workspaces Container');
	    $tikilib->set_preference('ws_container', $id);
	    $tikilib->set_preference('feature_perspective', 'y');
	    $this->ws_container = (int) $prefs['ws_container'];
	    return $id;
	}
    }

    /** Get the ws_container stored in $prefs. This function is used to avoid certain calls to the $prefs array.
     *
     * @return The ws container id stored in the var $prefs
     */
    public function get_ws_container()
    {
		return $this->ws_container;
    }
    
    /** Create a new WS with one group inside it with the associated perm 'tiki_p_ws_view'.
     *
     * @param $name Name of the Workspace
     * @param $parentWS Name of the ParentWS, if ParentWS is null, its default value will be 0
     * @param $groups An associative array of groups in the form array("groupName" => (string) name, "groupDescription" => (string) description,
     * "noCreateNewGroup" => boolean, "additionalPerms" => array("additionalperm1", "additionalperm2", ...))
     * @param $additionalPerms Associative array for giving more perms than the default perm 'tiki_p_ws_view'
     * @return The ID of the WS
     */
    public function create_ws ($name, $groups, $parentWS = 0, $description = '')
    {
		global $perspectivelib; require_once 'lib/perspectivelib.php';
		
		if (empty($parentWS))
			$parentWS = 0;
			
		$ws_id = parent::add_category($parentWS, $name, $description, $this->ws_container);
		
		foreach ($groups as $group)
		{
			$groupName = $group["groupName"];
			$groupDescription = $group["groupDescription"];
			$noCreateNewGroup = $group["noCreateNewGroup"];
			$additionalPerms = $group["additionalPerms"];
		
			if ($noCreateNewGroup)
			{
				$this->set_permissions_for_group_in_ws ($ws_id, $groupName, array($this->viewPerm));
				if ($additionalPerms != null)
					$this->set_permissions_for_group_in_ws($ws_id, $groupName, $additionalPerms);
			}
			else
				$this->add_ws_group ($ws_id, $name, $groupName, $groupDescription, $additionalPerms);
		}
		
		//We create the perspective for the WS
		$wsValue = $this->get_ws_perspective_value ($ws_id);
		$pspId = $perspectivelib->replace_perspective(null, $wsValue);
		//I set this for the ws identificacion, because we can have two ws with the same name and for psp
		//this could be a problem in order to get the psp from the db
		$perspectivelib->replace_preferences($pspId, array('wsId' => $ws_id, 'wsName' => $name, 'wsHomepage' => '', 'wsStyle' => '', 'sitemycode' => '<div style="align: left; padding-left: 15px;">You are currently in '.$name.' workspace. <a class="link" href="tiki-switch_perspective.php">{tr}Reset Tiki to its normal status!{/tr}</a></div>')); 
			
		return $ws_id;
    }

    /** Add new group to a WS
     *
     * @param $id_ws The WS id you want to add the group
     * @param $wsName The name of the WS, it can be null
     * @param $nameGroup The name of the group you want to create
     * @param $additionalPerms Associative array for giving more perms than the default perm 'tiki_p_ws_view'
     * @return If the WS was sucesfully created true, if not false.
     */<commit_msg>[FIX] I18N [FIX] Set-up category jail (not fully functional because hidden categories are not auto-assigned, but category jailing works where deployed for objects categorized in the workspace) [ENH] Set-up permissions view for the perspective to prevent non-members from seeing it.<commit_after><?php
/**
 * wslib.php - TikiWiki CMS/GroupWare
 *
 * This library enables the basic management of workspaces (WS)
 * TODO: Probably we need to modify this to adapt it to perspectives and 
 * new stuff added to Tiki in the last Tikifest.
 * 
 * @package	lib
 * @author	<NAME> (mangapower) <<EMAIL>>
 * @author	<NAME> (axold) <<EMAIL>>
 * @license	http://www.opensource.org/licenses/lgpl-2.1.php
 */

//Controlling Access
require_once 'tiki-setup.php';
if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
  header("location: index.php");
  exit;
}

//Rest of Imports
global $categlib; include_once 'lib/categories/categlib.php';

/**
 * wslib - The Workspaces Library for TikiWiki
 * TODO: Refine documentation!!!
 *
 * @category	TikiWiki
 * @package	lib/workspaces
 * @version	$Id
 */
class wslib extends CategLib 
{
    /** Stores the $prefs['ws_container'] for avoid to check it in every function */
    private $ws_container;

    /** Stores this objectype, this is WS */
    private $objectType;
	
	/** Stores the view Perm */
	private $viewPerm;

    /** Constructor, give the dbtiki to its parent, this is Categlib */
    public function __construct()
    {
		global $prefs;
		$this->ws_container = (int) $prefs['ws_container'];
		$this->objectType = 'ws';
		$this->viewPerm = 'tiki_p_ws_view';
    }

    /** Initialize the Workspaces in TikiWiki setting a container in the category table and return its ID
     *
     * @return The ws_container ID if it wasn't set before the ws_container, if not, null
     */
    public function init_ws()
    {
	if (!$this->ws_container)
	{
	    global $prefs, $tikilib;
	    $id = parent::add_category(0, '', 'Workspaces Container');
	    $tikilib->set_preference('ws_container', $id);
	    $tikilib->set_preference('feature_perspective', 'y');
	    $this->ws_container = (int) $prefs['ws_container'];
	    return $id;
	}
    }

    /** Get the ws_container stored in $prefs. This function is used to avoid certain calls to the $prefs array.
     *
     * @return The ws container id stored in the var $prefs
     */
    public function get_ws_container()
    {
		return $this->ws_container;
    }
    
    /** Create a new WS with one group inside it with the associated perm 'tiki_p_ws_view'.
     *
     * @param $name Name of the Workspace
     * @param $parentWS Name of the ParentWS, if ParentWS is null, its default value will be 0
     * @param $groups An associative array of groups in the form array("groupName" => (string) name, "groupDescription" => (string) description,
     * "noCreateNewGroup" => boolean, "additionalPerms" => array("additionalperm1", "additionalperm2", ...))
     * @param $additionalPerms Associative array for giving more perms than the default perm 'tiki_p_ws_view'
     * @return The ID of the WS
     */
    public function create_ws ($name, $groups, $parentWS = 0, $description = '')
    {
		global $perspectivelib, $userlib; require_once 'lib/perspectivelib.php';
		
		if (empty($parentWS))
			$parentWS = 0;
			
		$ws_id = parent::add_category($parentWS, $name, $description, $this->ws_container);

		//We create the perspective for the WS
		$wsValue = $this->get_ws_perspective_value ($ws_id);
		$pspId = $perspectivelib->replace_perspective(null, tr('Workspace %0', $name));

		foreach ($groups as $group)
		{
			$groupName = $group["groupName"];
			$groupDescription = $group["groupDescription"];
			$noCreateNewGroup = $group["noCreateNewGroup"];
			$additionalPerms = $group["additionalPerms"];
		
			if ($noCreateNewGroup)
			{
				$this->set_permissions_for_group_in_ws ($ws_id, $groupName, array($this->viewPerm));
				if ($additionalPerms != null)
					$this->set_permissions_for_group_in_ws($ws_id, $groupName, $additionalPerms);
			}
			else
				$this->add_ws_group ($ws_id, $name, $groupName, $groupDescription, $additionalPerms);

			$userlib->assign_object_permission( $groupName, $pspId, 'perspective', 'tiki_p_perspective_view' );
		}
		
		//I set this for the ws identificacion, because we can have two ws with the same name and for psp
		//this could be a problem in order to get the psp from the db
		$perspectivelib->replace_preferences($pspId, array(
			'wsId' => $ws_id, 
			'wsName' => $name, 
			'wsHomepage' => '', 
			'wsStyle' => '', 
			'sitemycode' => '<div style="align: left; padding-left: 15px;">' . tr('You are currently in workspace %0.', $name) . '<a class="link" href="tiki-switch_perspective.php">' . tra('Reset Tiki to its normal status!') . '</a></div>',
			'category_jail' => $ws_id,
		)); 
			
		return $ws_id;
    }

    /** Add new group to a WS
     *
     * @param $id_ws The WS id you want to add the group
     * @param $wsName The name of the WS, it can be null
     * @param $nameGroup The name of the group you want to create
     * @param $additionalPerms Associative array for giving more perms than the default perm 'tiki_p_ws_view'
     * @return If the WS was sucesfully created true, if not false.
     */
{'fraction_non_alphanumeric': 0.12769981088882254, 'fraction_numerical': 0.002388772767990445, 'mean_word_length': 3.6432532347504623, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7427068', 'n_tokens_mistral': 3460, 'n_tokens_neox': 3192, 'n_words': 1018}