/** * Converts a DOMElement item to a Zotero_Tag object * * @param DOMElement $xml Tag data as DOMElement * @param int $libraryID Library ID * @return Zotero_Tag Zotero tag object */ public static function convertXMLToTag(DOMElement $xml, &$itemKeysToUpdate) { $libraryID = (int) $xml->getAttribute('libraryID'); $tag = self::getByLibraryAndKey($libraryID, $xml->getAttribute('key')); if (!$tag) { $tag = new Zotero_Tag(); $tag->libraryID = $libraryID; $tag->key = $xml->getAttribute('key'); } $tag->name = $xml->getAttribute('name'); $type = (int) $xml->getAttribute('type'); $tag->type = $type ? $type : 0; $tag->dateAdded = $xml->getAttribute('dateAdded'); $tag->dateModified = $xml->getAttribute('dateModified'); $dataChanged = $tag->hasChanged(); $itemKeys = $xml->getElementsByTagName('items'); $oldKeys = $tag->getLinkedItems(true); if ($itemKeys->length) { $newKeys = explode(' ', $itemKeys->item(0)->nodeValue); } else { $newKeys = array(); } $addKeys = array_diff($newKeys, $oldKeys); $removeKeys = array_diff($oldKeys, $newKeys); // If the data has changed, all old and new items need to change if ($dataChanged) { $itemKeysToUpdate = array_merge($oldKeys, $addKeys); } else { $itemKeysToUpdate = array_merge($addKeys, $removeKeys); } $tag->setLinkedItems($newKeys); return $tag; }
/** * Converts a DOMElement item to a Zotero_Tag object * * @param DOMElement $xml Tag data as DOMElement * @param int $libraryID Library ID * @return Zotero_Tag Zotero tag object */ public static function convertXMLToTag(DOMElement $xml) { $libraryID = (int) $xml->getAttribute('libraryID'); $tag = self::getByLibraryAndKey($libraryID, $xml->getAttribute('key')); if (!$tag) { $tag = new Zotero_Tag(); $tag->libraryID = $libraryID; $tag->key = $xml->getAttribute('key'); } $tag->name = $xml->getAttribute('name'); $type = (int) $xml->getAttribute('type'); $tag->type = $type ? $type : 0; $tag->dateAdded = $xml->getAttribute('dateAdded'); $tag->dateModified = $xml->getAttribute('dateModified'); $itemKeys = $xml->getElementsByTagName('items'); if ($itemKeys->length) { $itemKeys = explode(' ', $itemKeys->item(0)->nodeValue); $itemIDs = array(); foreach ($itemKeys as $key) { $item = Zotero_Items::getByLibraryAndKey($libraryID, $key); if (!$item) { // Return a specific error for a wrong-library tag issue that I can't reproduce throw new Exception("Linked item {$key} of tag {$libraryID}/{$tag->key} not found", Z_ERROR_TAG_LINKED_ITEM_NOT_FOUND); //throw new Exception("Linked item $key of tag $libraryID/$tag->key not found", Z_ERROR_ITEM_NOT_FOUND); } $itemIDs[] = $item->id; } $tag->setLinkedItems($itemIDs); } else { $tag->setLinkedItems(array()); } return $tag; }