/[drupal]/contributions/modules/comment_notify/comment_notify.install
ViewVC logotype

Contents of /contributions/modules/comment_notify/comment_notify.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.16 - (show annotations) (download) (as text)
Wed Apr 8 01:52:02 2009 UTC (7 months, 2 weeks ago) by greggles
Branch: MAIN
CVS Tags: DRUPAL-6--1-3
Branch point for: DRUPAL-6--1
Changes since 1.15: +27 -3 lines
File MIME type: text/x-php
bug #409850 by greggles: Anonymous subscription affects comment_notify_user_settings table
1 <?php
2 // $Id: comment_notify.install,v 1.15 2009/04/08 01:24:20 greggles Exp $
3 /**
4 * @file
5 * comment_notify.install.
6 */
7
8 /**
9 * Implementation of hook_install().
10 */
11 function comment_notify_install() {
12 // Create the table.
13 drupal_install_schema('comment_notify');
14
15 // Insert a record for each existing comment.
16 if ( $GLOBALS['db_type']=='pgsql') {
17 db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(c.mail || coalesce(u.mail, u.init) || c.uid || c.name || c.nid) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
18 }
19 else {
20 db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
21 }
22
23 // Set module weight low so that other modules act on the comment first.
24 db_query("UPDATE {system} SET weight = 10 WHERE name = 'comment_notify'");
25 }
26
27 /**
28 * Implementation of hook_uninstall().
29 */
30 function comment_notify_uninstall() {
31 drupal_uninstall_schema('comment_notify');
32 variable_del('node_notify_default_mailtext');
33 db_query("DELETE FROM {variable} WHERE name LIKE 'comment_notify_%'");
34 }
35
36 /**
37 * Implementation of hook_schema().
38 */
39 function comment_notify_schema() {
40 $schema['comment_notify'] = array(
41 'description' => t('Stores information about which commenters on the site have subscriped to followup emails.'),
42 'fields' => array(
43 'cid' => array(
44 'type' => 'int',
45 'unsigned' => TRUE,
46 'description' => 'The comment id from {comments}.cid',
47 'not null' => TRUE,
48 'disp-width' => '11'),
49 'notify' => array(
50 'type' => 'int',
51 'description' => 'A boolean indicator for whether or not they subscribed: 1 means subscribed, 0 means not subscribed.',
52 'size' => 'tiny',
53 'not null' => TRUE,
54 'disp-width' => '11'),
55 'notify_hash' => array(
56 'type' => 'varchar',
57 'description' => 'An md5 hash of unique information about the commenter. Used for unsubscribing users.',
58 'length' => '32',
59 'not null' => TRUE,
60 'default' => ''),
61 'notified' => array(
62 'type' => 'int',
63 'description' => 'A boolean indicator for whether or not a notification for the comment has been sent: 1 means yes, 0 means no.',
64 'size' => 'tiny',
65 'not null' => TRUE,
66 'default' => 0,
67 'disp-width' => '11'),
68 ),
69 'primary key' => array('cid'),
70 'indexes' => array(
71 'notify_hash' => array('notify_hash')),
72 );
73 $schema['comment_notify_user_settings'] = array(
74 'fields' => array(
75 'uid' => array(
76 'type' => 'int',
77 'unsigned' => TRUE,
78 'description' => 'The user id from {users}.cid',
79 'not null' => TRUE,
80 'disp-width' => '11'),
81 'node_notify' => array(
82 'type' => 'int',
83 'size' => 'tiny',
84 'not null' => TRUE,
85 'default' => 0,
86 'disp-width' => '11'),
87 'comment_notify' => array(
88 'type' => 'int',
89 'size' => 'tiny',
90 'not null' => TRUE,
91 'default' => 0,
92 'disp-width' => '11'),
93 ),
94 'primary key' => array('uid'),
95 );
96
97 return $schema;
98 }
99
100 /**
101 * Sync up the two tables after as part of http://drupal.org/node/297791
102 */
103 function comment_notify_update_6000() {
104 $return[] = update_sql("DELETE FROM {comment_notify} WHERE cid NOT IN (SELECT cid FROM {comments})");
105 return $return;
106 }
107
108 /**
109 * Permissions got renamed, update them.
110 */
111 function comment_notify_update_6001() {
112 $ret = array();
113 $result = db_query("SELECT pid, perm FROM {permission}");
114 while ($row = db_fetch_object($result)) {
115 $permissions = explode(', ', $row->perm);
116 if (in_array('Subscribe to comments', $permissions) && !in_array('subscribe to comments', $permissions)) {
117 $permissions[] = 'subscribe to comments';
118 }
119 if (in_array('Administer comment notify', $permissions) && !in_array('administer comment notify', $permissions)) {
120 $permissions[] = 'administer comment notify';
121 }
122
123 $permissions = implode(', ', $permissions);
124 $ret[] = update_sql("UPDATE {permission} SET perm = '%s' WHERE pid = %d", $permissions, $row->pid);
125 }
126 return $ret;
127 }
128
129 /**
130 * Drop the notify column from the {comments} table. This column will only exist
131 * if the 5.x-1.x version of comment_notify was installed at some point. Since
132 * the 5.x-2.x version of the module {comment_notify}.notify has been used instead.
133 */
134 function comment_notify_update_6002() {
135 $ret = array();
136 if (db_column_exists('comments', 'notify')) {
137 db_drop_field($ret, 'comments', 'notify');
138 }
139 return $ret;
140 }
141
142 /**
143 * Create a new table to store user preferences and move the $user->data there.
144 */
145 function comment_notify_update_6003() {
146 $ret = array();
147 // This determines how many users will be processed in each batch run.
148 $num_users_per_batch = 100;
149
150 // Multi-part update.
151 if (!isset($_SESSION['comment_notify_update_6003'])) {
152 // We need to start at uid 1, so initialize our variable
153 // to the value below that.
154 $_SESSION['comment_notify_update_6003'] = 1;
155 $_SESSION['comment_notify_update_6003_max'] = db_result(db_query("SELECT MAX(uid) FROM {users}"));
156
157 // Create the table.
158 $schema['comment_notify_user_settings'] = array(
159 'fields' => array(
160 'uid' => array(
161 'type' => 'serial',
162 'unsigned' => TRUE,
163 'description' => 'The user id from {users}.cid',
164 'not null' => TRUE,
165 'disp-width' => '11'),
166 'node_notify' => array(
167 'type' => 'int',
168 'size' => 'tiny',
169 'not null' => TRUE,
170 'default' => 0,
171 'disp-width' => '11'),
172 'comment_notify' => array(
173 'type' => 'int',
174 'size' => 'tiny',
175 'not null' => TRUE,
176 'default' => 0,
177 'disp-width' => '11'),
178 ),
179 'primary key' => array('uid'),
180 );
181
182 db_create_table($ret, 'comment_notify_user_settings', $schema['comment_notify_user_settings']);
183 }
184 // Do the next batch of the deed.
185
186 // Find the next N records to update, or do the final batch.
187 $next = min($_SESSION['comment_notify_update_6003'] + $num_users_per_batch, $_SESSION['comment_notify_update_6003_max']);
188
189 // Check to make sure that the {comment_notify_user_settings} table exists.
190 // If for some reason it was not created above, we might lose data when
191 // we delete the comment_notify data that is currently in {users}.data.
192 // If the table doesn't exist, then alert the user and don't allow any
193 // more batches to be processed.
194 if (!db_table_exists('comment_notify_user_settings')) {
195 unset($_SESSION['comment_notify_update_6003']);
196 unset($_SESSION['comment_notify_update_6003_max']);
197
198 // Alert the user that there was an error.
199 $ret[] = array('success' => FALSE, 'query' => t('For some reason the {comment_notify_user_settings} table was not properly created, and so per-user comment_notify settings could not be copied from {users}.data. You will need to run this update again.'));
200 return $ret;
201 }
202
203 // Transfer the data in our specified range of uid values.
204 $uid = $_SESSION['comment_notify_update_6003'];
205 while ($uid <= $next) {
206 // Get the value of {users}.data.
207 $data = db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $uid));
208 $settings = array('uid' => $uid);
209 if (!empty($data)) {
210 $data = unserialize($data);
211 if (isset($data['node_notify_mailalert'])) {
212 $settings['node_notify'] = $data['node_notify_mailalert'];
213 unset($data['node_notify_mailalert']);
214 }
215 if (isset($data['comment_notify_mailalert'])) {
216 $settings['comment_notify'] = $data['comment_notify_mailalert'];
217 unset($data['comment_notify_mailalert']);
218 }
219 $fields_sql = '';
220 $values_sql = '';
221 foreach ($settings as $field => $value) {
222 $fields_sql .= "$field, ";
223 $values_sql .= '%d, ';
224 }
225 // Trim off any trailing commas and spaces.
226 $fields_sql = rtrim($fields_sql, ', ');
227 $values_sql = rtrim($values_sql, ', ');
228
229 // Add this user and settings to {comment_notify_user_settings} only if
230 // at least one setting was found in {users}.data for this user.
231 if (count($settings) > 1) {
232 db_query("INSERT INTO {comment_notify_user_settings} ($fields_sql) VALUES ($values_sql)", $settings);
233
234 // Remove this comment_notify data from {users}.data.
235 db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $uid);
236 }
237 }
238 $uid++;
239 }
240
241 // Remember where we left off.
242 $_SESSION['comment_notify_update_6003'] = $next;
243
244 if ($_SESSION['comment_notify_update_6003'] == $_SESSION['comment_notify_update_6003_max']) {
245 // We're done, clear these out.
246 unset($_SESSION['comment_notify_update_6003']);
247 unset($_SESSION['comment_notify_update_6003_max']);
248
249 // Provide an explaination of what we did.
250 $ret[] = array('success' => TRUE, 'query' => t('Moved comment_notify user settings data from the {users} table into the {comment_notify_user_settings} table.'));
251 }
252 elseif ($uid == $next) {
253 unset($_SESSION['comment_notify_update_6003']);
254 unset($_SESSION['comment_notify_update_6003_max']);
255 $ret[] = array('success' => FALSE, 'query' => t('Something is maybe not right.'));
256 }
257 else {
258 // Report how much is left to complete.
259 $ret['#finished'] = $_SESSION['comment_notify_update_6003'] / $_SESSION['comment_notify_update_6003_max'];
260 }
261
262 return $ret;
263 }
264
265 /**
266 * Add a "notified" column to {comment_notify} to keep track of whether
267 * notifications have already been sent out for a given comment. This
268 * prevents new notifications from being sent when a comment is edited.
269 */
270 function comment_notify_update_6004() {
271 $ret = array();
272
273 db_add_field($ret, 'comment_notify', 'notified', array('type' => 'int', 'size' => 'small', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
274
275 // Set the value in the notified column to 1 for all existing records.
276 $ret[] = update_sql('UPDATE {comment_notify} SET notified = 1');
277
278 return $ret;
279 }
280
281 /**
282 * Having our foreign keys auto_increment could lead to unexpected results.
283 */
284 function comment_notify_update_6005() {
285 $ret = array();
286
287 db_change_field($ret, 'comment_notify', 'cid', 'cid',
288 array('type' => 'int',
289 'unsigned' => TRUE,
290 'description' => 'The comment id from {comments}.cid',
291 'not null' => TRUE,
292 'disp-width' => '11'));
293
294 db_change_field($ret, 'comment_notify_user_settings', 'uid', 'uid',
295 array('type' => 'int',
296 'unsigned' => TRUE,
297 'description' => 'The user id from {users}.cid',
298 'not null' => TRUE,
299 'disp-width' => '11'));
300
301 return $ret;
302 }

  ViewVC Help
Powered by ViewVC 1.1.2