private function exportTerm() { $this->getOutput()->disable(); wfResetOutputBuffers(); $request = $this->getRequest(); $config = $this->getConfig(); $request->response()->header("Content-type: application/xml; charset=utf-8"); if ($request->getCheck('downloadTerm')) { $filename = urlencode($config->get('Sitename') . '-' . wfTimestamp() . '.xml'); $request->response()->header("Content-disposition: attachment; filename={$filename}"); } $pages = explode("\n", $request->getVal('pages')); $pageSet = array(); foreach ($pages as $title) { $title = trim($title); if (OntologyValidator::isExistTitleText($title)) { $titleArray = explode(':', $title); $ontAbbr = $titleArray[0]; $termID = str_replace(' ', '_', $titleArray[1]); if (!array_key_exists($ontAbbr, $pageSet)) { $pageSet[$ontAbbr][] = $termID; } else { if (!in_array($termID, $pageSet[$ontAbbr])) { $pageSet[$ontAbbr][] = $termID; } } } } $export = ''; $initial = true; foreach ($pageSet as $ontAbbr => $terms) { $ontology = new OntologyData($ontAbbr); $iris = array(); foreach ($terms as $term) { $iris[] = $ontology->getPrefix() . $term; } $rdf = $ontology->getRDF(); $xml = $rdf->exportDescribe($ontology->getGraph(), $iris); if ($initial) { $initial = false; } else { $xml = preg_replace('/^<\\?xml[^?>]*\\?>[\\s]?/', '', $xml); $xml = preg_replace('/<rdf\\:RDF[\\s]?xmlns\\:rdf[^\\s>]*[\\s]?xmlns:rdfs[^\\s>]*[\\s]?>[\\s]?/', '', $xml); } $xml = preg_replace('/<\\/rdf\\:RDF>/', '', $xml); $export .= $xml; } $export .= '</rdf:RDF>'; wfDebugLog('OntoKiWi', 'OKW\\Special\\ExportOntology: output term RDF/XML generated'); print $export; }
public static function extractSupClass($params, $ontology) { $options = array(); $valids = array(); $invalids = array(); $supClasses = array(); foreach ($params as $param) { $index = uniqid(); $pair = explode('=', $param, 2); if (count($pair) == 2) { $name = $pair[0]; $name = preg_replace('/[\\s]*(<!--.*?(?=-->)-->)[\\s]*/', '', $name); $name = strtolower(trim($name)); $value = $pair[1]; $value = preg_replace('/[\\s]*(<!--.*?(?=-->)-->)[\\s]*/', '', $value); $value = trim($value); $options[$index][] = $name; $options[$index][] = $value; if ($name == 'subclassof') { if (strtolower($value) == 'thing') { $supClassIRI = $GLOBALS['okwRDFConfig']['Thing']; $valids[$index]['iri'] = $supClassIRI; $valids[$index]['id'] = DisplayHelper::getShortTerm($supClassIRI); $valids[$index]['title'] = DisplayHelper::getShortTerm($supClassIRI); } else { $supClassIRI = $ontology->convertToIRI($value); $term = $ontology->parseTermByIRI($supClassIRI); if (!is_null($term)) { $title = $ontology->getOntAbbr() . ':' . $term->id; if (OntologyValidator::isExistTitleText($title)) { $valids[$index]['iri'] = $supClassIRI; $valids[$index]['id'] = $ontology->convertToID($supClassIRI); $valids[$index]['title'] = $ontology->convertToTitle($supClassIRI); } else { $invalids[$index] = self::ERROR_INVALID_TERM; } } else { $invalids[$index] = self::ERROR_INVALID_TERM; } } } else { $invalids[$index] = self::ERROR_INVALID_MAGICWORD; } } else { $options[$index][] = $param; $invalids[$index] = self::ERROR_EXCESS_INPUT; } } return array($options, $valids, $invalids); }
/** * Function * * @param string $skin * @param array $contentActions * @return boolean */ public static function displayTab($skin, &$contentActions) { global $wgRequest, $wgUser; if (method_exists($skin, 'getTitle')) { $title = $skin->getTitle(); } else { $title = $skin->mTitle; } if (!isset($title) || $title->getNamespace() == NS_SPECIAL) { return true; } if ($title->userCan('viewedittab')) { if ($title->exists()) { $contentActions['edit']['text'] = wfMessage('okw_edit')->text(); $formEditTabText = 'formedit'; if (!OntologyValidator::isExistTitle($title)) { return true; } } else { $contentActions['edit']['text'] = wfMessage('okw_create')->text(); $formEditTabText = 'formcreate'; } $formEditTabText = wfMessage($formEditTabText)->text(); if ($wgRequest->getVal('action') == 'formedit') { $class = 'selected'; } else { $class = ''; } $formEditTab = array('class' => $class, 'text' => $formEditTabText, 'href' => $title->getLocalURL('action=formedit')); $tabKeys = array_keys($contentActions); $tabValues = array_values($contentActions); $editTabLocation = array_search('edit', $tabKeys); if ($editTabLocation == null) { $editTabLocation = array_search('viewsource', $tabKeys); } if ($editTabLocation == null) { $editTabLocation = -1; } array_splice($tabKeys, $editTabLocation, 0, 'form_edit'); array_splice($tabValues, $editTabLocation, 0, array($formEditTab)); $contentActions = array(); for ($i = 0; $i < count($tabKeys); $i++) { $contentActions[$tabKeys[$i]] = $tabValues[$i]; } } return true; }
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); } }
private static function checkClass($termIRI, $ontology, $newWiki) { $term = $ontology->parseTermByIRI($termIRI); if (!is_null($term)) { $title = $ontology->getOntAbbr() . ':' . $term->id; if ($newWiki) { return OntologyValidator::isValidTitleText($title); } else { return OntologyValidator::isExistTitleText($title); } } else { return false; } }