TripalFieldDownloader.inc

File

tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc
View source
  1. <?php
  2. abstract class TripalFieldDownloader {
  3. /**
  4. * Sets the label shown to the user describing this formatter. It
  5. * should be a short identifier. Use the $full_label for a more
  6. * descriptive label.
  7. */
  8. static public $label = 'Generic';
  9. /**
  10. * A more verbose label that better describes the formatter.
  11. */
  12. static public $full_label = 'Generic File format';
  13. /**
  14. * Indicates the default extension for the outputfile.
  15. */
  16. static public $default_extension = 'txt';
  17. /**
  18. * The data collection assigned to this downloader.
  19. */
  20. protected $collection = NULL;
  21. /**
  22. * The collection ID
  23. */
  24. protected $collection_id = NULL;
  25. /**
  26. * An array of collection_bundle records for the content types that
  27. * belong to this data collection.
  28. */
  29. protected $collection_bundles = NULL;
  30. /**
  31. * The output file URI.
  32. */
  33. protected $outfile = '';
  34. /**
  35. * An array of printable fields. Because fields can come from multiple
  36. * bundles and those bundles can be from multiple sites, it is possible that
  37. * 1) two bundles use the same field and we want to conslidate to a
  38. * single printable field; and 2) that a remote site may use the same term
  39. * for a field as a bundle on the local site. The only way to sort out this
  40. * mess is to use the term accession numbers. Therefore, the array contains
  41. * a unique list of printable fields using their accession numbers as keys
  42. * and a field label as the value.
  43. *
  44. */
  45. protected $printable_fields = array();
  46. /**
  47. * The remote site json data returned for the entity
  48. */
  49. protected $remote_entity = '';
  50. /**
  51. * An array that associates a field ID with a term.
  52. *
  53. * The first-level key is the site ID. For the local site this will be
  54. * the word 'local' for all others it will be the numeric id. The second
  55. * level key is the bundle bundle name. For local bundles this will
  56. * always be bio_data_xxxx. Third, are two subkeys: by_field and
  57. * by_accession. To lookup a field's term you use the 'by_field' subkey
  58. * with the field_id as the next level. To lookup the field ID for a term
  59. * use the 'by_accession' subkey with the accession as the next level. Below
  60. * is an example of the structure of this array.
  61. *
  62. * @code
  63. Array (
  64. [local] => Array(
  65. [bio_data_7] => Array(
  66. [by_field] => Array(
  67. [56] => data:2091,
  68. [57] => OBI:0100026,
  69. [17] => schema:name,
  70. [58] => data:2044,
  71. [34] => data:0842,
  72. [67] => schema:alternateName,
  73. ),
  74. [by_accession] => Array (
  75. [data:2091] => 56,
  76. [OBI:0100026] => 57,
  77. [schema:name] => 17,
  78. [data:2044] => 58,
  79. [data:0842] => 34,
  80. [schema:alternateName] => 67,
  81. ),
  82. ),
  83. ),
  84. )
  85. * @endcode
  86. */
  87. protected $fields2terms = array();
  88. /**
  89. * A list of field and instance items, indexed first by site_id with 'local'
  90. * being the key for local fields and the numeric site_id for remote
  91. * fields. The second-levle key is the bundle_name and the the field_id.
  92. * Below the field_id are the keys 'field' or 'instance' where the
  93. * value of each is the field or instance details respectively.
  94. */
  95. protected $fields = array();
  96. /**
  97. * The file handle for an opeend file using during writing.
  98. */
  99. protected $fh;
  100. /**
  101. * Constructs a new instance of the TripalFieldDownloader class.
  102. *
  103. * @param $collection_id
  104. * The ID for the collection.
  105. * @param $outfile_name
  106. * The name of the output file to create. The name should not include
  107. * a path.
  108. */
  109. public function __construct($collection_id, $outfile_name) {
  110. if (!$outfile_name) {
  111. throw new Exception("Please provide an outputfilename");
  112. }
  113. // Get the collection record and store it.
  114. $collection = db_select('tripal_collection', 'tc')
  115. ->fields('tc')
  116. ->condition('collection_id', $collection_id, '=')
  117. ->execute()
  118. ->fetchObject();
  119. if (!$collection) {
  120. throw new Exception(t("Cannot find a collection that matches the provided id: !id", array('!id' => $collection_id)));
  121. }
  122. $this->collection = $collection;
  123. // Make sure the user directory exists
  124. $user = user_load($this->collection->uid);
  125. $user_dir = 'public://tripal/users/' . $user->uid;
  126. // Set the collection ID of the collection that this downloader will use.
  127. $this->collection_id = $collection_id;
  128. $this->outfile = $user_dir . '/' . $outfile_name;
  129. // A data collection may have multiple bundles. We'll need to get
  130. // them all and store them.
  131. $collection_bundles = db_select('tripal_collection_bundle')
  132. ->fields('tripal_collection_bundle')
  133. ->condition('collection_id', $collection_id, '=')
  134. ->execute();
  135. while ($collection_bundle = $collection_bundles->fetchObject()) {
  136. $collection_bundle->ids = unserialize($collection_bundle->ids);
  137. $collection_bundle->fields = unserialize($collection_bundle->fields);
  138. $this->collection_bundles[] = $collection_bundle;
  139. }
  140. if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
  141. $message = 'Could not access the directory on the server for storing this file.';
  142. watchdog('tripal', $message, array(), WATCHDOG_ERROR);
  143. drupal_json_output(array(
  144. 'status' => 'failed',
  145. 'message' => $message,
  146. 'file_id' => '',
  147. ));
  148. return;
  149. }
  150. // Map the fields to their term accessions.
  151. $this->setFields();
  152. $this->setFields2Terms();
  153. $this->setPrintableFields();
  154. }
  155. /**
  156. * Inidcates if a given field is supported by this Downloader class.
  157. *
  158. * @param $field
  159. * A field info array.
  160. */
  161. public function isFieldSupported($field, $instance) {
  162. $field_name = $field['field_name'];
  163. $field_type = $field['type'];
  164. $this_formatter = get_class($this);
  165. // If a field is a TripalField then check its supported downloaders.
  166. if (tripal_load_include_field_class($field_type)) {
  167. $formatters = $field_type::$download_formatters;
  168. if (in_array($this_formatter, $formatters)) {
  169. return TRUE;
  170. }
  171. }
  172. // If this is a remote field then check that differently.
  173. if ($field['storage']['type'] == 'tripal_remote_field' ) {
  174. if (in_array($this_formatter, $instance['formatters'])) {
  175. return TRUE;
  176. }
  177. }
  178. }
  179. /**
  180. * Retrieves the URL for the downloadable file.
  181. */
  182. public function getURL() {
  183. return $this->outfile;
  184. }
  185. /**
  186. * Removes the downloadable file.
  187. */
  188. public function delete() {
  189. $fid = db_select('file_managed', 'fm')
  190. ->fields('fm', array('fid'))
  191. ->condition('uri', $this->outfile)
  192. ->execute()
  193. ->fetchField();
  194. if ($fid) {
  195. $file = file_load($fid);
  196. file_usage_delete($file, 'tripal', 'data-collection');
  197. file_delete($file, TRUE);
  198. }
  199. }
  200. /**
  201. *
  202. * @param TripalJob $job
  203. */
  204. public function writeInit(TripalJob $job = NULL) {
  205. $user = user_load($this->collection->uid);
  206. $this->fh = fopen(drupal_realpath($this->outfile), "w");
  207. if (!$this->fh) {
  208. throw new Exception("Cannout open collection file: " . $this->outfile);
  209. }
  210. // Add the headers to the file.
  211. $headers = $this->getHeader();
  212. if ($headers) {
  213. foreach ($headers as $line) {
  214. fwrite($this->fh, $line . "\r\n");
  215. }
  216. }
  217. }
  218. /**
  219. * Write a single entity to the file.
  220. *
  221. * Before calling this function call the initWrite() function to
  222. * establish the file and write headers.
  223. *
  224. * @param $entity
  225. * The Entity to write.
  226. * @param TripalJob $job
  227. */
  228. public function writeEntity($entity, TripalJob $job = NULL){
  229. $lines = $this->formatEntity($entity);
  230. foreach ($lines as $line) {
  231. fwrite($this->fh, $line . "\r\n");
  232. }
  233. }
  234. /**
  235. * Closes the output file once writing of all entities is completed.
  236. *
  237. * @param TripalJob $job
  238. */
  239. public function writeDone(TripalJob $job = NULL) {
  240. fclose($this->fh);
  241. $user = user_load($this->collection->uid);
  242. $file = new stdClass();
  243. $file->uri = $this->outfile;
  244. $file->filename = basename($this->outfile);
  245. $file->filemime = file_get_mimetype($this->outfile);
  246. $file->uid = $user->uid;
  247. $file->status = FILE_STATUS_PERMANENT;
  248. // Check if this file already exists. If it does then just update
  249. // the stats.
  250. $fid = db_select('file_managed', 'fm')
  251. ->fields('fm', array('fid'))
  252. ->condition('uri', $this->outfile)
  253. ->execute()
  254. ->fetchField();
  255. if ($fid) {
  256. $file->fid = $fid;
  257. $file = file_save($file);
  258. }
  259. else {
  260. $file = file_save($file);
  261. $fid = $file->fid;
  262. $file = file_load($fid);
  263. }
  264. // We use the fid for the last argument because these files
  265. // aren't really associated with any entity, but we need a value./
  266. // But, only add the usage if it doens't already exists.
  267. $usage = file_usage_list($file);
  268. if (array_key_exists('tripal', $usage)) {
  269. if (!array_key_exists('data-collection', $usage['tripal'])) {
  270. file_usage_add($file, 'tripal', 'data-collection', $fid);
  271. }
  272. }
  273. }
  274. /**
  275. * Creates the downloadable file.
  276. *
  277. * @param $job
  278. * If this function is run as a Tripal Job then this argument can be
  279. * set to the Tripaljob object for keeping track of progress.
  280. */
  281. public function write(TripalJob $job = NULL) {
  282. $this->initWrite($job);
  283. $num_handled = 0;
  284. foreach ($this->collection_bundles as $bundle_collection) {
  285. $collection_bundle_id = $bundle_collection->collection_bundle_id;
  286. $bundle_name = $bundle_collection->bundle_name;
  287. $entity_ids = $bundle_collection->ids;
  288. $fields = $bundle_collection->fields;
  289. $site_id = $bundle_collection->site_id;
  290. foreach ($entity_ids as $entity_id) {
  291. $num_handled++;
  292. if ($job) {
  293. $job->setItemsHandled($num_handled);
  294. }
  295. // if we have a site_id then we need to get the entity from the
  296. // remote service. Otherwise create the entity from the local system.
  297. if ($site_id) {
  298. $entity = $this->loadRemoteEntity($entity_id, $site_id, $bundle_name);
  299. if (!$entity) {
  300. continue;
  301. }
  302. }
  303. else {
  304. $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, $fields, FALSE);
  305. $entity = $result[$entity_id];
  306. }
  307. if (!$entity) {
  308. continue;
  309. }
  310. $lines = $this->formatEntity($entity);
  311. foreach ($lines as $line) {
  312. fwrite($this->fh, $line . "\r\n");
  313. }
  314. }
  315. }
  316. $this->finishWrite($job);
  317. }
  318. /**
  319. * Setups a download stream for the file.
  320. */
  321. public function download() {
  322. }
  323. /**
  324. * A helper function for the setFields() function.
  325. *
  326. * Adds local fields to the list of fields.
  327. */
  328. private function setLocalFields() {
  329. foreach ($this->collection_bundles as $collection_bundle) {
  330. $bundle_name = $collection_bundle->bundle_name;
  331. if ($collection_bundle->site_id) {
  332. continue;
  333. }
  334. foreach ($collection_bundle->fields as $field_id) {
  335. $field = field_info_field_by_id($field_id);
  336. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
  337. $this->fields['local'][$bundle_name][$field_id]['field'] = $field;
  338. $this->fields['local'][$bundle_name][$field_id]['instance'] = $instance;
  339. }
  340. }
  341. }
  342. /**
  343. * A helper function for the setFields() function.
  344. *
  345. * Adds remote fields to the list of fields.
  346. */
  347. private function setRemoteFields() {
  348. // We can't use the Tripal ws API extensions if the
  349. // tripal_ws module is not enabled.
  350. if (!module_exists('tripal_ws')) {
  351. return;
  352. }
  353. foreach ($this->collection_bundles as $collection_bundle) {
  354. $bundle_name = $collection_bundle->bundle_name;
  355. $site_id = $collection_bundle->site_id;
  356. // Skip local fields.
  357. if (!$site_id) {
  358. continue;
  359. }
  360. // Iterate through the fields of this collection and get the
  361. // info for each one from the class. We will create "fake" field and
  362. // instance info arrays.
  363. foreach ($collection_bundle->fields as $field_id) {
  364. $field = tripal_get_remote_field_info($site_id, $bundle_name, $field_id);
  365. $instance = tripal_get_remote_field_instance_info($site_id, $bundle_name, $field_id);
  366. $this->fields[$site_id][$bundle_name][$field_id]['field'] = $field;
  367. $this->fields[$site_id][$bundle_name][$field_id]['instance'] = $instance;
  368. }
  369. }
  370. }
  371. /**
  372. * Sets the fields array
  373. */
  374. protected function setFields() {
  375. $this->setLocalFields();
  376. $this->setRemoteFields();
  377. }
  378. /**
  379. * Sets the fields2term array.
  380. *
  381. * The fields2term array provides an easy lookup for mapping a term
  382. * to it's accession number.
  383. **/
  384. protected function setFields2Terms() {
  385. foreach ($this->fields as $site => $bundles) {
  386. foreach ($bundles as $bundle_name => $bundle_fields) {
  387. foreach ($bundle_fields as $field_id => $info) {
  388. $instance = $info['instance'];
  389. $accession = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
  390. $this->fields2terms[$site][$bundle_name]['by_field'][$field_id] = $accession;
  391. $this->fields2terms[$site][$bundle_name]['by_accession'][$accession] = $field_id;
  392. }
  393. }
  394. }
  395. }
  396. /**
  397. * Conslidates all the fields into a single list of accession numbers.
  398. *
  399. * The array of printable fields will contain an array containing the
  400. * accession number and the label. The title used is from the first
  401. * occurance of an accession.
  402. */
  403. protected function setPrintableFields() {
  404. foreach ($this->fields as $site => $bundles) {
  405. foreach ($bundles as $bundle_name => $bundle_fields) {
  406. foreach ($bundle_fields as $field_id => $info) {
  407. $field = $info['field'];
  408. $instance = $info['instance'];
  409. $accession = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
  410. if (!array_key_exists($accession, $this->printable_fields)) {
  411. // Only include fields that support this downloader type in
  412. // or list of printable fields.
  413. if ($this->isFieldSupported($field, $instance)) {
  414. $this->printable_fields[$accession] = $instance['label'];
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. /**
  422. * Formats the entity and the specified fields for output.
  423. *
  424. * This function should be implemented by a child class. It should iterate
  425. * over the fields for the entity and return the appropriate format. It may
  426. * return multiple lines of output if necessary.
  427. *
  428. * @param $entity
  429. * The entity object. The fields that should be formatted are already
  430. * loaded.
  431. *
  432. * @return
  433. * An array of strings (one per line of output.
  434. */
  435. abstract protected function formatEntity($entity);
  436. /**
  437. * Retrieves header lines
  438. *
  439. * This function should be implemented by a child class. It should return
  440. * the header lines for an output file.
  441. */
  442. abstract protected function getHeader();
  443. }