public function parse(Node $node) { foreach ($node->getChildren() as $child) { if ($child instanceof TextNode) { $result = $this->enumerationDataType->parse($child->getText()); } else { $result = $this->parse($child); } if ($result !== null) { return $result; } } return null; }
public function parse(Node $node) { if ($node instanceof PropertyNode) { $children = $node->getChildren('LinkNode'); foreach ($children as $child) { if (!$child->isExternalLink()) { $link = $child->getDestination(); if ($link) { return OntologyNamespaces::getUri($link->encoded(), OntologyNamespaces::DBPEDIA_INSTANCE_NAMESPACE); } } } } return null; }
public function parse(Node $node) { $children = $node->getChildren(); foreach ($children as $child) { if ($child instanceof TextNode) { $text = $child->getText(); if (preg_match("~no~", $text) || preg_match("~false~", $text)) { return "false"; } else { if (preg_match("~yes~", $text) || preg_match("~true~", $text)) { return "true"; } } } } return null; }
/** * Parses integer or double values in a Node. * If the indicated data type is found, the value is returned. * * @param Node $node * @return string */ public function parse(Node $node) { $children = $node->getChildren(); foreach ($children as $child) { if ($child instanceof TextNode) { $input = $child->getText(); self::catchLargeNumbers($input, $this->language); if ($this->integer) { $output = self::parseIntegerValue($input); if ($output != "") { return $output; } } if ($this->double) { $output = self::parseFloatValue($input); if ($output != "") { return $output; } } } } }
private static function parseDegrees(Node $node) { $result = null; foreach ($node->getChildren() as $child) { if ($child instanceof TextNode) { if (preg_match('~(-?[.0-9]{1,12})~', $child->getText(), $matches)) { $result = $matches[1]; } } else { $result = self::parseDegrees($child); } if ($result !== null) { return $result; } } }
/** * This Method parse property templates like {{convert|...} * * @param Node $node * @return array */ private function catchTemplates($node) { if ($node instanceof TemplateNode) { $children = $node->getChildren(); $templateName = $node->getTitle()->decoded(); $value = null; $unit = null; foreach ($children as $childNode) { // creates an array of TextNodes from the PropertyNodes of the TemplateNode $childrenChilds[] = $childNode->getChildren('TextNode'); } /////////////////////////////////////////////////////////////////////////////////////// // Start of template parsing /////////////////////////////////////////////////////////////////////////////////////// // How to: // There are two cases how templates are build // - only values // {{convert|original_value|original_unit|conversion_unit|round_to|...}} // - key and value as a pair connected by "=" // {{height|first_unit=first_value|second_unit=second_value|...}} // The first value after "{{" is the templateName and every "|" will result in a new // PropertyNode of the TemplateNode. The $childrenChilds[][] array contains the // TextNodes of these children. // With $childrenChilds[0][0]->getText() you get the text from the first TextNode of // the first PropertyNode. For example: // {{convert|ORIGINAL_VALUE|original_unit|conversion_unit|round_to|...}} or // {{height|first_unit=FIRST_VALUE|second_unit=second_value|...}} // With $childrenChilds[1][0]->getText() you get the text from the first TextNode // of the second PropertyNode. // With $childrenChilds[0][0]->getParent()->getKey() you get the key of the first // PropertyNode. For example: // {{height|FIRST_UNIT=first_value|second_unit=second_value|...}} // The first case (convert template example) has no key. /////////////////////////////////////////////////////////////////////////////////////// // http://en.wikipedia.org/wiki/Template:Convert // http://it.wikipedia.org/wiki/Template:Converti // {{convert|original_value|original_unit|conversion_unit|round_to|...}} if ($templateName == 'Convert' || $templateName == 'Converti') { $value = self::catchValue($childrenChilds[0][0]->getText()); $unit = self::catchUnit($childrenChilds[1][0]->getText()); } elseif ($templateName == 'Height') { $value = self::catchValue($childrenChilds[0][0]->getText()); $unit = self::catchUnit($childrenChilds[0][0]->getParent()->getKey()); // If the TemplateNode has a second PropertyNode ... if (isset($childrenChilds[1][0])) { $secondUnit = self::catchUnit($childrenChilds[1][0]->getParent()->getKey()); // If the height template contains foot and inch they will converted into centimetres. if ($unit == 'ft' && $secondUnit == 'in') { $secondValue = self::catchValue($childrenChilds[1][0]->getText()); $ftToCm = $value * 30.48; $inToCm = $secondValue * 2.54; $value = $ftToCm + $inToCm; $unit = 'centimetre'; } } } elseif ($templateName == 'Auto in') { $value = self::catchValue($childrenChilds[0][0]->getText()); $unit = "inch"; } elseif ($templateName == 'Km to mi') { $value = self::catchValue($childrenChilds[0][0]->getText()); $unit = "kilometre"; } elseif ($templateName == 'Km2 to mi2') { $value = self::catchValue($childrenChilds[0][0]->getText()); $unit = "square kilometre"; } elseif ($templateName == 'Pop density km2 to mi2' || $templateName == 'Pd km2 to mi2') { $value = self::catchValue($childrenChilds[0][0]->getText()); $unit = "inhabitants per square kilometre"; } else { $this->logger->debug("Template not found: \"" . $templateName . "\". " . PHP_EOL . "Property: " . $this->node->getKey() . PHP_EOL . "Source: " . $this->node->getSourceUri()); return null; } // If there is a mapping but the parsing falied -> return null and log it if ($value === null || $unit === null) { $this->logger->debug("Template parsing failed: \"" . $templateName . "\". " . PHP_EOL . "NodeToString:" . self::nodeToString($node) . PHP_EOL . "Property: " . $this->node->getKey() . PHP_EOL . "Source: " . $this->node->getSourceUri()); return null; } else { return self::outputGenerator($value, $unit); } } else { foreach ($node->getChildren() as $child) { $result = $this->catchTemplates($child); if ($result != null) { return $result; } } return null; } }
/** * * @param $node * @return string */ public static function nodeToString(Node $node) { $string = ''; if ($node instanceof TextNode) { $string .= $node->getText() . " "; } elseif ($node instanceof PropertyNode || $node instanceof TemplateNode || $node instanceof LinkNode) { $children = $node->getChildren(); foreach ($children as $childNode) { $string .= self::nodeToString($childNode); } } return $string; }