esconsut1 / php-rails-clone

A PHP framework that "tries" to live up to what the good guys did with Rails, without the brain damaged results that the Cake PHP guys did.

esconsut1 (author)
Thu Sep 18 11:09:16 -0700 2008
commit  a2909b2a1b530c0ab23f45bfbf6b5f36dd9c5caf
tree    e9423f02d5d99ebcad97dbf033c69f565acb8536
parent  6f6d146fb0b359969bf344bd58844057a45f3549
php-rails-clone / lib / swish.php
100644 143 lines (116 sloc) 3.488 kb
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?
 
class Query
{
    var $results;
    var $hits;
    var $search;
    var $serps;
    var $offset;
    
    var $term;
    var $page;
    var $limit;
    var $images;
    var $exact_match;
    
    var $seconds;
    
    // The swish index
    private $db;
    
    function Query($index = '') {
        $this->hits = 0;
        $this->results = array();
        $this->search = false;
        $this->index($index);
        $this->offset = 1;
        $this->seconds = 0;
        $this->exact_match = false;
    }
    
    // Sets and cleans up the term
    function term($q = '', $or = false) {
        $this->term = trim(strtolower(preg_replace('/[^\w\- ]+/', ' ', $q)));
        
        // we make an or term out of multiple terms
        if ($or && strpos($q, ' ') >= 0 && !preg_match('/\bor\b|\band\b/', $q)) {
            $terms = preg_split('/[\s]+/', $q, -1, PREG_SPLIT_NO_EMPTY);
            $this->term = implode(' OR ', $terms);
        }
        
        if ($this->exact_match == true) {
            $this->term = '"' . $this->term . '"';
        }
    }
    
    function setpage($num = null) {
        $num = is_numeric($num) ? intval($num) : 1;
        $num = $num > 100 ? 100 : $num;
        $num = $num < 1 ? 1 : $num;
        $this->page = $num;
    }
    
    function limit($num = null) {
        $num = intval($num) ? intval($num) : 20;
        $num = $num > 512 ? 512 : $num;
        $num = $num < 1 ? 20 : $num;
        $this->limit = $num;
    }
    
    function images($img = false) {
        $this->images = $img ? true : false;
    }
 
    // Sets the index
    function index($index = false) {
        if (!$index) {
            return(false);
        }
        
        if (file_exists($index)) {
           $this->db = new Swish($index);
           $this->search = $this->db->prepare();
        }
    }
    
    // Just get the number of results found from an index
    function get_hits() {
        if (!$this->search) {
            return(false);
        }
        
        $st = microtime(true);
        try {
            $this->results = $this->search->execute($this->term);
        } catch(Exception $e) {
            return(false);
        }
        $this->seconds = microtime(true) - $st;
        
        $this->hits = $this->results->hits;
        return($this->results->hits);
    }
    
    // Set a filter
    function filter($key, $valstart, $valend) {
        $this->search->setLimit($key, $valstart, $valend);
    }
    
    // Set an result order
    function order($order) {
        $this->search->setSort($order);
    }
    
    // Get us some results
    function get_results() {
        if (!$this->results) {
            $this->get_hits();
        }
        
        if (!$this->hits || !$this->results) {
            return(false);
        }
        
        // Seek to first position
        $seek = 0;
        if ($this->page > 1 && (($this->page-1) * $this->limit + 1) < $this->hits) {
            $seek = ($this->page-1) * $this->limit + 1;
            $this->results->seekResult($seek);
            $this->offset = $seek;
        }
 
        $x = 0;
        while($rec = $this->results->nextResult()) {
            if (!$rec) {
                continue;
            }
            
$rec->sdate = date('Y-m-d', $rec->date);
 
            $this->serps[] = (array)$rec;
            $x++;
            if ($x >= $this->limit) {
                break;
            }
        }
       
        return($x);
    }
}
 
?>