Example #1
0
 public function toJSON($asArray = false, $prettyPrint = false, $includeEmpty = false, $unformattedFields = false)
 {
     if ($this->id || $this->key) {
         if (!$this->loaded['primaryData']) {
             $this->loadPrimaryData(true);
         }
         if (!$this->loaded['itemData']) {
             $this->loadItemData();
         }
     }
     $regularItem = $this->isRegularItem();
     $arr = array();
     $arr['itemType'] = Zotero_ItemTypes::getName($this->itemTypeID);
     if ($this->isAttachment()) {
         $val = $this->attachmentLinkMode;
         $arr['linkMode'] = strtolower(Zotero_Attachments::linkModeNumberToName($val));
     }
     // For regular items, show title and creators first
     if ($regularItem) {
         // Get 'title' or the equivalent base-mapped field
         $titleFieldID = Zotero_ItemFields::getFieldIDFromTypeAndBase($this->itemTypeID, 'title');
         $titleFieldName = Zotero_ItemFields::getName($titleFieldID);
         if ($includeEmpty || $this->itemData[$titleFieldID] !== false) {
             $arr[$titleFieldName] = $this->itemData[$titleFieldID] !== false ? $this->itemData[$titleFieldID] : "";
         }
         // Creators
         $arr['creators'] = array();
         $creators = $this->getCreators();
         foreach ($creators as $creator) {
             $c = array();
             $c['creatorType'] = Zotero_CreatorTypes::getName($creator['creatorTypeID']);
             // Single-field mode
             if ($creator['ref']->fieldMode == 1) {
                 $c['name'] = $creator['ref']->lastName;
             } else {
                 $c['firstName'] = $creator['ref']->firstName;
                 $c['lastName'] = $creator['ref']->lastName;
             }
             $arr['creators'][] = $c;
         }
         if (!$arr['creators'] && !$includeEmpty) {
             unset($arr['creators']);
         }
     } else {
         $titleFieldID = false;
     }
     // Item metadata
     $fields = array_keys($this->itemData);
     foreach ($fields as $field) {
         if ($field == $titleFieldID) {
             continue;
         }
         if ($unformattedFields) {
             $value = $this->itemData[$field];
         } else {
             $value = $this->getField($field);
         }
         if (!$includeEmpty && ($value === false || $value === "")) {
             continue;
         }
         $arr[Zotero_ItemFields::getName($field)] = $value ? $value : "";
     }
     // Embedded note for notes and attachments
     if (!$regularItem) {
         // Use sanitized version
         $arr['note'] = $this->getNote(true);
     }
     if ($this->isAttachment()) {
         $val = $this->attachmentLinkMode;
         $arr['linkMode'] = strtolower(Zotero_Attachments::linkModeNumberToName($val));
         $val = $this->attachmentMIMEType;
         if ($includeEmpty || $val !== false && $val !== "") {
             $arr['contentType'] = $val;
         }
         $val = $this->attachmentCharset;
         if ($includeEmpty || $val !== false && $val !== "") {
             $arr['charset'] = $val;
         }
         if ($this->isImportedAttachment()) {
             $arr['filename'] = $this->attachmentFilename;
             $val = $this->attachmentStorageHash;
             if ($includeEmpty || $val) {
                 $arr['md5'] = $val;
             }
             $val = $this->attachmentStorageModTime;
             if ($includeEmpty || $val) {
                 $arr['mtime'] = $val;
             }
         }
     }
     if ($this->getDeleted()) {
         $arr['deleted'] = 1;
     }
     // Tags
     $arr['tags'] = array();
     $tags = $this->getTags();
     if ($tags) {
         foreach ($tags as $tag) {
             $t = array('tag' => $tag->name);
             if ($tag->type != 0) {
                 $t['type'] = $tag->type;
             }
             $arr['tags'][] = $t;
         }
     }
     if ($asArray) {
         return $arr;
     }
     $mask = JSON_HEX_TAG | JSON_HEX_AMP;
     if ($prettyPrint) {
         $json = Zotero_Utilities::json_encode_pretty($arr, $mask);
     } else {
         $json = json_encode($arr, $mask);
     }
     // Until JSON_UNESCAPED_SLASHES is available
     $json = str_replace('\\/', '/', $json);
     return $json;
 }
 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 toJSON($asArray = false, $prettyPrint = false)
 {
     if (!$this->loaded) {
         $this->load();
     }
     $arr['name'] = $this->name;
     $parentKey = $this->getParentKey();
     $arr['parent'] = $parentKey ? $parentKey : false;
     if ($asArray) {
         return $arr;
     }
     $mask = JSON_HEX_TAG | JSON_HEX_AMP;
     if ($prettyPrint) {
         $json = Zotero_Utilities::json_encode_pretty($arr, $mask);
     } else {
         $json = json_encode($arr, $mask);
     }
     // Until JSON_UNESCAPED_SLASHES is available
     $json = str_replace('\\/', '/', $json);
     return $json;
 }
Example #4
0
 /**
  * Converts a Zotero_Item object to a SimpleXMLElement Atom object
  *
  * @param	object				$item		Zotero_Item object
  * @param	string				$content
  * @return	SimpleXMLElement					Item data as SimpleXML element
  */
 public static function convertItemToAtom(Zotero_Item $item, $queryParams, $apiVersion = null, $permissions = null, $sharedData = null)
 {
     $content = $queryParams['content'];
     $contentIsHTML = sizeOf($content) == 1 && $content[0] == 'html';
     $contentParamString = urlencode(implode(',', $content));
     $style = $queryParams['style'];
     $entry = '<entry xmlns="' . Zotero_Atom::$nsAtom . '" xmlns:zapi="' . Zotero_Atom::$nsZoteroAPI . '"/>';
     $xml = new SimpleXMLElement($entry);
     $title = $item->getDisplayTitle(true);
     $title = $title ? $title : '[Untitled]';
     $xml->title = $title;
     $author = $xml->addChild('author');
     $createdByUserID = null;
     switch (Zotero_Libraries::getType($item->libraryID)) {
         case 'group':
             $createdByUserID = $item->createdByUserID;
             break;
     }
     if ($createdByUserID) {
         $author->name = Zotero_Users::getUsername($createdByUserID);
         $author->uri = Zotero_URI::getUserURI($createdByUserID);
     } else {
         $author->name = Zotero_Libraries::getName($item->libraryID);
         $author->uri = Zotero_URI::getLibraryURI($item->libraryID);
     }
     $id = Zotero_URI::getItemURI($item);
     /*if (!$contentIsHTML) {
     			$id .= "?content=$content";
     		}*/
     $xml->id = $id;
     $xml->published = Zotero_Date::sqlToISO8601($item->getField('dateAdded'));
     $xml->updated = Zotero_Date::sqlToISO8601($item->getField('dateModified'));
     $link = $xml->addChild("link");
     $link['rel'] = "self";
     $link['type'] = "application/atom+xml";
     $href = Zotero_Atom::getItemURI($item);
     if (!$contentIsHTML) {
         $href .= "?content={$contentParamString}";
     }
     $link['href'] = $href;
     $parent = $item->getSource();
     if ($parent) {
         // TODO: handle group items?
         $parentItem = Zotero_Items::get($item->libraryID, $parent);
         $link = $xml->addChild("link");
         $link['rel'] = "up";
         $link['type'] = "application/atom+xml";
         $href = Zotero_Atom::getItemURI($parentItem);
         if (!$contentIsHTML) {
             $href .= "?content={$contentParamString}";
         }
         $link['href'] = $href;
     }
     $link = $xml->addChild('link');
     $link['rel'] = 'alternate';
     $link['type'] = 'text/html';
     $link['href'] = Zotero_URI::getItemURI($item);
     // If appropriate permissions and the file is stored in ZFS, get file request link
     if ($permissions && $permissions->canAccess($item->libraryID, 'files')) {
         $details = Zotero_S3::getDownloadDetails($item);
         if ($details) {
             $link = $xml->addChild('link');
             $link['rel'] = 'enclosure';
             $type = $item->attachmentMIMEType;
             if ($type) {
                 $link['type'] = $type;
             }
             $link['href'] = $details['url'];
             if (!empty($details['filename'])) {
                 $link['title'] = $details['filename'];
             }
             if (!empty($details['size'])) {
                 $link['length'] = $details['size'];
             }
         }
     }
     $xml->addChild('zapi:key', $item->key, Zotero_Atom::$nsZoteroAPI);
     $xml->addChild('zapi:itemType', Zotero_ItemTypes::getName($item->itemTypeID), Zotero_Atom::$nsZoteroAPI);
     if ($item->isRegularItem()) {
         $val = $item->creatorSummary;
         if ($val !== '') {
             $xml->addChild('zapi:creatorSummary', htmlspecialchars($val), Zotero_Atom::$nsZoteroAPI);
         }
         $val = substr($item->getField('date', true, true, true), 0, 4);
         if ($val !== '' && $val !== '0000') {
             $xml->addChild('zapi:year', $val, Zotero_Atom::$nsZoteroAPI);
         }
     }
     if (!$parent && $item->isRegularItem()) {
         if ($permissions && !$permissions->canAccess($item->libraryID, 'notes')) {
             $numChildren = $item->numAttachments();
         } else {
             $numChildren = $item->numChildren();
         }
         $xml->addChild('zapi:numChildren', $numChildren, Zotero_Atom::$nsZoteroAPI);
     }
     $xml->addChild('zapi:numTags', $item->numTags(), Zotero_Atom::$nsZoteroAPI);
     $xml->content = '';
     //
     // DOM XML from here on out
     //
     $contentNode = dom_import_simplexml($xml->content);
     $domDoc = $contentNode->ownerDocument;
     $multiFormat = sizeOf($content) > 1;
     // Create a root XML document for multi-format responses
     if ($multiFormat) {
         $contentNode->setAttribute('type', 'application/xml');
         /*$multicontent = $domDoc->createElementNS(
         			Zotero_Atom::$nsZoteroAPI, 'multicontent'
         		);
         		$contentNode->appendChild($multicontent);*/
     }
     foreach ($content as $type) {
         // Set the target to either the main <content>
         // or a <multicontent> <content>
         if (!$multiFormat) {
             $target = $contentNode;
         } else {
             $target = $domDoc->createElementNS(Zotero_Atom::$nsZoteroAPI, 'subcontent');
             $contentNode->appendChild($target);
         }
         $target->setAttributeNS(Zotero_Atom::$nsZoteroAPI, "zapi:type", $type);
         if ($type == 'html') {
             if (!$multiFormat) {
                 $target->setAttribute('type', 'xhtml');
             }
             $div = $domDoc->createElement('div');
             $div->setAttribute('xmlns', Zotero_Atom::$nsXHTML);
             $target->appendChild($div);
             $html = $item->toHTML(true);
             $subNode = dom_import_simplexml($html);
             $importedNode = $domDoc->importNode($subNode, true);
             $div->appendChild($importedNode);
         } else {
             if ($type == 'citation') {
                 if (!$multiFormat) {
                     $target->setAttribute('type', 'xhtml');
                 }
                 if (isset($sharedData[$type][$item->libraryID . "/" . $item->key])) {
                     $html = $sharedData[$type][$item->libraryID . "/" . $item->key];
                 } else {
                     if ($sharedData !== null) {
                         error_log("Citation not found in sharedData -- retrieving individually");
                     }
                     $html = Zotero_Cite::getCitationFromCiteServer($item, $style);
                 }
                 $html = new SimpleXMLElement($html);
                 $html['xmlns'] = Zotero_Atom::$nsXHTML;
                 $subNode = dom_import_simplexml($html);
                 $importedNode = $domDoc->importNode($subNode, true);
                 $target->appendChild($importedNode);
             } else {
                 if ($type == 'bib') {
                     if (!$multiFormat) {
                         $target->setAttribute('type', 'xhtml');
                     }
                     if (isset($sharedData[$type][$item->libraryID . "/" . $item->key])) {
                         $html = $sharedData[$type][$item->libraryID . "/" . $item->key];
                     } else {
                         if ($sharedData !== null) {
                             error_log("Bibliography not found in sharedData -- retrieving individually");
                         }
                         $html = Zotero_Cite::getBibliographyFromCitationServer(array($item), $style);
                     }
                     $html = new SimpleXMLElement($html);
                     $html['xmlns'] = Zotero_Atom::$nsXHTML;
                     $subNode = dom_import_simplexml($html);
                     $importedNode = $domDoc->importNode($subNode, true);
                     $target->appendChild($importedNode);
                 } else {
                     if ($type == 'json') {
                         $target->setAttributeNS(Zotero_Atom::$nsZoteroAPI, "zapi:etag", $item->etag);
                         $textNode = $domDoc->createTextNode($item->toJSON(false, $queryParams['pprint'], true));
                         $target->appendChild($textNode);
                     } else {
                         if ($type == 'csljson') {
                             $arr = $item->toCSLItem();
                             $mask = JSON_HEX_TAG | JSON_HEX_AMP;
                             if ($queryParams['pprint']) {
                                 $json = Zotero_Utilities::json_encode_pretty($arr, $mask);
                             } else {
                                 $json = json_encode($arr, $mask);
                             }
                             // Until JSON_UNESCAPED_SLASHES is available
                             $json = str_replace('\\/', '/', $json);
                             $textNode = $domDoc->createTextNode($json);
                             $target->appendChild($textNode);
                         } else {
                             if ($type == 'full') {
                                 if (!$multiFormat) {
                                     $target->setAttribute('type', 'xhtml');
                                 }
                                 $fullXML = Zotero_Items::convertItemToXML($item, array(), $apiVersion);
                                 $fullXML->addAttribute("xmlns", Zotero_Atom::$nsZoteroTransfer);
                                 $subNode = dom_import_simplexml($fullXML);
                                 $importedNode = $domDoc->importNode($subNode, true);
                                 $target->appendChild($importedNode);
                             } else {
                                 if (in_array($type, Zotero_Translate::$exportFormats)) {
                                     $export = Zotero_Translate::doExport(array($item), $type);
                                     $target->setAttribute('type', $export['mimeType']);
                                     // Insert XML into document
                                     if (preg_match('/\\+xml$/', $export['mimeType'])) {
                                         // Strip prolog
                                         $body = preg_replace('/^<\\?xml.+\\n/', "", $export['body']);
                                         $subNode = $domDoc->createDocumentFragment();
                                         $subNode->appendXML($body);
                                         $target->appendChild($subNode);
                                     } else {
                                         $textNode = $domDoc->createTextNode($export['body']);
                                         $target->appendChild($textNode);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $xml;
 }
Example #5
0
 /**
  * Converts group to a JSON object
  */
 public function toJSON($asArray = false, $prettyPrint = false, $includeEmpty = false)
 {
     if (($this->id || $this->libraryID) && !$this->loaded) {
         $this->load();
     }
     $arr = array();
     $arr['name'] = $this->name;
     $arr['owner'] = $this->ownerUserID;
     $arr['type'] = $this->type;
     if ($this->description || $includeEmpty) {
         $arr['description'] = $this->description;
     }
     if ($this->url || $includeEmpty) {
         $arr['url'] = $this->url;
     }
     if ($this->hasImage) {
         $arr['hasImage'] = 1;
     }
     if ($this->libraryEnabled) {
         $arr['libraryEnabled'] = 1;
         $arr['libraryEditing'] = $this->libraryEditing;
         $arr['libraryReading'] = $this->libraryReading;
         $arr['fileEditing'] = $this->fileEditing;
     } else {
         $arr['libraryEnabled'] = 0;
     }
     $admins = $this->getAdmins();
     if ($admins) {
         $arr['admins'] = $admins;
     }
     $members = $this->getMembers();
     if ($members) {
         $arr['members'] = $members;
     }
     if ($asArray) {
         return $arr;
     }
     $mask = JSON_HEX_TAG | JSON_HEX_AMP;
     if ($prettyPrint) {
         $json = Zotero_Utilities::json_encode_pretty($arr, $mask);
     } else {
         $json = json_encode($arr, $mask);
     }
     // Until JSON_UNESCAPED_SLASHES is available
     $json = str_replace('\\/', '/', $json);
     return $json;
 }