comment.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * @file
  4. * Support for comment destinations.
  5. */
  6. // TODO:
  7. // Make sure this works with updates, explicit destination keys
  8. /**
  9. * Destination class implementing migration into comments.
  10. */
  11. class MigrateDestinationComment extends MigrateDestinationEntity {
  12. static public function getKeySchema() {
  13. return array(
  14. 'cid' => array(
  15. 'type' => 'int',
  16. 'unsigned' => TRUE,
  17. 'description' => 'ID of destination entity',
  18. ),
  19. );
  20. }
  21. /**
  22. * Save the original setting of comment_maintain_node_statistics
  23. * @var boolean
  24. */
  25. protected $maintainNodeStatistics;
  26. /**
  27. * Return an options array for comment destinations.
  28. *
  29. * @param string $language
  30. * Default language for comments created via this destination class.
  31. * @param string $text_format
  32. * Default text format for comments created via this destination class.
  33. */
  34. static public function options($language, $text_format) {
  35. return compact('language', 'text_format');
  36. }
  37. /**
  38. * Basic initialization
  39. *
  40. * @param string $bundle
  41. * A.k.a. the content type (page, article, etc.) of the ... comment?.
  42. * @param array $options
  43. * Options applied to comments.
  44. */
  45. public function __construct($bundle, array $options = array()) {
  46. parent::__construct('comment', $bundle, $options);
  47. }
  48. /**
  49. * Returns a list of fields available to be mapped for comments attached to
  50. * a particular bundle (node type)
  51. *
  52. * @param Migration $migration
  53. * Optionally, the migration containing this destination.
  54. * @return array
  55. * Keys: machine names of the fields (to be passed to addFieldMapping)
  56. * Values: Human-friendly descriptions of the fields.
  57. */
  58. public function fields($migration = NULL) {
  59. $fields = array();
  60. // First the core (comment table) properties
  61. $fields['cid'] = t('Comment: <a href="@doc">Existing comment ID</a>',
  62. array('@doc' => 'http://drupal.org/node/1349714#cid'));
  63. $fields['nid'] = t('Comment: <a href="@doc">Node (by Drupal ID)</a>',
  64. array('@doc' => 'http://drupal.org/node/1349714#nid'));
  65. $fields['uid'] = t('Comment: <a href="@doc">User (by Drupal ID)</a>',
  66. array('@doc' => 'http://drupal.org/node/1349714#uid'));
  67. $fields['pid'] = t('Comment: <a href="@doc">Parent (by Drupal ID)</a>',
  68. array('@doc' => 'http://drupal.org/node/1349714#pid'));
  69. $fields['subject'] = t('Comment: <a href="@doc">Subject</a>',
  70. array('@doc' => 'http://drupal.org/node/1349714#subject'));
  71. $fields['created'] = t('Comment: <a href="@doc">Created timestamp</a>',
  72. array('@doc' => 'http://drupal.org/node/1349714#created'));
  73. $fields['changed'] = t('Comment: <a href="@doc">Modified timestamp</a>',
  74. array('@doc' => 'http://drupal.org/node/1349714#changed'));
  75. $fields['status'] = t('Comment: <a href="@doc">Status</a>',
  76. array('@doc' => 'http://drupal.org/node/1349714#status'));
  77. $fields['hostname'] = t('Comment: <a href="@doc">Hostname/IP address</a>',
  78. array('@doc' => 'http://drupal.org/node/1349714#hostname'));
  79. $fields['name'] = t('Comment: <a href="@doc">User name (not username)</a>',
  80. array('@doc' => 'http://drupal.org/node/1349714#name'));
  81. $fields['mail'] = t('Comment: <a href="@doc">Email address</a>',
  82. array('@doc' => 'http://drupal.org/node/1349714#mail'));
  83. $fields['homepage'] = t('Comment: <a href="@doc">Homepage</a>',
  84. array('@doc' => 'http://drupal.org/node/1349714#homepage'));
  85. $fields['language'] = t('Comment: <a href="@doc">Language</a>',
  86. array('@doc' => 'http://drupal.org/node/1349714#language'));
  87. $fields['thread'] = t('Comment: <a href="@doc">Thread</a>',
  88. array('@doc' => 'http://drupal.org/node/1349714#thread'));
  89. // Then add in anything provided by handlers
  90. $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
  91. $fields += migrate_handler_invoke_all('Comment', 'fields', $this->entityType, $this->bundle, $migration);
  92. return $fields;
  93. }
  94. /**
  95. * Delete a batch of comments at once.
  96. *
  97. * @param $cids
  98. * Array of comment IDs to be deleted.
  99. */
  100. public function bulkRollback(array $cids) {
  101. migrate_instrument_start('comment_delete_multiple');
  102. $this->prepareRollback($cids);
  103. $result = comment_delete_multiple($cids);
  104. $this->completeRollback($cids);
  105. migrate_instrument_stop('comment_delete_multiple');
  106. return $result;
  107. }
  108. /**
  109. * Import a single comment.
  110. *
  111. * @param $comment
  112. * Comment object to build. Prefilled with any fields mapped in the Migration.
  113. * @param $row
  114. * Raw source data object - passed through to prepare/complete handlers.
  115. * @return array
  116. * Array of key fields (cid only in this case) of the comment that was saved if
  117. * successful. FALSE on failure.
  118. */
  119. public function import(stdClass $comment, stdClass $row) {
  120. $migration = Migration::currentMigration();
  121. // Updating previously-migrated content?
  122. if (isset($row->migrate_map_destid1)) {
  123. if (isset($comment->cid)) {
  124. if ($comment->cid != $row->migrate_map_destid1) {
  125. throw new MigrateException(t("Incoming cid !cid and map destination nid !destid1 don't match",
  126. array('!cid' => $comment->cid, '!destid1' => $row->migrate_map_destid1)));
  127. }
  128. }
  129. else {
  130. $comment->cid = $row->migrate_map_destid1;
  131. }
  132. }
  133. // Fix up timestamps
  134. if (isset($comment->created)) {
  135. $comment->created = MigrationBase::timestamp($comment->created);
  136. }
  137. if (isset($comment->changed)) {
  138. $comment->changed = MigrationBase::timestamp($comment->changed);
  139. }
  140. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  141. if (!isset($comment->cid)) {
  142. throw new MigrateException(t('System-of-record is DESTINATION, but no destination cid provided'));
  143. }
  144. $rawcomment = $comment;
  145. $old_comment = comment_load($comment->cid);
  146. if (empty($old_comment)) {
  147. throw new MigrateException(t('System-of-record is DESTINATION, but commend !cid does not exist',
  148. array('!cid' => $comment->cid)));
  149. }
  150. if (!isset($comment->nid)) {
  151. $comment->nid = $old_comment->nid;
  152. }
  153. if (!isset($comment->created)) {
  154. $comment->created = $old_comment->created;
  155. }
  156. if (!isset($comment->changed)) {
  157. $comment->changed = $old_comment->changed;
  158. }
  159. $this->prepare($comment, $row);
  160. foreach ($rawcomment as $field => $value) {
  161. $old_comment->$field = $comment->$field;
  162. }
  163. $comment = $old_comment;
  164. }
  165. else {
  166. // Set some default properties.
  167. $defaults = array(
  168. 'language' => $this->language,
  169. 'node_type' => $this->bundle,
  170. 'subject' => '',
  171. 'status' => COMMENT_PUBLISHED,
  172. 'uid' => 0,
  173. 'cid' => 0,
  174. 'pid' => 0,
  175. );
  176. foreach ($defaults as $field => $value) {
  177. if (!isset($comment->$field)) {
  178. $comment->$field = $value;
  179. }
  180. }
  181. $this->prepare($comment, $row);
  182. // Make sure we have a nid
  183. if (!isset($comment->nid) || !$comment->nid) {
  184. throw new MigrateException(t('No node ID provided for comment'));
  185. }
  186. // comment_save() hardcodes hostname, so if we're trying to set it we
  187. // need to save it and apply it after
  188. if (isset($comment->hostname)) {
  189. $hostname = $comment->hostname;
  190. }
  191. }
  192. if (isset($comment->cid) && $comment->cid) {
  193. $updating = TRUE;
  194. }
  195. else {
  196. $updating = FALSE;
  197. }
  198. migrate_instrument_start('comment_save');
  199. comment_save($comment);
  200. migrate_instrument_stop('comment_save');
  201. if (isset($hostname) && isset($comment->cid) && $comment->cid > 0) {
  202. db_update('comment')
  203. ->fields(array('hostname' => $hostname))
  204. ->condition('cid', $comment->cid)
  205. ->execute();
  206. }
  207. $this->complete($comment, $row);
  208. if (isset($comment->cid) && $comment->cid > 0) {
  209. $return = array($comment->cid);
  210. if ($updating) {
  211. $this->numUpdated++;
  212. }
  213. else {
  214. $this->numCreated++;
  215. }
  216. }
  217. else {
  218. $return = FALSE;
  219. }
  220. return $return;
  221. }
  222. public function preImport() {
  223. // If maintaining node statistics is enabled, temporarily disable it
  224. $this->maintainNodeStatistics =
  225. variable_get('comment_maintain_node_statistics', TRUE);
  226. if ($this->maintainNodeStatistics) {
  227. $GLOBALS['conf']['comment_maintain_node_statistics'] = FALSE;
  228. }
  229. }
  230. public function postImport() {
  231. // If originally enabled, re-enable and rebuild the stats
  232. if ($this->maintainNodeStatistics) {
  233. $GLOBALS['conf']['comment_maintain_node_statistics'] = TRUE;
  234. // Copied from devel_rebuild_node_comment_statistics
  235. // Empty table
  236. db_truncate('node_comment_statistics')->execute();
  237. // TODO: DBTNG. Ignore keyword is Mysql only? Is only used in the rare case when
  238. // two comments on the same node share same timestamp.
  239. $sql = "
  240. INSERT IGNORE INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) (
  241. SELECT c.nid, c.cid, c.created, c.name, c.uid, c2.comment_count FROM {comment} c
  242. JOIN (
  243. SELECT c.nid, MAX(c.created) AS created, COUNT(*) AS comment_count FROM {comment} c WHERE status=:published GROUP BY c.nid
  244. ) AS c2 ON c.nid = c2.nid AND c.created=c2.created
  245. )";
  246. db_query($sql, array(':published' => COMMENT_PUBLISHED));
  247. // Insert records into the node_comment_statistics for nodes that are missing.
  248. $query = db_select('node', 'n');
  249. $query->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid');
  250. $query->addField('n', 'changed', 'last_comment_timestamp');
  251. $query->addField('n', 'uid', 'last_comment_uid');
  252. $query->addField('n', 'nid');
  253. $query->addExpression('0', 'comment_count');
  254. $query->addExpression('NULL', 'last_comment_name');
  255. $query->isNull('ncs.comment_count');
  256. db_insert('node_comment_statistics')
  257. ->from($query)
  258. ->execute();
  259. }
  260. }
  261. }
  262. class MigrateCommentNodeHandler extends MigrateDestinationHandler {
  263. public function __construct() {
  264. $this->registerTypes(array('node'));
  265. }
  266. public function fields($entity_type, $bundle) {
  267. $fields = array();
  268. $fields['comment'] = t('Whether comments may be posted to the node');
  269. return $fields;
  270. }
  271. }