book.php File Reference

Search the library catalogue. More...

Go to the source code of this file.

Functions

 is_wildcard ($term)
 check for wildcards in search term
 normalize ($term)
 strip funny characters from a search term
 build_z3950_query ($search)
 build a Z3950 search term in RPN format
 grs_analyze ($grs)
 translate a Z39.50 result in GRS format to a more human-readable form
 book_info ($search)
 search in the library catalogue, using the Z39.50 protocol

Variables

 $grs_dict


Detailed Description

Search the library catalogue.

These functions provide an interface to the library catalogue. It is possible to search for books in the catalogue and obtain bibliographic data from it, using the Z39.50 protocol.

Requires the PHP/YAZ module; see www.indexdata.dk/phpyaz for details.

Definition in file book.php.


Function Documentation

book_info ( search  ) 

search in the library catalogue, using the Z39.50 protocol

Parameters:
$search The input parameter $search is an associative array containing key-value pairs. They represent a search term of the form (key_1 = value_1) AND (key_2 = value_2) AND ... .
Example: if you want to search for books about apples AND written by Newton, you would use:

$search = array ('title' => 'apple', 'author' => 'Newton')

Returns:
an associative array containing the results in human readable form.

Definition at line 235 of file book.php.

References build_z3950_query(), and grs_analyze().

Referenced by edit_item().

00235                             {
00236         global $z_host, $z_port, $z_auth, $z_db, $z_query;
00237 
00238         # connect to z39.50 server
00239 
00240         $z_url =  "$z_host:$z_port/$z_db";
00241         $z_opts = array("user" => $z_user, "password" => $z_pass);
00242 
00243         $z_handle = yaz_connect($z_url, $z_opts) or 
00244                 user_error(yaz_error($z_handle), E_USER_ERROR);
00245 
00246         yaz_syntax($z_handle, "usmarc");        // choose usmarc syntax
00247         yaz_range($z_handle, 1,100);            // max. 100 results
00248         yaz_element($z_handle, "F");            // request "full" records
00249 
00250         # build query, perform search
00251 
00252         $z_query =build_z3950_query($search); 
00253 
00254 
00255         yaz_search($z_handle, "rpn", $z_query);
00256 
00257         yaz_wait() or
00258                 user_error( yaz_error($z_handle), E_USER_ERROR);
00259 
00260         $ans = array();
00261 
00262         $i = 1;
00263 
00264         # analyze response, build answer 
00265 
00266         while ( $z_record = yaz_record($z_handle,$i++, "array")) {
00267 
00268                  $a = grs_analyze($z_record);
00269                 # $a['signature'] = $sig;
00270                  $ans[] = $a;
00271 
00272         }
00273 
00274         yaz_close($z_handle);
00275 
00276         return $ans;
00277 }

Here is the call graph for this function:

build_z3950_query ( search  ) 

build a Z3950 search term in RPN format

Build an string representation of a Z3950 query in RPN format (see the YAZ documentation for details).

Parameters:
$search The input parameter $search is an associative array containing key-value pairs. They represent a search term of the form (key_1 = value_1) AND (key_2 = value_2) AND ....
Example:

$search = array ('title' => 'apple', 'author' => 'Newton')

means you want to search for books where title contains 'apple' AND author contains 'Newton'.

Wildcards at the end of a word are recognized, i.e. "appl*" will match 'apple' as well as 'application'.

Returns:
a search expression looking like this:
@and @attr 1=1017 "Apple" @attr 1=1017 "Newton"

Note that the search expression uses RPN (reverse polish notation) semantics, hence the "\@and" comes first.

The @attr x=y terms are search attributes, as defined in the BIB-1 standard. See http://www.loc.gov/z3950/agency/defns/bib1.html for details.

Note:
Due to quirks with the PICA library catalogue's Z39.50 server, we found it best to map both title and author to attribute 1017 ("any"). Your mileage may vary.

Definition at line 108 of file book.php.

References is_wildcard().

Referenced by book_info().

00108                                     {
00109 
00110         $i = 0;
00111         $z_query = "";
00112 
00113 
00114         foreach ($search as $key => $term) {
00115 
00116                 if (empty($term)) {continue;}
00117 
00118                 switch($key) {
00119 
00120                 case "signature": 
00121                         $z_query .= '@or @attr 5=1 @attr 1=54 "';
00122                         $z_query .= normalize($term);
00123                         $z_query .= '" @attr 5=1 @attr 1=53 "';
00124                         $z_query .= normalize($term);
00125                         $z_query .= '" ';
00126 
00127                         $i++;
00128                         break;
00129 
00130                 case "isbn":  
00131 
00132                         $z_query .= '@attr 5=1 @attr 1=7 "';
00133                         $z_query .= normalize($term);
00134                         $z_query .= '" ';
00135 
00136                         $i++;
00137                         break;
00138 
00139                 case "author":  
00140                 case "title":  
00141 
00142                         foreach (split(' ', $term) as $word) {
00143 
00144                                 if (empty($term)) {continue;}
00145                                                 
00146                                 $z_query .= (is_wildcard($word)) ? 
00147                                                 "@attr 5=1 " : "";
00148 
00149                                 $z_query .= '@attr 1=1016 "';
00150                                 $z_query .= normalize($word);
00151                                 $z_query .= '" ';
00152                                 $i++;
00153                         }
00154                         
00155                         break;
00156                 }
00157         }
00158 
00159         while ($i > 1) {
00160                 $z_query = "@and " . $z_query;
00161                 $i--;
00162         }
00163 
00164 
00165         return $z_query;
00166 }

Here is the call graph for this function:

grs_analyze ( grs  ) 

translate a Z39.50 result in GRS format to a more human-readable form

Parameters:
$grs Z39.50 result in GRS format
Returns:
a human-readable associative array containing bibliographical
Note:
The array $grs_dict is used as a reference for this translation.

Definition at line 176 of file book.php.

References $grs_dict.

Referenced by book_info().

00176                            {
00177         global $grs_dict;
00178 
00179         $ans = array();
00180 
00181         foreach ($grs_dict as $key => $value )  {
00182                 foreach ($grs as $rec)  {
00183                         if (preg_match($key, $rec[0])) {
00184 
00185                                 $v = preg_replace("/[,\/ ]*$/","",$rec[1]);
00186 
00187                                 # The PICA z39.50 server delivers
00188                                 # the book signature mingled together with
00189                                 # the book's location.  We strip the 
00190                                 # location - which is enclosed in exclamation 
00191                                 # marks - from the final result. 
00192 
00193                                 if ($value == "signature") {
00194                                         $v = preg_replace("/![^!]*!/","",$v);
00195                                 }
00196 
00197                                 $ans[$value] .= "$v ; ";
00198                         }
00199                 }
00200 
00201                 # get rid of several semicolons, colons, commas, etc. in a row
00202 
00203                 $ans[$value] = preg_replace("/;\s*;/",";",$ans[$value]);
00204                 $ans[$value] = preg_replace("/:\s*;/",":",$ans[$value]);
00205                 $ans[$value] = preg_replace("/\/\s*;/","/",$ans[$value]);
00206                 $ans[$value] = preg_replace("/\.\s*;/",".",$ans[$value]);
00207         }
00208 
00209 
00210         $s = split(";", $ans[signature]);
00211         $ans[signature] = $s[0];        
00212 
00213         foreach ($ans as $key => $value) {
00214                 $ans[$key] = trim($value, " ;" );
00215         }
00216 
00217         return $ans;
00218 }

is_wildcard ( term  ) 

check for wildcards in search term

Returns:
true if search term $term ends with a wildcard (?*)

Definition at line 48 of file book.php.

Referenced by build_z3950_query().

00048                              {
00049         return (preg_match('/[?*]\s*$/', $term) == 1 );
00050 }

normalize ( term  ) 

strip funny characters from a search term

Returns:
a "normalized" $term

Definition at line 55 of file book.php.

00055                           {
00056 
00057         setlocale(LC_CTYPE,'de_DE');
00058 
00059         # strip whitespace
00060 
00061         $term = preg_replace('/\s\s+/', ' ', $term);
00062         $term = trim($term);
00063 
00064         # strip non-word, non-space characters, e.g. comma, brace, etc.
00065         $term = preg_replace('/[^\w\s\/_-]/', '', $term);
00066 
00067         $term = strtolower($term);
00068 
00069         return $term;
00070 }


Generated on Fri Jul 14 17:38:55 2006 for semapp by  doxygen 1.4.7