privatemsg.install

  1. 1 privatemsg.install
  2. 7-1 privatemsg.install
  3. 7-2 privatemsg.install
  4. 6-2 privatemsg.install

Install file for privatemsg.module

File

privatemsg.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * Install file for privatemsg.module
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function privatemsg_schema() {
  10. $schema = array();
  11. $schema['pm_index'] = array(
  12. 'description' => '{pm_index} holds indexing information about messages and recipients for fast retrieval',
  13. 'fields' => array(
  14. 'mid' => array(
  15. 'description' => 'Private Message ID',
  16. 'type' => 'int',
  17. 'not null' => TRUE,
  18. 'unsigned' => TRUE,
  19. ),
  20. 'thread_id' => array(
  21. 'description' => 'Messages thread ID',
  22. 'type' => 'int',
  23. 'not null' => TRUE,
  24. 'unsigned' => TRUE,
  25. ),
  26. 'recipient' => array(
  27. 'description' => 'ID of the recipient object, typically user',
  28. 'type' => 'int',
  29. 'not null' => TRUE,
  30. 'unsigned' => TRUE,
  31. ),
  32. 'is_new' => array(
  33. 'description' => 'Whether the user has read this message',
  34. 'type' => 'int',
  35. 'default' => 1,
  36. 'not null' => TRUE,
  37. 'unsigned' => TRUE,
  38. ),
  39. 'deleted' => array(
  40. 'description' => 'Whether the user has deleted this message',
  41. 'type' => 'int',
  42. 'unsigned' => TRUE,
  43. 'not null' => TRUE,
  44. 'default' => 0
  45. ),
  46. 'type' => array(
  47. 'description' => 'Type of recipient object',
  48. 'type' => 'varchar',
  49. 'length' => 255,
  50. 'not null' => TRUE,
  51. 'default' => 'user'
  52. ),
  53. ),
  54. 'primary key' => array('mid', 'recipient', 'type'),
  55. 'indexes' => array(
  56. 'list' => array('recipient', 'type', 'deleted', 'is_new'),
  57. 'messages' => array('mid', 'recipient', 'type'),
  58. 'participants' => array('thread_id', 'recipient', 'type', 'deleted'),
  59. ),
  60. );
  61. $schema['pm_message'] = array(
  62. 'description' => '{pm_messages} holds the message information',
  63. 'fields' => array(
  64. 'mid' => array(
  65. 'description' => 'Private Message ID',
  66. 'type' => 'serial',
  67. 'not null' => TRUE,
  68. 'unsigned' => TRUE,
  69. ),
  70. 'author' => array(
  71. 'description' => 'UID of the author',
  72. 'type' => 'int',
  73. 'not null' => TRUE,
  74. 'unsigned' => TRUE,
  75. ),
  76. 'subject' => array(
  77. 'description' => 'Subject text of the message',
  78. 'type' => 'varchar',
  79. 'length' => 255,
  80. 'not null' => TRUE,
  81. ),
  82. 'body' => array(
  83. 'description' => 'Body of the message',
  84. 'type' => 'text',
  85. 'not null' => TRUE,
  86. 'size' => 'big',
  87. ),
  88. 'format' => array(
  89. 'type' => 'varchar',
  90. 'length' => 255,
  91. 'not null' => FALSE,
  92. 'description' => 'The {filter_formats}.format of the message text.',
  93. ),
  94. 'timestamp' => array(
  95. 'description' => 'Time when the message was sent',
  96. 'type' => 'int',
  97. 'not null' => TRUE,
  98. 'unsigned' => TRUE,
  99. ),
  100. 'has_tokens' => array(
  101. 'description' => 'Indicates if the message has tokens',
  102. 'type' => 'int',
  103. 'size' => 'small',
  104. 'not null' => TRUE,
  105. 'unsigned' => TRUE,
  106. 'default' => 0,
  107. ),
  108. 'reply_to_mid' => array(
  109. 'description' => 'mid of the message this is a reply to (if any)',
  110. 'type' => 'int',
  111. 'default' => 0,
  112. 'not null' => TRUE,
  113. 'unsigned' => TRUE,
  114. )
  115. ),
  116. 'primary key' => array('mid'),
  117. );
  118. $schema['pm_setting'] = array(
  119. 'description' => '{pm_setting} holds user specific (including defaults) settings',
  120. 'fields' => array(
  121. 'id' => array(
  122. 'description' => 'Together with type, associates a setting to a user, role, global default, ...',
  123. 'type' => 'int',
  124. 'not null' => TRUE,
  125. 'unsigned' => TRUE,
  126. ),
  127. 'type' => array(
  128. 'description' => 'Together with id, associates a setting to a user, role, global default, ...',
  129. 'type' => 'varchar',
  130. 'length' => 128,
  131. 'not null' => TRUE,
  132. ),
  133. 'setting' => array(
  134. 'description' => 'The name of a setting',
  135. 'type' => 'varchar',
  136. 'length' => 128,
  137. 'not null' => TRUE,
  138. ),
  139. 'value' => array(
  140. 'description' => 'Holds the value of a given setting',
  141. 'type' => 'int',
  142. ),
  143. ),
  144. 'primary key' => array('id', 'type', 'setting'),
  145. );
  146. return $schema;
  147. }
  148. /**
  149. * Implements hook_install().
  150. */
  151. function privatemsg_install() {
  152. // Define default formats for date format types.
  153. variable_set("date_format_privatemsg_current_day", 'g:i a');
  154. variable_set("date_format_privatemsg_current_year", 'M j');
  155. variable_set("date_format_privatemsg_years", 'n/j/y');
  156. }
  157. /**
  158. * Implements hook_uninstall().
  159. */
  160. function privatemsg_uninstall() {
  161. variable_del('private_message_view_template');
  162. variable_del('privatemsg_per_page');
  163. variable_del('privatemsg_display_loginmessage');
  164. variable_del('privatemsg_display_fields');
  165. variable_del('privatemsg_display_fields_weights');
  166. variable_del('privatemsg_display_link_self');
  167. variable_del('privatemsg_view_default_amount');
  168. variable_del('privatemsg_view_max_amount');
  169. variable_del('privatemsg_view_use_max_as_default');
  170. variable_del('privatemsg_display_profile_links');
  171. variable_del('privatemsg_link_node_types');
  172. variable_del('privatemsg_display_on_teaser');
  173. variable_del('privatemsg_no_messages_notification');
  174. variable_del('privatemsg_display_on_comments');
  175. }
  176. /**
  177. * Implements hook_update_dependencies().
  178. */
  179. function privatemsg_update_dependencies() {
  180. if (module_exists('privatemsg_filter')) {
  181. $dependencies['privatemsg']['7004']['privatemsg_filter'] = '7001';
  182. return $dependencies;
  183. }
  184. }
  185. /**
  186. * Implements hook_update_last_removed().
  187. */
  188. function privatemsg_update_last_removed() {
  189. // Only upgrading from 6.x-1.3 or later is supported.
  190. return 6007;
  191. }
  192. /**
  193. * Add has_tokens field to indicate if a message is using tokens.
  194. */
  195. function privatemsg_update_7000() {
  196. db_add_field('pm_message', 'has_tokens', array(
  197. 'description' => 'Indicates if the message has tokens',
  198. 'type' => 'int',
  199. 'size' => 'small',
  200. 'not null' => TRUE,
  201. 'unsigned' => TRUE,
  202. 'default' => 0,
  203. ));
  204. }
  205. /**
  206. * Update format column to varchar.
  207. */
  208. function privatemsg_update_7001() {
  209. db_change_field('pm_message', 'format', 'format', array(
  210. 'type' => 'varchar',
  211. 'length' => 255,
  212. 'not null' => FALSE,
  213. 'description' => 'The {filter_formats}.format of the message text.',
  214. ));
  215. }
  216. /**
  217. * Add {pm_disable} table.
  218. */
  219. function privatemsg_update_7002() {
  220. // Make sure to not run this update twice.
  221. if (db_table_exists('pm_disable')) {
  222. return;
  223. }
  224. $schema['pm_disable'] = array(
  225. 'description' => '{pm_disable} holds the list of users that have disabled private messaging',
  226. 'fields' => array(
  227. 'uid' => array(
  228. 'description' => 'ID of the user',
  229. 'type' => 'int',
  230. 'not null' => TRUE,
  231. 'unsigned' => TRUE,
  232. ),
  233. ),
  234. 'primary key' => array('uid'),
  235. );
  236. if (!(db_table_exists('pm_disable'))) {
  237. db_create_table('pm_disable', $schema['pm_disable']);
  238. }
  239. }
  240. /**
  241. * Change schema to allow other recipients than single users.
  242. */
  243. function privatemsg_update_7003() {
  244. // Make sure to not run this update twice.
  245. if (db_field_exists('pm_index', 'recipient')) {
  246. return;
  247. }
  248. db_drop_index('pm_index', 'list');
  249. db_drop_index('pm_index', 'messages');
  250. db_drop_index('pm_index', 'participants');
  251. db_change_field('pm_index', 'uid', 'recipient', array(
  252. 'description' => 'ID of the recipient object, typically user',
  253. 'type' => 'int',
  254. 'not null' => TRUE,
  255. 'unsigned' => TRUE,
  256. ));
  257. db_add_field('pm_index', 'type', array(
  258. 'description' => 'Type of recipient object',
  259. 'type' => 'varchar',
  260. 'not null' => TRUE,
  261. 'length' => '255',
  262. 'default' => 'user',
  263. ), array(
  264. 'indexes' => array(
  265. 'list' => array('recipient', 'type', 'deleted', 'is_new'),
  266. 'messages' => array('mid', 'recipient', 'type'),
  267. 'participants' => array('thread_id', 'recipient', 'type', 'deleted'),
  268. ),
  269. ));
  270. }
  271. /**
  272. * Remove duplicate entries in {pm_index}.
  273. */
  274. function privatemsg_update_7004(&$sandbox) {
  275. // If the primary key already exists, this doesn't need to run.
  276. if (db_index_exists('pm_index', 'PRIMARY')) {
  277. return;
  278. }
  279. // First run, initialize sandbox and check if we are ready to run.
  280. if (!isset($sandbox['current_thread_id'])) {
  281. $sandbox['current_thread_id'] = 0;
  282. // Assume that the thread ids are distributed more or less equally over the
  283. // whole data set. This allows us to calculate the approximate progress.
  284. $sandbox['max'] = db_query('SELECT MAX(thread_id) FROM {pm_index}')->fetchField();
  285. }
  286. // Fetch the next 10 thread_ids.
  287. $result = db_query_range('SELECT DISTINCT thread_id FROM {pm_index} WHERE thread_id > :thread_id ORDER BY thread_id ASC', 0, 20, array(':thread_id' => $sandbox['current_thread_id']));
  288. $threads = $result->fetchCol();
  289. if (!empty($threads)) {
  290. // By limiting this slow query (having condition) on a specific set of
  291. // threads, this allows us to process the slow having part on a relatively
  292. // small subset of pm_index that can be selected based on the thread_id
  293. // index. There might also be cases where is_new and/or deleted are not
  294. // equal, use MAX(is_new) and MIN(deleted) for these cases.
  295. $sql = 'SELECT COUNT(*) AS count, pmi.recipient, pmi.type, pmi.mid, pmi.thread_id, MAX(pmi.is_new) As is_new, MIN(pmi.deleted) AS deleted FROM {pm_index} pmi WHERE thread_id IN (:threads) GROUP BY pmi.recipient, pmi.type, pmi.mid, pmi.thread_id HAVING COUNT(*) > 1';
  296. $result = db_query($sql, array(':threads' => $threads));
  297. foreach ($result as $row) {
  298. // Delete all occurrences of these entries.
  299. db_delete('pm_index')
  300. ->condition('mid', $row->mid)
  301. ->condition('recipient', $row->recipient)
  302. ->condition('type', $row->type)
  303. ->execute();
  304. // Re-insert a single entry. We do this to explicitly keep messages
  305. // unread and undeleted if there are conflicting entries.
  306. db_insert('pm_index')
  307. ->fields(array(
  308. 'mid' => $row->mid,
  309. 'thread_id' => $row->thread_id,
  310. 'recipient' => $row->recipient,
  311. 'type' => $row->type,
  312. 'is_new' => $row->is_new,
  313. 'deleted' => $row->deleted,
  314. ))
  315. ->execute();
  316. }
  317. $sandbox['current_thread_id'] = max($threads);
  318. }
  319. // Set #finished based on sandbox.
  320. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current_thread_id'] / $sandbox['max']);
  321. }
  322. /**
  323. * Add primary key to {pm_index}.
  324. */
  325. function privatemsg_update_7005() {
  326. if (!db_index_exists('pm_index', 'PRIMARY')) {
  327. db_add_primary_key('pm_index', array('mid', 'recipient', 'type'));
  328. }
  329. }
  330. /**
  331. * Define default formats for date format types.
  332. */
  333. function privatemsg_update_7006() {
  334. variable_set("date_format_privatemsg_current_day", 'g:i a');
  335. variable_set("date_format_privatemsg_current_year", 'M j');
  336. variable_set("date_format_privatemsg_years", 'n/j/y');
  337. }
  338. /**
  339. * Add the {pm_settings} table.
  340. */
  341. function privatemsg_update_7200() {
  342. $table = array(
  343. 'description' => '{pm_setting} holds user specific (including defaults) settings',
  344. 'fields' => array(
  345. 'id' => array(
  346. 'description' => 'Together with type, associates a setting to a user, role, global default, ...',
  347. 'type' => 'int',
  348. 'not null' => TRUE,
  349. 'unsigned' => TRUE,
  350. ),
  351. 'type' => array(
  352. 'description' => 'Together with id, associates a setting to a user, role, global default, ...',
  353. 'type' => 'varchar',
  354. 'length' => 128,
  355. 'not null' => TRUE,
  356. ),
  357. 'setting' => array(
  358. 'description' => 'The name of a setting',
  359. 'type' => 'varchar',
  360. 'length' => 128,
  361. 'not null' => TRUE,
  362. ),
  363. 'value' => array(
  364. 'description' => 'Holds the value of a given setting',
  365. 'type' => 'int',
  366. ),
  367. ),
  368. 'primary key' => array('id', 'type', 'setting'),
  369. );
  370. db_create_table('pm_setting', $table);
  371. }
  372. /**
  373. * Migrate settings from {pm_disable} and delete table.
  374. */
  375. function privatemsg_update_7201() {
  376. $ret = array();
  377. $result = db_query('SELECT * FROM {pm_disable}');
  378. foreach ($result as $row) {
  379. privatemsg_set_setting('user', $row->uid, 'disabled', 1);
  380. }
  381. db_drop_table('pm_disable');
  382. return $ret;
  383. }
  384. /**
  385. * Add field to track what message this is a reply to (if any).
  386. */
  387. function privatemsg_update_7202() {
  388. db_add_field('pm_message', 'reply_to_mid',
  389. array(
  390. 'description' => 'mid of the message this is a reply to (if any)',
  391. 'type' => 'int',
  392. 'default' => 0,
  393. 'not null' => TRUE,
  394. 'unsigned' => TRUE,
  395. )
  396. );
  397. }

Functions

Namesort descending Description
privatemsg_install Implements hook_install().
privatemsg_schema Implements hook_schema().
privatemsg_uninstall Implements hook_uninstall().
privatemsg_update_7000 Add has_tokens field to indicate if a message is using tokens.
privatemsg_update_7001 Update format column to varchar.
privatemsg_update_7002 Add {pm_disable} table.
privatemsg_update_7003 Change schema to allow other recipients than single users.
privatemsg_update_7004 Remove duplicate entries in {pm_index}.
privatemsg_update_7005 Add primary key to {pm_index}.
privatemsg_update_7006 Define default formats for date format types.
privatemsg_update_7200 Add the {pm_settings} table.
privatemsg_update_7201 Migrate settings from {pm_disable} and delete table.
privatemsg_update_7202 Add field to track what message this is a reply to (if any).
privatemsg_update_dependencies Implements hook_update_dependencies().
privatemsg_update_last_removed Implements hook_update_last_removed().