protected function loadItemData($reload = false) { if ($this->loaded['itemData'] && !$reload) return; Z_Core::debug("Loading item data for item $this->id"); // TODO: remove? if (!$this->id) { trigger_error('Item ID not set before attempting to load data', E_USER_ERROR); } if (!is_numeric($this->id)) { trigger_error("Invalid itemID '$this->id'", E_USER_ERROR); } if ($this->cacheEnabled) { $cacheVersion = 1; $cacheKey = $this->getCacheKey("itemData", $cacheVersion . isset(Z_CONFIG::$CACHE_VERSION_ITEM_DATA) ? "_" . Z_CONFIG::$CACHE_VERSION_ITEM_DATA : "" ); $fields = Z_Core::$MC->get($cacheKey); } else { $fields = false; } if ($fields === false) { $sql = "SELECT fieldID, value FROM itemData WHERE itemID=?"; $stmt = Zotero_DB::getStatement($sql, true, Zotero_Shards::getByLibraryID($this->libraryID)); $fields = Zotero_DB::queryFromStatement($stmt, $this->id); if ($this->cacheEnabled) { Z_Core::$MC->set($cacheKey, $fields ? $fields : array()); } } $itemTypeFields = Zotero_ItemFields::getItemTypeFields($this->itemTypeID); if ($fields) { foreach ($fields as $field) { $this->setField($field['fieldID'], $field['value'], true, true); } } // Mark nonexistent fields as loaded if ($itemTypeFields) { foreach($itemTypeFields as $fieldID) { if (is_null($this->itemData[$fieldID])) { $this->itemData[$fieldID] = false; } } } $this->loaded['itemData'] = true; }
public function newItem() { if (empty($_GET['itemType'])) { $this->e400("'itemType' not provided"); } $itemType = $_GET['itemType']; if ($itemType == 'attachment') { if (empty($_GET['linkMode'])) { $this->e400("linkMode required for itemType=attachment"); } $linkModeName = $_GET['linkMode']; try { $linkMode = Zotero_Attachments::linkModeNameToNumber(strtoupper($linkModeName)); } catch (Exception $e) { $this->e400("Invalid linkMode '{$linkModeName}'"); } } $itemTypeID = Zotero_ItemTypes::getID($itemType); if (!$itemTypeID) { $this->e400("Invalid item type '{$itemType}'"); } // TODO: check If-Modified-Since and return 304 if not changed $cacheKey = "newItemJSON_" . $itemTypeID; if ($itemType == 'attachment') { $cacheKey .= "_" . $linkMode; } $ttl = 60; if ($this->queryParams['pprint']) { $cacheKey .= "_pprint"; } $json = Z_Core::$MC->get($cacheKey); if ($json) { header("Content-Type: application/json"); echo $json; exit; } // Generate template $json = array('itemType' => $itemType); if ($itemType == 'attachment') { $json['linkMode'] = $linkModeName; } $fieldIDs = Zotero_ItemFields::getItemTypeFields($itemTypeID); $first = true; foreach ($fieldIDs as $fieldID) { $fieldName = Zotero_ItemFields::getName($fieldID); if ($itemType == 'attachment' && $fieldName == 'url' && !preg_match('/_url$/', $linkModeName)) { continue; } $json[$fieldName] = ""; if ($first && $itemType != 'note' && $itemType != 'attachment') { $creatorTypeID = Zotero_CreatorTypes::getPrimaryIDForType($itemTypeID); $creatorTypeName = Zotero_CreatorTypes::getName($creatorTypeID); $json['creators'] = array(array('creatorType' => $creatorTypeName, 'firstName' => '', 'lastName' => '')); $first = false; } } if ($itemType == 'note' || $itemType == 'attachment') { $json['note'] = ''; } $json['tags'] = array(); if ($itemType != 'note' && $itemType != 'attachment') { $json['attachments'] = array(); $json['notes'] = array(); } if ($itemType == 'attachment') { $json['contentType'] = ''; $json['charset'] = ''; if (preg_match('/^imported_/', $linkModeName)) { $json['filename'] = ''; $json['md5'] = null; $json['mtime'] = null; //$json['zip'] = false; } } header("Content-Type: application/json"); if ($this->queryParams['pprint']) { $json = Zotero_Utilities::json_encode_pretty($json); Z_Core::$MC->set($cacheKey, $json, $ttl); } else { $json = json_encode($json); Z_Core::$MC->set($cacheKey, $json, $ttl); } echo $json; exit; }
private function loadItemData() { Z_Core::debug("Loading item data for item {$this->id}"); // TODO: remove? if ($this->loaded['itemData']) { trigger_error("Item data for item {$this->id} already loaded", E_USER_ERROR); } if (!$this->id) { trigger_error('Item ID not set before attempting to load data', E_USER_ERROR); } if (!is_numeric($this->id)) { trigger_error("Invalid itemID '{$this->id}'", E_USER_ERROR); } $cacheKey = $this->getCacheKey("itemData"); $fields = Z_Core::$MC->get($cacheKey); if ($fields === false) { $sql = "SELECT fieldID, value FROM itemData WHERE itemID=?"; $stmt = Zotero_DB::getStatement($sql, true, Zotero_Shards::getByLibraryID($this->libraryID)); $fields = Zotero_DB::queryFromStatement($stmt, $this->id); Z_Core::$MC->set($cacheKey, $fields ? $fields : array()); } $itemTypeFields = Zotero_ItemFields::getItemTypeFields($this->itemTypeID); if ($fields) { foreach ($fields as $field) { $this->setField($field['fieldID'], $field['value'], true, true); } } // Mark nonexistent fields as loaded if ($itemTypeFields) { foreach ($itemTypeFields as $fieldID) { if (is_null($this->itemData[$fieldID])) { $this->itemData[$fieldID] = false; } } } $this->loaded['itemData'] = true; }
public function newItem() { if (empty($_GET['itemType'])) { $this->e400("'itemType' not provided"); } $itemType = $_GET['itemType']; if ($itemType == 'attachment') { if (empty($_GET['linkMode'])) { $this->e400("linkMode required for itemType=attachment"); } $linkModeName = $_GET['linkMode']; try { $linkMode = Zotero_Attachments::linkModeNameToNumber(strtoupper($linkModeName)); } catch (Exception $e) { $this->e400("Invalid linkMode '{$linkModeName}'"); } } $itemTypeID = Zotero_ItemTypes::getID($itemType); if (!$itemTypeID) { $this->e400("Invalid item type '{$itemType}'"); } // TODO: check If-Modified-Since and return 304 if not changed $cacheVersion = 1; $cacheKey = "newItemJSON" . "_" . $this->apiVersion . "_" . $itemTypeID . "_" . $cacheVersion; if ($itemType == 'attachment') { $cacheKey .= "_" . $linkMode; } $cacheKey .= '_' . $this->apiVersion; $ttl = 60; $json = Z_Core::$MC->get($cacheKey); if ($json) { header("Content-Type: application/json"); echo $json; exit; } // Generate template $json = array('itemType' => $itemType); if ($itemType == 'attachment') { $json['linkMode'] = $linkModeName; } $fieldIDs = Zotero_ItemFields::getItemTypeFields($itemTypeID); $first = true; foreach ($fieldIDs as $fieldID) { $fieldName = Zotero_ItemFields::getName($fieldID); // Before v3, computerProgram's 'versionNumber' was just 'version' if ($this->apiVersion < 3 && $fieldID == 81) { $fieldName = 'version'; } if ($itemType == 'attachment' && $fieldName == 'url' && !preg_match('/_url$/', $linkModeName)) { continue; } $json[$fieldName] = ""; if ($first && $itemType != 'note' && $itemType != 'attachment') { $creatorTypeID = Zotero_CreatorTypes::getPrimaryIDForType($itemTypeID); $creatorTypeName = Zotero_CreatorTypes::getName($creatorTypeID); $json['creators'] = array(array('creatorType' => $creatorTypeName, 'firstName' => '', 'lastName' => '')); $first = false; } } if ($itemType == 'note' || $itemType == 'attachment') { $json['note'] = ''; } $json['tags'] = array(); if ($this->apiVersion >= 2) { $json['collections'] = array(); $json['relations'] = new stdClass(); } if ($this->apiVersion == 1) { if ($itemType != 'note' && $itemType != 'attachment') { $json['attachments'] = array(); $json['notes'] = array(); } } if ($itemType == 'attachment') { $json['contentType'] = ''; $json['charset'] = ''; if (preg_match('/^imported_/', $linkModeName)) { $json['filename'] = ''; $json['md5'] = null; $json['mtime'] = null; //$json['zip'] = false; } } header("Content-Type: application/json"); $json = Zotero_Utilities::formatJSON($json); Z_Core::$MC->set($cacheKey, $json, $ttl); echo $json; exit; }