/** */ public function testTiming() { $t1 = microtime(true); $this->statsd->timing("test.timing", microtime(true) - $t1); $this->statsd->timing("test.timing", microtime(true) - $t1); $this->statsd->timing("test.timing", microtime(true) - $t1); }
public static function multiResponse($options, $overrideFormat = false) { $format = $overrideFormat ? $overrideFormat : $options['requestParams']['format']; if (empty($options['results'])) { $options['results'] = ['results' => [], 'total' => 0]; } if ($options['results'] && isset($options['results']['results'])) { $totalResults = $options['results']['total']; $options['results'] = $options['results']['results']; if ($options['requestParams']['v'] >= 3) { header("Total-Results: {$totalResults}"); } } switch ($format) { case 'atom': case 'csljson': case 'json': case 'keys': case 'versions': $link = Zotero_API::buildLinkHeader($options['action'], $options['uri'], $totalResults, $options['requestParams']); if ($link) { header($link); } break; } if (!empty($options['head'])) { return; } switch ($format) { case 'atom': $t = microtime(true); $response = Zotero_Atom::createAtomFeed($options['action'], $options['title'], $options['uri'], $options['results'], $totalResults, $options['requestParams'], $options['permissions'], isset($options['fixedValues']) ? $options['fixedValues'] : null); StatsD::timing("api." . $options['action'] . ".multiple.createAtomFeed." . implode("-", $options['requestParams']['content']), (microtime(true) - $t) * 1000); return $response; case 'csljson': $json = Zotero_Cite::getJSONFromItems($options['results'], true); echo Zotero_Utilities::formatJSON($json); break; case 'json': echo Zotero_API::createJSONResponse($options['results'], $options['requestParams'], $options['permissions']); break; case 'keys': echo implode("\n", $options['results']) . "\n"; break; case 'versions': if (!empty($options['results'])) { echo Zotero_Utilities::formatJSON($options['results']); } else { echo Zotero_Utilities::formatJSON(new stdClass()); } break; case 'writereport': echo Zotero_Utilities::formatJSON($options['results']); break; default: throw new Exception("Unexpected format '" . $options['requestParams']['format'] . "'"); } }
/** * Converts a Zotero_Item object to a SimpleXMLElement Atom object * * Note: Increment Z_CONFIG::$CACHE_VERSION_ATOM_ENTRY when changing * the response. * * @param object $item Zotero_Item object * @param string $content * @return SimpleXMLElement Item data as SimpleXML element */ public static function convertItemToAtom(Zotero_Item $item, $queryParams, $permissions, $sharedData = null) { $t = microtime(true); // Uncached stuff or parts of the cache key $version = $item->version; $parent = $item->getSource(); $isRegularItem = !$parent && $item->isRegularItem(); $downloadDetails = $permissions->canAccess($item->libraryID, 'files') ? Zotero_Storage::getDownloadDetails($item) : false; if ($isRegularItem) { $numChildren = $permissions->canAccess($item->libraryID, 'notes') ? $item->numChildren() : $item->numAttachments(); } // <id> changes based on group visibility in v1 if ($queryParams['v'] < 2) { $id = Zotero_URI::getItemURI($item, false, true); } else { $id = Zotero_URI::getItemURI($item); } $libraryType = Zotero_Libraries::getType($item->libraryID); // Any query parameters that have an effect on the output // need to be added here $allowedParams = array('content', 'style', 'css', 'linkwrap'); $cachedParams = Z_Array::filterKeys($queryParams, $allowedParams); $cacheVersion = 2; $cacheKey = "atomEntry_" . $item->libraryID . "/" . $item->id . "_" . md5($version . json_encode($cachedParams) . ($downloadDetails ? 'hasFile' : '') . ($libraryType == 'group' ? 'id' . $id : '')) . "_" . $queryParams['v'] . "_" . $cacheVersion . (isset(Z_CONFIG::$CACHE_VERSION_ATOM_ENTRY) ? "_" . Z_CONFIG::$CACHE_VERSION_ATOM_ENTRY : "") . (in_array('bib', $queryParams['content']) && isset(Z_CONFIG::$CACHE_VERSION_BIB) ? "_" . Z_CONFIG::$CACHE_VERSION_BIB : ""); $xmlstr = Z_Core::$MC->get($cacheKey); if ($xmlstr) { try { // TEMP: Strip control characters $xmlstr = Zotero_Utilities::cleanString($xmlstr, true); $doc = new DOMDocument(); $doc->loadXML($xmlstr); $xpath = new DOMXpath($doc); $xpath->registerNamespace('atom', Zotero_Atom::$nsAtom); $xpath->registerNamespace('zapi', Zotero_Atom::$nsZoteroAPI); $xpath->registerNamespace('xhtml', Zotero_Atom::$nsXHTML); // Make sure numChildren reflects the current permissions if ($isRegularItem) { $xpath->query('/atom:entry/zapi:numChildren')->item(0)->nodeValue = $numChildren; } // To prevent PHP from messing with namespace declarations, // we have to extract, remove, and then add back <content> // subelements. Otherwise the subelements become, say, // <default:span xmlns="http://www.w3.org/1999/xhtml"> instead // of just <span xmlns="http://www.w3.org/1999/xhtml">, and // xmlns:default="http://www.w3.org/1999/xhtml" gets added to // the parent <entry>. While you might reasonably think that // // echo $xml->saveXML(); // // and // // $xml = new SimpleXMLElement($xml->saveXML()); // echo $xml->saveXML(); // // would be identical, you would be wrong. $multiFormat = !!$xpath->query('/atom:entry/atom:content/zapi:subcontent')->length; $contentNodes = array(); if ($multiFormat) { $contentNodes = $xpath->query('/atom:entry/atom:content/zapi:subcontent'); } else { $contentNodes = $xpath->query('/atom:entry/atom:content'); } foreach ($contentNodes as $contentNode) { $contentParts = array(); while ($contentNode->hasChildNodes()) { $contentParts[] = $doc->saveXML($contentNode->firstChild); $contentNode->removeChild($contentNode->firstChild); } foreach ($contentParts as $part) { if (!trim($part)) { continue; } // Strip the namespace and add it back via SimpleXMLElement, // which keeps it from being changed later if (preg_match('%^<[^>]+xmlns="http://www.w3.org/1999/xhtml"%', $part)) { $part = preg_replace('%^(<[^>]+)xmlns="http://www.w3.org/1999/xhtml"%', '$1', $part); $html = new SimpleXMLElement($part); $html['xmlns'] = "http://www.w3.org/1999/xhtml"; $subNode = dom_import_simplexml($html); $importedNode = $doc->importNode($subNode, true); $contentNode->appendChild($importedNode); } else { if (preg_match('%^<[^>]+xmlns="http://zotero.org/ns/transfer"%', $part)) { $part = preg_replace('%^(<[^>]+)xmlns="http://zotero.org/ns/transfer"%', '$1', $part); $html = new SimpleXMLElement($part); $html['xmlns'] = "http://zotero.org/ns/transfer"; $subNode = dom_import_simplexml($html); $importedNode = $doc->importNode($subNode, true); $contentNode->appendChild($importedNode); } else { $docFrag = $doc->createDocumentFragment(); $docFrag->appendXML($part); $contentNode->appendChild($docFrag); } } } } $xml = simplexml_import_dom($doc); StatsD::timing("api.items.itemToAtom.cached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.items.itemToAtom.hit"); // Skip the cache every 10 times for now, to ensure cache sanity if (Z_Core::probability(10)) { $xmlstr = $xml->saveXML(); } else { return $xml; } } catch (Exception $e) { error_log($xmlstr); error_log("WARNING: " . $e); } } $content = $queryParams['content']; $contentIsHTML = sizeOf($content) == 1 && $content[0] == 'html'; $contentParamString = urlencode(implode(',', $content)); $style = $queryParams['style']; $entry = '<?xml version="1.0" encoding="UTF-8"?>' . '<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; $lastModifiedByUserID = null; switch (Zotero_Libraries::getType($item->libraryID)) { case 'group': $createdByUserID = $item->createdByUserID; // Used for zapi:lastModifiedByUser below $lastModifiedByUserID = $item->lastModifiedByUserID; 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); } $xml->id = $id; $xml->published = Zotero_Date::sqlToISO8601($item->dateAdded); $xml->updated = Zotero_Date::sqlToISO8601($item->dateModified); $link = $xml->addChild("link"); $link['rel'] = "self"; $link['type'] = "application/atom+xml"; $href = Zotero_API::getItemURI($item); if (!$contentIsHTML) { $href .= "?content={$contentParamString}"; } $link['href'] = $href; 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_API::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, true); // If appropriate permissions and the file is stored in ZFS, get file request link if ($downloadDetails) { $details = $downloadDetails; $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 (isset($details['size'])) { $link['length'] = $details['size']; } } $xml->addChild('zapi:key', $item->key, Zotero_Atom::$nsZoteroAPI); $xml->addChild('zapi:version', $item->version, Zotero_Atom::$nsZoteroAPI); if ($lastModifiedByUserID) { $xml->addChild('zapi:lastModifiedByUser', Zotero_Users::getUsername($lastModifiedByUserID), Zotero_Atom::$nsZoteroAPI); } $xml->addChild('zapi:itemType', Zotero_ItemTypes::getName($item->itemTypeID), Zotero_Atom::$nsZoteroAPI); if ($isRegularItem) { $val = $item->creatorSummary; if ($val !== '') { $xml->addChild('zapi:creatorSummary', htmlspecialchars($val), Zotero_Atom::$nsZoteroAPI); } $val = $item->getField('date', true, true, true); if ($val !== '') { if ($queryParams['v'] < 3) { $val = substr($val, 0, 4); if ($val !== '0000') { $xml->addChild('zapi:year', $val, Zotero_Atom::$nsZoteroAPI); } } else { $sqlDate = Zotero_Date::multipartToSQL($val); if (substr($sqlDate, 0, 4) !== '0000') { $xml->addChild('zapi:parsedDate', Zotero_Date::sqlToISO8601($sqlDate), Zotero_Atom::$nsZoteroAPI); } } } $xml->addChild('zapi:numChildren', $numChildren, Zotero_Atom::$nsZoteroAPI); } if ($queryParams['v'] < 3) { $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->createElementNS(Zotero_Atom::$nsXHTML, 'div'); $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, $queryParams); } $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), $queryParams); } $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') { if ($queryParams['v'] < 2) { $target->setAttributeNS(Zotero_Atom::$nsZoteroAPI, "zapi:etag", $item->etag); } $textNode = $domDoc->createTextNode($item->toJSON(false, $queryParams, true)); $target->appendChild($textNode); } else { if ($type == 'csljson') { $arr = $item->toCSLItem(); $json = Zotero_Utilities::formatJSON($arr); $textNode = $domDoc->createTextNode($json); $target->appendChild($textNode); } 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); } } } } } } } } // TEMP if ($xmlstr) { $uncached = $xml->saveXML(); if ($xmlstr != $uncached) { $uncached = str_replace('<zapi:year></zapi:year>', '<zapi:year/>', $uncached); $uncached = str_replace('<content zapi:type="none"></content>', '<content zapi:type="none"/>', $uncached); $uncached = str_replace('<zapi:subcontent zapi:type="coins" type="text/html"></zapi:subcontent>', '<zapi:subcontent zapi:type="coins" type="text/html"/>', $uncached); $uncached = str_replace('<title></title>', '<title/>', $uncached); $uncached = str_replace('<note></note>', '<note/>', $uncached); $uncached = str_replace('<path></path>', '<path/>', $uncached); $uncached = str_replace('<td></td>', '<td/>', $uncached); if ($xmlstr != $uncached) { error_log("Cached Atom item entry does not match"); error_log(" Cached: " . $xmlstr); error_log("Uncached: " . $uncached); Z_Core::$MC->set($cacheKey, $uncached, 3600); // 1 hour for now } } } else { $xmlstr = $xml->saveXML(); Z_Core::$MC->set($cacheKey, $xmlstr, 3600); // 1 hour for now StatsD::timing("api.items.itemToAtom.uncached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.items.itemToAtom.miss"); } return $xml; }
public function logTotalRequestTime() { if (!Z_CONFIG::$STATSD_ENABLED) { return; } try { if (!empty($this->objectLibraryID)) { $shardID = Zotero_Shards::getByLibraryID($this->objectLibraryID); $shardInfo = Zotero_Shards::getShardInfo($shardID); $shardHostID = (int) $shardInfo['shardHostID']; StatsD::timing("api.request.total_by_shard.{$shardHostID}", (microtime(true) - $this->startTime) * 1000, 0.25); } } catch (Exception $e) { error_log("WARNING: " . $e); } StatsD::timing("api.memcached", Z_Core::$MC->requestTime * 1000, 0.25); StatsD::timing("api.request.total", (microtime(true) - $this->startTime) * 1000, 0.25); }
public static function deleteByLibrary($libraryID) { Zotero_DB::beginTransaction(); // Delete from MySQL self::deleteByLibraryMySQL($libraryID); // Delete from Elasticsearch $type = self::getWriteType(); $libraryQuery = new \Elastica\Query\Term(); $libraryQuery->setTerm("libraryID", $libraryID); $query = new \Elastica\Query($libraryQuery); $start = microtime(true); $response = $type->deleteByQuery($query); StatsD::timing("elasticsearch.client.item_fulltext.delete_library", (microtime(true) - $start) * 1000); if ($response->hasError()) { throw new Exception($response->getError()); } Zotero_DB::commit(); }
public function toResponseJSON($requestParams = [], Zotero_Permissions $permissions, $sharedData = null) { $t = microtime(true); if (!$this->loaded['primaryData']) { $this->loadPrimaryData(); } if (!$this->loaded['itemData']) { $this->loadItemData(); } // Uncached stuff or parts of the cache key $version = $this->version; $parent = $this->getSource(); $isRegularItem = !$parent && $this->isRegularItem(); $downloadDetails = $permissions->canAccess($this->libraryID, 'files') ? Zotero_Storage::getDownloadDetails($this) : false; if ($isRegularItem) { $numChildren = $permissions->canAccess($this->libraryID, 'notes') ? $this->numChildren() : $this->numAttachments(); } $libraryType = Zotero_Libraries::getType($this->libraryID); // Any query parameters that have an effect on the output // need to be added here $allowedParams = ['include', 'style', 'css', 'linkwrap']; $cachedParams = Z_Array::filterKeys($requestParams, $allowedParams); $cacheVersion = 1; $cacheKey = "jsonEntry_" . $this->libraryID . "/" . $this->id . "_" . md5($version . json_encode($cachedParams) . ($downloadDetails ? 'hasFile' : '') . ($libraryType == 'group' ? Zotero_URI::getItemURI($this, true) : '')) . "_" . $requestParams['v'] . "_" . $cacheVersion . (isset(Z_CONFIG::$CACHE_VERSION_RESPONSE_JSON_ITEM) ? "_" . Z_CONFIG::$CACHE_VERSION_RESPONSE_JSON_ITEM : "") . (in_array('bib', $requestParams['include']) && isset(Z_CONFIG::$CACHE_VERSION_BIB) ? "_" . Z_CONFIG::$CACHE_VERSION_BIB : ""); $cached = Z_Core::$MC->get($cacheKey); if (false && $cached) { // Make sure numChildren reflects the current permissions if ($isRegularItem) { $cached['meta']->numChildren = $numChildren; } StatsD::timing("api.items.itemToResponseJSON.cached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.items.itemToResponseJSON.hit"); // Skip the cache every 10 times for now, to ensure cache sanity if (!Z_Core::probability(10)) { return $cached; } } $json = ['key' => $this->key, 'version' => $version, 'library' => Zotero_Libraries::toJSON($this->libraryID)]; $json['links'] = ['self' => ['href' => Zotero_API::getItemURI($this), 'type' => 'application/json'], 'alternate' => ['href' => Zotero_URI::getItemURI($this, true), 'type' => 'text/html']]; if ($parent) { $parentItem = Zotero_Items::get($this->libraryID, $parent); $json['links']['up'] = ['href' => Zotero_API::getItemURI($parentItem), 'type' => 'application/json']; } // If appropriate permissions and the file is stored in ZFS, get file request link if ($downloadDetails) { $details = $downloadDetails; $type = $this->attachmentMIMEType; if ($type) { $json['links']['enclosure'] = ['type' => $type]; } $json['links']['enclosure']['href'] = $details['url']; if (!empty($details['filename'])) { $json['links']['enclosure']['title'] = $details['filename']; } if (isset($details['size'])) { $json['links']['enclosure']['length'] = $details['size']; } } // 'meta' $json['meta'] = new stdClass(); if (Zotero_Libraries::getType($this->libraryID) == 'group') { $createdByUserID = $this->createdByUserID; $lastModifiedByUserID = $this->lastModifiedByUserID; if ($createdByUserID) { $json['meta']->createdByUser = Zotero_Users::toJSON($createdByUserID); } if ($lastModifiedByUserID && $lastModifiedByUserID != $createdByUserID) { $json['meta']->lastModifiedByUser = Zotero_Users::toJSON($lastModifiedByUserID); } } if ($isRegularItem) { $val = $this->getCreatorSummary(); if ($val !== '') { $json['meta']->creatorSummary = $val; } $val = $this->getField('date', true, true, true); if ($val !== '') { $sqlDate = Zotero_Date::multipartToSQL($val); if (substr($sqlDate, 0, 4) !== '0000') { $json['meta']->parsedDate = Zotero_Date::sqlToISO8601($sqlDate); } } $json['meta']->numChildren = $numChildren; } // 'include' $include = $requestParams['include']; foreach ($include as $type) { if ($type == 'html') { $json[$type] = trim($this->toHTML()); } else { if ($type == 'citation') { if (isset($sharedData[$type][$this->libraryID . "/" . $this->key])) { $html = $sharedData[$type][$this->libraryID . "/" . $this->key]; } else { if ($sharedData !== null) { //error_log("Citation not found in sharedData -- retrieving individually"); } $html = Zotero_Cite::getCitationFromCiteServer($this, $requestParams); } $json[$type] = $html; } else { if ($type == 'bib') { if (isset($sharedData[$type][$this->libraryID . "/" . $this->key])) { $html = $sharedData[$type][$this->libraryID . "/" . $this->key]; } else { if ($sharedData !== null) { //error_log("Bibliography not found in sharedData -- retrieving individually"); } $html = Zotero_Cite::getBibliographyFromCitationServer([$this], $requestParams); // Strip prolog $html = preg_replace('/^<\\?xml.+\\n/', "", $html); $html = trim($html); } $json[$type] = $html; } else { if ($type == 'data') { $json[$type] = $this->toJSON(true, $requestParams, true); } else { if ($type == 'csljson') { $json[$type] = $this->toCSLItem(); } else { if (in_array($type, Zotero_Translate::$exportFormats)) { $export = Zotero_Translate::doExport([$this], $type); $json[$type] = $export['body']; unset($export); } } } } } } } // TEMP if ($cached) { $cachedStr = Zotero_Utilities::formatJSON($cached); $uncachedStr = Zotero_Utilities::formatJSON($json); if ($cachedStr != $uncachedStr) { error_log("Cached JSON item entry does not match"); error_log(" Cached: " . $cachedStr); error_log("Uncached: " . $uncachedStr); //Z_Core::$MC->set($cacheKey, $uncached, 3600); // 1 hour for now } } else { /*Z_Core::$MC->set($cacheKey, $json, 10); StatsD::timing("api.items.itemToResponseJSON.uncached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.items.itemToResponseJSON.miss");*/ } return $json; }
public static function processUploadFromQueue($syncProcessID) { if (Z_Core::probability(30)) { $sql = "DELETE FROM syncProcesses WHERE started < (NOW() - INTERVAL 180 MINUTE)"; Zotero_DB::query($sql); } if (Z_Core::probability(30)) { $sql = "UPDATE syncUploadQueue SET started=NULL WHERE started IS NOT NULL AND errorCheck!=1 AND\n\t\t\t\t\t\tstarted < (NOW() - INTERVAL 12 MINUTE) AND finished IS NULL AND dataLength<250000"; Zotero_DB::query($sql); } if (Z_Core::probability(30)) { $sql = "UPDATE syncUploadQueue SET tries=0 WHERE started IS NULL AND\n\t\t\t\t\ttries>=5 AND finished IS NULL"; Zotero_DB::query($sql); } Zotero_DB::beginTransaction(); // Get a queued process $smallestFirst = Z_CONFIG::$SYNC_UPLOAD_SMALLEST_FIRST; $sortByQuota = !empty(Z_CONFIG::$SYNC_UPLOAD_SORT_BY_QUOTA); $sql = "SELECT syncUploadQueue.* FROM syncUploadQueue "; if ($sortByQuota) { $sql .= "LEFT JOIN storageAccounts USING (userID) "; } $sql .= "WHERE started IS NULL "; if (self::$minErrorCheckRequiredSize) { $sql .= "AND (errorCheck=2 OR dataLength<" . self::$minErrorCheckRequiredSize . ") "; } if ($smallestFirst && self::$maxSmallestSize) { $sql .= "AND dataLength<" . self::$maxSmallestSize . " "; } $sql .= "ORDER BY tries > 4, "; if ($sortByQuota) { $sql .= "quota DESC, "; } if ($smallestFirst) { //$sql .= "ROUND(dataLength / 1024 / 10), "; $sql .= "dataLength, "; } $sql .= "added LIMIT 1 FOR UPDATE"; $row = Zotero_DB::rowQuery($sql); // No pending processes if (!$row) { Zotero_DB::commit(); return 0; } $host = gethostbyname(gethostname()); $startedTimestamp = microtime(true); list($started, $startedMS) = self::getTimestampParts($startedTimestamp); $sql = "UPDATE syncUploadQueue SET started=FROM_UNIXTIME(?), processorHost=INET_ATON(?) WHERE syncUploadQueueID=?"; Zotero_DB::query($sql, array($started, $host, $row['syncUploadQueueID'])); Zotero_DB::commit(); Zotero_DB::close(); $processData = array("syncUploadQueueID" => $row['syncUploadQueueID'], "userID" => $row['userID'], "dataLength" => $row['dataLength']); Z_Core::$MC->set("syncUploadProcess_" . $syncProcessID, $processData, 86400); $error = false; $lockError = false; try { $xml = new SimpleXMLElement($row['xmldata'], LIBXML_COMPACT | LIBXML_PARSEHUGE); $timestamp = self::processUploadInternal($row['userID'], $xml, $row['syncUploadQueueID'], $syncProcessID); } catch (Exception $e) { $error = true; $code = $e->getCode(); $msg = $e->getMessage(); } Zotero_DB::beginTransaction(); // Mark upload as finished — NULL indicates success if (!$error) { $sql = "UPDATE syncUploadQueue SET finished=FROM_UNIXTIME(?) WHERE syncUploadQueueID=?"; Zotero_DB::query($sql, array($timestamp, $row['syncUploadQueueID'])); StatsD::increment("sync.process.upload.success"); StatsD::updateStats("sync.process.upload.size", $row['dataLength']); StatsD::timing("sync.process.upload.process", round((microtime(true) - $startedTimestamp) * 1000)); StatsD::timing("sync.process.upload.total", max(0, time() - strtotime($row['added'])) * 1000); try { $sql = "INSERT INTO syncUploadProcessLog\n\t\t\t\t\t\t(userID, dataLength, processorHost, processDuration, totalDuration, error)\n\t\t\t\t\t\tVALUES (?,?,INET_ATON(?),?,?,?)"; Zotero_DB::query($sql, array($row['userID'], $row['dataLength'], $host, round((double) microtime(true) - $startedTimestamp, 2), max(0, min(time() - strtotime($row['added']), 65535)), 0)); } catch (Exception $e) { Z_Core::logError($e); } try { self::processPostWriteLog($row['syncUploadQueueID'], $row['userID'], $timestamp); } catch (Exception $e) { Z_Core::logError($e); } } else { if (strpos($msg, "Lock wait timeout exceeded; try restarting transaction") !== false || strpos($msg, "Deadlock found when trying to get lock; try restarting transaction") !== false || strpos($msg, "Too many connections") !== false || strpos($msg, "Can't connect to MySQL server") !== false || strpos($msg, "Connection refused") !== false || strpos($msg, "Connection timed out") !== false || $code == Z_ERROR_LIBRARY_TIMESTAMP_ALREADY_USED || $code == Z_ERROR_SHARD_READ_ONLY || $code == Z_ERROR_SHARD_UNAVAILABLE) { Z_Core::logError($e); $sql = "UPDATE syncUploadQueue SET started=NULL, tries=tries+1 WHERE syncUploadQueueID=?"; Zotero_DB::query($sql, $row['syncUploadQueueID']); $lockError = true; StatsD::increment("sync.process.upload.errorTemporary"); } else { // As of PHP 5.3.2 we can't serialize objects containing SimpleXMLElements, // and since the stack trace includes one, we have to catch this and // manually reconstruct an exception $serialized = serialize(new Exception(iconv("utf-8", "utf-8//IGNORE", $msg), $e->getCode())); Z_Core::logError($e); $sql = "UPDATE syncUploadQueue SET finished=?, errorCode=?, errorMessage=? WHERE syncUploadQueueID=?"; Zotero_DB::query($sql, array(Zotero_DB::getTransactionTimestamp(), $e->getCode(), $serialized, $row['syncUploadQueueID'])); StatsD::increment("sync.process.upload.errorPermanent"); try { $sql = "INSERT INTO syncUploadProcessLog\n\t\t\t\t\t\t(userID, dataLength, processorHost, processDuration, totalDuration, error)\n\t\t\t\t\t\tVALUES (?,?,INET_ATON(?),?,?,?)"; Zotero_DB::query($sql, array($row['userID'], $row['dataLength'], $host, round((double) microtime(true) - $startedTimestamp, 2), max(0, min(time() - strtotime($row['added']), 65535)), 1)); } catch (Exception $e) { Z_Core::logError($e); } } } // Clear read locks $sql = "DELETE FROM syncUploadQueueLocks WHERE syncUploadQueueID=?"; Zotero_DB::query($sql, $row['syncUploadQueueID']); Zotero_DB::commit(); if ($lockError) { return -1; } else { if ($error) { return -2; } } return 1; }
public function toResponseJSON($requestParams = []) { $t = microtime(true); // Child collections and items can't be cached (easily) $numCollections = $this->numCollections(); $numItems = $this->numItems(); if (!$requestParams['uncached']) { $cacheKey = $this->getCacheKey($requestParams); $cached = Z_Core::$MC->get($cacheKey); if ($cached) { Z_Core::debug("Using cached JSON for {$this->libraryKey}"); $cached['meta']->numCollections = $numCollections; $cached['meta']->numItems = $numItems; StatsD::timing("api.collections.toResponseJSON.cached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.collections.toResponseJSON.hit"); return $cached; } } $json = ['key' => $this->key, 'version' => $this->version, 'library' => Zotero_Libraries::toJSON($this->libraryID)]; // 'links' $json['links'] = ['self' => ['href' => Zotero_API::getCollectionURI($this), 'type' => 'application/json'], 'alternate' => ['href' => Zotero_URI::getCollectionURI($this, true), 'type' => 'text/html']]; $parentID = $this->getParentID(); if ($parentID) { $parentCol = Zotero_Collections::get($this->libraryID, $parentID); $json['links']['up'] = ['href' => Zotero_API::getCollectionURI($parentCol), 'type' => "application/atom+xml"]; } // 'meta' $json['meta'] = new stdClass(); $json['meta']->numCollections = $numCollections; $json['meta']->numItems = $numItems; // 'include' $include = $requestParams['include']; foreach ($include as $type) { if ($type == 'data') { $json[$type] = $this->toJSON($requestParams); } } if (!$requestParams['uncached']) { Z_Core::$MC->set($cacheKey, $json); StatsD::timing("api.collections.toResponseJSON.uncached", (microtime(true) - $t) * 1000); StatsD::increment("memcached.collections.toResponseJSON.miss"); } return $json; }
public static function statsdTiming($stat, $time, $prefix = false, $unit = 'us') { switch ($unit) { case 's': $time = $time / 1000; break; case 'ms': $time = $time * 1; break; case 'us': $time = $time * 1000; break; case 'ns': $time = $time * 1000000; break; } $_prefix = $prefix ? $prefix : self::statsdPrefix(false, false); StatsD::timing($_prefix . $stat, $time); }
<?php $start = microtime(true); include 'index.php'; $end = microtime(true); StatsD::timing("response", round(($end - $start) * 1000, 1));
private static function makeRequest(array $queryParams, $mode, $json) { $servers = Z_CONFIG::$CITATION_SERVERS; // Try servers in a random order shuffle($servers); foreach ($servers as $server) { $url = "http://" . $server . self::buildURLPath($queryParams, $mode); $start = microtime(true); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); //error_log("curl -d " . escapeshellarg($json) . " " . escapeshellarg($url)); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:")); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_HEADER, 0); // do not return HTTP headers curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); $time = microtime(true) - $start; //error_log("Bib request took " . round($time, 3)); StatsD::timing("api.cite.{$mode}", $time * 1000); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($code == 400) { throw new Exception("Invalid style", Z_ERROR_CITESERVER_INVALID_STYLE); } if ($code == 404) { throw new Exception("Style not found", Z_ERROR_CITESERVER_INVALID_STYLE); } if ($code != 200) { error_log($code . " from citation server -- trying another " . "[URL: '{$url}'] [INPUT: '{$json}'] [RESPONSE: '{$response}']"); } // If no response, try another server if (!$response) { continue; } break; } if (!$response) { throw new Exception("Error generating {$mode}"); } $response = json_decode($response); if (!$response) { throw new Exception("Error generating {$mode} -- invalid response"); } return $response; }
function process_request(WPCOM_JSON_API_Endpoint $endpoint, $path_pieces) { $this->endpoint = $endpoint; // Process API request and time it $api_timer = microtime(true); $response = call_user_func_array(array($endpoint, 'callback'), $path_pieces); $api_timer = 1000 * (microtime(true) - $api_timer); if (defined('WPCOM_JSON_API__DEBUG') && WPCOM_JSON_API__DEBUG) { // Don't track API timings per node / DC for now, maybe in the future $statsd = new StatsD(); $statsd_prefix = 'com.wordpress.web.ALL.ALL.rest_api.method'; $statsd_name = str_replace(':', '.', $endpoint->stat); if (!$response && !is_array($response) || is_wp_error($response)) { $statsd->timing("{$statsd_prefix}.error.{$statsd_name}", $api_timer); } else { $statsd->timing("{$statsd_prefix}.ok.{$statsd_name}", $api_timer); } } return $response; }
<?php $time = microtime(true); require_once dirname(__FILE__) . '/../config/ProjectConfiguration.class.php'; $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false); //$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true); sfContext::createInstance($configuration)->dispatch(); $time = (microtime(true) - $time) * 1000; StatsD::timing("rayku.index_php", $time);