rass_tag_search.php

00001 <?php
00002 # Sample script for RAWS tutorial 9: see http://rambla.eu/ for more info.
00003 #
00004 # Copyright (C) 2010 rambla.be
00005 
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #      http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 require_once '../RawsClient/Raws/Rass.php';
00018 
00019 # Provide the path to a local testfile, that can be uploaded via RASS
00020 define('LOCAL_FILE', '/my/path/to/a/file.mp4');
00021 
00022 # Provide your own RASS credentials here
00023 define('USER', 'xxx'); # your user account name
00024 define('PWD', 'xxx'); # your user account pwd
00025 define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")
00026 
00027 define('BASE', 'http://rass.' . CDN . '.rambla.be/');
00028 
00029 # all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
00030 try {
00031   # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
00032   $rass = new Rass(USER, PWD, BASE);
00033   
00034   # Create a "tutorial9" directory under the root-directory
00035   $dir_entry = $rass->createDir("tutorial9", True);
00036   
00037   # Upload a file 'eagle.mp4' to your dir and attach some tags to it
00038   $item_entry = $rass->postItem($dir_entry->path, "eagle.mp4", LOCAL_FILE);
00039   $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("bird"), new Rass_Extension_Tag("birdwatching")), array());
00040   
00041   # Upload a file 'owl.mp4' to your dir and attach some tags to it
00042   $item_entry = $rass->postItem($dir_entry->path, "owl.mp4", LOCAL_FILE);
00043   $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("bird"), new Rass_Extension_Tag("documentary")), array());
00044   
00045   # Upload a file 'snake.mp4' to your dir and attach some tags to it
00046   $item_entry = $rass->postItem($dir_entry->path, "snake.mp4", LOCAL_FILE);
00047   $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("reptile"), new Rass_Extension_Tag("documentary")), array());
00048   
00049   # Upload a file 'spiderman.mp4' to your dir and attach some tags to it
00050   $item_entry = $rass->postItem($dir_entry->path, "spiderman.mp4", LOCAL_FILE);
00051   $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("superhero"), new Rass_Extension_Tag("movie"),  new Rass_Extension_Tag("rebirth")), array());
00052   
00053   # Upload two test files, without any tags
00054   $item_entry = $rass->postItem($dir_entry->path, "test1.mp4", LOCAL_FILE);
00055   $item_entry = $rass->postItem($dir_entry->path, "test2.mp4", LOCAL_FILE);
00056   
00057   
00058   # GET feed
00059   
00060   echo "Getting meta entries for files in directory: " . $dir_entry->path . "\n";
00061   $query = new Rass_MetaQuery("file");
00062   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00063   foreach ($feed as $entry) {
00064     echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
00065     foreach ($entry->content->params->tag as $t) {
00066       echo "Tag: " . $t->text . "\n";
00067     }
00068   }
00069 
00070   echo "Getting meta entries for files in directory: " . $dir_entry->path . " corresponding to the pattern 'test?.*' \n";
00071   $query = new Rass_MetaQuery("file");
00072   $query->setName("test?.*");
00073   $feed = $rass->getMetaFeed("/", $query);
00074   foreach ($feed as $entry) {
00075     echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
00076     foreach ($entry->content->params->tag as $t) {
00077       echo "Tag: " . $t->text . "\n";
00078     }
00079   }
00080   
00081   
00082   # 'tag' searches
00083   
00084   echo "\n\nSearch for all files tagged with 'bird' :";
00085   $query->setSearchArg("tag", array("bird"));
00086   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00087   foreach ($feed as $entry) {
00088     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00089   }
00090 
00091   echo "\n\nSearch for all files tagged with 'bird' OR 'reptile' :";
00092   $query->setSearchArg("tag", array("bird", "reptile"));
00093   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00094   foreach ($feed as $entry) {
00095     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00096   }
00097 
00098   #  with 'animal' AND 'documentary'
00099   echo "\n\nSearch for all files tagged with 'animal' AND 'documentary' :";
00100   $query->setSearchArg("tag", array(), array("animal", "documentary"));
00101   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00102   foreach ($feed as $entry) {
00103     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00104   }
00105 
00106   echo "\n\nSearch for all files tagged with 'animal' BUT NOT with 'bird' :";
00107   $query->setSearchArg("tag", array("animal"), array(), array("bird"));
00108   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00109   foreach ($feed as $entry) {
00110     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00111   }
00112   
00113   echo "\n\nSearch for all files tagged with 'bird' or 'reptile' BUT NOT with 'animal' :";
00114   $query->setSearchArg("tag", array("bird","reptile"), array(), array("animal"));
00115   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00116   foreach ($feed as $entry) {
00117     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00118   }
00119   
00120   
00121   # 'qtag' or 'iqtag' searches
00122 
00123   echo "\n\nCase-sensitive search for all files with a tag that contains the string 'ani' :";
00124   $query = new Rass_MetaQuery("file");
00125   $query->setSearchArg("qtag", array("ani"));
00126   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00127   foreach ($feed as $entry) {
00128     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00129   }
00130 
00131   # qtag is case sensitive
00132   echo "\n\nCase-sensitive search for all files with a tag that contains the string 'Ani' :";
00133   $query = new Rass_MetaQuery("file");
00134   $query->setSearchArg("qtag", array("Ani"));
00135   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00136   foreach ($feed as $entry) {
00137     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00138   }
00139 
00140   # iqtag is case insensitive
00141   echo "\n\nCase-insensitive search for all files with a tag that contains the string 'Ani' :";
00142   $query = new Rass_MetaQuery("file");
00143   $query->setSearchArg("iqtag", array("Ani"));
00144   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00145   foreach ($feed as $entry) {
00146     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00147   }
00148   
00149   # search operators can also be used with (i)qtag
00150   echo "\n\nCase-insensitive search for all files with a tag that contains the string 'HERO' and the string 'BIR' :";
00151   $query = new Rass_MetaQuery("file");
00152   $query->setSearchArg("iqtag", array(), array("HERO", "BIR"));
00153   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00154   foreach ($feed as $entry) {
00155     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00156   }
00157   
00158   # combined searches are NOT ALLOWED => if set, the tag argument will prevail
00159   echo "\n\nSearch for all files tagged with 'animal' or 'superhero' (the iqtag argument will be ignored) :";
00160   $query = new Rass_MetaQuery("file");
00161   $query->setSearchArg("tag", array("animal", "superhero"));
00162   $query->setSearchArg("iqtag", array(), array(), array("Document"));
00163   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00164   foreach ($feed as $entry) {
00165     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00166   }
00167 
00168   # combined searches are NOT ALLOWED => you can unset a search argument
00169   echo "\n\nSearch for all files that DON'T have a tag containing the string 'Document' (the tag argument has been unset) :";
00170   $query = new Rass_MetaQuery("file");
00171   $query->setSearchArg("tag", array("animal", "superhero"));
00172   $query->unsetSearchArg("tag");
00173   $query->setSearchArg("iqtag", array(), array(), array("Document"));
00174   $feed = $rass->getMetaFeed($dir_entry->path, $query);
00175   foreach ($feed as $entry) {
00176     echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
00177   }
00178   
00179   // # DELETE directory on the CDN. Set second argument to True to delete recursively.
00180   // $rass->deleteDir($dir_entry->path, True);
00181   // echo "\nDeleted dir: " . $dir_entry->path . "\n";
00182   
00183 
00184 
00185 }
00186 catch(Zend_Gdata_App_Exception $e) {
00187     # Report the exception to the user
00188     echo "\nCaught exception: " . get_class($e) . "\n";
00189     echo "Message: " . $e->getMessage() . "\n";
00190     # get the HTTP status code
00191     echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
00192     echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
00193     # get the raws:error elements
00194     $rawsResponse = Raws::parseRawsResponse($e->getResponse());
00195     echo "Raws Code: " . $rawsResponse->getCode() . "\n";
00196     echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
00197     $reasons = $rawsResponse->getReasons();
00198     foreach ($reasons as $reason) {
00199       echo "Raws Reason: " . $reason . "\n";
00200     }
00201     $details = $rawsResponse->getDetails();
00202     foreach ($details as $key => $value) {
00203       echo "Raws Detail: " . $key . " -> " . $value . "\n";
00204     }
00205 }
00206 catch (Zend_Exception $e) {
00207     echo "Caught exception: " . get_class($e) . "\n";
00208     echo "Message: " . $e->getMessage() . "\n";
00209 }

Generated on Mon Mar 28 15:03:20 2011 for RawsClient PHP by  doxygen 1.5.3