root/branches/release-40/php/lib/mtdb_mysql.php @ 2568

Revision 2568, 8.9 kB (checked in by fumiakiy, 18 months ago)

The number of day should be N - 1 because it's compared to current_date. BugId:80061

  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id$
7
8require_once("ezsql".DIRECTORY_SEPARATOR."ezsql_mysql.php");
9require_once("mtdb_base.php");
10
11class MTDatabase_mysql extends MTDatabaseBase {
12    var $vendor = 'mysql';
13    function apply_limit_sql($sql, $limit, $offset = 0) {
14        $limit = intval($limit);
15        $offset = intval($offset);
16        $limitStr = '';
17        if ($limit == -1) $limit = 0;
18        if ($limit || $offset) {
19            if (!$limit) $limit = 2147483647;
20            $limitStr = 'limit ' . ($offset ? $offset . ',' : '')
21                . $limit;
22        }
23        $sql = preg_replace('/<LIMIT>/', $limitStr, $sql);
24        return $sql;
25    }
26    function limit_by_day_sql($column, $days) {
27        $days -= 1;
28        return 'date_add(' . $column .', interval ' . 
29            $days . ' day) >= current_date()';
30    }
31
32    function query_start($query)
33    {
34        // For reg expressions
35        $query = trim($query); 
36
37        // Query was an insert, delete, update, replace
38        if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
39        {
40            return false;
41        }
42
43        $this->savedqueries[] = $query;
44
45        // Flush cached values..
46        $this->flush();
47
48        // Log how the function was called
49        $this->func_call = "\$db->query_start(\"$query\")";
50
51        // Keep track of the last query for debug..
52        $this->last_query = $query;
53
54        // Perform the query via std mysql_query function..
55        $this->result = @mysql_query($query,$this->dbh);
56        $this->num_queries++;
57
58        // If there is an error then take note of it..
59        if ( mysql_error() )
60        {
61            $this->print_error();
62            return false;
63        }
64       
65        // Take note of column info
66        $i=0;
67        while ($i < @mysql_num_fields($this->result))
68        {
69            $this->col_info[$i] = @mysql_fetch_field($this->result);
70            $i++;
71        }
72
73        $this->last_result = array();
74        $this->num_rows = 0;
75     
76        // If debug ALL queries
77        $this->trace || $this->debug_all ? $this->debug() : null ;
78
79        return true;
80    }
81
82    function query_fetch($output=OBJECT) {
83        if ( $row = @mysql_fetch_object($this->result) )
84        {
85            $this->num_rows++;
86
87            if ( $output == OBJECT )
88            {
89                return $row;
90            }
91            // If the output is an associative array then return row as such..
92            elseif ( $output == ARRAY_A )
93            {
94                return $this->convert_fieldname(get_object_vars($row));
95            }
96            // If the output is an numerical array then return row as such..
97            elseif ( $output == ARRAY_N )
98            {
99                return array_values(get_object_vars($row));
100            }
101        }
102        return null;
103    }
104
105    function query_finish() {
106        if (isset($this->result)) {
107            @mysql_free_result($this->result);
108            unset($this->result);
109        }
110    }
111
112    function &fetch_entry_tags($args) {
113        $class = 'entry';
114        if (isset($args['class'])) {
115            $class = $args['class'];
116        } else {
117          $args['class'] = $class;
118        }
119       
120        # load tags
121        if (isset($args['entry_id'])) {
122            if (!isset($args['tags'])) {
123              if (isset($this->_entry_tag_cache[$args['entry_id']]))
124                    return $this->_entry_tag_cache[$args['entry_id']];
125            }
126            $tags =& $this->fetch_tags_by_entry($args);
127            if (!isset($args['tags']))
128                $this->_entry_tag_cache[$args['entry_id']] = $tags;
129            return $tags;
130        }
131        $blog_filter = $this->include_exclude_blogs($args);
132        if ($blog_filter == '' and isset($args['blog_id'])) {
133            if (!isset($args['tags'])) {
134                if (!isset($args['entry_id'])) {
135                    if (isset($this->_blog_tag_cache[$args['blog_id']]))
136                        return $this->_blog_tag_cache[$args['blog_id']];
137                }
138            }
139            $blog_filter = ' = '. intval($args['blog_id']);
140        }
141        if ($blog_filter != '') 
142            $blog_filter = 'and objecttag_blog_id ' . $blog_filter;
143        if (!isset($args['include_private'])) {
144            $private_filter = 'and (tag_is_private = 0 or tag_is_private is null)';
145        }
146        if (isset($args['tags']) && ($args['tags'] != '')) {
147            $tag_list = '';
148            require_once("MTUtil.php");
149            $tag_array = tag_split($args['tags']);
150            foreach ($tag_array as $tag) {
151                if ($tag_list != '') $tag_list .= ',';
152                $tag_list .= "'" . $this->escape($tag) . "'";
153            }
154            if ($tag_list != '') {
155                $tag_filter = 'and (tag_name in (' . $tag_list . '))';
156                $private_filter = '';
157            }
158        }
159        $sort_col = isset($args['sort_by']) ? $args['sort_by'] : 'name';
160        $sort_col = "tag_$sort_col";
161        if (isset($args['sort_order']) and $args['sort_order'] == 'descend') {
162            $order = 'desc';
163        } else {
164            $order = 'asc';
165        }
166        $id_order = '';
167        if ($sort_col == 'tag_name') {
168            $sort_col = 'lower(tag_name)';
169        } else {
170            $id_order = ', lower(tag_name)';
171        }
172
173
174        $sql = "
175            select tag_id, tag_name, count(distinct entry_id) as tag_count
176              from mt_tag left join mt_objecttag on objecttag_tag_id = tag_id
177                          left join mt_entry on entry_id = objecttag_object_id
178             where objecttag_object_datasource='entry'
179               and entry_status = 2
180                   and entry_class='$class'
181                   $blog_filter
182                   $tag_filter
183                   $entry_filter
184                   $private_filter
185          group by tag_id, tag_name
186          order by $sort_col $order $id_order";
187        $tags = $this->get_results($sql, ARRAY_A);
188        return $tags;
189    }
190
191    function &fetch_tags_by_entry($args) {
192        $class = 'entry';
193        if (isset($args['class'])) {
194            $class = $args['class'];
195        }
196
197        # load tags by entry_id
198        if (isset($args['entry_id'])) {
199            $entry_filter = 'and B.objecttag_object_id = '.intval($args['entry_id']);
200        }
201        if (isset($args['blog_id'])) {
202            $blog_filter = 'and A.objecttag_blog_id = '.intval($args['blog_id']);
203        }
204
205        if (!isset($args['include_private'])) {
206            $private_filter = 'and (tag_is_private = 0 or tag_is_private is null)';
207        }
208
209        if (isset($args['tags']) && ($args['tags'] != '')) {
210            $tag_list = '';
211            require_once("MTUtil.php");
212            $tag_array = tag_split($args['tags']);
213            foreach ($tag_array as $tag) {
214                if ($tag_list != '') $tag_list .= ',';
215                $tag_list .= "'" . $this->escape($tag) . "'";
216            }
217            if ($tag_list != '') {
218                $tag_filter = 'and (tag_name in (' . $tag_list . '))';
219                $private_filter = '';
220            }
221        }
222
223        $sql = "
224            select
225                A.objecttag_tag_id tag_id
226                , C.tag_name
227                , count(A.objecttag_tag_id) as tag_count
228                , B.objecttag_object_id
229                , A.objecttag_blog_id
230            from
231                mt_objecttag A
232                left join mt_objecttag B on A.objecttag_tag_id = B.objecttag_tag_id
233                left join mt_entry D on A.objecttag_object_id = D.entry_id $entry_filter $blog_filter
234                ,mt_tag C
235            where
236                D.entry_class='$class'
237                and A.objecttag_object_datasource='entry'
238                and C.tag_id=A.objecttag_tag_id
239                and entry_status = 2
240                $tag_filter
241                $private_filter
242            group by
243                A.objecttag_blog_id
244                , A.objecttag_tag_id
245                , B.objecttag_object_id
246          order by
247                C.tag_name";
248        $tags = $this->get_results($sql, ARRAY_A);
249        return $tags;
250    }
251
252    function &fetch_tag_by_name($tag_name) {
253        $tag_name = $this->escape($tag_name);
254        $tag = $this->get_row("
255            select *
256              from mt_tag
257             where tag_name = binary '$tag_name'
258        ", ARRAY_A);
259        $this->_tag_id_cache[$tag['tag_id']] = $tag;
260        return $tag;
261    }
262
263    function entries_recently_commented_on_sql($subsql) {
264      $sql = $subsql;
265        $sql = preg_replace("/from mt_entry\s+left/i",
266                    ",MAX(comment_created_on) as cco from mt_entry\ninner join mt_comment on comment_entry_id = entry_id and comment_visible = 1\nleft",
267                    $sql);
268        $sql = preg_replace("/order by(.+)/i",
269                    "group by entry_id order by cco desc, \$1 <LIMIT>",
270                   $sql);
271      return $sql;
272    }
273
274
275}
276?>
Note: See TracBrowser for help on using the browser.