cms/drupal/modules/forum/forum.install
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Install, update, and uninstall functions for the Forum module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Implements hook_install().
       
    10  */
       
    11 function forum_install() {
       
    12   // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module.
       
    13   db_update('system')
       
    14     ->fields(array('weight' => 1))
       
    15     ->condition('name', 'forum')
       
    16     ->execute();
       
    17   // Forum topics are published by default, but do not have any other default
       
    18   // options set (for example, they are not promoted to the front page).
       
    19   variable_set('node_options_forum', array('status'));
       
    20 }
       
    21 
       
    22 /**
       
    23  * Implements hook_enable().
       
    24  */
       
    25 function forum_enable() {
       
    26   // If we enable forum at the same time as taxonomy we need to call
       
    27   // field_associate_fields() as otherwise the field won't be enabled until
       
    28   // hook modules_enabled is called which takes place after hook_enable events.
       
    29   field_associate_fields('taxonomy');
       
    30   // Create the forum vocabulary if it does not exist.
       
    31   $vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
       
    32   if (!$vocabulary) {
       
    33     $edit = array(
       
    34       'name' => t('Forums'),
       
    35       'machine_name' => 'forums',
       
    36       'description' => t('Forum navigation vocabulary'),
       
    37       'hierarchy' => 1,
       
    38       'module' => 'forum',
       
    39       'weight' => -10,
       
    40     );
       
    41     $vocabulary = (object) $edit;
       
    42     taxonomy_vocabulary_save($vocabulary);
       
    43     variable_set('forum_nav_vocabulary', $vocabulary->vid);
       
    44   }
       
    45 
       
    46   // Create the 'taxonomy_forums' field if it doesn't already exist.
       
    47   if (!field_info_field('taxonomy_forums')) {
       
    48     $field = array(
       
    49       'field_name' => 'taxonomy_forums',
       
    50       'type' => 'taxonomy_term_reference',
       
    51       'settings' => array(
       
    52         'allowed_values' => array(
       
    53           array(
       
    54             'vocabulary' => $vocabulary->machine_name,
       
    55             'parent' => 0,
       
    56           ),
       
    57         ),
       
    58       ),
       
    59     );
       
    60     field_create_field($field);
       
    61 
       
    62     // Create a default forum so forum posts can be created.
       
    63     $edit = array(
       
    64       'name' => t('General discussion'),
       
    65       'description' => '',
       
    66       'parent' => array(0),
       
    67       'vid' => $vocabulary->vid,
       
    68     );
       
    69     $term = (object) $edit;
       
    70     taxonomy_term_save($term);
       
    71 
       
    72     // Create the instance on the bundle.
       
    73     $instance = array(
       
    74       'field_name' => 'taxonomy_forums',
       
    75       'entity_type' => 'node',
       
    76       'label' => $vocabulary->name,
       
    77       'bundle' => 'forum',
       
    78       'required' => TRUE,
       
    79       'widget' => array(
       
    80         'type' => 'options_select',
       
    81       ),
       
    82       'display' => array(
       
    83         'default' => array(
       
    84           'type' => 'taxonomy_term_reference_link',
       
    85          'weight' => 10,
       
    86         ),
       
    87         'teaser' => array(
       
    88           'type' => 'taxonomy_term_reference_link',
       
    89          'weight' => 10,
       
    90         ),
       
    91       ),
       
    92     );
       
    93     field_create_instance($instance);
       
    94   }
       
    95 
       
    96   // Ensure the forum node type is available.
       
    97   node_types_rebuild();
       
    98   $types = node_type_get_types();
       
    99   node_add_body_field($types['forum']);
       
   100 }
       
   101 
       
   102 /**
       
   103  * Implements hook_uninstall().
       
   104  */
       
   105 function forum_uninstall() {
       
   106   // Load the dependent Taxonomy module, in case it has been disabled.
       
   107   drupal_load('module', 'taxonomy');
       
   108 
       
   109   variable_del('forum_containers');
       
   110   variable_del('forum_hot_topic');
       
   111   variable_del('forum_per_page');
       
   112   variable_del('forum_order');
       
   113   variable_del('forum_block_num_active');
       
   114   variable_del('forum_block_num_new');
       
   115   variable_del('node_options_forum');
       
   116 
       
   117   field_delete_field('taxonomy_forums');
       
   118   // Purge field data now to allow taxonomy module to be uninstalled
       
   119   // if this is the only field remaining.
       
   120   field_purge_batch(10);
       
   121 }
       
   122 
       
   123 /**
       
   124  * Implements hook_schema().
       
   125  */
       
   126 function forum_schema() {
       
   127   $schema['forum'] = array(
       
   128     'description' => 'Stores the relationship of nodes to forum terms.',
       
   129     'fields' => array(
       
   130       'nid' => array(
       
   131         'type' => 'int',
       
   132         'unsigned' => TRUE,
       
   133         'not null' => TRUE,
       
   134         'default' => 0,
       
   135         'description' => 'The {node}.nid of the node.',
       
   136       ),
       
   137       'vid' => array(
       
   138         'type' => 'int',
       
   139         'unsigned' => TRUE,
       
   140         'not null' => TRUE,
       
   141         'default' => 0,
       
   142         'description' => 'Primary Key: The {node}.vid of the node.',
       
   143       ),
       
   144       'tid' => array(
       
   145         'type' => 'int',
       
   146         'unsigned' => TRUE,
       
   147         'not null' => TRUE,
       
   148         'default' => 0,
       
   149         'description' => 'The {taxonomy_term_data}.tid of the forum term assigned to the node.',
       
   150       ),
       
   151     ),
       
   152     'indexes' => array(
       
   153       'forum_topic' => array('nid', 'tid'),
       
   154       'tid' => array('tid'),
       
   155     ),
       
   156     'primary key' => array('vid'),
       
   157     'foreign keys' => array(
       
   158       'forum_node' => array(
       
   159         'table' => 'node',
       
   160         'columns' => array(
       
   161           'nid' => 'nid',
       
   162           'vid' => 'vid',
       
   163         ),
       
   164       ),
       
   165     ),
       
   166   );
       
   167 
       
   168   $schema['forum_index'] = array(
       
   169     'description' => 'Maintains denormalized information about node/term relationships.',
       
   170     'fields' => array(
       
   171       'nid' => array(
       
   172         'description' => 'The {node}.nid this record tracks.',
       
   173         'type' => 'int',
       
   174         'unsigned' => TRUE,
       
   175         'not null' => TRUE,
       
   176         'default' => 0,
       
   177       ),
       
   178       'title' => array(
       
   179         'description' => 'The title of this node, always treated as non-markup plain text.',
       
   180         'type' => 'varchar',
       
   181         'length' => 255,
       
   182         'not null' => TRUE,
       
   183         'default' => '',
       
   184       ),
       
   185       'tid' => array(
       
   186          'description' => 'The term ID.',
       
   187          'type' => 'int',
       
   188          'unsigned' => TRUE,
       
   189          'not null' => TRUE,
       
   190          'default' => 0,
       
   191       ),
       
   192       'sticky' => array(
       
   193         'description' => 'Boolean indicating whether the node is sticky.',
       
   194         'type' => 'int',
       
   195         'not null' => FALSE,
       
   196         'default' => 0,
       
   197         'size' => 'tiny',
       
   198       ),
       
   199       'created' => array(
       
   200         'description' => 'The Unix timestamp when the node was created.',
       
   201         'type' => 'int',
       
   202         'unsigned' => TRUE,
       
   203         'not null' => TRUE,
       
   204         'default'=> 0,
       
   205       ),
       
   206       'last_comment_timestamp' => array(
       
   207         'type' => 'int',
       
   208         'not null' => TRUE,
       
   209         'default' => 0,
       
   210         'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
       
   211       ),
       
   212       'comment_count' => array(
       
   213         'type' => 'int',
       
   214         'unsigned' => TRUE,
       
   215         'not null' => TRUE,
       
   216         'default' => 0,
       
   217         'description' => 'The total number of comments on this node.',
       
   218       ),
       
   219     ),
       
   220     'indexes' => array(
       
   221       'forum_topics' => array('nid', 'tid', 'sticky', 'last_comment_timestamp'),
       
   222       'created' => array('created'),
       
   223       'last_comment_timestamp' => array('last_comment_timestamp'),
       
   224     ),
       
   225     'foreign keys' => array(
       
   226       'tracked_node' => array(
       
   227         'table' => 'node',
       
   228         'columns' => array('nid' => 'nid'),
       
   229       ),
       
   230       'term' => array(
       
   231         'table' => 'taxonomy_term_data',
       
   232         'columns' => array(
       
   233           'tid' => 'tid',
       
   234         ),
       
   235       ),
       
   236     ),
       
   237   );
       
   238 
       
   239 
       
   240   return $schema;
       
   241 }
       
   242 
       
   243 /**
       
   244  * Implements hook_update_dependencies().
       
   245  */
       
   246 function forum_update_dependencies() {
       
   247   $dependencies['forum'][7003] = array(
       
   248     // Forum update 7003 uses field API update functions, so must run after
       
   249     // Field API has been enabled.
       
   250     'system' => 7020,
       
   251     // Forum update 7003 relies on updated taxonomy module schema. Ensure it
       
   252     // runs after all taxonomy updates.
       
   253     'taxonomy' => 7010,
       
   254   );
       
   255   return $dependencies;
       
   256 }
       
   257 
       
   258 /**
       
   259  * Add new index to forum table.
       
   260  */
       
   261 function forum_update_7000() {
       
   262   db_drop_index('forum', 'nid');
       
   263   db_add_index('forum', 'forum_topic', array('nid', 'tid'));
       
   264 }
       
   265 
       
   266 /**
       
   267  * Create new {forum_index} table.
       
   268  */
       
   269 function forum_update_7001() {
       
   270   $forum_index = array(
       
   271     'description' => 'Maintains denormalized information about node/term relationships.',
       
   272     'fields' => array(
       
   273       'nid' => array(
       
   274         'description' => 'The {node}.nid this record tracks.',
       
   275         'type' => 'int',
       
   276         'unsigned' => TRUE,
       
   277         'not null' => TRUE,
       
   278         'default' => 0,
       
   279       ),
       
   280       'title' => array(
       
   281         'description' => 'The title of this node, always treated as non-markup plain text.',
       
   282         'type' => 'varchar',
       
   283         'length' => 255,
       
   284         'not null' => TRUE,
       
   285         'default' => '',
       
   286       ),
       
   287       'tid' => array(
       
   288          'description' => 'The term ID.',
       
   289          'type' => 'int',
       
   290          'unsigned' => TRUE,
       
   291          'not null' => TRUE,
       
   292          'default' => 0,
       
   293       ),
       
   294       'sticky' => array(
       
   295         'description' => 'Boolean indicating whether the node is sticky.',
       
   296         'type' => 'int',
       
   297         'not null' => FALSE,
       
   298         'default' => 0,
       
   299         'size' => 'tiny',
       
   300       ),
       
   301       'created' => array(
       
   302         'description' => 'The Unix timestamp when the node was created.',
       
   303         'type' => 'int',
       
   304         'unsigned' => TRUE,
       
   305         'not null' => TRUE,
       
   306         'default'=> 0,
       
   307       ),
       
   308       'last_comment_timestamp' => array(
       
   309         'type' => 'int',
       
   310         'not null' => TRUE,
       
   311         'default' => 0,
       
   312         'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
       
   313       ),
       
   314       'comment_count' => array(
       
   315         'type' => 'int',
       
   316         'unsigned' => TRUE,
       
   317         'not null' => TRUE,
       
   318         'default' => 0,
       
   319         'description' => 'The total number of comments on this node.',
       
   320       ),
       
   321     ),
       
   322     'indexes' => array(
       
   323       'forum_topics' => array('tid', 'sticky', 'last_comment_timestamp'),
       
   324     ),
       
   325     'foreign keys' => array(
       
   326       'tracked_node' => array(
       
   327         'table' => 'node',
       
   328         'columns' => array('nid' => 'nid'),
       
   329       ),
       
   330       'term' => array(
       
   331         'table' => 'taxonomy_term_data',
       
   332         'columns' => array(
       
   333           'tid' => 'tid',
       
   334         ),
       
   335       ),
       
   336     ),
       
   337   );
       
   338   db_create_table('forum_index', $forum_index);
       
   339 
       
   340   $select = db_select('node', 'n');
       
   341   $forum_alias = $select->join('forum', 'f', 'n.vid = f.vid');
       
   342   $ncs_alias = $select->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
       
   343   $select
       
   344     ->fields('n', array('nid', 'title', 'sticky', 'created'))
       
   345     ->fields($forum_alias, array('tid'))
       
   346     ->fields($ncs_alias, array('last_comment_timestamp', 'comment_count'));
       
   347 
       
   348   db_insert('forum_index')
       
   349     ->fields(array('nid', 'title', 'sticky', 'created', 'tid', 'last_comment_timestamp', 'comment_count'))
       
   350     ->from($select)
       
   351     ->execute();
       
   352 }
       
   353 
       
   354 /**
       
   355  * @addtogroup updates-7.x-extra
       
   356  * @{
       
   357  */
       
   358 
       
   359 /**
       
   360  * Add new index to forum_index table.
       
   361  */
       
   362 function forum_update_7002() {
       
   363   db_drop_index('forum_index', 'forum_topics');
       
   364   db_add_index('forum_index', 'forum_topics', array('nid', 'tid', 'sticky', 'last_comment_timestamp'));
       
   365 }
       
   366 
       
   367 /**
       
   368  * Rename field to 'taxonomy_forums'.
       
   369  */
       
   370 function forum_update_7003() {
       
   371   $messages = array();
       
   372 
       
   373   $new_field_name = 'taxonomy_forums';
       
   374 
       
   375   // Test to see if the taxonomy_forums field exists.
       
   376   $fields = _update_7000_field_read_fields(array('field_name' => $new_field_name));
       
   377   if ($fields) {
       
   378     // Since the field exists, we're done.
       
   379     return;
       
   380   }
       
   381 
       
   382   // Calculate the old field name.
       
   383   $vid = variable_get('forum_nav_vocabulary', 0);
       
   384   $vocabulary_machine_name = db_select('taxonomy_vocabulary', 'tv')
       
   385     ->fields('tv', array('machine_name'))
       
   386     ->condition('vid', $vid)
       
   387     ->execute()
       
   388     ->fetchField();
       
   389   $old_field_name = 'taxonomy_' . $vocabulary_machine_name;
       
   390 
       
   391   // Read the old fields.
       
   392   $old_fields = _update_7000_field_read_fields(array('field_name' => $old_field_name));
       
   393   foreach ($old_fields as $old_field) {
       
   394     if ($old_field['storage']['type'] != 'field_sql_storage') {
       
   395       $messages[] = t('Cannot rename field %id (%old_field_name) to %new_field_name because it does not use the field_sql_storage storage type.', array(
       
   396         '%id' => $old_field['id'],
       
   397         '%old_field_name' => $old_field_name,
       
   398         '%new_field_name' => $new_field_name,
       
   399       ));
       
   400       continue;
       
   401     }
       
   402 
       
   403     // Update {field_config}.
       
   404     db_update('field_config')
       
   405       ->fields(array('field_name' => $new_field_name))
       
   406       ->condition('id', $old_field['id'])
       
   407       ->execute();
       
   408 
       
   409     // Update {field_config_instance}.
       
   410     db_update('field_config_instance')
       
   411       ->fields(array('field_name' => $new_field_name))
       
   412       ->condition('field_id', $old_field['id'])
       
   413       ->execute();
       
   414 
       
   415     // The tables that need updating in the form 'old_name' => 'new_name'.
       
   416     $tables = array(
       
   417       'field_data_' . $old_field_name => 'field_data_' . $new_field_name,
       
   418       'field_revision_' . $old_field_name => 'field_revision_' . $new_field_name,
       
   419     );
       
   420     foreach ($tables as $old_table => $new_table) {
       
   421       $old_column_name = $old_field_name . '_tid';
       
   422       $new_column_name = $new_field_name . '_tid';
       
   423 
       
   424       // Rename the column.
       
   425       db_drop_index($old_table, $old_column_name);
       
   426       db_change_field($old_table, $old_column_name, $new_column_name, array(
       
   427         'type' => 'int',
       
   428         'unsigned' => TRUE,
       
   429         'not null' => FALSE,
       
   430       ));
       
   431       db_drop_index($old_table, $new_column_name);
       
   432       db_add_index($old_table, $new_column_name, array($new_column_name));
       
   433 
       
   434       // Rename the table.
       
   435       db_rename_table($old_table, $new_table);
       
   436     }
       
   437   }
       
   438 
       
   439   cache_clear_all('*', 'cache_field', TRUE);
       
   440 
       
   441   return $messages;
       
   442 }
       
   443 
       
   444 /**
       
   445  * Update {forum_index} so that only published nodes are indexed.
       
   446  */
       
   447 function forum_update_7011() {
       
   448   $select = db_select('node', 'n')
       
   449     ->fields('n', array('nid'))
       
   450     ->condition('status', 0 );
       
   451 
       
   452   db_delete('forum_index')
       
   453     ->condition('nid', $select, 'IN')
       
   454     ->execute();
       
   455 }
       
   456 
       
   457 /**
       
   458  * Add 'created' and 'last_comment_timestamp' indexes.
       
   459  */
       
   460 function forum_update_7012() {
       
   461   db_add_index('forum_index', 'created', array('created'));
       
   462   db_add_index('forum_index', 'last_comment_timestamp', array('last_comment_timestamp'));
       
   463 }
       
   464 
       
   465 /**
       
   466  * @} End of "addtogroup updates-7.x-extra".
       
   467  */