File: PluginPersistence.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (143 lines) | stat: -rw-r--r-- 3,140 bytes parent folder | download | duplicates (4)
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
<?php
/**
 * Handles persistence of data for plugins
 *
 * @author Olivier Berger
 *
 */

/**
 * Handles persistence of data for a plugin as a whole
 *
 * @author Olivier Berger
 *
 */

// May create an interface with 2 implementations : JSON or php serialize...

class PluginPersistentStore extends Error {

	var $plugin_id;

	/*

	CREATE TABLE plugins_persistence (
	plugin_id integer,
	persisted_key character varying(100),
	persisted_data bytea,
	-- CONSTRAINT plugins_persistence_pkey PRIMARY KEY (plugin_id, persisted_key) not necessary to have unique keys
	-- May need an index on (plugin_id, persisted_key)
	)
*/

	const persistence_table = 'plugins_persistence';

	/**
	 * Constructor
	 *
	 * @param integer $plugin_id
	 */
	public function __construct($plugin_id) {
		$this->plugin_id = $plugin_id;
	}

	protected function additionalAndClause() {
		return '';
	}

	/**
	 * Loads persisted objects (saved in serialized form)
	 *
	 * @param string $key
	 * @return array
	 */
	public function readObject($key) {
		$result = array();
		$res = db_query_params('SELECT persisted_data
					FROM '.self::persistence_table.'
					WHERE plugin_id = $1
					AND persisted_key = $2 '.$self->additionalAndClause(),
			array($this->plugin_id,
				$key));
		$rows = db_numrows($res);

		/*
		if ($rows > 1) {
			$this->setError(_('More than one value for the plugin + key'))
		}*/
		for ($i = 0; $i < $rows; $i++) {
			$data = db_result($res, $i, 'persisted_data');
			$object = unserialize($data);
			$result[] = $object;
		}
		return $result;
	}

	public function saveObject($object, $key) {
		$data = serialize($object);
		$res = db_query_params('INSERT INTO '.self::persistence_table.' (plugin_id, persisted_key, persisted_data) VALUES ($1, $2, $3)',
			array($this->plugin_id, $key, $data));
	}

}

/**
 * Handles persistence of data for a plugin, by group
 *
 * @author olivier
 *
 */
class PluginGroupPersistentStore {
	var $group_plugin_id;

	/*
	CREATE TABLE group_plugin_persistence (
	group_plugin_id integer,
	persisted_key character varying(100),
	persisted_data bytea,
	CONSTRAINT plugins_persistence_pkey PRIMARY KEY (group_plugin_id, persisted_key)

		*/
	public function __construct($plugin_id, $group_id) {
		parent::__construct($plugin_id);

		$res = db_query_params('SELECT group_plugin_id
						FROM group_plugin
						WHERE plugin_id=$1
						AND group_id=$2', array($plugin_id, $group_id));
		$rows = db_numrows($res);

		if ($rows > 1) {
			$this->setError(_('More than one value for the plugin + key'));
		}
		$this->group_plugin_id = db_result($res, 0, 'persisted_data');
	}

	protected function additionalAndClause() {
		$clause = parent::additionalAndClause();
		$clause .= 'AND group_plugin_id='.$this->group_plugin_id;
		return $clause;
	}

}

/**
 * Handles persistence of data for a plugin, by user
 *
 * @author olivier
 *
 */
class PluginUserPersistentStore {
	var $user_id;

	/*

   CREATE TABLE user_plugin_persistence (
   user_plugin_id integer,
   persisted_key character varying(100),
   persisted_data bytea,
   CONSTRAINT plugins_persistence_pkey PRIMARY KEY (user_plugin_id, persisted_key)

	   */

}