예제 #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;
 }
예제 #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;
     }
 }