root/branches/release-38/php/lib/mtdb_mysql.php @ 2285

Revision 2285, 8.9 kB (checked in by fumiakiy, 19 months ago)

TagRank and TagCount counts tags associated to pages correctly now. BugId:62435

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