/zf/library/Zend/Gdata/Docs.php

http://github.com/eryx/php-framework-benchmark · PHP · 303 lines · 117 code · 30 blank · 156 comment · 14 complexity · d465b442bba1d310bf760892e1065ab8 MD5 · raw file

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