public static function display($perm, $formName, $title) { $html = ''; $sql = new SQLStore(wfGetDB(DB_SLAVE)); if ($formName == 'edit') { $titleArray = explode(':', $title); $ontAbbr = $titleArray[0]; $termID = str_replace(' ', '_', $titleArray[1]); $magic = $sql->getAnnotationMagicWords($ontAbbr); $ontology = new OntologyData($ontAbbr); $term = $ontology->parseTermByID($termID); $html .= self::printEditForm($perm, $ontAbbr, $term, $magic); wfDebugLog('OntoKiWi', 'OKW\\Display\\FormDisplayPrinter:: display: edit form'); } else { if ($formName == 'create') { $sql = new SQLStore(wfGetDB(DB_SLAVE)); $ontologies = $sql->getOntologies(); $magic = $sql->getAnnotationMagicWords(); $html .= self::printCreateForm($perm, $ontAbbr, $title, $ontologies, $magic); wfDebugLog('OntoKiWi', 'OKW\\Display\\FormDisplayPrinter:: display: create form'); } else { #TODO: Throw Exception } } return $html; }
/** * Function * * registerDatabase() * * Check and setup okw_ontology, okw_object_property and okw_annotation_property exists in MediaWiki database. */ private function registerDatabase() { $sql = new SQLStore(wfGetDB(DB_MASTER)); if (!$sql->isSetUp()) { wfDebugLog('OntoKiWi', 'Database: setting up OntoKiWi MySQL tables'); $sql->setup(); } }
public function __construct($ontAbbr) { $sql = new SQLStore(wfGetDB(DB_SLAVE)); $sqlResult = $sql->getOntologyAttributes($ontAbbr, array('end_point', 'ontology_graph_url', 'term_url_prefix', 'ontology_creation_digit')); $this->ontAbbr = $ontAbbr; $this->endpoint = $sqlResult->end_point; $this->graph = $sqlResult->ontology_graph_url; $this->prefix = $sqlResult->term_url_prefix; $this->digit = intval($sqlResult->ontology_creation_digit); $this->prefixNS = $GLOBALS['okwRDFConfig']['prefixNS']; $rdfFactory = new RDFStoreFactory(); $this->rdf = $rdfFactory->createRDFStore($sqlResult->end_point); }
protected static function inputHTML() { $sql = new SQLStore(wfGetDB(DB_SLAVE)); $ontologies = $sql->getOntologies(); $html = <<<END <form id="export-form" class="export-form" action="" method="post"> <fieldset> <legend>Export Ontology</legend> <p>Please select an ontology to export.</p> <select name="ontology"> <option disabled selected value=""> -- select an ontology -- </option> END; foreach ($ontologies as $ont) { $html .= '<option value="'; $html .= $ont['ontAbbr']; $html .= '">'; $html .= $ont['fullName'] . ' (' . $ont['ontAbbr'] . ')'; $html .= '</option>'; } $html .= <<<END </select></br> <input type="checkbox" value="1" name="downloadOntology" checked> <label for="download">Save as file</label><br/> <input type="submit" value="Export" name="exportOntology"> </fieldset> <fieldset> <legend>Export Pages as Ontology</legend> <p>Please enter the titles in the text box below to export corresponding WikiPages as ontology, one title per line.</p> <textarea name="pages" cols="40" rows="10"></textarea><br/> <input type="checkbox" value="1" name="downloadTerm" checked> <label for="download">Save as file</label><br/> <input type="submit" value="Export" name="exportTerm"> </fieldset> </form> END; return $html; }
public static function isExistTitleText($title) { if (Title::newFromText($title)->exists() && self::isValidTitleText($title)) { $title = explode(':', $title); $ontAbbr = $title[0]; $sql = new SQLStore(wfGetDB(DB_SLAVE)); if ($sql->hasOntology($ontAbbr)) { $sqlResult = $sql->getOntologyAttributes($ontAbbr, array('end_point', 'ontology_graph_url', 'term_url_prefix')); $endpoint = $sqlResult->end_point; $graph = $sqlResult->ontology_graph_url; $prefix = $sqlResult->term_url_prefix; $term = $prefix . str_replace(' ', '_', $title[1]); $rdfFactory = new RDFStoreFactory(); $rdf = $rdfFactory->createRDFStore($sqlResult->end_point); if ($rdf->existClass($graph, $term)) { return true; } } } return false; }
protected function doSave() { global $wgUser; $action = $this; $article = $this->page; $title = $article->getTitle(); $output = $action->getOutput(); $titleArray = explode(':', $title->getText()); $ontAbbr = $titleArray[0]; $termID = str_replace(' ', '_', $titleArray[1]); $update = new OntologyUpdate($ontAbbr, $termID); if ($title->exists()) { $wikiText = $article->getPage()->getContent()->getWikitextForTransclusion(); } else { $wikiText = ''; } /* if ( $title->exists() ) { if ( $this->perm >= self::ONTOLOGY_MANAGER ) { #$update->updateSubClassOf( $this->options['subclassof'] ); } else if ( $this->perm == self::ONTOLOGY_MASTER ) { $update->updateIRI( $this->options['term-iri'] ); $update->updateType( $this->options['term-type'] ); } } else { if ( $this->perm >= self::ONTOLOGY_PUBLIC ) { #$update->updateSubClassOf( $this->options['subclassof'] ); $update->updateType( $this->options['term-type'] ); } } $update->updateLabel( $this->options['term-label'] ); if ( key_exists( 'annotation-type', $this->options ) && key_exists( 'annotation-text', $this->options ) ) { $newWikiText = ActionHelper::changeOntologyAnnotation( $oldWikiText, $this->options['annotation-type'], $this->options['annotation-text'] ); } */ if ($this->perm >= self::ONTOLOGY_USER) { if ($this->perm == self::ONTOLOGY_MASTER) { if ($title->exists()) { $update->updateIRI($this->options['term-iri']); $update->updateType($this->options['term-type']); } else { $update->updateType($this->options['term-type']); } wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: %s is allowed {ontology_master}: updated restricted information about the term [[%s]]', $wgUser->getName(), $title->getText())); } $common['label'] = $this->options['term-label']; list($wikiText, $common) = CommonParser::reformatWikiText($wikiText, $common); wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: reformatted common information wikitext of the term [[%s]]', $title->getText())); $update->updateLabel($common['label']); wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: updated common information about the term [[%s]]', $title->getText())); if (array_key_exists('annotation-type', $this->options) && array_key_exists('annotation-text', $this->options)) { if (sizeof($this->options['annotation-type']) != sizeof($this->options['annotation-text'])) { #TODO: Throw Exception } $sql = new SQLStore(wfGetDB(DB_SLAVE)); $magic = $sql->getAnnotationMagicWords(); $annotations = array(); foreach ($this->options['annotation-type'] as $index => $name) { $iri = $magic[$name]['iri']; $text = $this->options['annotation-text'][$index]; $annotations[$iri][] = $text; } list($wikiText, $annotations) = AnnotationParser::reformatWikiText($wikiText, $annotations); wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: reformatted annotation wikitext of the term [[%s]]', $title->getText())); $update->updateAnnotations($annotations); wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: updated annotation of the term [[%s]]', $title->getText())); } if (array_key_exists('subclassof', $this->options)) { list($wikiText, $supClasses) = HierarchyParser::reformatWikiText($ontAbbr, $wikiText, $this->options['subclassof']); wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: reformatted hierarchy wikitext of the term [[%s]]', $title->getText())); } if (array_key_exists('axiom-type', $this->options) && array_key_exists('axiom-text', $this->options)) { if (sizeof($this->options['axiom-type']) != sizeof($this->options['axiom-text'])) { #TODO: Throw Exception } $axioms = array(); foreach ($this->options['axiom-type'] as $index => $type) { $axioms[$index]['type'] = $type; $axioms[$index]['text'] = $this->options['axiom-text'][$index]; } list($wikiText, $axioms) = AxiomParser::reformatWikiText($ontAbbr, $wikiText, $axioms); wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: reformatted axiom wikitext of the term [[%s]]', $title->getText())); } $update->updateSubClassOf($supClasses, $axioms['subclassof']); wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: updated hierarchy of the term [[%s]]', $title->getText())); #TODO: Equivalent update wfDebugLog('OntoKiWi', sprintf('OKW\\Action\\FormEditAction: updated axiom of the term [[%s]]', $title->getText())); } $editor = $this->setupEditPage($wikiText); return $this->doMWStore($output, $title, $editor); }
public static function reformatWikiText($wikiText, $validAnnotation = null) { $sql = new SQLStore(wfGetDB(DB_SLAVE)); $magic = $sql->getAnnotationMagicWords(); $dictMagicIRI = array(); foreach ($magic as $name => $value) { $dictMagicIRI[$value['iri']] = $name; } $text = ''; $output = array(); preg_match_all('/{{\\s*[#]?Annotation\\s*:[\\s]*[^|]([^}]*)}}/', $wikiText, $matches, PREG_SET_ORDER); $options = array(); $valids = array(); $invalids = array(); if (!empty($matches) || !is_null($validAnnotation)) { foreach ($matches as $match) { preg_match_all('/[\\s]*[|]([^|]*)/', $match[1], $params, PREG_PATTERN_ORDER); list($option, $valid, $invalid) = self::extractAnnotation($params[1], $magic); $options = array_merge($options, $option); $valids = array_merge($valids, $valid); $invalids = array_merge($invalids, $invalid); } if (!is_null($validAnnotation)) { $output = array(); foreach ($validAnnotation as $iri => $value) { $output[$iri]['name'] = $dictMagicIRI[$iri]; $output[$iri]['value'] = $value; } } else { foreach ($valids as $index => $term) { if (!array_key_exists($term['iri'], $output)) { $output[$term['iri']]['name'] = $term['name']; if ($term['type'] == 'unique') { $output[$term['iri']]['value'] = array($term['value']); } else { $output[$term['iri']]['value'] = $term['value']; } } else { if ($term['type'] == 'unique') { $output[$term['iri']]['value'] = array($term['value']); } else { $output[$term['iri']]['value'] = array_merge($output[$term['iri']]['value'], $term['value']); } } } } $text .= <<<END {{ #Annotation: <!-- Auto formatted ontology annnotation wikitext --> END; foreach ($output as $iri => $term) { $value = $term['value']; $name = $term['name']; foreach ($value as $annote) { $text .= <<<END | {$name} = {$annote} END; } } foreach ($invalids as $index => $error) { $msg = self::getErrorMessage($error); if (sizeof($options[$index]) == 1) { $param = $options[$index][0]; $text .= <<<END | {$msg} {$param} END; } else { $name = $options[$index][0]; $value = $options[$index][1]; $text .= <<<END | {$msg} {$name} = {$value} END; } } $text .= <<<END \t\t\t }} END; $text .= preg_replace('/([\\s]?{{\\s*[#]?Annotation\\s*:[\\s]*[^|][^}]*}}[\\s]?)/', '', $wikiText); } else { $text = $wikiText; } foreach ($dictMagicIRI as $iri => $name) { if (!array_key_exists($iri, $output)) { $output[$iri]['value'] = null; } } return array($text, $output); }
private function importOntology() { $tmp = sys_get_temp_dir() . '/OntoKiWiQueue/'; if (file_exists($tmp)) { $this->recursiveRemoveDirectory($tmp); } mkdir($tmp); if (array_key_exists('OntoKiWi', $GLOBALS['wgDebugLogGroups'])) { $log = $GLOBALS['wgDebugLogGroups']['OntoKiWi']; } else { if ($GLOBALS['wgDebugLogFile'] && $GLOBALS['wgDebugLogFile'] != '') { $log = $GLOBALS['wgDebugLogFile']; } else { $log = sys_get_temp_dir() . '/mediawikiimportfromtext.log'; } } $request = $this->getRequest(); $options = $request->getValues(); $ontAbbr = $options['ontology_abbrv']; $graph = $options['ontology_graph_url']; $fullname = $options['ontology_fullname']; $id = strtolower($ontAbbr); $sql = new SQLStore(wfGetDB(DB_MASTER)); $status = $sql->insertOntology($id, $options); if ($status) { wfDebugLog('OntoKiWi', sprintf('OKW\\Special\\ImportOntology: valid ontology: queued pages will be stored in $s', $tmp)); $ontology = new OntologyData($ontAbbr); $rdf = $ontology->getRDF(); $this->importObjectProperty($id, $options, $sql, $rdf, $graph); $this->importAnnotationProperty($id, $options, $sql, $rdf, $graph); $ontID = $sql->getOntologyID($ontAbbr); $annotationMagic = $sql->getAnnotationMagicWords($ontID); $objectMagic = $sql->getObjectMagicWords($ontID); $objects = array(); foreach ($objectMagic as $magic => $object) { $objects[$magic] = $magic; $objects[$object['iri']] = $magic; $objects[$object['id']] = $magic; } $operations = array(); foreach ($GLOBALS['okwRDFConfig']['restriction']['operation'] as $operation => $operationIRI) { $operations[$operationIRI] = $operation; $operations[$operation] = $operation; } $types = array(); foreach ($GLOBALS['okwRDFConfig']['restriction']['type'] as $type => $typeIRI) { $types[$typeIRI] = $type; $types[$type] = $type; } $count = $rdf->countAllClass($graph); if ($count >= 10000) { $source = file_get_contents($options['source']); preg_match_all('/xmlns:([\\w]*)[\\s]?=[\\s]?"([^"]*)"[\\s]?/', $source, $matches, PREG_SET_ORDER); $prefix = array(); foreach ($matches as $match) { $prefix[$match[1]] = $match[2]; } if (preg_match_all('/[\\s]?<owl:Class[\\s]?rdf:about[\\s]?=[\\s]?"(&([\\w]*);)?([^"]*)"[\\s]?[\\/]?>/', $source, $matches, PREG_SET_ORDER)) { $classes = array(); foreach ($matches as $match) { if ($match[1] != '' && $match[2] != '') { $classes[] = $prefix[$match[2]] . $match[3]; } else { $classes[] = $match[3]; } } } else { $sql->deleteOntology($id); return array(self::EXCESSIVE_CLASS, null); } } else { if ($count == 0) { $sql->deleteOntology($id); return array(self::NO_CLASS_FOUND, null); } else { $classes = $rdf->getAllClass($graph); } } $filename = "Category:{$ontAbbr}"; file_put_contents($tmp . $filename, $fullname); $output = array(); foreach ($classes as $index => $class) { if ($class == $GLOBALS['okwRDFConfig']['Thing']) { continue; } $term = $ontology->parseTermByIRI($class); $id = $term->id; $filename = "{$ontAbbr}:{$id}"; if (!OntologyValidator::isValidTitleText($filename)) { throw new MWException("Unable to process term: {$id}. Please check the correctness of the Ontology"); } $related = $ontology->parseTermRelated($term); $wikiText = "[[Category:{$ontAbbr}]]"; $title = Title::newFromText($filename); if ($title->exists()) { continue; } $output[$class] = $term->label . " ({$ontAbbr}:{$id})"; $annotations = array(); foreach ($annotationMagic as $name => $value) { if (array_key_exists($value['iri'], $related)) { $annotations[$value['iri']] = $rdf->getObject($graph, $term->iri, $value['iri']); } } list($wikiText, $annotations) = AnnotationParser::reformatWikiText($wikiText, $annotations); $axiomData = $rdf->getAxiom($graph, $term->iri); $axioms = array(); foreach ($axiomData['subclassof'] as $data) { $axiom = array(); $axiom['type'] = 'subclassof'; $axiom['text'] = ManchesterSyntaxHandler::writeRecursiveManchester($data, array_merge($objects, $operations, $types)); $axioms[] = $axiom; } foreach ($axiomData['equivalent'] as $data) { $axiom = array(); $axiom['type'] = 'equivalent'; $axiom['text'] = ManchesterSyntaxHandler::writeRecursiveManchester($data, array_merge($objects, $operations, $types)); $axioms[] = $axiom; } list($wikiText, $axioms) = AxiomParser::reformatWikiText($ontAbbr, $wikiText, $axioms, true); $supClasses = array_keys($rdf->getSupClass($graph, $term->iri)); if (empty($supClasses)) { $supClasses = array($GLOBALS['okwRDFConfig']['Thing']); } list($wikiText, $supClasses) = HierarchyParser::reformatWikiText($ontAbbr, $wikiText, $supClasses); $common['label'] = $term->label; list($wikiText, $common) = CommonParser::reformatWikiText($wikiText, $common); file_put_contents($tmp . $filename, $wikiText); } wfDebugLog('OntoKiWi', 'OKW\\Special\\ImportOntology: ontology SPARQL query completes, pages will be created using maintenance scripts in the background'); $cmd = "( cd {$tmp} && for file in *; do php " . $GLOBALS['IP'] . "/maintenance/edit.php -u bot \$file < \$file; done && rm -R /tmp/OntoKiWiQueue ) > {$log} 2>&1 &"; exec($cmd, $output, $return); return array(self::SUCCESS, $output); } else { return array(self::INVALID_SPARQL, null); } }
protected static function supClassAxiomHTML($ontAbbr, $rootURL, $ontology, $term, $related, $axioms) { $sql = new SQLStore(wfGetDB(DB_SLAVE)); $magics = $sql->getObjectMagicWords($ontAbbr); $objects = array(); foreach ($magics as $magic => $iri) { $objects[$magic] = $magic; $objects[$iri] = $magic; $objects[DisplayHelper::getShortTerm($iri)] = $magic; } $operations = $GLOBALS['okwRDFConfig']['restriction']['operation']; $types = $GLOBALS['okwRDFConfig']['restriction']['type']; $html .= <<<END <!-- OKW SupClass Axiom Display Start --> <div id="okw-axiom-supclass-heading" class="supclass-heading">SubClassOf Axiom</div> <div id="okw-axiom-supclass-main" class="supclass-main"> <ul> END; foreach ($axioms as $axiom) { $axiomHTML = '<li>'; $axiomArray = preg_split('/\\s|([()])/', $axiom, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); foreach ($axiomArray as $word) { if ($word == '') { continue; } else { if (array_key_exists($word, $objects)) { $objectLabel = $objects[$word]; $axiomHTML .= "<b>{$objectLabel}</b>"; } else { if (array_key_exists($word, $types) || array_key_exists($word, $operations)) { $axiomHTML .= " {$word} "; } else { if ($word == '(' || $word == ')') { $axiomHTML .= $word; } else { $classIRI = $ontology->convertToIRI($word); $classID = $related[$classIRI]->id; $classLabel = $related[$classIRI]->label; $axiomHTML .= <<<END <a href="{$rootURL}{$ontAbbr}:{$classID}">{$classLabel}</a> END; } } } } } $axiomHTML .= '</li>'; $html .= $axiomHTML; } $html .= <<<END </ul></div> <!-- OKW SupClass Axiom Display End --> END; return $html; }
public static function reformatWikiText($ontAbbr, $wikiText, $validAxioms = null, $newWiki = false) { preg_match_all('/{{\\s*[#]?Axiom\\s*:[\\s]*[^|]([^}]*)}}/', $wikiText, $matches, PREG_SET_ORDER); $options = array(); $valids = array(); $invalids = array(); if (!empty($matches) || !is_null($validAxioms)) { $ontology = new OntologyData($ontAbbr); $sql = new SQLStore(wfGetDB(DB_SLAVE)); $magics = $sql->getObjectMagicWords($ontAbbr); $objects = array(); foreach ($magics as $magic => $object) { $objects[$magic] = $object['iri']; $objects[$object['iri']] = $object['iri']; $objects[$object['id']] = $object['iri']; } $operations = $GLOBALS['okwRDFConfig']['restriction']['operation']; $types = $GLOBALS['okwRDFConfig']['restriction']['type']; foreach ($matches as $match) { preg_match_all('/[\\s]*[|]([^|]*)/', $match[1], $params, PREG_PATTERN_ORDER); list($option, $valid, $invalid) = self::extractAxiom($params[1], $ontology, $objects, $operations, $types, $newWiki); $options = array_merge($options, $option); $valids = array_merge($valids, $valid); $invalids = array_merge($invalids, $invalid); } } if (!is_null($validAxioms)) { $valids = array(); $output = array(); foreach ($validAxioms as $value) { $index = uniqid(); $options[$index][] = $value['type']; $options[$index][] = $value['text']; list($valid, $data) = ManchesterSyntaxHandler::parseRecursiveManchester(true, $value['text'], $ontology, $objects, $operations, $types, $newWiki); if ($valid) { $valids[$index]['type'] = $value['type']; $valids[$index]['text'] = $value['text']; $valids[$index]['data'] = $data; } else { $invalids[$index] = self::ERROR_INVALID_SYNTAX; } } } else { $output = array(); foreach ($valids as $axiom) { $output[$axiom['type']][] = $axiom['data']; } } #TODO: Duplication checking if (!empty($valids) || !empty($invalids)) { $text = <<<END {{ #Axiom: <!-- Auto formatted ontology axiom wikitext --> END; foreach ($valids as $index => $axiom) { $type = $axiom['type']; $value = $axiom['text']; $text .= <<<END | {$type} = {$value} END; } foreach ($invalids as $index => $error) { $msg = self::getErrorMessage($error); if (sizeof($options[$index]) == 1) { $param = $options[$index][0]; $text .= <<<END | {$msg} {$param} END; } else { $name = $options[$index][0]; $value = $options[$index][1]; $text .= <<<END | {$msg} {$name} = {$value} END; } } $text .= <<<END }} END; } $text .= preg_replace('/([\\s]?{{\\s*[#]?Axiom\\s*:[\\s]*[^|][^}]*}}[\\s]?)/', '', $wikiText); return array($text, $output); }