/** * 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; }