/** * Write (or delte, if $delete is set to true) the data in the object * variables, to the wiki page corresponding to this page handler * @param boolean $delete */ public function writeOrDeleteDataToWiki( $delete = false ) { if ( $delete ) { if ( $this->checkWikiPageExists() ) { $this->initSMWWriter( $delete = true ); } else { return; } } else { $this->ensureWikiPageExists(); $this->initSMWWriter(); } $properties = $this->m_properties; foreach ( $properties as $cur_prop ) { $propertystring = $cur_prop['p']; // TODO: Remove old code: // $property = SMWPropertyValue::makeUserProperty( $propertystring ); $property_di = SMWDIProperty::newFromUserLabel($propertystring); $valuestring = RDFIOUtils::sanitizeSMWValue( $cur_prop['v'] ); $value = SMWDataValueFactory::newPropertyObjectValue( $property_di, $valuestring ); $propertyErrorText = $property->getErrorText(); $propertyHasError = ( $propertyErrorText != '' ); if ( $propertyHasError ) { $this->addError( "<p>In RDFIOPageHandler::writeOrDeleteDataToWiki(): " . $property->getErrorText() . "</p>" ); } $valueErrorText = $value->getErrorText(); $valueHasError = ( $valueErrorText != '' ); if ( $valueHasError ) { $this->addError( "<p>Error creating property value object in RDFIOPageHandler::writeOrDeleteDataToWiki():</p><p>" . $value->getErrorText() . "</p>" ); } if ( $delete ) { $this->m_smwwriter_remove->addPropertyObjectValue( $property, $value ); $editmessage = "Deleting properties. Last property delete: " . $propertystring . " : " . $valuestring; } else { $this->m_smwwriter_add->addPropertyObjectValue( $property, $value ); $editmessage = "Importing properties. Last property added: " . $propertystring . " : " . $valuestring; } } $this->m_smwwriter->update( $this->m_smwwriter_remove, $this->m_smwwriter_add, $editmessage ); $smwWriterError = $this->m_smwwriter->getError(); $smwWriterHasError = ( $smwWriterError != '' ); if ( $smwWriterHasError ) { $this->addError( "<p>SMWWriter Error: " . $smwWriterError . "</p>" ); } }
function execute( $par ) { global $wgRequest, $wgOut, $smwgARC2StoreConfig, $wgServer, $wgScriptPath, $wgUser; $this->setHeaders(); $output = ""; # Get request data from, e.g. $rdfio_action = $wgRequest->getText( 'rdfio_action' ); # instantiation $store = ARC2::getStore( $smwgARC2StoreConfig ); $output .= "\n===RDF Store Setup===\n'''Status:'''\n\n"; if ( !$store->isSetUp() ) { $output .= "* Store is '''not''' set up\n"; if ( $rdfio_action == "setup" ) { if ( !$wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) { die( 'Cross-site request forgery detected!' ); } else { if ( $this->m_issysop ) { $output .= "* Setting up now ...\n"; $store->setUp(); $output .= "* Done!\n"; } else { $errormessage = "Only sysops can perform this operation!"; $wgOut->addHTML( RDFIOUtils::formatErrorHTML( "Permission Error", $errormessage ) ); } } } } else { $output .= "* Store is already set up.\n"; } $wgOut->addWikiText( $output ); $htmlOutput = '<form method="get" action="' . $wgServer . $wgScriptPath . '/index.php/Special:ARC2Admin" name="createEditQuery"> <input type="submit" name="rdfio_action" value="setup">' . Html::Hidden( 'token', $wgUser->editToken() ) . ' </form>'; $wgOut->addHTML( $htmlOutput ); }
function execute( $par ) { global $wgOut, $wgUser; $this->setHeaders(); $this->handleRequestData(); $showscreensonly = false; if ( $this->m_action == 'Import' ) { if ( !$wgUser->matchEditToken( $this->m_edittoken ) ) { die( 'Cross-site request forgery detected!' ); } elseif ( !$this->m_haswriteaccess ) { $errortitle = "Permission error"; $errormessage = "The current user lacks access either to edit or create pages (or both) in this wiki."; $wgOut->addHTML( RDFIOUtils::formatErrorHTML( $errortitle, $errormessage ) ); $this->outputHTMLForm(); } else { $this->initSMWBatchWriter(); $existunabbrpropertyuris = $this->m_smwbatchwriter->checkForNamespacesWithoutPrefix(); $abbreviateuriscreen = ''; $unabbrentityurilist = ''; if ( $existunabbrpropertyuris && $this->m_show_abbrscreen_properties ) { $suggestedbaseuris = $this->m_smwbatchwriter->getNamespacesWithoutPrefix(); $abbreviateuriscreen = $this->generateURIAbbreviationScreen( $suggestedbaseuris ); $showscreensonly = true; } if ( $this->m_show_abbrscreen_entities ) { $unabbrentityuris = $this->m_smwbatchwriter->getUnabbrEntityURIs(); $unabbrentityurilist = $this->generateUnabbrEntityList( $unabbrentityuris ); $showscreensonly = true; } if ( $showscreensonly ) { $contentbeforehtmlform = $abbreviateuriscreen . "\n" . $unabbrentityurilist; $wgOut->addHTML( $this->getHTMLFormContent( $contentbeforehtmlform ) ); } else { $this->executeSMWBatchWriter(); } } } elseif ( $this->m_action == 'AddNsPrefixes' ) { $this->addNewNsPrefixes(); $this->executeSMWBatchWriter(); } else { $this->outputHTMLForm(); } }
/** * Use the namespaces from the RDF / SPARQL source, to shorten the URIs. * @param string $uri * @param array $nsPrefixes * @return string */ function abbreviateParserNSPrefixes($uri, $nsPrefixes) { foreach ($nsPrefixes as $namespace => $prefix) { $nslength = strlen($namespace); $basepart = ''; $localpart = ''; $uriContainsNamepace = substr($uri, 0, $nslength) === $namespace; if ($uriContainsNamepace) { $localpart = substr($uri, $nslength); $basepart = $prefix; break; } } /* * Take care of some special cases: */ if ($basepart === '' && $localpart === '') { $uriParts = $this->splitURI($uri); $basepart = $uriParts[0]; $localpart = $uriParts[1]; } if ($localpart === '') { $abbreviatedUri = $basepart; } elseif (RDFIOUtils::isURI($basepart)) { // FIXME: Shouldn't the above check the local part instead?? // Change ARC:s default "random string", to indicate more clearly that // it lacks title $abbreviatedUri = str_replace('arc', 'untitled', $localpart); } elseif (RDFIOUtils::isURI($basepart)) { // If the abbreviation does not seem to have succeeded, // fall back to use only the local part $abbreviatedUri = $localpart; } elseif (RDFIOUtils::endsWithColon($basepart)) { // Don't add another colon $abbreviatedUri = $basepart . $localpart; } elseif ($basepart == false || $basepart == '') { $abbreviatedUri = $localpart; } else { $abbreviatedUri = $basepart . ':' . $localpart; } return $abbreviatedUri; }
/** * Abbreviate the base URI into a "pseudo-wiki-title-namespace" * @param string $uri * @return string $uri */ public function abbreviateNSFromURIOld( $uri ) { $uriParts = RDFIOUtils::splitURI( $uri ); $basepart = $uriParts[0]; $localpart = $uriParts[1]; if ( $localpart == '' ) { $uri = $basepart; } elseif ( substr( $basepart, 0, 1 ) == '_' ) { // Change ARC:s default "random string", to indicate more clearly that // it lacks title $uri = str_replace( 'arc', 'untitled', $localpart ); } else { $basepart = $this->getPrefixForNS( $basepart ); if ( substr( $basepart, 0, 7 ) == 'http://' ) { // If the abbreviation does not seem to have succeeded, // fall back to use only the local part $uri = $localpart; } elseif ( substr( $basepart, -1 ) == ':' ) { // Don't add another colon $uri = $basepart . $localpart; } else { $uri = $basepart . ':' . $localpart; } } return $uri; }
function failMsg($message) { global $wgOut; $errorHtml = RDFIOUtils::formatErrorHTML("Error!", $message); $wgOut->addHTML($errorHtml); }
function showImportedTriples($importedTriples) { $output = ""; $style_css = <<<EOD \t \t table .rdfio- th { \t \t font-weight: bold; \t \t padding: 2px 4px; \t \t } \t \t table.rdfio-table td, \t \t table.rdfio-table th { \t \t font-size: 11px; \t \t } EOD; $output .= "<style>{$style_css}</style>"; //$wgOut->addInlineStyle($style_css); $output .= RDFIOUtils::formatSuccessMessageHTML("Success!", "Successfully imported the triples shown below!"); $output .= "<table class=\"wikitable sortable rdfio-table\"><tbody><tr><th>Subject</th><th>Predicate</th><th>Object</th></tr>"; foreach ($importedTriples as $triple) { $s = $triple['s']; $p = $triple['p']; $o = $triple['o']; if (RDFIOUtils::isURI($s)) { $s = "<a href=\"{$s}\">{$s}</a>"; } if (RDFIOUtils::isURI($p)) { $p = "<a href=\"{$p}\">{$p}</a>"; } if (RDFIOUtils::isURI($o)) { $o = "<a href=\"{$o}\">{$o}</a>"; } $output .= "<tr><td>{$s}</td><td>{$p}</td><td>{$o}</td></tr>"; } $output .= "</tbody></table>"; return $output; }
/** * Convert all URI Resolver URIs which have a corresponding Original URI, * to that Original URI. * @param string $text * @return string $text */ function convertURIsToOrigURIsInText( $text ) { $uris = RDFIOUtils::extractURIs( $text ); if ( $uris != '' ) { foreach ( $uris as $uri ) { $origuri = $this->m_store->getOrigURIForURI( $uri ); if ( $origuri != '' ) { $text = str_replace( $uri, $origuri, $text ); } } } return $text; }
static function showErrorMessage( $title, $message ) { global $wgOut; $errorhtml = RDFIOUtils::formatErrorHTML( $title, $message ); $wgOut->addHTML( $errorhtml ); }
/** * Get data from the request object and store it in class variables */ function getRequestData() { global $wgRequest, $wgArticlePath; $requestData = new RDFIORequestData(); $requestData->action = $wgRequest->getText('action'); $requestData->editToken = $wgRequest->getText('token'); $requestData->importSource = $wgRequest->getText('importsrc'); $requestData->nsPrefixInWikiTitlesProperties = $wgRequest->getBool('nspintitle_prop', false); // TODO: Remove? $requestData->nsPrefixInWikiTitlesEntities = $wgRequest->getBool('nspintitle_ent', false); // TODO: Remove? $requestData->externalRdfUrl = $wgRequest->getText('extrdfurl'); $requestData->importData = $wgRequest->getText('importdata'); $requestData->dataFormat = $wgRequest->getText('dataformat'); $requestData->hasWriteAccess = RDFIOUtils::currentUserHasWriteAccess(); $requestData->articlePath = $wgArticlePath; return $requestData; }
protected function import($limit = 10, $offset = 0) { global $wgOut, $wgRequest; //$rdfioUtils = new RDFIOUtils(); $externalSparqlUrl = $wgRequest->getText('extsparqlurl'); if ($externalSparqlUrl === '') { throw new RDFIOException('Empty SPARQL Url provided!'); } else { if (!RDFIOUtils::isURI($externalSparqlUrl)) { throw new RDFIOException('Invalid SPARQL Url provided! (Must start with \'http://\' or \'https://\')'); } } $sparqlQuery = urlencode("SELECT DISTINCT * WHERE { ?s ?p ?o } OFFSET {$offset} LIMIT {$limit}"); $sparqlQueryUrl = $externalSparqlUrl . '/' . '?query=' . $sparqlQuery; $sparqlResultXml = file_get_contents($sparqlQueryUrl); $sparqlResultXmlObj = simplexml_load_string($sparqlResultXml); $importTriples = array(); if (is_object($sparqlResultXmlObj)) { foreach ($sparqlResultXmlObj->results->children() as $result) { $triple = array(); // $wgOut->addHTML( print_r($result, true) ); foreach ($result as $binding) { if ($binding['name'] == 's') { $s = (string) $binding->uri[0]; if ($s == '') { throw new Exception('Could not extract subject from empty string (' . print_r($binding->uri, true) . '), in SPARQLImport'); } $triple['s'] = $s; $triple['s_type'] = $this->resourceType($triple['s']); } else { if ($binding['name'] == 'p') { $p = (string) $binding->uri[0]; if ($p == '') { throw new Exception('Could not extract predicate from empty string (' . print_r($binding->uri, true) . '), in SPARQLImport'); } $triple['p'] = $p; $triple['p_type'] = $this->resourceType($triple['p']); } else { if ($binding['name'] == 'o') { $o = (string) $binding->uri[0]; if ($o == '') { throw new Exception('Could not extract object from empty string (' . print_r($binding->uri, true) . '), in SPARQLImport'); } $triple['o'] = $o; $triple['o_type'] = $this->resourceType($triple['o']); $triple['o_datatype'] = ''; } } } } $importTriples[] = $triple; } $rdfImporter = new RDFIORDFImporter(); $rdfImporter->importTriples($importTriples); $wgOut->addHTML($rdfImporter->showImportedTriples($importTriples)); } else { RDFIOUtils::formatErrorHTML("Error", "There was a problem importing from the endpoint. Are you sure that the given URL is a valid SPARQL endpoint?"); } return $output = array('externalSparqlUrl' => $externalSparqlUrl); }
/** * For a URI that is defined using the "Original URI" property, return the wiki * article corresponding to that entity * @param string $uri * @return string $wikititle; */ function getWikiTitleByOriginalURI( $uri ) { $wikititleresolveruri = $this->getURIForOrigURI( $uri ); $resolveruri = $this->getURIResolverURI(); $wikititle = str_replace( $resolveruri, '', $wikititleresolveruri ); $wikititle = str_replace( 'Property-3A', '', $wikititle ); $wikititle = str_replace( 'Property:', '', $wikititle ); $wikititle = RDFIOUtils::unXmlifyUris( $wikititle ); return $wikititle; }