public function setCreator($orderIndex, Zotero_Creator $creator, $creatorTypeID) { if ($this->id && !$this->loaded['creators']) { $this->loadCreators(); } else { $this->loaded['creators'] = true; } if (!is_integer($orderIndex)) { throw new Exception("orderIndex must be an integer"); } if (!($creator instanceof Zotero_Creator)) { throw new Exception("creator must be a Zotero_Creator object"); } if (!is_integer($creatorTypeID)) { throw new Exception("creatorTypeID must be an integer"); } if (!Zotero_CreatorTypes::getID($creatorTypeID)) { throw new Exception("Invalid creatorTypeID '$creatorTypeID'"); } if ($this->libraryID != $creator->libraryID) { throw new Exception("Creator library IDs don't match"); } // If creatorTypeID isn't valid for this type, use the primary type if (!Zotero_CreatorTypes::isValidForItemType($creatorTypeID, $this->itemTypeID)) { $msg = "Invalid creator type $creatorTypeID for item type " . $this->itemTypeID . " -- changing to primary creator"; Z_Core::debug($msg); $creatorTypeID = Zotero_CreatorTypes::getPrimaryIDForType($this->itemTypeID); } // If creator already exists at this position, cancel if (isset($this->creators[$orderIndex]) && $this->creators[$orderIndex]['ref']->id == $creator->id && $this->creators[$orderIndex]['creatorTypeID'] == $creatorTypeID && !$creator->hasChanged()) { Z_Core::debug("Creator in position $orderIndex hasn't changed", 4); return false; } $this->creators[$orderIndex]['ref'] = $creator; $this->creators[$orderIndex]['creatorTypeID'] = $creatorTypeID; $this->changed['creators'][$orderIndex] = true; return true; }
public static function retrieveItem($zoteroItem) { if (!$zoteroItem) { throw new Exception("Zotero item not provided"); } // don't return URL or accessed information for journal articles if a // pages field exists $itemType = Zotero_ItemTypes::getName($zoteroItem->itemTypeID); $cslType = isset(self::$zoteroTypeMap[$itemType]) ? self::$zoteroTypeMap[$itemType] : false; if (!$cslType) { $cslType = "article"; } $ignoreURL = ($zoteroItem->getField("accessDate", true, true, true) || $zoteroItem->getField("url", true, true, true)) && in_array($itemType, array("journalArticle", "newspaperArticle", "magazineArticle")) && $zoteroItem->getField("pages", false, false, true) && self::$citePaperJournalArticleURL; $cslItem = array('id' => $zoteroItem->libraryID . "/" . $zoteroItem->key, 'type' => $cslType); // get all text variables (there must be a better way) // TODO: does citeproc-js permit short forms? foreach (self::$zoteroFieldMap as $variable => $fields) { if ($variable == "URL" && $ignoreURL) { continue; } foreach ($fields as $field) { $value = $zoteroItem->getField($field, false, true, true); if ($value !== "") { // Strip enclosing quotes if (preg_match(self::$quotedRegexp, $value)) { $value = substr($value, 1, strlen($value) - 2); } $cslItem[$variable] = $value; break; } } } // separate name variables $authorID = Zotero_CreatorTypes::getPrimaryIDForType($zoteroItem->itemTypeID); $creators = $zoteroItem->getCreators(); foreach ($creators as $creator) { if ($creator['creatorTypeID'] == $authorID) { $creatorType = "author"; } else { $creatorType = Zotero_CreatorTypes::getName($creator['creatorTypeID']); } $creatorType = isset(self::$zoteroNameMap[$creatorType]) ? self::$zoteroNameMap[$creatorType] : false; if (!$creatorType) { continue; } $nameObj = array('family' => $creator['ref']->lastName, 'given' => $creator['ref']->firstName); if (isset($cslItem[$creatorType])) { $cslItem[$creatorType][] = $nameObj; } else { $cslItem[$creatorType] = array($nameObj); } } // get date variables foreach (self::$zoteroDateMap as $key => $val) { $date = $zoteroItem->getField($val, false, true, true); if ($date) { $cslItem[$key] = array("raw" => $date); continue; $date = Zotero_Date::strToDate($date); if (!empty($date['part']) && !$date['month']) { // if there's a part but no month, interpret literally $cslItem[$variable] = array("literal" => $date['part']); } else { // otherwise, use date-parts $dateParts = array(); if ($date['year']) { $dateParts[] = $date['year']; if ($date['month']) { $dateParts[] = $date['month'] + 1; // Mimics JS if ($date['day']) { $dateParts[] = $date['day']; } } } $cslItem[$key] = array("date-parts" => array($dateParts)); } } } return $cslItem; }
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; }
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; }