Docs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Docs
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Gdata
  24. */
  25. require_once 'Zend/Gdata.php';
  26. /**
  27. * @see Zend_Gdata_Docs_DocumentListFeed
  28. */
  29. require_once 'Zend/Gdata/Docs/DocumentListFeed.php';
  30. /**
  31. * @see Zend_Gdata_Docs_DocumentListEntry
  32. */
  33. require_once 'Zend/Gdata/Docs/DocumentListEntry.php';
  34. /**
  35. * @see Zend_Gdata_App_Extension_Category
  36. */
  37. require_once 'Zend/Gdata/App/Extension/Category.php';
  38. /**
  39. * @see Zend_Gdata_App_Extension_Title
  40. */
  41. require_once 'Zend/Gdata/App/Extension/Title.php';
  42. /**
  43. * Service class for interacting with the Google Document List data API
  44. * @link http://code.google.com/apis/documents/
  45. *
  46. * @category Zend
  47. * @package Zend_Gdata
  48. * @subpackage Docs
  49. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  50. * @license http://framework.zend.com/license/new-bsd New BSD License
  51. */
  52. class Zend_Gdata_Docs extends Zend_Gdata
  53. {
  54. const DOCUMENTS_LIST_FEED_URI = 'https://docs.google.com/feeds/documents/private/full';
  55. const DOCUMENTS_FOLDER_FEED_URI = 'https://docs.google.com/feeds/folders/private/full';
  56. const DOCUMENTS_CATEGORY_SCHEMA = 'http://schemas.google.com/g/2005#kind';
  57. const DOCUMENTS_CATEGORY_TERM = 'http://schemas.google.com/docs/2007#folder';
  58. const AUTH_SERVICE_NAME = 'writely';
  59. protected $_defaultPostUri = self::DOCUMENTS_LIST_FEED_URI;
  60. private static $SUPPORTED_FILETYPES = array(
  61. 'TXT'=>'text/plain',
  62. 'CSV'=>'text/csv',
  63. 'TSV'=>'text/tab-separated-values',
  64. 'TAB'=>'text/tab-separated-values',
  65. 'HTML'=>'text/html',
  66. 'HTM'=>'text/html',
  67. 'DOC'=>'application/msword',
  68. 'DOCX'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  69. 'ODS'=>'application/vnd.oasis.opendocument.spreadsheet',
  70. 'ODT'=>'application/vnd.oasis.opendocument.text',
  71. 'RTF'=>'application/rtf',
  72. 'SXW'=>'application/vnd.sun.xml.writer',
  73. 'XLS'=>'application/vnd.ms-excel',
  74. 'XLSX'=>'application/vnd.ms-excel',
  75. 'PPT'=>'application/vnd.ms-powerpoint',
  76. 'PPS'=>'application/vnd.ms-powerpoint');
  77. /**
  78. * Create Gdata_Docs object
  79. *
  80. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  81. * when communicating with the Google servers.
  82. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  83. */
  84. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
  85. {
  86. $this->registerPackage('Zend_Gdata_Docs');
  87. parent::__construct($client, $applicationId);
  88. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  89. }
  90. /**
  91. * Looks up the mime type based on the file name extension. For example,
  92. * calling this method with 'csv' would return
  93. * 'text/comma-separated-values'. The Mime type is sent as a header in
  94. * the upload HTTP POST request.
  95. *
  96. * @param string $fileExtension
  97. * @return string The mime type to be sent to the server to tell it how the
  98. * multipart mime data should be interpreted.
  99. */
  100. public static function lookupMimeType($fileExtension) {
  101. return self::$SUPPORTED_FILETYPES[strtoupper($fileExtension)];
  102. }
  103. /**
  104. * Retreive feed object containing entries for the user's documents.
  105. *
  106. * @param mixed $location The location for the feed, as a URL or Query
  107. * @return Zend_Gdata_Docs_DocumentListFeed
  108. */
  109. public function getDocumentListFeed($location = null)
  110. {
  111. if ($location === null) {
  112. $uri = self::DOCUMENTS_LIST_FEED_URI;
  113. } else if ($location instanceof Zend_Gdata_Query) {
  114. $uri = $location->getQueryUrl();
  115. } else {
  116. $uri = $location;
  117. }
  118. return parent::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed');
  119. }
  120. /**
  121. * Retreive entry object representing a single document.
  122. *
  123. * @param mixed $location The location for the entry, as a URL or Query
  124. * @return Zend_Gdata_Docs_DocumentListEntry
  125. */
  126. public function getDocumentListEntry($location = null)
  127. {
  128. if ($location === null) {
  129. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  130. throw new Zend_Gdata_App_InvalidArgumentException(
  131. 'Location must not be null');
  132. } else if ($location instanceof Zend_Gdata_Query) {
  133. $uri = $location->getQueryUrl();
  134. } else {
  135. $uri = $location;
  136. }
  137. return parent::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry');
  138. }
  139. /**
  140. * Retreive entry object representing a single document.
  141. *
  142. * This method builds the URL where this item is stored using the type
  143. * and the id of the document.
  144. * @param string $docId The URL key for the document. Examples:
  145. * dcmg89gw_62hfjj8m, pKq0CzjiF3YmGd0AIlHKqeg
  146. * @param string $docType The type of the document as used in the Google
  147. * Document List URLs. Examples: document, spreadsheet, presentation
  148. * @return Zend_Gdata_Docs_DocumentListEntry
  149. */
  150. public function getDoc($docId, $docType) {
  151. $location = 'https://docs.google.com/feeds/documents/private/full/' .
  152. $docType . '%3A' . $docId;
  153. return $this->getDocumentListEntry($location);
  154. }
  155. /**
  156. * Retreive entry object for the desired word processing document.
  157. *
  158. * @param string $id The URL id for the document. Example:
  159. * dcmg89gw_62hfjj8m
  160. */
  161. public function getDocument($id) {
  162. return $this->getDoc($id, 'document');
  163. }
  164. /**
  165. * Retreive entry object for the desired spreadsheet.
  166. *
  167. * @param string $id The URL id for the document. Example:
  168. * pKq0CzjiF3YmGd0AIlHKqeg
  169. */
  170. public function getSpreadsheet($id) {
  171. return $this->getDoc($id, 'spreadsheet');
  172. }
  173. /**
  174. * Retreive entry object for the desired presentation.
  175. *
  176. * @param string $id The URL id for the document. Example:
  177. * dcmg89gw_21gtrjcn
  178. */
  179. public function getPresentation($id) {
  180. return $this->getDoc($id, 'presentation');
  181. }
  182. /**
  183. * Upload a local file to create a new Google Document entry.
  184. *
  185. * @param string $fileLocation The full or relative path of the file to
  186. * be uploaded.
  187. * @param string $title The name that this document should have on the
  188. * server. If set, the title is used as the slug header in the
  189. * POST request. If no title is provided, the location of the
  190. * file will be used as the slug header in the request. If no
  191. * mimeType is provided, this method attempts to determine the
  192. * mime type based on the slugHeader by looking for .doc,
  193. * .csv, .txt, etc. at the end of the file name.
  194. * Example value: 'test.doc'.
  195. * @param string $mimeType Describes the type of data which is being sent
  196. * to the server. This must be one of the accepted mime types
  197. * which are enumerated in SUPPORTED_FILETYPES.
  198. * @param string $uri (optional) The URL to which the upload should be
  199. * made.
  200. * Example: 'https://docs.google.com/feeds/documents/private/full'.
  201. * @return Zend_Gdata_Docs_DocumentListEntry The entry for the newly
  202. * created Google Document.
  203. */
  204. public function uploadFile($fileLocation, $title=null, $mimeType=null,
  205. $uri=null)
  206. {
  207. // Set the URI to which the file will be uploaded.
  208. if ($uri === null) {
  209. $uri = $this->_defaultPostUri;
  210. }
  211. // Create the media source which describes the file.
  212. $fs = $this->newMediaFileSource($fileLocation);
  213. if ($title !== null) {
  214. $slugHeader = $title;
  215. } else {
  216. $slugHeader = $fileLocation;
  217. }
  218. // Set the slug header to tell the Google Documents server what the
  219. // title of the document should be and what the file extension was
  220. // for the original file.
  221. $fs->setSlug($slugHeader);
  222. // Set the mime type of the data.
  223. if ($mimeType === null) {
  224. $filenameParts = explode('.', $fileLocation);
  225. $fileExtension = end($filenameParts);
  226. $mimeType = self::lookupMimeType($fileExtension);
  227. }
  228. // Set the mime type for the upload request.
  229. $fs->setContentType($mimeType);
  230. // Send the data to the server.
  231. return $this->insertDocument($fs, $uri);
  232. }
  233. /**
  234. * Creates a new folder in Google Docs
  235. *
  236. * @param string $folderName The folder name to create
  237. * @param string|null $folderResourceId The parent folder to create it in
  238. * ("folder%3Amy_parent_folder")
  239. * @return Zend_Gdata_Entry The folder entry created.
  240. * @todo ZF-8732: This should return a *subclass* of Zend_Gdata_Entry, but
  241. * the appropriate type doesn't exist yet.
  242. */
  243. public function createFolder($folderName, $folderResourceId=null) {
  244. $category = new Zend_Gdata_App_Extension_Category(self::DOCUMENTS_CATEGORY_TERM,
  245. self::DOCUMENTS_CATEGORY_SCHEMA);
  246. $title = new Zend_Gdata_App_Extension_Title($folderName);
  247. $entry = new Zend_Gdata_Entry();
  248. $entry->setCategory(array($category));
  249. $entry->setTitle($title);
  250. $uri = self::DOCUMENTS_LIST_FEED_URI;
  251. if ($folderResourceId != null) {
  252. $uri = self::DOCUMENTS_FOLDER_FEED_URI . '/' . $folderResourceId;
  253. }
  254. return $this->insertEntry($entry, $uri);
  255. }
  256. /**
  257. * Inserts an entry to a given URI and returns the response as an Entry.
  258. *
  259. * @param mixed $data The Zend_Gdata_Docs_DocumentListEntry or media
  260. * source to post. If it is a DocumentListEntry, the mediaSource
  261. * should already have been set. If $data is a mediaSource, it
  262. * should have the correct slug header and mime type.
  263. * @param string $uri POST URI
  264. * @param string $className (optional) The class of entry to be returned.
  265. * The default is a 'Zend_Gdata_Docs_DocumentListEntry'.
  266. * @return Zend_Gdata_Docs_DocumentListEntry The entry returned by the
  267. * service after insertion.
  268. */
  269. public function insertDocument($data, $uri,
  270. $className='Zend_Gdata_Docs_DocumentListEntry')
  271. {
  272. return $this->insertEntry($data, $uri, $className);
  273. }
  274. }