function zp_get_server_items($wpdb, $api_user_id, $zp_start) { $zp_import_contents = new ZotpressRequest(); $zp_account = zp_get_account($wpdb, $api_user_id); //$zp_account = $GLOBALS['zp_session'][$api_user_id]['zp_account']; // See if default exists $zp_default_style = "apa"; if (get_option("Zotpress_DefaultStyle")) { $zp_default_style = get_option("Zotpress_DefaultStyle"); } // Build request URL $zp_import_url = "https://api.zotero.org/" . $zp_account[0]->account_type . "/" . $api_user_id . "/items?"; if (is_null($zp_account[0]->public_key) === false && trim($zp_account[0]->public_key) != "") { $zp_import_url .= "key=" . $zp_account[0]->public_key . "&"; } $zp_import_url .= "format=atom&content=json,bib&style=" . $zp_default_style . "&limit=50&start=" . $zp_start; //var_dump($zp_import_url); // Read the external data $zp_xml = $zp_import_contents->get_request_contents($zp_import_url, false); // Stop in our tracks if there's a request error if ($zp_import_contents->request_error) { return $zp_import_contents->request_error; } // Make it DOM-traversable $doc_citations = new DOMDocument(); $doc_citations->loadXML($zp_xml); // Get last set if (!isset($GLOBALS['zp_session'][$api_user_id]['items']['last_set'])) { $last_set = ""; $links = $doc_citations->getElementsByTagName("link"); foreach ($links as $link) { if ($link->getAttribute('rel') == "last") { if (stripos($link->getAttribute('href'), "start=") !== false) { $last_set = explode("start=", $link->getAttribute('href')); $GLOBALS['zp_session'][$api_user_id]['items']['last_set'] = intval($last_set[1]); } else { $GLOBALS['zp_session'][$api_user_id]['items']['last_set'] = 0; } } } } $entries = $doc_citations->getElementsByTagName("entry"); // COMPARE EACH ENTRY TO LOCAL // Entries can be items or attachments (e.g. notes) foreach ($entries as $entry) { $item_key = $entry->getElementsByTagNameNS("http://zotero.org/ns/api", "key")->item(0)->nodeValue; $retrieved = $entry->getElementsByTagName("updated")->item(0)->nodeValue; // Check to see if item key exists in local if (array_key_exists($item_key, $GLOBALS['zp_session'][$api_user_id]['items']['zp_local_items'])) { // Check to see if it needs updating if ($retrieved != $GLOBALS['zp_session'][$api_user_id]['items']['zp_local_items'][$item_key]->retrieved) { $GLOBALS['zp_session'][$api_user_id]['items']['zp_items_to_update'][$item_key] = $GLOBALS['zp_session'][$api_user_id]['items']['zp_local_items'][$item_key]->id; //unset($GLOBALS['zp_session'][$api_user_id]['items']['zp_local_items'][$item_key]); // Leave only the local ones that should be deleted update_option('ZOTPRESS_DELETE_' . $api_user_id, get_option('ZOTPRESS_DELETE_' . $api_user_id) . "," . $item_key); } else { //unset($GLOBALS['zp_session'][$api_user_id]['items']['zp_local_items'][$item_key]); // Leave only the local ones that should be deleted update_option('ZOTPRESS_DELETE_' . $api_user_id, get_option('ZOTPRESS_DELETE_' . $api_user_id) . "," . $item_key); continue; } } // Item key doesn't exist in local, or needs updating, so collect metadata and add $item_type = $entry->getElementsByTagNameNS("http://zotero.org/ns/api", "itemType")->item(0)->nodeValue; // Get citation content (json and bib) $citation_content = ""; $citation_content_temp = new DOMDocument(); foreach ($entry->getElementsByTagNameNS("http://zotero.org/ns/api", "subcontent") as $child) { if ($child->attributes->getNamedItem("type")->nodeValue == "json") { $json_content = $child->nodeValue; } else { foreach ($child->childNodes as $child_content) { $citation_content_temp->appendChild($citation_content_temp->importNode($child_content, true)); $citation_content = $citation_content_temp->saveHTML(); } } } // Get basic metadata from JSON $json_content_decoded = json_decode($json_content); $author = ""; $author_other = ""; $date = ""; $year = ""; $title = ""; $numchildren = 0; $parent = ""; $link_mode = ""; if (count($json_content_decoded->creators) > 0) { foreach ($json_content_decoded->creators as $creator) { if ($creator->creatorType == "author") { $author .= $creator->lastName . ", "; } else { $author_other .= $creator->lastName . ", "; } } } else { $author .= $creator->creators["lastName"]; } // Determine if we use author or other author type if (trim($author) == "") { $author = $author_other; } // Remove last comma $author = preg_replace('~(.*)' . preg_quote(', ', '~') . '~', '$1' . '', $author, 1); $date = $json_content_decoded->date; $year = zp_extract_year($date); if (trim($year) == "") { $year = "1977"; } $title = $json_content_decoded->title; $numchildren = intval($entry->getElementsByTagNameNS("http://zotero.org/ns/api", "numChildren")->item(0)->nodeValue); // DOWNLOAD: Find URL if ($item_type == "attachment") { if (isset($json_content_decoded->linkMode)) { $link_mode = $json_content_decoded->linkMode; } } // PARENT foreach ($entry->getElementsByTagName("link") as $entry_link) { if ($entry_link->getAttribute('rel') == "up") { $temp = explode("items/", $entry_link->getAttribute('href')); $temp = explode("?", $temp[1]); $parent = $temp[0]; } // Get download URL if ($link_mode == "imported_file" && $entry_link->getAttribute('rel') == "self") { $citation_content = substr($entry_link->getAttribute('href'), 0, strpos($entry_link->getAttribute('href'), "?")); } } // If item key needs updating if (array_key_exists($item_key, $GLOBALS['zp_session'][$api_user_id]['items']['zp_items_to_update'])) { $GLOBALS['zp_session'][$api_user_id]['items']['zp_items_to_update'][$item_key] = array("api_user_id" => $zp_account[0]->api_user_id, "item_key" => $item_key, "retrieved" => zp_db_prep($retrieved), "json" => zp_db_prep($json_content), "author" => zp_db_prep($author), "zpdate" => zp_db_prep($date), "year" => zp_db_prep($year), "title" => zp_db_prep($title), "itemType" => $item_type, "linkMode" => $link_mode, "citation" => zp_db_prep($citation_content), "style" => zp_db_prep($zp_default_style), "numchildren" => $numchildren, "parent" => $parent); } else { if (!array_key_exists($item_key, $GLOBALS['zp_session'][$api_user_id]['items']['zp_local_items'])) { array_push($GLOBALS['zp_session'][$api_user_id]['items']['zp_items_to_add'], $zp_account[0]->api_user_id, $item_key, zp_db_prep($retrieved), zp_db_prep($json_content), zp_db_prep($author), zp_db_prep($date), zp_db_prep($year), zp_db_prep($title), $item_type, $link_mode, zp_db_prep($citation_content), zp_db_prep($zp_default_style), $numchildren, $parent); $GLOBALS['zp_session'][$api_user_id]['items']['query_total_items_to_add']++; } } } // foreach entry // LAST ITEM if ($GLOBALS['zp_session'][$api_user_id]['items']['last_set'] == $zp_start) { return false; } else { return true; } unset($zp_import_contents); unset($zp_import_url); unset($zp_xml); unset($doc_citations); unset($entries); }
function zp_get_items($wpdb, $api_user_id, $zp_start, $zp_collection = false) { $zp_import_contents = new ZotpressRequest(); $zp_account = zp_get_account($wpdb, $api_user_id); // Get default style $zp_default_style = "apa"; if (get_option("Zotpress_DefaultStyle")) { $zp_default_style = strtolower(get_option("Zotpress_DefaultStyle")); } // Build request URL if ($zp_collection) { $zp_collection_url = '/collections/' . $zp_collection; } else { $zp_collection_url = ''; } $zp_import_url = "https://api.zotero.org/" . $zp_account[0]->account_type . "/" . $zp_account[0]->api_user_id . $zp_collection_url . "/items?"; if (is_null($zp_account[0]->public_key) === false && trim($zp_account[0]->public_key) != "") { $zp_import_url .= "key=" . $zp_account[0]->public_key . "&"; } $zp_import_url .= "format=atom&content=json,bib&style=" . $zp_default_style . "&limit=50&start=" . $zp_start; // Make the request $zp_xml = $zp_import_contents->get_request_contents($zp_import_url, false); // Stop in our tracks if there's a request error if ($zp_import_contents->request_error) { return $zp_import_contents->request_error; } // Report any errors returned from Zotero if (trim(strtolower($zp_xml)) == "forbidden") { return "Zotero is telling Zotpress that access is forbidden. Are you sure that the Zotero API key you're using for this account is correct?"; } else { if (trim(strtolower($zp_xml)) == "invalid style") { return "Zotero is telling Zotpress that the style you're using is invalid. Are you sure that the name of the default style you've selected is correct?"; } } // Make it DOM-traversable $doc_citations = new DOMDocument(); $doc_citations->loadXML($zp_xml); // Get last set if (!isset($GLOBALS['zp_session'][$api_user_id]['items']['last_set'])) { $last_set = ""; $links = $doc_citations->getElementsByTagName("link"); foreach ($links as $link) { if ($link->getAttribute('rel') == "last") { if (stripos($link->getAttribute('href'), "start=") !== false) { $last_set = explode("start=", $link->getAttribute('href')); $GLOBALS['zp_session'][$api_user_id]['items']['last_set'] = intval($last_set[1]); } else { $GLOBALS['zp_session'][$api_user_id]['items']['last_set'] = 0; } } } } // PREPARE EACH ENTRY FOR DB INSERT // Entries can be items or attachments (e.g. notes) $entries = $doc_citations->getElementsByTagName("entry"); foreach ($entries as $entry) { $item_key = $entry->getElementsByTagNameNS("http://zotero.org/ns/api", "key")->item(0)->nodeValue; //// For selective import: Keep track of and skip duplicates //// Not working for some reason ////if ( $zp_collection ) ////{ // if ( array_key_exists( $item_key, $GLOBALS['zp_session'][$api_user_id]['duplicates']['items'] ) ) // continue; // else // $GLOBALS['zp_session'][$api_user_id]['duplicates']['items'][$item_key] = true; ////} $retrieved = $entry->getElementsByTagName("updated")->item(0)->nodeValue; $item_type = $entry->getElementsByTagNameNS("http://zotero.org/ns/api", "itemType")->item(0)->nodeValue; // Get citation content (json and bib) $citation_content = ""; $citation_content_temp = new DOMDocument(); foreach ($entry->getElementsByTagNameNS("http://zotero.org/ns/api", "subcontent") as $child) { if ($child->attributes->getNamedItem("type")->nodeValue == "json") { $json_content = $child->nodeValue; } else { foreach ($child->childNodes as $child_content) { $citation_content_temp->appendChild($citation_content_temp->importNode($child_content, true)); $citation_content = $citation_content_temp->saveHTML(); } } } // Get basic metadata from JSON $json_content_decoded = json_decode($json_content); $author = ""; $author_other = ""; $date = ""; $year = ""; $title = ""; $numchildren = 0; $parent = ""; $link_mode = ""; if (isset($json_content_decoded->creators)) { if (count($json_content_decoded->creators) > 0) { foreach ($json_content_decoded->creators as $creator) { if ($creator->creatorType == "author") { if (isset($creator->name)) { // One-name authors $author .= $creator->name . ", "; } else { $author .= $creator->lastName . ", "; } } else { if (isset($creator->name)) { // One-name authors $author_other .= $creator->name . ", "; } else { $author_other .= $creator->lastName . ", "; } } } } else { $author = ""; //if (isset($creator->name)) // One-name authors // $author .= $creator->creators["name"]; //else // $author .= $creator->creators["lastName"]; } } else { $author = ""; } // Determine if we use author or other author type if (trim($author) == "") { $author = $author_other; } // Remove last comma $author = preg_replace('~(.*)' . preg_quote(', ', '~') . '~', '$1' . '', $author, 1); $date = ""; if (isset($json_content_decoded->date)) { $date = $json_content_decoded->date; } $year = zp_extract_year($date); if (trim($year) == "") { $year = "0000"; } $title = ""; if (isset($json_content_decoded->title)) { $title = $json_content_decoded->title; } $numchildren = 0; if (isset($entry->getElementsByTagNameNS("http://zotero.org/ns/api", "numChildren")->item(0)->nodeValue)) { $numchildren = intval($entry->getElementsByTagNameNS("http://zotero.org/ns/api", "numChildren")->item(0)->nodeValue); } // DOWNLOAD: Find URL // for attachments, look at zapi:subcontent zapi:type="json" - linkMode - either imported_file or linked_url if ($item_type == "attachment") { if (isset($json_content_decoded->linkMode)) { $link_mode = $json_content_decoded->linkMode; } } // PARENT //if ( $zp_collection ) // This was setting the parent of attachments to the collection //{ // $parent = $zp_collection; //} //else // Regular //{ foreach ($entry->getElementsByTagName("link") as $entry_link) { if ($entry_link->getAttribute('rel') == "up") { $temp = explode("items/", $entry_link->getAttribute('href')); $temp = explode("?", $temp[1]); $parent = $temp[0]; } // Get download URL if ($link_mode == "imported_file" && $entry_link->getAttribute('rel') == "self") { $citation_content = substr($entry_link->getAttribute('href'), 0, strpos($entry_link->getAttribute('href'), "?")); } } //} // Prep for insert into db array_push($GLOBALS['zp_session'][$api_user_id]['items']['query_params'], $zp_account[0]->api_user_id, $item_key, zp_db_prep($retrieved), zp_db_prep($json_content), zp_db_prep($author), zp_db_prep($date), zp_db_prep($year), zp_db_prep($title), $item_type, $link_mode, zp_db_prep($citation_content), zp_db_prep($zp_default_style), $numchildren, $parent); $GLOBALS['zp_session'][$api_user_id]['items']['query_total_entries']++; } // foreach entry // LAST SET if ($GLOBALS['zp_session'][$api_user_id]['items']['last_set'] == $zp_start) { return false; } else { return true; } unset($zp_import_contents); unset($zp_import_url); unset($zp_xml); unset($doc_citations); unset($entries); }