Skip to content
joetm edited this page Mar 12, 2016 · 10 revisions

Using SameAs-Lite from within other scripts

<?php

// Composer autoload
// assuming we are in the SameAsLite webroot. Adjust the path, if necessary.
require_once 'vendor/autoload.php';

// Init the store

// if using a MySQL store:
/*
$options = [
    'username' => 'testuser',
    'password' => 'testpass',
    'dbName' => 'teststore',
    'host' => 'localhost',
    'port' => '3306',
    'charset' => 'utf8',
];
// Note: the store 'teststore' will be created if it does not exist
$store = new \SameAsLite\Store\MySQLStore('teststore', $options);
*/

// if using a SQLLite store:
$options = [
    'location' => './db/sameaslite-store.db', // adjust this to your SQLite db location
];
// Note: the store 'teststore' will be created if it does not exist
$store = new \SameAsLite\Store\SQLiteStore('teststore', $options);

// connect to the store
$store->connect();

// check if we are connected
if (!$store->isConnected()) {
    die("Something went wrong with connecting to the DB.
    Check permissions, db location, store config, etc.");
}

// do some queries - see the available methods in SQLiteStore.php
print PHP_EOL . "Querying some stuff out of the store: " . $store->getStoreName() . PHP_EOL;
print "-----------".PHP_EOL;

var_dump($store->querySymbol('Frankfurt'));
// or if you've added resource URIs instead of literals to your store:
var_dump($store->querySymbol('http://dbpedia.org/resource/Frankfurt'));

// disconnect from db
$store->disconnect();

?>

The output could look like this (if using SameAs-Lite with literal values):

Querying some stuff out of the store: teststore
-----------
array(7) {
  [0] =>
  string(3) "FFM"
  [1] =>
  string(8) "Franfurt"
  [2] =>
  string(9) "Frankfurt"
  [3] =>
  string(15) "Frankfurt a. M."
  [4] =>
  string(17) "Frankfurt am Main"
}

Using Stores

$store->assertPair('crisps', 'potato chips'); Create a relationship

$store->querySymbol('crisps'); Looks up all equivilent symbols. Returns ['crisps', 'potato chips']

$store->getCanon('potato chips'); Gets the canon for the symbol. Canons are created when a pair is asserted and neither of the symbols are in the store, and the first symbol is used as the canon. Returns 'crisps'

$store->removeSymbol('potato chips'); Removes that symbol from the store.

Using the WebApp

These stores can be used by themselves and accessed directly. However sameAs Lite also comes with a WebApp that can be used to provide a RESTful API and HTML interface to interact with the store. The WebApp uses Slim for routing.

To use the WebApp create a new \SameAsLite\WebApp object, passing in an array of options.

"Options described here" Stores can then be added to the app using the addDataset function:

$app->addDataset($store); Finally, run the app using run().

$app->run(); A full example can be found in examples/manual.php

Extending SameAs-Lite with new stores

All Stores bundled with sameAs Lite are in the \SameAsLite\Store namespace and all take a name for the store and an array of options. For MySQL these options are:

[ 'username' => , 'password' => , 'dbName' => <string, default=sameAsLite>, 'host' => , 'port' => <int, optional>, 'charset' => <string, optional> ] TODO: Needs some more help here, so that it works better out of the box (such as username/password).

For other Store options please see the documentation.

  1. SameAs-Lite
  2. Concepts
  3. Server Requirements
    1. Requirements
    2. Dependency Installation
  4. Installation & Configuration
    1. Environment Configuration
    2. SameAs Configuration
    3. Sample Data
  5. [Data Stores](Data Stores)
  6. Usage
    1. Web Usage
    2. API
    3. [API Examples](API Examples)
    4. Scripting
  7. Contributing to SameAs-Lite
    1. [Setting up a development environment](Setting up a development environment)
    2. [Coding Standards](Coding Standards)
    3. [Day-to-Day Development](Day-to-Day Development)
  8. [Open Source Governance](Open Source Governance)
  9. Support
  10. [Getting In Touch](Getting In Touch)
  11. [Legal Information](Legal Information)

Clone this wiki locally