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

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

我正在使用 Apache Chemistry CMIS PHP client 通过 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

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

我不是 CMIS 专家,但我认为 this forum post answers the question. See the answer from "jevon" that provides an example and a link to this page(参见 "Updating a document" 部分)

Apache Chemistry 网站上的 function coverage page 列出了 CMIS PHP 客户端可以做什么和不能做什么。目前不支持签出、签入和取消签出。我知道他们会欢迎贡献。

当然,底层 CMIS 规范支持它,因此您可以更新库以支持 checkout/checkin 或使用原始绑定。

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

编辑:添加了附加信息。

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

<?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();
}