From 2db4840d77c074d8621634fb5003fe2782531d09 Mon Sep 17 00:00:00 2001 From: Paul Barrett Date: Thu, 25 Jun 2009 16:14:57 +0200 Subject: [PATCH] Code changes to allow CMISSpaces (and similar clients) to work with the URL structures used by Alfresco and Drupal --- ktapi/ktapi.inc.php | 4 ++-- ktatompub/lib/cmis/KTCMISAPPFeed.inc.php | 18 +++++++++++++++--- ktatompub/services/cmis/ObjectFeed.inc.php | 18 ++++++++++++------ ktatompub/services/cmis/checkedout.inc.php | 12 ++++++------ ktatompub/services/cmis/folder.inc.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- ktatompub/services/cmis/index.php | 5 +++-- ktatompub/services/cmis/servicedocument.inc.php | 3 +++ ktatompub/services/cmis/types.inc.php | 21 +++++++++++++++++++-- lib/api/ktcmis/config/repositories.xml | 2 +- 9 files changed, 126 insertions(+), 39 deletions(-) diff --git a/ktapi/ktapi.inc.php b/ktapi/ktapi.inc.php index 5c7cacc..5da8b74 100644 --- a/ktapi/ktapi.inc.php +++ b/ktapi/ktapi.inc.php @@ -834,9 +834,9 @@ class KTAPI * @param string $foldername The folder name * @return object $folder The KTAPI_Folder object */ - public function &get_folder_by_name($foldername) + public function &get_folder_by_name($foldername, $parentId = 1) { - $folder = KTAPI_Folder::_get_folder_by_name($this, $foldername, 1); + $folder = KTAPI_Folder::_get_folder_by_name($this, $foldername, $parentId); return $folder; } diff --git a/ktatompub/lib/cmis/KTCMISAPPFeed.inc.php b/ktatompub/lib/cmis/KTCMISAPPFeed.inc.php index 1beb985..973681c 100644 --- a/ktatompub/lib/cmis/KTCMISAPPFeed.inc.php +++ b/ktatompub/lib/cmis/KTCMISAPPFeed.inc.php @@ -56,6 +56,7 @@ class KTCMISAPPFeed extends KTAPDoc { public function __construct($baseURI = NULL, $title = NULL, $link = NULL, $updated = NULL, $author = NULL, $id = NULL) { parent::__construct(); + $this->baseURI = $baseURI; $this->id = $id; $this->title = $title; @@ -65,6 +66,7 @@ class KTCMISAPPFeed extends KTAPDoc { private function constructHeader() { $feed = $this->newElement('feed'); + $feed->appendChild($this->newAttr('xmlns','http://www.w3.org/2007/app')); $feed->appendChild($this->newAttr('xmlns','http://www.w3.org/2005/Atom')); $feed->appendChild($this->newAttr('xmlns:cmis','http://www.cmis.org/2008/05')); $this->feed = &$feed; @@ -76,7 +78,7 @@ class KTCMISAPPFeed extends KTAPDoc { $link = $this->newElement('link'); $link->appendChild($this->newAttr('rel','self')); - $link->appendChild($this->newAttr('href', $this->baseURI . $_SERVER['QUERY_STRING'])); + $link->appendChild($this->newAttr('href', $this->baseURI . trim($_SERVER['QUERY_STRING'], '/'))); $feed->appendChild($link); if (!is_null($this->title)) @@ -103,8 +105,18 @@ class KTCMISAPPFeed extends KTAPDoc { public function &newField($name = NULL, $value = NULL, &$entry = NULL) { - $field = $this->newElement('cmis:' . $name,$value); - if(isset($entry))$entry->appendChild($field); + $append = false; + + if(func_num_args() > 3) + { + $append = ((func_get_arg(3) === true) ? true : false); + } + + $field = $this->newElement('cmis:' . $name,$value); + + if (isset($entry)) $entry->appendChild($field); + else if ($append) $this->feed->appendChild($field); + return $field; } diff --git a/ktatompub/services/cmis/ObjectFeed.inc.php b/ktatompub/services/cmis/ObjectFeed.inc.php index 461695d..86b765e 100644 --- a/ktatompub/services/cmis/ObjectFeed.inc.php +++ b/ktatompub/services/cmis/ObjectFeed.inc.php @@ -9,10 +9,14 @@ class CMISObjectFeed { * @param array $cmisEntry The entry data * @param string $parent The parent folder */ - static public function createEntry(&$feed, $cmisEntry, $parent) + static public function createEntry(&$feed, $cmisEntry, $parent, $path) { + preg_match('/^\/?cmis\/folder\/(.*)\/[^\/]*\/?$/', trim($_SERVER['QUERY_STRING'], '/'), $matches); + $path = $matches[1]; + $parent = preg_replace('/\/[^\/]*$/', '', $path); + $entry = $feed->newEntry(); - $feed->newId('urn:uuid:' . $cmisEntry['properties']['ObjectId']['value'] . '-' + $feed->newId('urn:uuid:' . $cmisEntry['properties']['Name']['value'] . '-' . strtolower($cmisEntry['properties']['ObjectTypeId']['value']), $entry); /* @@ -29,26 +33,28 @@ class CMISObjectFeed { // $entry->appendChild($link); $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel','cmis-parent')); - $link->appendChild($feed->newAttr('href', CMIS_BASE_URI . 'folder/' . $parent)); + $link->appendChild($feed->newAttr('href', CMIS_BASE_URI . 'folder/' . $path)); $entry->appendChild($link); if (strtolower($cmisEntry['properties']['ObjectTypeId']['value']) == 'folder') { $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel','cmis-folderparent')); - $link->appendChild($feed->newAttr('href', CMIS_BASE_URI . 'folder/' . $parent)); + $link->appendChild($feed->newAttr('href', CMIS_BASE_URI . 'folder/' . $path)); $entry->appendChild($link); $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel','cmis-children')); $link->appendChild($feed->newAttr('href', CMIS_BASE_URI . strtolower($cmisEntry['properties']['ObjectTypeId']['value']) - . '/' . $cmisEntry['properties']['ObjectId']['value'] . '/children')); + . '/' . $path . '/' . urlencode($cmisEntry['properties']['Name']['value']) + . '/children')); $entry->appendChild($link); $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel','cmis-descendants')); $link->appendChild($feed->newAttr('href', CMIS_BASE_URI . strtolower($cmisEntry['properties']['ObjectTypeId']['value']) - . '/' . $cmisEntry['properties']['ObjectId']['value'] . '/descendants')); + . '/' . $path . '/' . urlencode($cmisEntry['properties']['Name']['value']) + . '/descendants')); $entry->appendChild($link); } diff --git a/ktatompub/services/cmis/checkedout.inc.php b/ktatompub/services/cmis/checkedout.inc.php index c4c912a..4178dd7 100644 --- a/ktatompub/services/cmis/checkedout.inc.php +++ b/ktatompub/services/cmis/checkedout.inc.php @@ -20,11 +20,11 @@ $NavigationService->startSession($username, $password); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; -$output = ' -urn:uuid:checkedout - -Checked out Documents -false -'; +$feed = new KTCMISAPPFeed(KT_APP_BASE_URI, 'Checked out Documents', null, null, null, 'urn:uuid:checkedout'); + +$entry = null; +$feed->newField('hasMoreItems', 'false', $entry, true); + +$output = $feed->getAPPdoc(); ?> diff --git a/ktatompub/services/cmis/folder.inc.php b/ktatompub/services/cmis/folder.inc.php index c31ff7e..dd67db3 100644 --- a/ktatompub/services/cmis/folder.inc.php +++ b/ktatompub/services/cmis/folder.inc.php @@ -18,7 +18,7 @@ class CMISFolderFeed extends CMISObjectFeed { * @param string $feedType children or descendants * @return string CMIS AtomPub feed */ - static public function getFolderChildrenFeed($NavigationService, $repositoryId, $folderId, $feedType) + static public function getFolderChildrenFeed($NavigationService, $repositoryId, $folderId, $folderName, $feedType) { if ($feedType == 'children') { @@ -30,25 +30,15 @@ class CMISFolderFeed extends CMISObjectFeed { } else { - // error, we shouldn't be here - } - - // TODO dynamically get the requested folder's name to display correctly - if ($folderId == 'F1') - { - $folderName = 'Root Folder'; - } - else - { - $folderName = 'Folder'; + // error, we shouldn't be here, if we are then the wrong function was called } $feed = new KTCMISAPPFeed(KT_APP_BASE_URI, $folderName . ' ' . ucwords($feedType), null, null, null, - 'urn:uuid:' . $folderId . '-' . $feedType); + 'urn:uuid:' . $folderName . '-' . $feedType); foreach($entries as $cmisEntry) { - CMISFolderFeed::createEntry($feed, $cmisEntry, $folderId); + CMISFolderFeed::createEntry($feed, $cmisEntry, $folderName); } // false @@ -490,6 +480,62 @@ class CMISFolderFeed extends CMISObjectFeed { return $output; } + static public function getFolderData($query, &$locationName, &$tree) + { + $folderId = null; + + // TODO proper login credentials, or rather use the existing session available from the underlying CMIS code + $ktapi = new KTAPI(); + $ktapi->start_session('admin', 'admin'); + + $numQ = count($query); + + if($query[$numQ-1] == 'children' || $query[$numQ-1] == 'descendants') { + $offset = 1; + $tree = $query[$numQ-1]; + } + + $folderName = urldecode($query[$numQ-($offset+1)]); + + $locationName = $folderName; + + if ($numQ <= 5) + { + $parentId = 1; + } + else + { + $count = 2; + $lastParent = 0; + + while(++$count <= ($numQ - 3)) + { + if ($lastParent == 0) + { + $idUp = 1; + } + else + { + $idUp = $lastParent; + } + + $folderName = urldecode($query[$count]); + $folder = $ktapi->get_folder_by_name($folderName, $idUp); + + if (PEAR::isError($folder)) break; + + $currentId = $folder->get_folderid(); + $lastParent = $currentId; + } + + $parentId = $lastParent; + } + + $folder = $ktapi->get_folder_by_name($locationName, $parentId); + $folderId = CMISUtil::encodeObjectId('Folder', $folder->get_folderid()); + + return $folderId; + } } include 'services/cmis/RepositoryService.inc.php'; @@ -500,19 +546,21 @@ $RepositoryService = new RepositoryService(); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; -if (isset($query[3]) && (($query[3] == 'children') || ($query[3] == 'descendants'))) +$folderId = CMISFolderFeed::getFolderData($query, $folderName, $tree); + +if (isset($tree) && (($tree == 'children') || ($tree == 'descendants'))) { $NavigationService = new NavigationService(); $NavigationService->startSession($username, $password); - $output = CMISFolderFeed::getFolderChildrenFeed($NavigationService, $repositoryId, $query[2], $query[3]); + $output = CMISFolderFeed::getFolderChildrenFeed($NavigationService, $repositoryId, $folderId, $folderName, $tree); } else { $ObjectService = new ObjectService(); $ObjectService->startSession($username, $password); - $output = CMISFolderFeed::getFolderFeed($ObjectService, $repositoryId, $query[2]); + $output = CMISFolderFeed::getFolderFeed($ObjectService, $repositoryId, $folderId); } ?> diff --git a/ktatompub/services/cmis/index.php b/ktatompub/services/cmis/index.php index bd8acc5..8778c17 100644 --- a/ktatompub/services/cmis/index.php +++ b/ktatompub/services/cmis/index.php @@ -7,7 +7,8 @@ include_once('lib/cmis/KTCMISAPPServiceDoc.inc.php'); include_once('lib/cmis/KTCMISAPPFeed.inc.php'); -define ('CMIS_BASE_URI', KT_APP_BASE_URI . 'cmis/'); +define ('CMIS_BASE_URI', trim(KT_APP_BASE_URI, '/') . 'cmis/'); +//echo KT_APP_BASE_URI;exit; // hack for links not yet working in KT, use Alfresco to move things forward //define ('CMIS_BASE_URI_ALF', 'http://127.0.0.1:8080/alfresco/service/api/'); //define ('CMIS_BASE_URI', 'http://10.33.4.34:8080/alfresco/service/api/'); @@ -18,7 +19,7 @@ $username = $_SERVER['PHP_AUTH_USER']; $password = $_SERVER['PHP_AUTH_PW']; // NOTE this is just for demonstration purposes and attempting to auth with clients which send the username/password differently -// TODO disable once we have Drupal compatible login working +// TODO disable once we have at least Drupal compatible login working, can re-enable if necessary if (($username == '') && ($password == '')) { $username = $password = 'admin'; diff --git a/ktatompub/services/cmis/servicedocument.inc.php b/ktatompub/services/cmis/servicedocument.inc.php index ad30cb3..a6e3903 100644 --- a/ktatompub/services/cmis/servicedocument.inc.php +++ b/ktatompub/services/cmis/servicedocument.inc.php @@ -6,6 +6,9 @@ * NOTE Includes the results of a repositoryInfo call as well as service links */ +// NOTE currently we only support one repository, which will be the first one found in the repositories.xml config +// TODO multiple repositories as individual workspaces + include 'services/cmis/RepositoryService.inc.php'; $RepositoryService = new RepositoryService(); diff --git a/ktatompub/services/cmis/types.inc.php b/ktatompub/services/cmis/types.inc.php index 857d2e0..b55f2fa 100644 --- a/ktatompub/services/cmis/types.inc.php +++ b/ktatompub/services/cmis/types.inc.php @@ -35,7 +35,8 @@ switch($arg) break; case 'types': $types = $RepositoryService->getTypes($repositoryId); - $output = CMISTypeFeed::getTypeFeed('All Types', $types); + $type = (($query[2] == '') ? 'all' : $query[2]); + $output = CMISTypeFeed::getTypeFeed($type, $types); break; } @@ -54,7 +55,23 @@ class CMISTypeFeed { */ static public function getTypeFeed($typeDef, $types) { - $feed = new KTCMISAPPFeed(KT_APP_BASE_URI, $typeDef, null, null, null, 'urn:uuid:type-' . $query[2]); + $typesString = ''; + $typesHeading = ''; + switch($typeDef) + { + case 'all': + case 'children': + case 'descendants': + $typesString = 'types-' . $typeDef; + $typesHeading = 'All Types'; + break; + default: + $typesString = 'type-' . $typeDef; + $typesHeading = $typeDef; + break; + } + + $feed = new KTCMISAPPFeed(KT_APP_BASE_URI, $typesHeading, null, null, null, 'urn:uuid:' . $typesString); foreach($types as $type) { diff --git a/lib/api/ktcmis/config/repositories.xml b/lib/api/ktcmis/config/repositories.xml index 9950b68..301bccb 100644 --- a/lib/api/ktcmis/config/repositories.xml +++ b/lib/api/ktcmis/config/repositories.xml @@ -19,7 +19,7 @@ KnowledgeTree KnowledgeTree Document Management System 3.6.2 - F1 + Root%20Folder 0.61c -- libgit2 0.21.4