Docs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /**
  60. * @var string
  61. */
  62. protected $_defaultPostUri = self::DOCUMENTS_LIST_FEED_URI;
  63. /**
  64. * @var array
  65. */
  66. protected static $SUPPORTED_FILETYPES = array(
  67. 'TXT'=>'text/plain',
  68. 'CSV'=>'text/csv',
  69. 'TSV'=>'text/tab-separated-values',
  70. 'TAB'=>'text/tab-separated-values',
  71. 'HTML'=>'text/html',
  72. 'HTM'=>'text/html',
  73. 'DOC'=>'application/msword',
  74. 'DOCX'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  75. 'ODS'=>'application/vnd.oasis.opendocument.spreadsheet',
  76. 'ODT'=>'application/vnd.oasis.opendocument.text',
  77. 'RTF'=>'application/rtf',
  78. 'SXW'=>'application/vnd.sun.xml.writer',
  79. 'XLS'=>'application/vnd.ms-excel',
  80. 'XLSX'=>'application/vnd.ms-excel',
  81. 'PPT'=>'application/vnd.ms-powerpoint',
  82. 'PPS'=>'application/vnd.ms-powerpoint');
  83. /**
  84. * Create Gdata_Docs object
  85. *
  86. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  87. * when communicating with the Google servers.
  88. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  89. */
  90. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
  91. {
  92. $this->registerPackage('Zend_Gdata_Docs');
  93. parent::__construct($client, $applicationId);
  94. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  95. }
  96. /**
  97. * Looks up the mime type based on the file name extension. For example,
  98. * calling this method with 'csv' would return
  99. * 'text/comma-separated-values'. The Mime type is sent as a header in
  100. * the upload HTTP POST request.
  101. *
  102. * @param string $fileExtension
  103. * @return string The mime type to be sent to the server to tell it how the
  104. * multipart mime data should be interpreted.
  105. */
  106. public static function lookupMimeType($fileExtension) {
  107. return self::$SUPPORTED_FILETYPES[strtoupper($fileExtension)];
  108. }
  109. /**
  110. * Retreive feed object containing entries for the user's documents.
  111. *
  112. * @param mixed $location The location for the feed, as a URL or Query
  113. * @return Zend_Gdata_Docs_DocumentListFeed
  114. */
  115. public function getDocumentListFeed($location = null)
  116. {
  117. if ($location === null) {
  118. $uri = self::DOCUMENTS_LIST_FEED_URI;
  119. } else if ($location instanceof Zend_Gdata_Query) {
  120. $uri = $location->getQueryUrl();
  121. } else {
  122. $uri = $location;
  123. }
  124. return parent::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed');
  125. }
  126. /**
  127. * Retreive entry object representing a single document.
  128. *
  129. * @param mixed $location The location for the entry, as a URL or Query
  130. * @return Zend_Gdata_Docs_DocumentListEntry
  131. * @throws Zend_Gdata_App_InvalidArgumentException
  132. */
  133. public function getDocumentListEntry($location = null)
  134. {
  135. if ($location === null) {
  136. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  137. throw new Zend_Gdata_App_InvalidArgumentException(
  138. 'Location must not be null');
  139. } else if ($location instanceof Zend_Gdata_Query) {
  140. $uri = $location->getQueryUrl();
  141. } else {
  142. $uri = $location;
  143. }
  144. return parent::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry');
  145. }
  146. /**
  147. * Retreive entry object representing a single document.
  148. *
  149. * This method builds the URL where this item is stored using the type
  150. * and the id of the document.
  151. * @param string $docId The URL key for the document. Examples:
  152. * dcmg89gw_62hfjj8m, pKq0CzjiF3YmGd0AIlHKqeg
  153. * @param string $docType The type of the document as used in the Google
  154. * Document List URLs. Examples: document, spreadsheet, presentation
  155. * @return Zend_Gdata_Docs_DocumentListEntry
  156. */
  157. public function getDoc($docId, $docType) {
  158. $location = 'https://docs.google.com/feeds/documents/private/full/' .
  159. $docType . '%3A' . $docId;
  160. return $this->getDocumentListEntry($location);
  161. }
  162. /**
  163. * Retreive entry object for the desired word processing document.
  164. *
  165. * @param string $id The URL id for the document. Example: dcmg89gw_62hfjj8m
  166. * @return Zend_Gdata_Docs_DocumentListEntry
  167. */
  168. public function getDocument($id) {
  169. return $this->getDoc($id, 'document');
  170. }
  171. /**
  172. * Retreive entry object for the desired spreadsheet.
  173. *
  174. * @param string $id The URL id for the document. Example: pKq0CzjiF3YmGd0AIlHKqeg
  175. * @return Zend_Gdata_Docs_DocumentListEntry
  176. */
  177. public function getSpreadsheet($id) {
  178. return $this->getDoc($id, 'spreadsheet');
  179. }
  180. /**
  181. * Retreive entry object for the desired presentation.
  182. *
  183. * @param string $id The URL id for the document. Example: dcmg89gw_21gtrjcn
  184. * @return Zend_Gdata_Docs_DocumentListEntry
  185. */
  186. public function getPresentation($id) {
  187. return $this->getDoc($id, 'presentation');
  188. }
  189. /**
  190. * Upload a local file to create a new Google Document entry.
  191. *
  192. * @param string $fileLocation The full or relative path of the file to
  193. * be uploaded.
  194. * @param string $title The name that this document should have on the
  195. * server. If set, the title is used as the slug header in the
  196. * POST request. If no title is provided, the location of the
  197. * file will be used as the slug header in the request. If no
  198. * mimeType is provided, this method attempts to determine the
  199. * mime type based on the slugHeader by looking for .doc,
  200. * .csv, .txt, etc. at the end of the file name.
  201. * Example value: 'test.doc'.
  202. * @param string $mimeType Describes the type of data which is being sent
  203. * to the server. This must be one of the accepted mime types
  204. * which are enumerated in SUPPORTED_FILETYPES.
  205. * @param string $uri (optional) The URL to which the upload should be
  206. * made.
  207. * Example: 'https://docs.google.com/feeds/documents/private/full'.
  208. * @return Zend_Gdata_Docs_DocumentListEntry The entry for the newly
  209. * created Google Document.
  210. */
  211. public function uploadFile($fileLocation, $title=null, $mimeType=null,
  212. $uri=null)
  213. {
  214. // Set the URI to which the file will be uploaded.
  215. if ($uri === null) {
  216. $uri = $this->_defaultPostUri;
  217. }
  218. // Create the media source which describes the file.
  219. $fs = $this->newMediaFileSource($fileLocation);
  220. if ($title !== null) {
  221. $slugHeader = $title;
  222. } else {
  223. $slugHeader = $fileLocation;
  224. }
  225. // Set the slug header to tell the Google Documents server what the
  226. // title of the document should be and what the file extension was
  227. // for the original file.
  228. $fs->setSlug($slugHeader);
  229. // Set the mime type of the data.
  230. if ($mimeType === null) {
  231. $filenameParts = explode('.', $fileLocation);
  232. $fileExtension = end($filenameParts);
  233. $mimeType = self::lookupMimeType($fileExtension);
  234. }
  235. // Set the mime type for the upload request.
  236. $fs->setContentType($mimeType);
  237. // Send the data to the server.
  238. return $this->insertDocument($fs, $uri);
  239. }
  240. /**
  241. * Creates a new folder in Google Docs
  242. *
  243. * @param string $folderName The folder name to create
  244. * @param string|null $folderResourceId The parent folder to create it in
  245. * ("folder%3Amy_parent_folder")
  246. * @return Zend_Gdata_Entry The folder entry created.
  247. * @todo ZF-8732: This should return a *subclass* of Zend_Gdata_Entry, but
  248. * the appropriate type doesn't exist yet.
  249. */
  250. public function createFolder($folderName, $folderResourceId=null) {
  251. $category = new Zend_Gdata_App_Extension_Category(self::DOCUMENTS_CATEGORY_TERM,
  252. self::DOCUMENTS_CATEGORY_SCHEMA);
  253. $title = new Zend_Gdata_App_Extension_Title($folderName);
  254. $entry = new Zend_Gdata_Entry();
  255. $entry->setCategory(array($category));
  256. $entry->setTitle($title);
  257. $uri = self::DOCUMENTS_LIST_FEED_URI;
  258. if ($folderResourceId != null) {
  259. $uri = self::DOCUMENTS_FOLDER_FEED_URI . '/' . $folderResourceId;
  260. }
  261. return $this->insertEntry($entry, $uri);
  262. }
  263. /**
  264. * Inserts an entry to a given URI and returns the response as an Entry.
  265. *
  266. * @param mixed $data The Zend_Gdata_Docs_DocumentListEntry or media
  267. * source to post. If it is a DocumentListEntry, the mediaSource
  268. * should already have been set. If $data is a mediaSource, it
  269. * should have the correct slug header and mime type.
  270. * @param string $uri POST URI
  271. * @param string $className (optional) The class of entry to be returned.
  272. * The default is a 'Zend_Gdata_Docs_DocumentListEntry'.
  273. * @return Zend_Gdata_Docs_DocumentListEntry The entry returned by the
  274. * service after insertion.
  275. */
  276. public function insertDocument($data, $uri,
  277. $className='Zend_Gdata_Docs_DocumentListEntry')
  278. {
  279. return $this->insertEntry($data, $uri, $className);
  280. }
  281. }