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; }
function testRead() { // Set up response for mock client $this->response->setBody('Text content'); $this->response->setInfo(array('content_type' => 'text/text')); $this->mockClient->expects($this->any())->method('send')->will($this->returnValue($this->response)); $doc = new MLPHP\Document($this->mockClient); $result = $doc->read($doc->getURI()); $this->assertEquals($result, $doc->getContent()); $this->assertEquals('text/text', $doc->getContentType()); }
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); }
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'); }
use MarkLogic\MLPHP; ?> <!DOCTYPE html> <html lang="en-us"> <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);
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>';
if (!empty($redirect)) { require_once 'loading.php'; } else { echo "<script>\$(window).load(function () { document.write('" . $items . " loaded') });</script>"; } $restClient = new MLPHP\RESTClient($mlphp['host'], $mlphp['port'], $mlphp['path'], $mlphp['version'], $mlphp['username'], $mlphp['password'], $mlphp['auth']); $rootdir = 'bills'; $subdirs = array('110', '111', '112'); // directories to import from // Loop through files from subdirectories foreach ($subdirs as $subdir) { $count = 0; $dir = $rootdir . '/' . $subdir; if ($handle = opendir($dir)) { //echo "Writing files from directory: " . $dir . "<br />"; $doc = new MLPHP\Document($restClient); while (false !== ($file = readdir($handle))) { if (substr($file, 0, 1) !== ".") { $doc->setContentType("application/xml"); $content = $doc->setContentFile($dir . '/' . $file)->getContent(); $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);
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>
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software 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. */ // Setup use MarkLogic\MLPHP; require_once 'setup.php'; $client = $mlphp->newClient(); $client->setUsername($mlphp->config['username-admin']); $client->setPassword($mlphp->config['password-admin']); // Load some documents $doc = new MLPHP\Document($client); $doc->setContentFile("hamlet.xml")->write("/" . "hamlet.xml"); $doc->setContentFile("macbeth.xml")->write("/" . "macbeth.xml"); $doc->setContentFile("example.json")->write("/" . "example.json"); // Search for a string $search = new MLPHP\Search($client); $search->setPageLength(2); $results = $search->retrieve('donalbain'); echo "Simple text search results:\n\n"; print_r($results); echo "\n\n\n"; // Search by key-value for an element $search->setPageLength(1); $results = $search->retrieveKeyValueElement('PLAYSUBT', '', 'HAMLET'); echo "Key-value (for an element) search results:\n\n"; print_r($results);
<?php /* Copyright 2002-2012 MarkLogic Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software 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. */ use MarkLogic\MLPHP; // Set up global vars and class autoloading require_once 'setup.php'; // Create a REST client object for the MarkLogic database $client = $mlphp->newClient(); $doc = new MLPHP\Document($client); $uri = $_REQUEST['uri']; $parts = explode('/', $uri); $filename = end($parts); $content = $doc->read($uri); header('Content-Type: ' . $doc->getContentType()); header('Content-Disposition: attachment; filename=' . $filename); echo $content;
<div id="header"> <div id="logo"><a href="index.php">U.S. Bill Search</a></div> <div id="subtitle">Powered by <a href="../index.php">MLPHP</a></div> </div><!-- /header --> <div id="back-link"><a href="<?php echo $link; ?> ">Back to results</a></div> <?php // Load the XML source $doc = new MLPHP\Document($restClient, $uri); $xml = new DOMDocument(); $xml->loadXML($doc->read()); // Load the XSLT $xsl = new DOMDocument(); $xsl->load('bill.xsl'); // Configure the transformer $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); // attach the xsl rules echo $proc->transformToXML($xml); ?> <div id="footer">Powered by <a href="../index.php">MLPHP</a></div> </div><!-- /wrapper --> </body> </html>
<?php /* Copyright 2002-2012 MarkLogic Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software 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. */ use MarkLogic\MLPHP; require_once 'setup.php'; $client = $mlphp->newClient(); $doc = new MLPHP\Document($client); $uri = $_REQUEST['uri']; $doc->delete($uri); header('content-type: text/html'); echo 'deleted';
// Set the content type for the document $doc5->setContentFile($file5); // Set the file as the document content $uri5 = '/' . $file5; // Define the URI for the document $doc5->write($uri5); // Write the document to the database // Link to the binary PDF file from the database echo "Display a link to the PDF document:\n"; displayLink($uri5, $doc4->getContentType(), 'PDF File'); // Helper function echo "\n\n"; // Files from a directory $dir = __DIR__ . '/several'; // Get directory relative to current directory $doc6 = new MLPHP\Document($client); // Create a Document object (passing the REST client) if ($handle = opendir($dir)) { // 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
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";