~ubuntu-branches/debian/sid/ampache/sid

« back to all changes in this revision

Viewing changes to lib/class/database_object.abstract.php

  • Committer: Package Import Robot
  • Author(s): Charlie Smotherman
  • Date: 2013-08-27 13:19:48 UTC
  • mfrom: (1.2.9)
  • Revision ID: package-import@ubuntu.com-20130827131948-1czew0zxn6u70dtv
Tags: 3.6-rzb2752+dfsg-1
* New upsteam snapshot.  Contains important bug fixes to the installer.
* Correct typo in ampache-common.postrm.
* Remove courtousy copy of php-getid3, during repack.  Closes: #701526
* Update package to use dh_linktree to make the needed sym links to the
  needed system libs that were removed during repack.
* Update debian/rules to reflect upstreams removing/moving of modules.
* Update debian/ampache-common.install to reflect upstreams removal of files.
* Updated to use new apache2.4 API. Closes: #669756
* Updated /debian/po/de.po thx David Prévot for the patch.  Closes:  #691963
* M3U import is now ordered, fixed upstream.  Closes: #684984
* Text input area has been resized so IPv6 addresses will now fit, fixed
  upstream.  Closes:  #716230
* Added ampache-common.preinst to make sure that the courtousy copies of code
  dirs are empty so dh_linktree can do it's magic on upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
 
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
 
2
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
3
3
/**
4
 
 * database_object Class
5
 
 *
6
4
 *
7
5
 * LICENSE: GNU General Public License, version 2 (GPLv2)
8
 
 * Copyright (c) 2001 - 2011 Ampache.org All Rights Reserved
 
6
 * Copyright 2001 - 2013 Ampache.org
9
7
 *
10
8
 * This program is free software; you can redistribute it and/or
11
9
 * modify it under the terms of the GNU General Public License v2
20
18
 * along with this program; if not, write to the Free Software
21
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
20
 *
23
 
 * @package     Ampache
24
 
 * @copyright   2001 - 2011 Ampache.org
25
 
 * @license     http://opensource.org/licenses/gpl-2.0 GPLv2
26
 
 * @link        http://www.ampache.org/
27
21
 */
28
22
 
29
23
/**
33
27
 * database based objects in ampache. It attempts to do some standard
34
28
 * caching for all of the objects to cut down on the database calls
35
29
 *
36
 
 * @package     Ampache
37
 
 * @copyright   2001 - 2011 Ampache.org
38
 
 * @license     http://opensource.org/licenses/gpl-2.0 GPLv2
39
 
 * @link        http://www.ampache.org/
40
 
 * @abstract
41
30
 */
42
31
abstract class database_object {
43
32
 
44
 
        private static $object_cache = array();
45
 
 
46
 
        // Statistics for debugging
47
 
        public static $cache_hit = 0;
48
 
        private static $_enabled = false;
49
 
 
50
 
        /**
51
 
         * get_info
52
 
         * retrieves the info from the database and puts it in the cache
53
 
         */
54
 
        public function get_info($id,$table_name='') {
55
 
 
56
 
                $table_name = $table_name ? Dba::escape($table_name) : Dba::escape(strtolower(get_class($this)));
57
 
 
58
 
                // Make sure we've got a real id
59
 
                if (!is_numeric($id)) { return array(); }
60
 
 
61
 
                if (self::is_cached($table_name,$id)) {
62
 
                        return self::get_from_cache($table_name,$id);
63
 
                }
64
 
 
65
 
                $sql = "SELECT * FROM `$table_name` WHERE `id`='$id'";
66
 
                $db_results = Dba::read($sql);
67
 
 
68
 
                if (!$db_results) { return array(); }
69
 
 
70
 
                $row = Dba::fetch_assoc($db_results);
71
 
 
72
 
                self::add_to_cache($table_name,$id,$row);
73
 
 
74
 
                return $row;
75
 
 
76
 
        } // get_info
77
 
 
78
 
        /**
79
 
         * clear_cache
80
 
         */
81
 
        public static function clear_cache() {
82
 
                self::$object_cache = array();
83
 
        }
84
 
 
85
 
        /**
86
 
         * is_cached
87
 
         * this checks the cache to see if the specified object is there
88
 
         */
89
 
        public static function is_cached($index,$id) {
90
 
 
91
 
                // Make sure we've got some parents here before we dive below
92
 
                if (!isset(self::$object_cache[$index])) { return false; }
93
 
 
94
 
                return isset(self::$object_cache[$index][$id]);
95
 
 
96
 
        } // is_cached
97
 
 
98
 
        /**
99
 
         * get_from_cache
100
 
         * This attempts to retrive the specified object from the cache we've got here
101
 
         */
102
 
        public static function get_from_cache($index,$id) {
103
 
 
104
 
                // Check if the object is set
105
 
                if (isset(self::$object_cache[$index])
106
 
                        && isset(self::$object_cache[$index][$id])
107
 
                        ) {
108
 
 
109
 
                        self::$cache_hit++;
110
 
                        return self::$object_cache[$index][$id];
111
 
                }
112
 
 
113
 
                return false;
114
 
 
115
 
        } // get_from_cache
116
 
 
117
 
        /**
118
 
         * add_to_cache
119
 
         * This adds the specified object to the specified index in the cache
120
 
         */
121
 
        public static function add_to_cache($index,$id,$data) {
122
 
 
123
 
                if (!self::$_enabled) { return false; }
124
 
 
125
 
                $value = is_null($data) ? false : $data;
126
 
                self::$object_cache[$index][$id] = $value;
127
 
 
128
 
        } // add_to_cache
129
 
 
130
 
        /**
131
 
         * remove_from_cache
132
 
         * This function clears something from the cache, there are a few places we need to do this
133
 
         * in order to have things display correctly
134
 
         */
135
 
        public static function remove_from_cache($index,$id) {
136
 
 
137
 
                if (isset(self::$object_cache[$index]) && isset(self::$object_cache[$index][$id])) {
138
 
                        unset(self::$object_cache[$index][$id]);
139
 
                }
140
 
 
141
 
        } // remove_from_cache
142
 
 
143
 
        /**
144
 
         * _auto_init
145
 
         * Load in the cache settings once so we can avoid function calls
146
 
         */
147
 
        public static function _auto_init() {
148
 
 
149
 
                self::$_enabled = Config::get('memory_cache');
150
 
 
151
 
        } // _auto_init
 
33
    private static $object_cache = array();
 
34
 
 
35
    // Statistics for debugging
 
36
    public static $cache_hit = 0;
 
37
    private static $_enabled = false;
 
38
 
 
39
    /**
 
40
     * get_info
 
41
     * retrieves the info from the database and puts it in the cache
 
42
     */
 
43
    public function get_info($id,$table_name='') {
 
44
 
 
45
        $table_name = $table_name ? Dba::escape($table_name) : Dba::escape(strtolower(get_class($this)));
 
46
 
 
47
        // Make sure we've got a real id
 
48
        if (!is_numeric($id)) { return array(); }
 
49
 
 
50
        if (self::is_cached($table_name,$id)) {
 
51
            return self::get_from_cache($table_name,$id);
 
52
        }
 
53
 
 
54
        $sql = "SELECT * FROM `$table_name` WHERE `id`='$id'";
 
55
        $db_results = Dba::read($sql);
 
56
 
 
57
        if (!$db_results) { return array(); }
 
58
 
 
59
        $row = Dba::fetch_assoc($db_results);
 
60
 
 
61
        self::add_to_cache($table_name,$id,$row);
 
62
 
 
63
        return $row;
 
64
 
 
65
    } // get_info
 
66
 
 
67
    /**
 
68
     * clear_cache
 
69
     */
 
70
    public static function clear_cache() {
 
71
        self::$object_cache = array();
 
72
    }
 
73
 
 
74
    /**
 
75
     * is_cached
 
76
     * this checks the cache to see if the specified object is there
 
77
     */
 
78
    public static function is_cached($index,$id) {
 
79
 
 
80
        // Make sure we've got some parents here before we dive below
 
81
        if (!isset(self::$object_cache[$index])) { return false; }
 
82
 
 
83
        return isset(self::$object_cache[$index][$id]);
 
84
 
 
85
    } // is_cached
 
86
 
 
87
    /**
 
88
     * get_from_cache
 
89
     * This attempts to retrive the specified object from the cache we've got here
 
90
     */
 
91
    public static function get_from_cache($index,$id) {
 
92
 
 
93
        // Check if the object is set
 
94
        if (isset(self::$object_cache[$index])
 
95
            && isset(self::$object_cache[$index][$id])
 
96
            ) {
 
97
 
 
98
            self::$cache_hit++;
 
99
            return self::$object_cache[$index][$id];
 
100
        }
 
101
 
 
102
        return false;
 
103
 
 
104
    } // get_from_cache
 
105
 
 
106
    /**
 
107
     * add_to_cache
 
108
     * This adds the specified object to the specified index in the cache
 
109
     */
 
110
    public static function add_to_cache($index,$id,$data) {
 
111
 
 
112
        if (!self::$_enabled) { return false; }
 
113
 
 
114
        $value = is_null($data) ? false : $data;
 
115
        self::$object_cache[$index][$id] = $value;
 
116
 
 
117
    } // add_to_cache
 
118
 
 
119
    /**
 
120
     * remove_from_cache
 
121
     * This function clears something from the cache, there are a few places we need to do this
 
122
     * in order to have things display correctly
 
123
     */
 
124
    public static function remove_from_cache($index,$id) {
 
125
 
 
126
        if (isset(self::$object_cache[$index]) && isset(self::$object_cache[$index][$id])) {
 
127
            unset(self::$object_cache[$index][$id]);
 
128
        }
 
129
 
 
130
    } // remove_from_cache
 
131
 
 
132
    /**
 
133
     * _auto_init
 
134
     * Load in the cache settings once so we can avoid function calls
 
135
     */
 
136
    public static function _auto_init() {
 
137
 
 
138
        self::$_enabled = Config::get('memory_cache');
 
139
 
 
140
    } // _auto_init
152
141
 
153
142
} // end database_object