예제 #1
0
 function testWrite()
 {
     $uri = '/text.txt';
     $content = 'Text content';
     $doc = new MLPHP\Document(parent::$client);
     $doc->setContent($content)->setContentType('text/text');
     $doc->write($uri);
     $response = $doc->getResponse();
     $this->assertEquals(201, $response->getHttpCode());
     return $doc;
 }
예제 #2
0
 function testWrite()
 {
     // Set up response for mock client
     $this->response->setInfo(array('http_code' => 201));
     $this->mockClient->expects($this->any())->method('send')->will($this->returnValue($this->response));
     $uri = '/text.txt';
     $content = 'Text content';
     $doc = new MLPHP\Document($this->mockClient);
     $doc->setContent($content)->setContentType('text/text');
     $doc->write($uri);
     $response = $doc->getResponse();
     $this->assertEquals(201, $response->getHttpCode());
 }
예제 #3
0
 function setUp()
 {
     global $mlphp;
     if (substr($mlphp->config['mlversion'], 0, 3) < 8) {
         $this->markTestSkipped('Test requires MarkLogic 8 or greater');
     }
     $uri = '/text.xml';
     $content = '<doc>
                 <foo bar="baz">hello</foo>
                 <one two="3">world</one>
                 <a b="c">!</one>
               </doc>';
     $doc = new MLPHP\Document(parent::$client);
     $doc->setContent($content)->setContentType('application/xml');
     $doc->write($uri);
     $this->db = new MLPHP\Database(parent::$manageClient, 'mlphp-test-db');
 }
예제 #4
0
<head>
<title>Example: Connect to ML with a REST client</title>
</head>

<?php 
// Set up global vars and class autoloading
require_once 'setup.php';
$client = $mlphp->newClient();
$client->setUsername($mlphp->config['username-admin']);
$client->setPassword($mlphp->config['password-admin']);
// Write a doc via PUT
$doc = new MLPHP\Document($client);
$file = 'example.xml';
$doc->setContentFile($file);
$uri = "/" . $file;
echo '<br />Write: ' . $doc->write($uri)->getUri() . '<br />' . PHP_EOL;
// Read a doc via GET
echo '<br />Read: ' . $doc->read($uri) . '<br />' . PHP_EOL;
// Delete a doc via DELETE
echo '<br />Delete: ' . $doc->delete($uri)->getUri() . '<br />' . PHP_EOL;
// Test 301 redirect
$govTrackClient = new MLPHP\RESTClient('www.govtrack.us', 0, 'api', 'v1');
// Get Senate bills from bill endpoint
$params = array('bill_type' => 'senate_bill');
// 'bill' resource results in redirect ('bill/' does not)
$request = new MLPHP\RESTRequest('GET', 'bill', $params);
$response = $govTrackClient->send($request);
//print_r($response->getBody());
echo '<br />Title of second bill object: <br />';
$obj = json_decode($response->getBody());
echo $obj->objects[1]->title . '<br /><br />' . PHP_EOL;
예제 #5
0
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// A Simple MLPHP Application
// 1. Complete the installation steps, see: mlphp/README.md
// 2. Tell the app how to talk to MarkLogic.
// Adjust the path below as needed to find the MLPHP project's vendor directory.
require_once __DIR__ . '/../vendor/autoload.php';
use MarkLogic\MLPHP;
// 3. Create a REST client that talks to MarkLogic.
$mlphp = new MLPHP\MLPHP(array('username' => 'rest-writer-user', 'password' => 'writer-pw', 'host' => 'localhost', 'port' => 8077, 'version' => 'v1', 'auth' => 'digest'));
$client = $mlphp->newClient();
// 4. Add a document to the MarkLogic database.
$document = new MLPHP\Document($client);
$document->setContent('<app><description>My first MLPHP app.</description></app>');
$document->write('/myfirstapp.xml');
// 5. Search the MarkLogic database.
$search = new MLPHP\Search($client);
$results = $search->retrieve('MLPHP');
// 6. Display a result.
echo '<html>';
echo '<style>.highlight { background-color: yellow; }</style>';
if ($results->getTotal() > 0) {
    $matches = $results->getResultByIndex(1)->getMatches();
    echo $matches[0]->getContent();
} else {
    echo 'No results found.';
}
echo '</html>';
예제 #6
0
                $uri = '/bills/' . $subdir . '/' . $file;
                // URI example: '/bills/112/h321.xml'
                $dom = new DOMDocument();
                $dom->loadXML($content);
                // Only write bills with related bills and short titles
                $num_rel_bills = $dom->getElementsByTagName('relatedbill')->length;
                $len_title = strlen($dom->getElementsByTagName('title')->item(0)->nodeValue);
                if ($num_rel_bills == 0 || $len_title > 80) {
                    continue;
                }
                $xpath = new DOMXPath($dom);
                // Set collection base on bill type. Example: 'hr' (House resolution)
                $type = $xpath->query('//bill/@type')->item(0)->nodeValue;
                $params = array("collection" => $type);
                $count++;
                //echo $count . ': ' . $uri . ' (' . $type . ')<br />' . PHP_EOL;
                // Write content to database via REST client
                if ($count++ >= 200) {
                    //break;
                }
                $doc->write($uri, $params);
            }
        }
        closedir($handle);
        $_SESSION['documents_loaded_usbills'] = TRUE;
    }
}
?>

</body>
</html>
예제 #7
0
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use MarkLogic\MLPHP;
?>
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Example: Chaining Document Methods</title>
</head>

<?php 
// Set up global vars and class autoloading
require_once 'setup.php';
$client = $mlphp->newClient();
$client->setUsername($mlphp->config['username-admin']);
$client->setPassword($mlphp->config['password-admin']);
// Write text as a document to the database
$doc = new MLPHP\Document($client);
echo $doc->setContent('Hello, PHP!')->write('/chained1.txt')->getContent();
echo '<br />';
echo $doc->setContentFile('example.xml')->write('/chained2.xml')->getContent();
echo '<br />';
$doc2 = new MLPHP\Document($client);
echo $doc2->write();
?>

</body>
</html>
예제 #8
0
 public static function loadDocsJSON($client)
 {
     // Load json files
     $dir = __DIR__ . DIRECTORY_SEPARATOR . 'docs' . DIRECTORY_SEPARATOR . 'nv';
     $count = 0;
     if ($handle = opendir($dir)) {
         parent::$logger->debug("Writing files from directory: " . $dir);
         $doc = new MLPHP\Document($client);
         while (false !== ($file = readdir($handle))) {
             if (substr($file, 0, 1) !== ".") {
                 $doc->setContentType("application/json");
                 $content = $doc->setContentFile($dir . '/' . $file)->getContent();
                 $uri = '/legislators/' . $file;
                 // URI example: '/legislators/Care.json'
                 $obj = json_decode($content);
                 $params = array("collection" => $obj->{'old_roles'}->{'2009-2010'}[0]->party);
                 $count++;
                 parent::$logger->debug($count . ': ' . $uri);
                 // Write content to database via REST client
                 $doc->write($uri, $params);
             }
         }
         closedir($handle);
     }
     parent::$logger->debug('JSON files loaded: ' . $count);
 }
예제 #9
0
    // Create a directory handle for reading
    echo 'Reading files from directory: ' . $dir . "\n\n";
    $files = array();
    while (false !== ($file = readdir($handle))) {
        // Read each file in the directory
        if (substr($file, 0, 1) !== '.') {
            // Ignore special files
            $files[] = $file;
            // Store the file URIs in an array
            $doc6->setContentFile($dir . '/' . $file);
            // Set the file as the document content
            $doc6->setContentType('application/xml');
            // Set the content type for the document
            $uri6 = '/' . $file;
            // Define the URI for the document
            $doc6->write($uri6);
            // Write the document to the database
        }
    }
    closedir($handle);
}
// Read the documents from the database
echo "Read the documents:\n";
foreach ($files as $i => $file) {
    // Loop through the file URIs
    echo 'File #' . ($i + 1) . ': ';
    $uri6 = '/' . $file;
    // Define the URI for the document
    // Display each document
    echo htmlspecialchars($doc6->read($uri6)) . "\n";
}
예제 #10
0
echo "Quality updated:\n";
echo $meta1->getQuality() . PHP_EOL;
// Read and display the quality of the metadata object
$doc1->writeMetadata($meta1);
// Write the metadata object for the document to the database
echo "Final written quality:\n";
$meta1 = $doc1->readMetadata();
// Read the metadata for the document
print_r($meta1->getQuality());
// Read and display the quality for the metadata object
echo "\n\n\n";
// Update multiple metadata at once via method chaining
$meta1->addCollections(array('sugary', 'fresh'))->addProperties(array('rating' => '9/10'));
$perm4 = new MLPHP\Permission('doc-editor', array('read', 'update', 'insert'));
$meta1->addPermissions($perm4)->setQuality($meta1->getQuality() + 1);
echo "Metadata (collections, properties, permissions, and quality) updated via method chaining:\n";
// Write, read, and display
$doc1->writeMetadata($meta1);
print_r($doc1->readMetadata());
echo "\n\n\n";
// Update metadata simultaneously with document write
$doc2 = new MLPHP\Document($client);
$doc2->setContent('More content');
$uri2 = '/example_updated.xml';
// Add metadata as params
$params2 = array('collection' => 'round', 'prop:status' => 'current', 'perm:doc-editor' => 'insert', 'quality' => 99);
echo "Metadata updated via params:\n";
// Write, read, and display
$doc2->write($uri2, $params2);
print_r($doc2->readMetadata());
echo "\n\n\n";