Jacek Kowalski
2012-06-23 175a52e78df3f3462927885ce6d732cb1b36a818
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
if(!extension_loaded('yaz')) {
    error::add('Brak rozszerzenia YAZ. Wyszukiwanie w bazach Biblioteki Narodowej niemożliwe.');
}
 
class YAZ {
    private static $connection;
    static $timeout = 10;
    
    static function connect($host) {
        self::$connection = yaz_connect($host, array('charset' => 'UTF-8'));
        yaz_syntax(self::$connection, 'marc21');
    }
    
    static function search($query, $start=1, $num=10) {
        yaz_search(self::$connection, 'rpn', $query);
        yaz_range(self::$connection, $start, $num);
        yaz_wait();
        self::is_error();
    }
    
    static function scan($query) {
        yaz_scan(self::$connection, 'rpn', $query);
        yaz_wait();
        self::is_error();
    }
    
    static function scan_result() {
        return yaz_scan_result(self::$connection);
    }
    
    static function scan_get($start=1, $num=10) {
        yaz_range(self::$connection, $start, $num);
        yaz_present(self::$connection);
        yaz_wait();
        self::is_error();
    }
    
    static function hits() {
        return yaz_hits(self::$connection);
    }
    
    static function return_MARCs() {
        $hits = self::hits();
        
        $records = array();
        
        $time = time();
        
        for($i=1; $i<=$hits; $i++) {
            if($time+self::$timeout <= time()) {
                break;
            }
            $record = yaz_record(self::$connection, $i, 'raw');
            $records[] = MARC21::from_string($record);
        }
        
        return $records;
    }
    
    static function return_arrays() {
        $return = array();
        
        $MARCs = self::return_MARCs();
 
        foreach($MARCs as $MARC) {
            $return[] = MARC21::to_array( $MARC );
        }
        
        return $return;
    }
    
    static function is_error() {
        if($e = yaz_error(self::$connection)) {
            error::add('Błąd YAZ: '.$e);
        }
        else
        {
            return FALSE;
        }
    }
}
?>