• R/O
  • HTTP
  • SSH
  • HTTPS

nucleus-plugins: Commit

Nucleus CMS日本語版用プラグインのうち、日本語版開発者がサポートしているもの


Commit MetaInfo

Revisiondf6da6d091e530bfbff8897a94bff83f262f5948 (tree)
Time2006-10-15 19:52:39
Authorhsur <hsur@1ca2...>
Commiterhsur

Log Message

original version

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@448 1ca29b6e-896d-4ea0-84a5-967f57386b96

Change Summary

Incremental Difference

--- /dev/null
+++ b/trunk/NP_RetainOptions/NP_RetainOptions.php
@@ -0,0 +1,156 @@
1+<?php
2+
3+
4+class NP_RetainOptions extends NucleusPlugin {
5+
6+ function getNAME() { return 'Retain Options'; }
7+ function getAuthor() { return 'Andy'; }
8+ function getURL() { return ''; }
9+ function getVersion() { return '0.5'; }
10+ function getDescription() {
11+ return 'Retain plugin options while you update(uninstall and reinstall) plugins. Keep up to one day';
12+ }
13+
14+ function install() {
15+ $this->createOption("disable", "Disable this plugin", "yesno", "no");
16+ $query = 'CREATE TABLE IF NOT EXISTS '. sql_table('plug_retainoptions_plugin'). ' ('
17+ . 'id int(11) not null auto_increment, '
18+ . 'pluginname VARCHAR(40) NOT NULL, '
19+ . 'storetime DATETIME, '
20+ . 'PRIMARY KEY (id))';
21+ sql_query($query);
22+ $query = 'CREATE TABLE IF NOT EXISTS '. sql_table('plug_retainoptions_options'). ' ('
23+ . 'id int(11) not null, '
24+ . 'optionid int(11) not null auto_increment, '
25+ . 'optionname VARCHAR(20) NOT NULL, '
26+ . 'optioncontext VARCHAR(20), '
27+ . 'PRIMARY KEY (optionid))';
28+ sql_query($query);
29+ $query = 'CREATE TABLE IF NOT EXISTS '. sql_table('plug_retainoptions'). ' ('
30+ . 'optionid int(11) NOT NULL,'
31+ . 'contextid int(11),'
32+ . 'optionvalue VARCHAR(255)'
33+ . ') ';
34+ sql_query($query);
35+ }
36+
37+ function unInstall() {
38+ sql_query('DROP TABLE ' .sql_table('plug_retainoptions_plugin'));
39+ sql_query('DROP TABLE ' .sql_table('plug_retainoptions_options'));
40+ sql_query('DROP TABLE ' .sql_table('plug_retainoptions'));
41+ }
42+
43+ function supportsFeature($what) {
44+ switch($what)
45+ {
46+ case 'SqlTablePrefix':
47+ return 1;
48+ default:
49+ return 0;
50+ }
51+ }
52+ function getTableList() {
53+ return array(sql_table('plug_retainoptions_plugin'), sql_table('plug_retainoptions'));
54+ }
55+ function getEventList() {
56+ return array('PreDeletePlugin','PostAddPlugin');
57+ }
58+
59+ function event_PreDeletePlugin(&$data) {
60+ if ($this->getOption('disable') == 'yes') return;
61+ $plugid = $data['plugid'];
62+ $result = sql_query('SELECT pfile FROM '.sql_table('plugin'). ' WHERE pid='.$plugid);
63+ $plugin = mysql_fetch_array($result);
64+ $pname = strtolower($plugin['pfile']);
65+ mysql_free_result($result);
66+ if ($pname == get_class($this)) return; // don't retain this plugin
67+ $currenttime = mysqldate(time());
68+ sql_query('INSERT INTO '.sql_table('plug_retainoptions_plugin')
69+ . " (pluginname, storetime) VALUES ('$pname', $currenttime)");
70+ $id = mysql_insert_id();
71+ $descs = sql_query('SELECT oid, oname, ocontext FROM '.sql_table('plugin_option_desc')
72+ . ' WHERE opid='.$plugid);
73+ while ($desc = mysql_fetch_array($descs)) {
74+ sql_query('INSERT INTO '.sql_table('plug_retainoptions_options'). ' SET '
75+ . "id=$id"
76+ . ', optionname="'.$desc['oname'].'"'
77+ . ', optioncontext="'.$desc['ocontext'].'"');
78+ $optionid = mysql_insert_id();
79+ $options = sql_query('SELECT ovalue, ocontextid FROM '.sql_table('plugin_option')
80+ . ' WHERE oid='.$desc['oid']);
81+ while ($option = mysql_fetch_array($options)) {
82+ sql_query('INSERT INTO '.sql_table('plug_retainoptions'). ' SET '
83+ . "optionid=$optionid"
84+ . ', contextid='.$option['ocontextid']
85+ . ', optionvalue="'.$option['ovalue'].'"');
86+ }
87+ mysql_free_result($options);
88+ }
89+ mysql_free_result($descs);
90+ }
91+
92+ function event_PostAddPlugin(&$data) {
93+ if ($this->getOption('disable') == 'yes') return;
94+ $plugin = & $data['plugin'];
95+ $pname = get_class($plugin);
96+ $oldesttimestamp = mysqldate(time() - 24*60*60);
97+ $result = sql_query('SELECT id FROM '.sql_table('plug_retainoptions_plugin')
98+ ." WHERE pluginname='$pname' AND STORETIME>=$oldesttimestamp");
99+ $nums = mysql_num_rows($result);
100+ if (!$nums) { $this->cleanup(); return; }
101+ while ($nums--) $row = mysql_fetch_array($result);
102+ mysql_free_result($result);
103+ $id = $row['id'];
104+ $options = sql_query('SELECT optionid, optionname, optioncontext FROM '
105+ . sql_table('plug_retainoptions_options')
106+ . " WHERE id=$id");
107+ while ($option = mysql_fetch_array($options)) {
108+ $optionname = $option['optionname'];
109+ $contextname = $option['optioncontext'];
110+ $odescs = sql_query('SELECT oid FROM '.sql_table('plugin_option_desc')
111+ . ' WHERE opid='.$plugin->plugid
112+ . ' AND oname="'.$optionname.'"'
113+
114+ . ' AND ocontext="'.$contextname.'"');
115+ // restore values only when option name and option context are same
116+ if ($odesc = mysql_fetch_array($odescs)) {
117+ $values = sql_query('SELECT contextid, optionvalue FROM '
118+ . sql_table('plug_retainoptions')
119+ . ' WHERE optionid='.$option['optionid']);
120+ while ($value = mysql_fetch_array($values)) {
121+ // call plugin function instead of directly store in DB
122+ // because some items/blogs/categories might not exist
123+ $plugin->_setOption($contextname,$value['contextid'],
124+ $optionname, $value['optionvalue']);
125+ }
126+ mysql_free_result($values);
127+ }
128+ mysql_free_result($odescs);
129+ }
130+ mysql_free_result($options);
131+ $this->cleanup();
132+ }
133+
134+ function cleanup() {
135+ $oldesttimestamp = time() - 24*60*60;
136+ $result = sql_query('SELECT id FROM '.sql_table('plug_retainoptions_plugin')
137+ ." WHERE STORETIME<$oldesttimestamp");
138+ while ($row = mysql_fetch_array($result)) {
139+ $options = sql_query('SELECT optionid FROM '
140+ . sql_table('plug_retainoptions_options')
141+ . ' WHERE id='.$row['id']);
142+ while ($option = mysql_fetch_array($options)) {
143+ sql_query('DELETE FROM '.sql_table('plug_retainoptions')
144+ . ' WHERE optionid='.$option['optionid']);
145+ }
146+ mysql_free_result($options);
147+ sql_query('DELETE FROM '. sql_table('plug_retainoptions_options')
148+ . ' WHERE id='.$row['id']);
149+ }
150+ mysql_free_result($result);
151+ sql_query('DELETE FROM '.sql_table('plug_retainoptions_plugin')
152+ ." WHERE STORETIME<$oldesttimestamp");
153+ }
154+
155+}
156+?>
\ No newline at end of file
Show on old repository browser