Esempio n. 1
0
 public static function parse($parser)
 {
     $parser->disableCache();
     $title = $parser->getTitle()->getText();
     $titleArray = explode(':', $title);
     $ontAbbr = $titleArray[0];
     $termID = str_replace(' ', '_', $titleArray[1]);
     $ontology = new OntologyData($ontAbbr);
     $term = $ontology->parseTermByID($termID);
     $params = array();
     for ($i = 2; $i < func_num_args(); $i++) {
         $params[] = func_get_arg($i);
     }
     list($options, $valids, $invalids) = self::extractSupClass($params, $ontology);
     $pathType = $GLOBALS['okwHierarchyConfig']['pathType'];
     $supClasses = array();
     if (!empty($valids)) {
         foreach ($valids as $index => $value) {
             $supClasses[] = $value['iri'];
             $hierarchy = $ontology->parseTermHierarchy($term, $pathType, $value['iri']);
             if ($value['iri'] == $GLOBALS['okwRDFConfig']['Thing']) {
                 $GLOBALS['okwCache']['hierarchy'][$index] = $hierarchy;
             } else {
                 foreach ($hierarchy as $path) {
                     if (!empty($path['path'])) {
                         $GLOBALS['okwCache']['hierarchy'][$index] = $hierarchy;
                     }
                 }
             }
         }
     }
     wfDebugLog('OntoKiWi', sprintf('OKW\\Parser\\HierarchyParser: parsed hierarchy {%s} for [[%s]]', join(';', $supClasses), $title));
     wfDebugLog('OntoKiWi', '[caches] OKW\\Parser\\HierarchyParser: hierarchy');
     return array('', 'noparse' => true);
 }
Esempio n. 2
0
 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;
 }
Esempio n. 3
0
 public static function display($title)
 {
     $cache = $GLOBALS['okwCache'];
     $html = '';
     $titleArray = explode(':', $title);
     $ontAbbr = $titleArray[0];
     $termID = str_replace(' ', '_', $titleArray[1]);
     $ontology = new OntologyData($ontAbbr);
     $term = $ontology->parseTermByID($termID);
     if (!empty($cache['hierarchy']) || !empty($cache['axiom']['subclassof']) || !empty($cache['axiom']['equivalent'])) {
         $html .= self::printPageSidebar($ontology, $term, $cache);
     }
     if ($ontology->existClass($term->iri) || !empty($cache['annotation'])) {
         $html .= self::printPageHeader($ontology, $term, $cache);
     }
     return DisplayHelper::tidyHTML($html);
 }
Esempio n. 4
0
 public static function parseClassResult($classResult)
 {
     $classes = array();
     foreach ($classResult as $result) {
         if (isset($classes[$result['class']])) {
             if (isset($result['subClass'])) {
                 $classes[$result['class']]->hasChild = true;
             }
         } else {
             $class = OntologyData::makeClass(array('iri' => $result['class'], 'label' => null, 'type' => null, 'hasChild' => null));
             $class->label = '';
             if (isset($result['label'])) {
                 $class->label = $result['label'];
             }
             $class->hasChild = false;
             if (isset($result['subClass'])) {
                 $class->hasChild = true;
             }
             $classes[$result['class']] = $class;
         }
     }
     asort($classes);
     return $classes;
 }
Esempio n. 5
0
 private function exportOntology()
 {
     $this->getOutput()->disable();
     wfResetOutputBuffers();
     $request = $this->getRequest();
     $config = $this->getConfig();
     $request->response()->header("Content-type: application/xml; charset=utf-8");
     if ($request->getCheck('downloadOntology')) {
         $filename = urlencode($config->get('Sitename') . '-' . wfTimestamp() . '.xml');
         $request->response()->header("Content-disposition: attachment; filename={$filename}");
     }
     $ontAbbr = $request->getVal('ontology');
     $ontology = new OntologyData($ontAbbr);
     $rdf = $ontology->getRDF();
     $xml = $rdf->exportOntology($ontology->getGraph());
     wfDebugLog('OntoKiWi', 'OKW\\Special\\ExportOntology: output ontology RDF/XML generated');
     print $xml;
 }
Esempio n. 6
0
 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);
     }
 }