Search code examples
phpsparqlowlontologyprotege

Make ontology visible to PHP web page using ARC2? Publish online?


I have some PHP code this is intended to create a dynamic web page, based on the result of a SPARQL query. It isn't working right now, and I think that's because my ontology isn't published online yet. How do I do the publishing part?

<html>
  <body>

     <?php
     include_once('semsol/ARC2.php'); /* ARC2 static class inclusion */ 


     $dbpconfig = array
     (
     "remote_store_endpoint" => "http://dbpedia.org/sparql",
     );

     $store = ARC2::getRemoteStore($dbpconfig); 

     if ($errs = $store->getErrors()) 
         {
           echo "<h1>getRemoteSotre error<h1>" ;
         }

         $query = '
                     PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                     PREFIX owl: <http://www.w3.org/2002/07/owl#>
                     PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                     PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
                     PREFIX uni: <http://www.semanticweb.org/admin/ontologies/2017/4/untitled-ontology-19#>
                     SELECT ?property ?subject ?prop ?object 
                     WHERE 
                     {
                     uni:Product ?property ?subject .
                     OPTIONAL {?subject ?prop ?object } 

                     }

                     ';


                     $rows = $store->query($query, 'rows'); /* execute the query */

                      if ($errs = $store->getErrors()) 
                      {
                         echo "Query errors" ;
                         print_r($errs);
                      }

          /* display the results in an HTML table */
          echo "<table border='1'>" ;
          foreach( $rows as $row ) 
          { 
            /* loop for each returned row */
            print "<tr><td>" .$row['l'] . "</td><td>" . $row['c']. "</td></tr>";
          }
          echo "</table>"

  ?>
  </body>
</html>

Solution

  • Are you still interested in this? The following works for me. I have tried to illustrate how to test individual components of a multi-part system, instead of just building everything, testing the final product and then saying "it doesn't work."

    Background: I'm using Ubuntu 16 server running in AWS. It appears that you might have some experience with Fuseki as a backend. With small modifications, these instructions should be applicable to OSes other than Ubuntu Linux, running in different physical or virtual environments, and for accessing sparql endpoints other than Fuseki. You could run the PHP script and the Fuseki process on the same servers, or two different servers.

    Your prerequisite responsibilities: a working webserver & php interpreter. The default Ubuntu apache2 and php7 packages worked for me with minimal or zero additional configuration. You could follow instructions like these, skipping the MySQL steps. The same article provides some tips for configuring the firewall. You're responsible for determining an appropriate level of world-accessibility.

    • Download (http://www-eu.apache.org/dist/jena/binaries/apache-jena-fuseki-2.6.0.tar.gz, for example) and unpack one of the Fuseki 2 archive files
    • If you are going to access the Fuseki dataset manager remotely, edit the run/shiro.ini file to enable an appropriate remote access policy
    • For testing/proof-of-concept, cd into the Fuseki directory and start Fuseki like this ./fuseki-server --mem /default Don't close this terminal session! You will need to open another session for additional tasks. A different method for launching Fuseki is required if you want the process and data to persist.
    • Use a web browser to visit the Fuseki page, which will be at an address like this: http://server.domain:3030
    • Click the add data button and upload some data. I had previously downloaded the pizza ontology as pizza.owl on my local filesystem, so I clicked the select files... button, navigated to my pizza.owl file, clicked open, and then clicked upload now. If you use the pizza ontology, you should see that roughly 1980 triples are inserted.
    • Now, before running any script, do a reality check and run a query within the Fuseki web interface. (Click the query button tab in the upper left.)

    What toppings are there, and what classes do they belong to?

    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
    
    SELECT *
    WHERE {
      ?recursiveTopping rdfs:subClassOf+ pizza:PizzaTopping .
      ?recursiveTopping rdfs:label ?toppingLabel .
      ?recursiveTopping rdfs:subClassOf ?immediateParent .
      filter(!isBlank(?immediateParent)) 
      filter( ?immediateParent != pizza:PizzaTopping )
    }
    order by ?immediateParent ?recursiveTopping
    

    If the SPARQL query works in the Fuseki web interface, you are ready to embed it in a php script/page.

    • create a new folder in your web server's root directory. I'm using Apache httpd on Ubuntu, so that's /var/www/html. You may need to do this as root, like this: sudo mkdir /var/www/html/ARC2_pizza
    • cd into the new directory
    • copy the source code from the bottom of this message
    • tell the computer that you want to past some text into a file: cat > index.php Then paste in the text from the clipboard. The required action will depend on the interface you're using. In a Ubuntu terminal, use Shift-Control-v. In putty, click the right mouse button. Or you could use a text editor like nano or gedit to create index.php... just remember that editing files in /var/www/html typically requires root privileges, unless you have changed the ownership or access permissions on the directory, which might not be appreciated by your web server.

    The script's not ready to run yet!

    You need to ensure that the ARC2 library is installed, that your script knows where to find it, and that the remote_store_endpoint parameter is set to the right address.

    There are probably multiple ways to install ARC2. I used composer, which in turn can be installed multiple ways, including apt-get

    sudo apt-get install composer
    

    or curl

    sudo apt-get install curl
    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
    

    followed by

    cd /var/www/html/ARC2_pizza composer require "semsol/arc2": "dev-master"

    With respect to remote_store_endpoint: if you are running the php script on the same server as the Fuseki server (as I do), you can use localhost. Otherwise, use the fully-qualified public IP address or name of the Fuseki server.

    Once the script is customized for your circumstances, test it from the command line (as opposed to visiting it with a web browser.)

    php /var/www/html/ARC2_pizza/index.php

    The results won't look pretty, but if you look closely, you should see the names of several pizza ingredients, separated with HTML table tags like ...<td><tr>...

    If that worked, you can finally test it from a web browser. If you have used a setup like mine, point your browser to an address like http://server.domain/ARC2_pizza

    If you don't see a table of pizza toppings in your web browser, then there's something wrong with your web server or firewall settings, not with Fuseki, the query, the PHP and ARC2 installations, or the script.

    Here's the source code for ARC2_pizza/index.php.

    This is based on the same tutorial that your question is based on. Don't forget to configure your ARC2 location and your endpoint address

    <html>
      <body>
    
         <?php
    
    // customize your ARC2 location
         include_once("/var/www/html/phprdftest/arc2-master/ARC2.php");
    
         $dbpconfig = array
         (
    // customize your endpoint address
         "remote_store_endpoint" => "http://localhost:3030/default/query",
         );
    
         $store = ARC2::getRemoteStore($dbpconfig); 
    
         if ($errs = $store->getErrors()) 
             {
               echo "<h1>getRemoteSotre error<h1>" ;
             }
    
             $query = '
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
    
    SELECT *
    WHERE {
      ?recursiveTopping rdfs:subClassOf+ pizza:PizzaTopping .
      ?recursiveTopping rdfs:label ?toppingLabel .
      ?recursiveTopping rdfs:subClassOf ?immediateParent .
      filter(!isBlank(?immediateParent)) 
      filter( ?immediateParent != pizza:PizzaTopping )
    }
    order by ?immediateParent ?recursiveTopping
                         ';
    
    
                         $rows = $store->query($query, 'rows'); /* execute the query */
    
                          if ($errs = $store->getErrors()) 
                          {
                             echo "Query errors" ;
                             print_r($errs);
                          }
    
              /* display the results in an HTML table */
              echo "<table border='1'>" ;
              foreach( $rows as $row ) 
              { 
                /* loop for each returned row */
                print "<tr><td>" .$row['recursiveTopping'] . "</td><td>" . 
                  $row['toppingLabel']. "</td><td>" . 
                  $row['immediateParent']. "</td></tr>";
              }
              echo "</table>"
    
      ?>
      </body>
    </html>