Esempio n. 1
0
 protected function parseUserValue($value)
 {
     $value = trim($value);
     if ($value == '') {
         //do not accept empty strings
         $this->addError(wfMsgForContent('smw_emptystring'));
     } else {
         $parser = new ChemEqParser();
         if ($parser->checkFormula($value) === true) {
             $this->m_xsdValue = smwfXMLContentEncode($parser->getWikiFormat());
             $this->m_html = $parser->getHtmlFormat();
             if ($this->m_caption === false) {
                 $this->m_caption = $this->m_html;
             }
             $this->m_wikitext = $value;
         } else {
             $this->addError($value . ":" . $parser->getError() . "<br>");
         }
         if ($this->m_caption === false) {
             $this->m_caption = $value;
         }
     }
     return true;
 }
Esempio n. 2
0
 /**
  * Checks a chemical equation for syntactical correctness.
  * 
  * Grammar for chemical equations:
  * CE := CF [{'+'|'*'|'x'} CF]* [{'<-' | '<->' | '->' } CF [{'+'|'*'|'x'} CF]*]+
  * CF is a chemical formula (see checkFormula())
  *
  * The fields <mError>, <mWikiFormat> and <mHtmlFormat> will be set. 
  *  
  * @param string $eq Wiki text of a chemical equation
  * @return boolean <true> if the expression is a valid chemical equation,
  *                 <false> otherwise
  */
 public function checkEquation($eq)
 {
     $eq = trim(htmlspecialchars_decode($eq));
     $formulaParser = new ChemEqParser();
     try {
         $result = $this->splitEquation($eq);
         $wikiFormat = "";
         $htmlFormat = "";
         $delimCount = 0;
         $delimiters = array('<-', '<->', '->', '+', '*', 'x');
         foreach ($result as $term) {
             $formulas = $this->splitChemTerm($term);
             foreach ($formulas as $formula) {
                 if (in_array($formula, $delimiters)) {
                     $delimCount--;
                 }
                 switch ($formula) {
                     case "<-":
                         $htmlFormat .= ' ← ';
                         $wikiFormat .= $formula;
                         break;
                     case "->":
                         $htmlFormat .= ' → ';
                         $wikiFormat .= $formula;
                         break;
                     case "<->":
                         $htmlFormat .= ' ⇌ ';
                         $wikiFormat .= $formula;
                         break;
                     case "+":
                         $htmlFormat .= ' + ';
                         $wikiFormat .= $formula;
                         break;
                     case "*":
                     case "x":
                         $htmlFormat .= '●';
                         $wikiFormat .= "*";
                         break;
                     default:
                         if ($formulaParser->checkFormula($formula) === true) {
                             $delimCount++;
                             $wikiFormat .= $formulaParser->getWikiFormat();
                             $htmlFormat .= $formulaParser->getHtmlFormat();
                         } else {
                             throw new Exception($formulaParser->getError());
                         }
                 }
                 if ($delimCount < 0 || $delimCount > 1) {
                     throw new Exception(wfMsgForContent('smw_no_alternating_formula', $eq));
                 }
             }
         }
         if ($delimCount != 1) {
             // In the end, there must be one more formula than operators
             throw new Exception(wfMsgForContent('smw_no_alternating_formula', $eq));
         }
         $this->mWikiFormat = $wikiFormat;
         $this->mHtmlFormat = $htmlFormat;
         $this->mError = "";
         return true;
     } catch (Exception $e) {
         $this->mWikiFormat = "";
         $this->mHtmlFormat = "";
         $this->mError = $e->getMessage();
         return false;
     }
 }
 /**
  * Encapsulates an annotation as XML.
  *
  * @param $instance
  * @param $annotation
  * @param $smwValues
  *
  * @return XML string (fragment)
  */
 private static function encapsulateAsAnnotation(Title $instance, Title $annotationTitle, $smwValues)
 {
     $id = uniqid(rand());
     $count = 0;
     $singleProperties = "";
     $multiProperties = "";
     $isFormula = false;
     $chemistryParser = new ChemEqParser();
     $gi_store = SGAGardeningIssuesAccess::getGardeningIssuesAccess();
     foreach ($smwValues as $smwValue) {
         //list($smwValue, $uri) = $tuple;
         if ($smwValue instanceof SMWRecordValue) {
             // n-ary property
             $needRepaste = false;
             $parameters = "";
             foreach ($smwValue->getDVs() as $params) {
                 if ($params == NULL) {
                     $parameters .= "<param></param>";
                     continue;
                 }
                 if ($params->getTypeID() == '_che') {
                     $isFormula = true;
                     $chemistryParser->checkEquation(array_shift($params->getDBkeys()));
                     $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                     $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                 } else {
                     if ($params->getTypeID() == '_chf') {
                         $isFormula = true;
                         $chemistryParser->checkFormula(array_shift($params->getDBkeys()));
                         $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                         $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                     } else {
                         // escape potential HTML in a CDATA section
                         $value = "<![CDATA[" . html_entity_decode(array_shift($params->getDBkeys())) . " " . html_entity_decode($params->getUnit()) . "]]>";
                     }
                 }
                 // check if re-paste is needed
                 $needRepaste |= html_entity_decode(array_shift($params->getDBkeys())) != array_shift($params->getDBkeys()) || $params->getUnit() != '';
                 // check if target is a wikipage and built param
                 $isLink = $params instanceof SMWWikiPageValue ? "isLink=\"true\"" : "";
                 $parameters .= "<param {$isLink}>{$value}</param>";
             }
             $repasteMarker = $isFormula || $needRepaste ? "needRepaste=\"true\"" : "";
             $title = htmlspecialchars($annotationTitle->getDBkey());
             $titleURLEscaped = htmlspecialchars(self::urlescape($annotationTitle->getDBkey()));
             $issues = $gi_store->getGardeningIssuesForPairs('smw_consistencybot', array(SMW_GARDISSUE_WRONG_DOMAIN_VALUE, SMW_GARDISSUE_TOO_LOW_CARD, SMW_GARDISSUE_TOO_HIGH_CARD, SMW_GARD_ISSUE_MISSING_PARAM, SMW_GARDISSUE_WRONG_TARGET_VALUE), NULL, array($instance, $annotationTitle));
             $gi_issues = SMWOntologyBrowserErrorHighlighting::getAnnotationIssuesAsXML($issues, $smwValue);
             // no metadata available on n-ary properties
             $multiProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_{$id}{$count}\" {$repasteMarker}>" . $parameters . "{$gi_issues}</annotation>";
         } else {
             if ($smwValue instanceof SMWWikiPageValue || $smwValue->getTypeID() == '_uri') {
                 // relation
                 $title = htmlspecialchars($annotationTitle->getDBkey());
                 $titleURLEscaped = htmlspecialchars(self::urlescape($annotationTitle->getDBkey()));
                 $issues = $gi_store->getGardeningIssuesForPairs('smw_consistencybot', array(SMW_GARDISSUE_WRONG_DOMAIN_VALUE, SMW_GARDISSUE_TOO_LOW_CARD, SMW_GARDISSUE_TOO_HIGH_CARD, SMW_GARDISSUE_WRONG_TARGET_VALUE), NULL, array($instance, $annotationTitle));
                 $gi_issues = SMWOntologyBrowserErrorHighlighting::getAnnotationIssuesAsXML($issues, $smwValue);
                 // metadata
                 // check if metadata patch is applied
                 $metadataTags = "<metadata id=\"" . $id . "_meta_" . $count . "\">";
                 if (method_exists($smwValue, "getMetadataMap")) {
                     // read metadata
                     foreach ($smwValue->getMetadataMap() as $mdProperty => $mdValues) {
                         foreach ($mdValues as $mdValue) {
                             $metadataTags .= "<property name=\"" . htmlspecialchars($mdProperty) . "\">" . htmlspecialchars($mdValue) . "</property>";
                         }
                     }
                 }
                 $metadataTags .= "</metadata>";
                 if ($smwValue instanceof SMWWikiPageValue && !is_null($smwValue->getTitle())) {
                     $targetNotExists = $smwValue->getTitle()->exists() ? "" : "notexists=\"true\"";
                     $singleProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_{$id}{$count}\">" . "<param isLink=\"true\" {$targetNotExists}><![CDATA[" . $smwValue->getTitle()->getPrefixedDBkey() . "]]></param>" . $gi_issues . $metadataTags . "</annotation>";
                 } else {
                     if ($smwValue->getTypeID() == '_uri') {
                         // any URI
                         $uri = $smwValue->getWikiValue();
                         if (strpos($uri, "#") !== false) {
                             $local = substr($uri, strpos($uri, "#") + 1);
                         } else {
                             if (strrpos($uri, "/") !== false) {
                                 $local = substr($uri, strrpos($uri, "/") + 1);
                             } else {
                                 $local = $uri;
                             }
                         }
                         $uri_att = 'uri="' . htmlspecialchars($uri) . '"';
                         $singleProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_{$id}{$count}\">" . "<param isLink=\"true\" {$uri_att}><![CDATA[" . $local . "]]></param>" . $metadataTags . "</annotation>";
                     }
                 }
             } else {
                 if ($smwValue != NULL) {
                     // normal attribute
                     if ($smwValue->getTypeID() == '_che') {
                         $isFormula = true;
                         $chemistryParser->checkEquation(array_shift($smwValue->getDBkeys()));
                         $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                         $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                     }
                     if ($smwValue->getTypeID() == '_che') {
                         $isFormula = true;
                         $chemistryParser->checkEquation(array_shift($smwValue->getDBkeys()));
                         $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                         $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                     } else {
                         if ($smwValue->getTypeID() == '_chf') {
                             $isFormula = true;
                             $chemistryParser->checkFormula(array_shift($smwValue->getDBkey()));
                             $formulaAsHTML = html_entity_decode($chemistryParser->getHtmlFormat());
                             $value = "<![CDATA[" . $formulaAsHTML . "]]>";
                         } else {
                             // escape potential HTML in a CDATA section
                             if ($smwValue->getTypeID() == '__typ') {
                                 //SMW_DV_Types
                                 $value = implode(",", $smwValue->getTypeLabels());
                                 $value = strip_tags($value, "<sub><sup><b><i>");
                                 $value = "<![CDATA[" . html_entity_decode($value) . " " . $smwValue->getUnit() . "]]>";
                             }
                             if ($smwValue->getTypeID() == '__tls') {
                                 // SMW_DV_TypeList
                                 $value = "<![CDATA[" . html_entity_decode($smwValue->getWikiValue()) . "]]>";
                             } else {
                                 // small hack for datetime type. It may occur that there is a T at the end.
                                 if ($smwValue->getTypeID() == '_dat') {
                                     $val = array_shift($smwValue->getDBkeys());
                                     $xsdValue = substr($val, -1) == 'T' ? str_replace('T', '', $val) : $val;
                                 } else {
                                     $xsdValue = array_shift($smwValue->getDBkeys());
                                 }
                                 $value = strip_tags($xsdValue, "<sub><sup><b><i>");
                                 $value = "<![CDATA[" . html_entity_decode($value) . " " . $smwValue->getUnit() . "]]>";
                             }
                         }
                     }
                     //special attribute mark for all things needed to get re-pasted in FF.
                     $repasteMarker = $isFormula || strip_tags(array_shift($smwValue->getDBkeys())) != array_shift($smwValue->getDBkeys()) || $smwValue->getUnit() != '' ? "needRepaste=\"true\"" : "";
                     $title = htmlspecialchars($annotationTitle->getDBkey());
                     $titleURLEscaped = htmlspecialchars(self::urlescape($annotationTitle->getDBkey()));
                     $issues = $gi_store->getGardeningIssuesForPairs('smw_consistencybot', array(SMW_GARDISSUE_WRONG_DOMAIN_VALUE, SMW_GARDISSUE_TOO_LOW_CARD, SMW_GARDISSUE_TOO_HIGH_CARD, SMW_GARDISSUE_WRONG_UNIT), NULL, array($instance, $annotationTitle));
                     // gardening issues
                     $gi_issues = SMWOntologyBrowserErrorHighlighting::getAnnotationIssuesAsXML($issues, $smwValue);
                     // metadata
                     // check if metadata patch is applied
                     $metadataTags = "<metadata id=\"" . $id . "_meta_" . $count . "\">";
                     if (method_exists($smwValue, "getMetadataMap")) {
                         // read metadata
                         foreach ($smwValue->getMetadataMap() as $mdProperty => $mdValues) {
                             foreach ($mdValues as $mdValue) {
                                 $metadataTags .= "<property name=\"" . htmlspecialchars($mdProperty) . "\">" . htmlspecialchars($mdValue) . "</property>";
                             }
                         }
                     }
                     $metadataTags .= "</metadata>";
                     $singleProperties .= "<annotation  title_url=\"{$titleURLEscaped}\" title=\"" . $title . "\" id=\"ID_" . $id . $count . "\" {$repasteMarker}>" . "<param>" . $value . "</param>" . $gi_issues . $metadataTags . "</annotation>";
                 }
             }
         }
         $count++;
     }
     return $singleProperties . $multiProperties;
 }