Search code examples
phpgdata-apigoogle-docs-apizend-gdata

Restoring a Google Doc from Trash using Zend GData


I am creating a filemanger that uses Google Docs for storage, but I am having difficulty working out how to restore a file that has been sent to the trash. I can send to trash by using this code:

$resourceId = "file:12345";
$link = "https://docs.google.com/feeds/default/private/full/";
$file = $docs->getDocumentListEntry($link.$resourceId);
$file->delete();

I can then view all trashed documents using:

$docs = new Zend_Gdata_Docs($client);
$docs->setMajorProtocolVersion(3);
$feed = $docs->getDocumentListFeed($link."-/trashed");

foreach($feed->entries as $entry) {
  ...
}

My question is how can I then restore one of these files back to location it was before it was deleted as you can in Google Docs proper?


Solution

  • Ok, so I worked out how to restore trashed files in my Google Docs App. Code is as follows:

    $service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
    $client = Zend_Gdata_ClientLogin::getHttpClient($gUser, $gPass, $service);
    $client->setHeaders('If-Match: *');
    $gdocs = new Zend_GData_Docs($client);
    $gdocs->setMajorProtocolVersion(3);
    
    $slug = array('If-Match'=>'*'); 
    
    $link = "https://docs.google.com/feeds/default/private/full/".$resourceId;
    $entry = $gdocs->getDocumentListEntry($link);
    $xml = $entry->getXML();
    $feed = str_replace('label="trashed"', 'label=""',$xml);
    
    $entryResult = $gdocs->updateEntry($feed, $entry->getEditLink()->href,null,$slug);
    

    This will also work for unstarring a document and with other category elements. So for example:

    Replace:

    $feed = str_replace('label="trashed"', 'label=""',$xml);
    

    With:

    $feed = str_replace('label="starred"', 'label=""',$xml);