如何使用CMIS PHP将文件上传到Alfresco时进行文件版本控制


How to do file versioning when uploading files to Alfresco using CMIS PHP

我使用Apache Chemistry CMIS PHP客户端通过ATOM将文档从本地文件夹上传到Alfresco Community Edition 5.1。以下是我使用的脚本:

require_once ('cmis_repository_wrapper.php');
require_once ('cmis_service.php');
$repo_url = "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom";
$repo_username = "user";
$repo_password = "pass";
$client = new CMISService($repo_url, $repo_username, $repo_password);
$repo_folder = "/alfrescoFolder";
$source_folder = "localFolder/";
$source_files = array_diff(scandir("$source_folder", 1), array('..', '.'));
$myfolder = $client->getObjectByPath($repo_folder);
foreach($source_files as $file)
{
    try
    {
        $upload = $client->createDocumentFromSource($myfolder->id, $file, "$source_folder/$file");
    }
    catch(Exception $e)
    {
        echo "Some error here.";
    }
}

如果Alfresco存储库中还不存在文档,则此脚本运行良好,文档上传也不会出现问题。例如,假设我的Alfresco存储库中有一个名为example.txt的文档,因此,如果我尝试从本地文件夹上传具有相同名称的文档,我会得到CMIS约束异常。我不知道如何上传现有文档的新版本。

到目前为止,这是我尝试过的,但不起作用:

$objs = $client->getChildren($myfolder->id);
foreach($source_files as $file)
{
    foreach($objs->objectList as $obj)
    {
        if($obj->properties['cmis:name'] == $file)
        {
            try
            {
                $checkedout = $client->checkOut($obj->id);
                $client->checkIn($checkedout->id);
            }
            catch(Exception $e)
            {
                echo "Some error here.";
            }
        }
        else
        {
            try
            {
                $upload = $client->createDocumentFromSource($myfolder->id, $file, "$source_folder/$file", array('cmis:objectTypeId'=>'D:ex:document'));
            }
            catch(Exception $e)
            {
                echo "Some error here";
            }
        }
    }
}

我得到这个错误:

DEBUG: postEntry: myURL = http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom/checkedoutDEBUG: postEntry: entry_template = {title} {SUMMARY} {CONTENT} {PROPERTIES} DEBUG: postEntry: properties_xml = b549c715-9a9d-427c-bd4b-c6ea29d222cb;1.0 DEBUG: postEntry: hash_values = Array ( [PROPERTIES] => b549c715-9a9d-427c-bd4b-c6ea29d222cb;1.0 [SUMMARY] => {summary} ) DEBUG: postEntry: post_value = b549c715-9a9d-427c-bd4b-c6ea29d222cb;1.0

有趣的是,文档实际上被锁定以进行编辑,所以我真的不知道发生了什么。我也不知道签出然后签入文档是否是我应该对文档进行版本化的方式。

TL;DR

我希望能够指定我正在上传的文档是现有文档的新版本。有人知道我该怎么做吗?

Apache Chemistry网站上的函数覆盖页列出了CMIS PHP客户端可以做什么和不能做什么。目前不支持签出、签入和取消签出。我知道他们会欢迎捐款。

当然,底层CMIS规范支持它,因此您可以更新库以支持签出/签入,也可以使用原始绑定。

我不是CMIS的专家,但我认为这个论坛帖子回答了这个问题。请参阅"jevon"的答案,该答案提供了一个示例和指向此页面的链接(请参阅"更新文档"部分)

我最近发现了一个替代CMIS PHP库,它实现了版本控制服务,包括示例用法。我已经用它成功地解决了我在问题中发布的问题。

编辑:添加附加信息

因此,为了实现版本控制,我使用了库中提供的示例代码。我使用的脚本可以用于创建新文档以及更新和版本现有文档。所以,它是:

<?php
require_once(__DIR__ . '/../vendor/autoload.php');
if (!is_file(__DIR__ . '/conf/Configuration.php')) {
    die("Please add your connection credentials to the file '"" . __DIR__ . "/conf/Configuration.php'".'n");
} else {
    require_once(__DIR__ . '/conf/Configuration.php');
}
$major = (boolean) isset($argv[1]) ? $argv[1] : false;
$httpInvoker = new 'GuzzleHttp'Client(
    array(
        'defaults' => array(
            'auth' => array(
                CMIS_BROWSER_USER,
                CMIS_BROWSER_PASSWORD
            )
        )
    )
);
$parameters = array(
    'Dkd'PhpCmis'SessionParameter::BINDING_TYPE => 'Dkd'PhpCmis'Enum'BindingType::BROWSER,
    'Dkd'PhpCmis'SessionParameter::BROWSER_URL => CMIS_BROWSER_URL,
    'Dkd'PhpCmis'SessionParameter::BROWSER_SUCCINCT => false,
    'Dkd'PhpCmis'SessionParameter::HTTP_INVOKER_OBJECT => $httpInvoker,
);
$sessionFactory = new 'Dkd'PhpCmis'SessionFactory();
// If no repository id is defined use the first repository
if (CMIS_REPOSITORY_ID === null) {
    $repositories = $sessionFactory->getRepositories($parameters);
    $repositoryId = $repositories[0]->getId();
} else {
    $repositoryId = CMIS_REPOSITORY_ID;
}
$parameters['Dkd'PhpCmis'SessionParameter::REPOSITORY_ID] = $repositoryId;
$session = $sessionFactory->createSession($parameters);
$rootFolder = $session->getObject($session->createObjectId($session->getRootFolder()->getId()));
try {
    $document = null;
    $stream = 'GuzzleHttp'Stream'Stream::factory(fopen($filePath, 'r'));
    foreach ($rootFolder->getChildren() as $child) {
        if ($child->getName() === $fileName) {
            $document = $child;
            break;
        }
    }
    if (!$document) {
        $properties = array(
            'Dkd'PhpCmis'PropertyIds::OBJECT_TYPE_ID => 'cmis:document',
            'Dkd'PhpCmis'PropertyIds::NAME => $fileName
        );
        $document = $session->createDocument($properties, $rootFolder, $stream);
        $document = $session->getObject($document);
    }
    $checkedOutDocumentId = $document->getVersionSeriesCheckedOutId();
    if ($checkedOutDocumentId) {
        $checkedOutDocumentId = $session->createObjectId($checkedOutDocumentId);
    } else {
        $checkedOutDocumentId = $document->checkOut();
    }
    $checkedInDocumentId = $session->getObject($checkedOutDocumentId)->checkIn(
        $major,
        array(
            'Dkd'PhpCmis'PropertyIds::DESCRIPTION => 'New description'
        ),
        $stream,
        'Comments'
    );

} catch ('Dkd'PhpCmis'Exception'CmisVersioningException $e) {
    echo "********* ERROR **********'n";
    echo $e->getMessage() . "'n";
    echo "**************************'n";
    exit();
}