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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
<?php
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id: mtdb_mysql.php 2568 2008-06-13 05:41:20Z fumiakiy $
require_once("ezsql".DIRECTORY_SEPARATOR."ezsql_mysql.php");
require_once("mtdb_base.php");
class MTDatabase_mysql extends MTDatabaseBase {
var $vendor = 'mysql';
function apply_limit_sql($sql, $limit, $offset = 0) {
$limit = intval($limit);
$offset = intval($offset);
$limitStr = '';
if ($limit == -1) $limit = 0;
if ($limit || $offset) {
if (!$limit) $limit = 2147483647;
$limitStr = 'limit ' . ($offset ? $offset . ',' : '')
. $limit;
}
$sql = preg_replace('/<LIMIT>/', $limitStr, $sql);
return $sql;
}
function limit_by_day_sql($column, $days) {
$days -= 1;
return 'date_add(' . $column .', interval ' .
$days . ' day) >= current_date()';
}
function query_start($query)
{
// For reg expressions
$query = trim($query);
// Query was an insert, delete, update, replace
if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
{
return false;
}
$this->savedqueries[] = $query;
// Flush cached values..
$this->flush();
// Log how the function was called
$this->func_call = "\$db->query_start(\"$query\")";
// Keep track of the last query for debug..
$this->last_query = $query;
// Perform the query via std mysql_query function..
$this->result = @mysql_query($query,$this->dbh);
$this->num_queries++;
// If there is an error then take note of it..
if ( mysql_error() )
{
$this->print_error();
return false;
}
// Take note of column info
$i=0;
while ($i < @mysql_num_fields($this->result))
{
$this->col_info[$i] = @mysql_fetch_field($this->result);
$i++;
}
$this->last_result = array();
$this->num_rows = 0;
// If debug ALL queries
$this->trace || $this->debug_all ? $this->debug() : null ;
return true;
}
function query_fetch($output=OBJECT) {
if ( $row = @mysql_fetch_object($this->result) )
{
$this->num_rows++;
if ( $output == OBJECT )
{
return $row;
}
// If the output is an associative array then return row as such..
elseif ( $output == ARRAY_A )
{
return $this->convert_fieldname(get_object_vars($row));
}
// If the output is an numerical array then return row as such..
elseif ( $output == ARRAY_N )
{
return array_values(get_object_vars($row));
}
}
return null;
}
function query_finish() {
if (isset($this->result)) {
@mysql_free_result($this->result);
unset($this->result);
}
}
function &fetch_entry_tags($args) {
$class = 'entry';
if (isset($args['class'])) {
$class = $args['class'];
} else {
$args['class'] = $class;
}
# load tags
if (isset($args['entry_id'])) {
if (!isset($args['tags'])) {
if (isset($this->_entry_tag_cache[$args['entry_id']]))
return $this->_entry_tag_cache[$args['entry_id']];
}
$tags =& $this->fetch_tags_by_entry($args);
if (!isset($args['tags']))
$this->_entry_tag_cache[$args['entry_id']] = $tags;
return $tags;
}
$blog_filter = $this->include_exclude_blogs($args);
if ($blog_filter == '' and isset($args['blog_id'])) {
if (!isset($args['tags'])) {
if (!isset($args['entry_id'])) {
if (isset($this->_blog_tag_cache[$args['blog_id']]))
return $this->_blog_tag_cache[$args['blog_id']];
}
}
$blog_filter = ' = '. intval($args['blog_id']);
}
if ($blog_filter != '')
$blog_filter = 'and objecttag_blog_id ' . $blog_filter;
if (!isset($args['include_private'])) {
$private_filter = 'and (tag_is_private = 0 or tag_is_private is null)';
}
if (isset($args['tags']) && ($args['tags'] != '')) {
$tag_list = '';
require_once("MTUtil.php");
$tag_array = tag_split($args['tags']);
foreach ($tag_array as $tag) {
if ($tag_list != '') $tag_list .= ',';
$tag_list .= "'" . $this->escape($tag) . "'";
}
if ($tag_list != '') {
$tag_filter = 'and (tag_name in (' . $tag_list . '))';
$private_filter = '';
}
}
$sort_col = isset($args['sort_by']) ? $args['sort_by'] : 'name';
$sort_col = "tag_$sort_col";
if (isset($args['sort_order']) and $args['sort_order'] == 'descend') {
$order = 'desc';
} else {
$order = 'asc';
}
$id_order = '';
if ($sort_col == 'tag_name') {
$sort_col = 'lower(tag_name)';
} else {
$id_order = ', lower(tag_name)';
}
$sql = "
select tag_id, tag_name, count(distinct entry_id) as tag_count
from mt_tag left join mt_objecttag on objecttag_tag_id = tag_id
left join mt_entry on entry_id = objecttag_object_id
where objecttag_object_datasource='entry'
and entry_status = 2
and entry_class='$class'
$blog_filter
$tag_filter
$entry_filter
$private_filter
group by tag_id, tag_name
order by $sort_col $order $id_order";
$tags = $this->get_results($sql, ARRAY_A);
return $tags;
}
function &fetch_tags_by_entry($args) {
$class = 'entry';
if (isset($args['class'])) {
$class = $args['class'];
}
# load tags by entry_id
if (isset($args['entry_id'])) {
$entry_filter = 'and B.objecttag_object_id = '.intval($args['entry_id']);
}
if (isset($args['blog_id'])) {
$blog_filter = 'and A.objecttag_blog_id = '.intval($args['blog_id']);
}
if (!isset($args['include_private'])) {
$private_filter = 'and (tag_is_private = 0 or tag_is_private is null)';
}
if (isset($args['tags']) && ($args['tags'] != '')) {
$tag_list = '';
require_once("MTUtil.php");
$tag_array = tag_split($args['tags']);
foreach ($tag_array as $tag) {
if ($tag_list != '') $tag_list .= ',';
$tag_list .= "'" . $this->escape($tag) . "'";
}
if ($tag_list != '') {
$tag_filter = 'and (tag_name in (' . $tag_list . '))';
$private_filter = '';
}
}
$sql = "
select
A.objecttag_tag_id tag_id
, C.tag_name
, count(A.objecttag_tag_id) as tag_count
, B.objecttag_object_id
, A.objecttag_blog_id
from
mt_objecttag A
left join mt_objecttag B on A.objecttag_tag_id = B.objecttag_tag_id
left join mt_entry D on A.objecttag_object_id = D.entry_id $entry_filter $blog_filter
,mt_tag C
where
D.entry_class='$class'
and A.objecttag_object_datasource='entry'
and C.tag_id=A.objecttag_tag_id
and entry_status = 2
$tag_filter
$private_filter
group by
A.objecttag_blog_id
, A.objecttag_tag_id
, B.objecttag_object_id
order by
C.tag_name";
$tags = $this->get_results($sql, ARRAY_A);
return $tags;
}
function &fetch_tag_by_name($tag_name) {
$tag_name = $this->escape($tag_name);
$tag = $this->get_row("
select *
from mt_tag
where tag_name = binary '$tag_name'
", ARRAY_A);
$this->_tag_id_cache[$tag['tag_id']] = $tag;
return $tag;
}
function entries_recently_commented_on_sql($subsql) {
$sql = $subsql;
$sql = preg_replace("/from mt_entry\s+left/i",
",MAX(comment_created_on) as cco from mt_entry\ninner join mt_comment on comment_entry_id = entry_id and comment_visible = 1\nleft",
$sql);
$sql = preg_replace("/order by(.+)/i",
"group by entry_id order by cco desc, \$1 <LIMIT>",
$sql);
return $sql;
}
}
?>
|