<commit_before><?php
/**
 * @copyright 2014 City of Bloomington, Indiana
 * @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
 * @author <NAME> <<EMAIL>>
 */
include __DIR__.'/authenticated_web_service.php';

class CMISService extends AuthenticatedWebService
{
	protected $repositoryUrl;
	protected $repositoryId;
	protected $rootFolderUrl;

	public $succinct = false;

	public function __construct($repositoryUrl, $username, $password, $repositoryId)
	{
		parent::__construct($repositoryUrl, $username, $password);

		$json = $this->doJSONRequest($repositoryUrl);

		$this->repositoryUrl = $repositoryUrl;
		$this->repositoryId  = $repositoryId;
		$this->rootFolderUrl = $json->$repositoryId->rootFolderUrl;
	}

	/**
	 * Decodes a JSON response and returns a decorated object
	 *
	 * @param string $url
	 * @param string $method
	 * @return stdClass
	 */
	protected function doJSONRequest($url, $method = 'GET')
	{
 		$json = json_decode($this->doRequest($url, $method));
		if ($json) {
			if (isset($json->exception)) {
				throw new Exception($json->message);
			}
			return $json;
		}
		else {
			throw new Exception(json_last_error_msg());
		}
	}

	// Repository Services
	public function getRepositories() { throw new Exception('methodNotImplemented'); }
	public function getRepositoryInfo($name) { throw new Exception('methodNotImplemented'); }
	public function getTypeChildren() { throw new Exception('methodNotImplemented'); }
	public function getTypeDescendants() { throw new Exception('methodNotImplemented'); }
	public function getTypeDefinition() { throw new Exception('methodNotImplemented'); }
	public function createType($type) { throw new Exception('methodNotImplemented'); }
	public function updateType($type) { throw new Exception('methodNotImplemented'); }
	public function deleteType($typeId) { throw new Exception('methodNotImplemented'); }

	// Navigation Services
	/**
	 * @param string $folderId
	 * @return CMISObject
	 */
	public function getFolderParent($folderId)
	{
		$url = "{$this->rootFolderUrl}?objectId=$folderId&cmisselector=parent";
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	/**
	 * @param string $folderId
	 * @return stdClass
	 */
	public function getChildren($folderId)
	{
		$url = $this->rootFolderUrl."?objectId=$folderId";
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	/**
	 * @NOTE This is a non-standard function
	 *
	 * This function is not in the CMIS 1.1 service specification
	 * It is added to this class for consistency in function naming.
	 *
	 * @param string $path
	 * @return stdClass
	 */
	public function getChildrenByPath($path)
	{
		$url = $this->rootFolderUrl.$path;
		if ($this->succinct) { $url.= '?succinct=true'; }

		return $this->doJSONRequest($url);
	}

	public function getDescendants($folderId) { throw new Exception('methodNotImplemented'); }
	public function getFolderTree($folderId) { throw new Exception('methodNotImplemented'); }
	public function getObjectParents($objectId) { throw new Exception('methodNotImplemented'); }
	public function getCheckedOutDocs($folderId=null) { throw new Exception('methodNotImplemented'); }

	// Object Services
	/**
	 * @param string $objectId
	 * @return CMISObject
	 */
	public function getObject($objectId)
	{
		$url = $this->rootFolderUrl."?objectId=$objectId&cmisselector=object";
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	/**
	 * @param string $path
	 * @return CMISObject
	 */
	public function getObjectByPath($path)
	{
		$url = $this->rootFolderUrl.$path.'?cmisselector=object';
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	public function createDocument($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createDocumentFromSource($sourceId, $properties=null, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createFolder($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createRelationship($properties) { throw new Exception('methodNotImplemented'); }
	public function createPolicy($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createItem($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function getAllowableActions($objectId) { throw new Exception('methodNotImplemented'); }
	public function getProperties($objectId) { throw new Exception('methodNotImplemented'); }

	/**
	 * @param string $objectId
	 * @param string $streamId
	 */
	public function getContentStream($objectId, $streamId=null)
	{
		$url = $this->rootFolderUrl."?cmisaction=getContentStream&objectId=$objectId";
		if ($streamId) {
			$url.= "&streamId=$streamId";
		}
		return $this->doRequest($url);
	}

	public function getRenditions($objectId) { throw new Exception('methodNotImplemented'); }
	public function updateProperties($objectId, $properties) { throw new Exception('methodNotImplemented'); }
	public function bulkUpdateProperties($objects) { throw new Exception('methodNotImplemented'); }
	public function moveObject($objectId, $targetFolderId, $sourceFolderId) { throw new Exception('methodNotImplemented'); }
	public function deleteObject($objectId) { throw new Exception('methodNotImplemented'); }
	public function deleteTree($folderId) { throw new Exception('methodNotImplemented'); }
	public function setContentStream($objectId, $content) { throw new Exception('methodNotImplemented'); }
	public function appendContentStream($objectId, $content) { throw new Exception('methodNotImplemented'); }
	public function deleteContentStream($objectId) { throw new Exception('methodNotImplemented'); }

	// Multi-filing Services
	public function addObjectToFolder($objectId, $folderId) { throw new Exception('methodNotImplemented'); }
	public function removeObjectFromFolder($objectId, $folderId=null) { throw new Exception('methodNotImplemented'); }

	// Discovery Services
	public function query($query)
	{
		$url = $this->repositoryUrl."/doQuery?q=".urlencode($query);
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	public function getContentChanges() { throw new Exception('methodNotImplemented'); }

	// Versioning Services
	public function checkOut($objectId) { throw new Exception('methodNotImplemented'); }
	public function cancelCheckOut($objectId) { throw new Exception('methodNotImplemented'); }
	public function checkIn($objectId) { throw new Exception('methodNotImplemented'); }
	public function getObjectOfLatestVersion($versionSeriesId) { throw new Exception('methodNotImplemented'); }
	public function getPropertiesOfLatestVersion($versionSeriesId) { throw new Exception('methodNotImplemented'); }<commit_msg>Added support for pagination<commit_after><?php
/**
 * @copyright 2014 City of Bloomington, Indiana
 * @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
 * @author <NAME> <<EMAIL>>
 */
include __DIR__.'/authenticated_web_service.php';

class CMISService extends AuthenticatedWebService
{
	protected $repositoryUrl;
	protected $repositoryId;
	protected $rootFolderUrl;

	public $succinct = false;
	public $maxItems = 20;

	public function __construct($repositoryUrl, $username, $password, $repositoryId)
	{
		parent::__construct($repositoryUrl, $username, $password);

		$json = $this->doJSONRequest($repositoryUrl);

		$this->repositoryUrl = $repositoryUrl;
		$this->repositoryId  = $repositoryId;
		$this->rootFolderUrl = $json->$repositoryId->rootFolderUrl;
	}

	/**
	 * Decodes a JSON response and returns a decorated object
	 *
	 * @param string $url
	 * @param string $method
	 * @return stdClass
	 */
	protected function doJSONRequest($url, $method = 'GET')
	{
		$response = $this->doRequest($url, $method);
 		$json = json_decode($response);
		if ($json) {
			if (isset($json->exception)) {
				throw new Exception($json->message);
			}
			return $json;
		}
		else {
			throw new Exception(json_last_error_msg());
		}
	}

	// Repository Services
	public function getRepositories() { throw new Exception('methodNotImplemented'); }
	public function getRepositoryInfo($name) { throw new Exception('methodNotImplemented'); }
	public function getTypeChildren() { throw new Exception('methodNotImplemented'); }
	public function getTypeDescendants() { throw new Exception('methodNotImplemented'); }
	public function getTypeDefinition() { throw new Exception('methodNotImplemented'); }
	public function createType($type) { throw new Exception('methodNotImplemented'); }
	public function updateType($type) { throw new Exception('methodNotImplemented'); }
	public function deleteType($typeId) { throw new Exception('methodNotImplemented'); }

	// Navigation Services
	/**
	 * @param string $folderId
	 * @return CMISObject
	 */
	public function getFolderParent($folderId)
	{
		$url = "{$this->rootFolderUrl}?objectId=$folderId&cmisselector=parent";
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	/**
	 * @param string $folderId
	 * @param int $skipCount
	 * @return stdClass
	 */
	public function getChildren($folderId, $skipCount=0)
	{
		$url = $this->rootFolderUrl."?objectId=$folderId&maxItems={$this->maxItems}";
		if ($skipCount)      { $url.= '&skipCount='.(int)$skipCount; }
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	/**
	 * @NOTE This is a non-standard function
	 *
	 * This function is not in the CMIS 1.1 service specification
	 * It is added to this class for consistency in function naming.
	 *
	 * @param string $path
	 * @param int $skipCount
	 * @return stdClass
	 */
	public function getChildrenByPath($path, $skipCount=0)
	{
		$url = $this->rootFolderUrl."$path?maxItems={$this->maxItems}";
		if ($skipCount)      { $url.= '&skipCount='.(int)$skipCount; }
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	public function getDescendants($folderId) { throw new Exception('methodNotImplemented'); }
	public function getFolderTree($folderId) { throw new Exception('methodNotImplemented'); }
	public function getObjectParents($objectId) { throw new Exception('methodNotImplemented'); }
	public function getCheckedOutDocs($folderId=null) { throw new Exception('methodNotImplemented'); }

	// Object Services
	/**
	 * @param string $objectId
	 * @return CMISObject
	 */
	public function getObject($objectId)
	{
		$url = $this->rootFolderUrl."?objectId=$objectId&cmisselector=object";
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	/**
	 * @param string $path
	 * @return CMISObject
	 */
	public function getObjectByPath($path)
	{
		$url = $this->rootFolderUrl.$path.'?cmisselector=object';
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	public function createDocument($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createDocumentFromSource($sourceId, $properties=null, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createFolder($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createRelationship($properties) { throw new Exception('methodNotImplemented'); }
	public function createPolicy($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function createItem($properties, $folderId=null) { throw new Exception('methodNotImplemented'); }
	public function getAllowableActions($objectId) { throw new Exception('methodNotImplemented'); }
	public function getProperties($objectId) { throw new Exception('methodNotImplemented'); }

	/**
	 * @param string $objectId
	 * @param string $streamId
	 */
	public function getContentStream($objectId, $streamId=null)
	{
		$url = $this->rootFolderUrl."?cmisaction=getContentStream&objectId=$objectId";
		if ($streamId) {
			$url.= "&streamId=$streamId";
		}
		return $this->doRequest($url);
	}

	public function getRenditions($objectId) { throw new Exception('methodNotImplemented'); }
	public function updateProperties($objectId, $properties) { throw new Exception('methodNotImplemented'); }
	public function bulkUpdateProperties($objects) { throw new Exception('methodNotImplemented'); }
	public function moveObject($objectId, $targetFolderId, $sourceFolderId) { throw new Exception('methodNotImplemented'); }
	public function deleteObject($objectId) { throw new Exception('methodNotImplemented'); }
	public function deleteTree($folderId) { throw new Exception('methodNotImplemented'); }
	public function setContentStream($objectId, $content) { throw new Exception('methodNotImplemented'); }
	public function appendContentStream($objectId, $content) { throw new Exception('methodNotImplemented'); }
	public function deleteContentStream($objectId) { throw new Exception('methodNotImplemented'); }

	// Multi-filing Services
	public function addObjectToFolder($objectId, $folderId) { throw new Exception('methodNotImplemented'); }
	public function removeObjectFromFolder($objectId, $folderId=null) { throw new Exception('methodNotImplemented'); }

	// Discovery Services
    /**
     * @param string $query
     * @param int $skipCount
     * @return stdClass
     */
	public function query($query, $skipCount=0)
	{
		$url = $this->repositoryUrl."/doQuery?maxItems={$this->maxItems}&q=".urlencode($query);
		if ($skipCount)      { $url.= '&skipCount='.(int)$skipCount; }
		if ($this->succinct) { $url.= '&succinct=true'; }

		return $this->doJSONRequest($url);
	}

	public function getContentChanges() { throw new Exception('methodNotImplemented'); }

	// Versioning Services
	public function checkOut($objectId) { throw new Exception('methodNotImplemented'); }
	public function cancelCheckOut($objectId) { throw new Exception('methodNotImplemented'); }
	public function checkIn($objectId) { throw new Exception('methodNotImplemented'); }
	public function getObjectOfLatestVersion($versionSeriesId) { throw new Exception('methodNotImplemented'); }
	public function getPropertiesOfLatestVersion($versionSeriesId) { throw new Exception('methodNotImplemented'); }
{'fraction_non_alphanumeric': 0.13809224757518665, 'fraction_numerical': 0.0011862396204033216, 'mean_word_length': 5.877159309021113, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 70, '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': '6309895', 'n_tokens_mistral': 4325, 'n_tokens_neox': 4047, 'n_words': 973}