/** * Convert an XML document to a multi dimensional array * Pass in an XML document (or SimpleXMLElement object) and this recrusively loops through and builds a representative array * * @param string $xml - XML document - can optionally be a SimpleXMLElement object * @return array ARRAY */ public static function toArray($xml) { if (is_string($xml)) { $xml = new SimpleXMLElement($xml); } $children = $xml->children(); if (!$children) { return (string) $xml; } $arr = array(); foreach ($children as $key => $node) { $node = XMLHelper::toArray($node); // support for 'anon' non-associative arrays if ($key == 'anon') { $key = count($arr); } // ensures all keys are in lowercase underscore just for ease of use $key = XMLHelper::_fromCamelCase($key); // if the node is already set, put it into an array if (isset($arr[$key])) { if (!is_array($arr[$key]) || isset($arr[$key][0]) && $arr[$key][0] == null) { $arr[$key] = array($arr[$key]); } $arr[$key][] = $node; } else { $arr[$key] = $node; } } return $arr; }
public function __construct($directory) { parent::__construct($directory, XMLHelper::parse('data/' . $directory . '/data.xml')); if ($this->data == null) { ErrorHelper::logError('Failed to load game data in ' . $directory); } $this->directory = $directory; }
public static function xml2array($xmlObject, $out = array()) { foreach ((array) $xmlObject as $index => $node) { $index = XMLHelper::dashesToCamelCase($index); $out[$index] = is_object($node) || is_array($node) ? XMLHelper::xml2array($node) : trim($node); } return $out; }
public function __construct() { parent::__construct('', XMLHelper::parse('data/data.xml')); $this->isDeveloper = true; if ($this->data == null) { ErrorHelper::logError('Failed to load developer data'); return; } $gamedirs = FileHelper::getGames('data'); foreach ($gamedirs as $gamedir) { $this->games[$gamedir] = new Game($gamedir); } // sorts the games according to the sort_order property // if no value is set, zero is assumed uasort($this->games, 'Developer::_sort'); }
public static function getData($game) { $data = LoadHelper::loadCached('https://promoterapp.com/dopresskit/' . $game->promoter['product'], PROMOTER_CACHE_DURATION); if ($data == null) { return; } $promoterxml = simplexml_load_string($data); $promoter = XMLHelper::xml2array($promoterxml); XMLHelper::collapse($promoter, 'reviews', 'review'); if (!isset($promoter['reviews'])) { return; } if (PROMOTER_OVERWRITE || !isset($game->quotes)) { $game->quotes = array(); } foreach ($promoter['reviews'] as $review) { $game->quotes[] = array('link' => $review['url'], 'description' => $review['quote'], 'name' => $review['reviewerName'], 'website' => $review['publicationName']); } }
/** * @see Filter::process() * @param $citationString string * @return MetadataDescription */ function &process($citationString) { $nullVar = null; // Check the availability of perl $perlCommand = Config::getVar('cli', 'perl'); if (empty($perlCommand) || !file_exists($perlCommand)) { return $nullVar; } // Convert to ASCII - Paracite doesn't handle UTF-8 well $citationString = String::utf8_to_ascii($citationString); // Call the paracite parser $wrapperScript = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'paracite.pl'; $paraciteCommand = $perlCommand . ' ' . escapeshellarg($wrapperScript) . ' ' . $this->getCitationModule() . ' ' . escapeshellarg($citationString); $xmlResult = shell_exec($paraciteCommand); if (empty($xmlResult)) { return $nullVar; } if (Config::getVar('i18n', 'charset_normalization') == 'On' && !String::utf8_compliant($xmlResult)) { $xmlResult = String::utf8_normalize($xmlResult); } // Create a temporary DOM document $resultDOM = new DOMDocument(); $resultDOM->recover = true; $resultDOM->loadXML($xmlResult); // Extract the parser results as an array $xmlHelper = new XMLHelper(); $metadata = $xmlHelper->xmlToArray($resultDOM->documentElement); // We have to merge subtitle and title as neither OpenURL // nor NLM can handle subtitles. if (isset($metadata['subtitle'])) { $metadata['title'] .= '. ' . $metadata['subtitle']; unset($metadata['subtitle']); } // Break up the authors field if (isset($metadata['authors'])) { $metadata['authors'] = String::trimPunctuation($metadata['authors']); $metadata['authors'] = String::iterativeExplode(array(':', ';'), $metadata['authors']); } // Convert pages to integers foreach (array('spage', 'epage') as $pageProperty) { if (isset($metadata[$pageProperty])) { $metadata[$pageProperty] = (int) $metadata[$pageProperty]; } } // Convert titles to title case foreach (array('title', 'chapter', 'publication') as $titleProperty) { if (isset($metadata[$titleProperty])) { $metadata[$titleProperty] = String::titleCase($metadata[$titleProperty]); } } // Map ParaCite results to OpenURL - null means // throw the value away. $metadataMapping = array('genre' => 'genre', '_class' => null, 'any' => null, 'authors' => 'au', 'aufirst' => 'aufirst', 'aufull' => null, 'auinit' => 'auinit', 'aulast' => 'aulast', 'atitle' => 'atitle', 'cappublication' => null, 'captitle' => null, 'date' => 'date', 'epage' => 'epage', 'featureID' => null, 'id' => null, 'issue' => 'issue', 'jnl_epos' => null, 'jnl_spos' => null, 'match' => null, 'marked' => null, 'num_of_fig' => null, 'pages' => 'pages', 'publisher' => 'pub', 'publoc' => 'place', 'ref' => null, 'rest_text' => null, 'spage' => 'spage', 'targetURL' => 'url', 'text' => null, 'ucpublication' => null, 'uctitle' => null, 'volume' => 'volume', 'year' => 'date'); // Ignore 'year' if 'date' is set if (isset($metadata['date'])) { $metadataMapping['year'] = null; } // Set default genre if (empty($metadata['genre'])) { $metadata['genre'] = OPENURL_GENRE_ARTICLE; } // Handle title, chapter and publication depending on // the (inferred) genre. Also instantiate the target schema. switch ($metadata['genre']) { case OPENURL_GENRE_BOOK: case OPENURL_GENRE_BOOKITEM: case OPENURL_GENRE_REPORT: case OPENURL_GENRE_DOCUMENT: $metadataMapping += array('publication' => 'btitle', 'chapter' => 'atitle'); if (isset($metadata['title'])) { if (!isset($metadata['publication'])) { $metadata['publication'] = $metadata['title']; } elseif (!isset($metadata['chapter'])) { $metadata['chapter'] = $metadata['title']; } unset($metadata['title']); } $openUrlSchemaName = 'lib.pkp.classes.metadata.openurl.OpenUrlBookSchema'; $openUrlSchemaClass = 'OpenUrlBookSchema'; break; case OPENURL_GENRE_ARTICLE: case OPENURL_GENRE_JOURNAL: case OPENURL_GENRE_ISSUE: case OPENURL_GENRE_CONFERENCE: case OPENURL_GENRE_PROCEEDING: case OPENURL_GENRE_PREPRINT: default: $metadataMapping += array('publication' => 'jtitle'); if (isset($metadata['title'])) { if (!isset($metadata['publication'])) { $metadata['publication'] = $metadata['title']; } elseif (!isset($metadata['atitle'])) { $metadata['atitle'] = $metadata['title']; } unset($metadata['title']); } $openUrlSchemaName = 'lib.pkp.classes.metadata.openurl.OpenUrlJournalSchema'; $openUrlSchemaClass = 'OpenUrlJournalSchema'; break; } // Instantiate an OpenURL description $openUrlDescription = new MetadataDescription($openUrlSchemaName, ASSOC_TYPE_CITATION); $openUrlSchema = new $openUrlSchemaClass(); // Map the ParaCite result to OpenURL foreach ($metadata as $paraciteElementName => $paraciteValue) { if (!empty($paraciteValue)) { // Trim punctuation if (is_string($paraciteValue)) { $paraciteValue = String::trimPunctuation($paraciteValue); } // Transfer the value to the OpenURL result array assert(array_key_exists($paraciteElementName, $metadataMapping)); $openUrlPropertyName = $metadataMapping[$paraciteElementName]; if (!is_null($openUrlPropertyName) && $openUrlSchema->hasProperty($openUrlPropertyName)) { if (is_array($paraciteValue)) { foreach ($paraciteValue as $singleValue) { $success = $openUrlDescription->addStatement($openUrlPropertyName, $singleValue); assert($success); } } else { $success = $openUrlDescription->addStatement($openUrlPropertyName, $paraciteValue); assert($success); } } } } // Crosswalk to NLM $crosswalkFilter = new OpenUrlNlmCitationSchemaCrosswalkFilter(); $nlmDescription =& $crosswalkFilter->execute($openUrlDescription); assert(is_a($nlmDescription, 'MetadataDescription')); // Add 'rest_text' as NLM comment (if given) if (isset($metadata['rest_text'])) { $nlmDescription->addStatement('comment', String::trimPunctuation($metadata['rest_text'])); } // Set display name and sequence id in the meta-data description // to the corresponding values from the filter. This is important // so that we later know which result came from which filter. $nlmDescription->setDisplayName($this->getDisplayName()); $nlmDescription->setSeq($this->getSeq()); return $nlmDescription; }
/** * Takes the raw xml result of a web service and * transforms it via XSL to a (preliminary) XML similar * to NLM which is then re-encoded into an array. Finally * some typical post-processing is performed. * FIXME: Rewrite parser/lookup filter XSL to produce real NLM * element-citation XML and factor this code into an NLM XML to * NLM description filter. * @param $xmlResult string or DOMDocument * @param $xslFileName string * @return array a metadata array */ function &transformWebServiceResults(&$xmlResult, $xslFileName) { // Send the result through the XSL to generate a (preliminary) NLM XML. $xslFilter = new XSLTransformationFilter(PersistableFilter::tempGroup('xml::*', 'xml::*'), 'Web Service Transformation'); $xslFilter->setXSLFilename($xslFileName); $xslFilter->setResultType(XSL_TRANSFORMER_DOCTYPE_DOM); $preliminaryNlm30DOM =& $xslFilter->execute($xmlResult); if (is_null($preliminaryNlm30DOM) || is_null($preliminaryNlm30DOM->documentElement)) { $translationParams = array('filterName' => $this->getDisplayName()); $this->addError(__('submission.citations.filter.webserviceResultTransformationError', $translationParams)); $nullVar = null; return $nullVar; } // Transform the result to an array. $xmlHelper = new XMLHelper(); $preliminaryNlm30Array = $xmlHelper->xmlToArray($preliminaryNlm30DOM->documentElement); $preliminaryNlm30Array =& $this->postProcessMetadataArray($preliminaryNlm30Array); return $preliminaryNlm30Array; }
/** * Takes the raw xml result of a web service and * transforms it via XSL to a (preliminary) XML similar * to NLM which is then re-encoded into an array. Finally * some typical post-processing is performed. * FIXME: Rewrite parser/lookup filter XSL to produce real NLM * element-citation XML and factor this code into an NLM XML to * NLM description filter. * @param $xmlResult string or DOMDocument * @param $xslFileName string * @return array a metadata array */ function &transformWebServiceResults(&$xmlResult, $xslFileName) { // Send the result through the XSL to generate a (preliminary) NLM XML. $xslFilter = new XSLTransformationFilter(); $xslFilter->setXSLFilename($xslFileName); $xslFilter->setResultType(XSL_TRANSFORMER_DOCTYPE_DOM); $preliminaryNlmDOM =& $xslFilter->execute($xmlResult); if (is_null($preliminaryNlmDOM)) { return $preliminaryNlmDOM; } // Transform the result to an array. $xmlHelper = new XMLHelper(); $preliminaryNlmArray = $xmlHelper->xmlToArray($preliminaryNlmDOM->documentElement); $preliminaryNlmArray =& $this->postProcessMetadataArray($preliminaryNlmArray); return $preliminaryNlmArray; }
public static function sendResponse($status = 200, $body = '', $content_type = 'json', $apiMode = true, $json_key = null) { $messages = RestUtils::getStatusCodeMessage($status); // Set the status $status_header = 'HTTP/1.1 ' . $status . ' ' . $messages['official']; header($status_header); // We need to create the body if none is passed (== error) if ($body === '' || $body === null) { $body = array("code" => $status, "message" => $messages['message']); } // And we can send the response if ($content_type == 'json') { /* ******************************* */ // JSON // /* ******************************* */ if ($apiMode == true) { // Now we add some metadata $response = array("provider" => "tuneefy", "api" => true, "version" => "0.9b", "status" => $status == 200 ? "splendid" : "bloody hell", "data" => $body); } else { $response = array("json_key" => $json_key, "data" => $body); } // Sets the content type header("Content-type: application/json; charset=UTF-8 "); echo json_encode($response); } else { if ($content_type == 'xml') { /* ******************************* */ // XML // /* ******************************* */ // Sets the content type header("Content-type: text/xml; charset=UTF-8 "); $xml = new XMLHelper(); // Adding the metadata $xml->push('response', array("provider" => "tuneefy", "api" => true, "version" => "0.9b", "status" => $status == 200 ? "splendid" : "bloody hell")); $xml->push('data'); function recursive_push($arrResult, $xml) { while (list($key, $value) = each($arrResult)) { if (is_array($value)) { if (is_numeric($key)) { // for elements that are integer keys, like js arrays $xml->push("item", array("rank" => $key)); } else { $xml->push($key); } recursive_push($value, $xml); $xml->pop(); } else { for ($i = 0; $i < count($value); $i++) { if (is_numeric($key)) { $xml->element("item", $value, array("id" => $key)); } else { $xml->element($key, $value); } } } } } recursive_push($body, $xml); $xml->pop(); // data $xml->pop(); // response echo utf8_encode($xml->getXml()); } } exit; }