[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/mollom/ -> mollom.install (source)

   1  <?php
   2  // $Id: mollom.install,v 1.2.2.33 2010/09/12 23:44:14 dries Exp $
   3  
   4  /**
   5   * @file
   6   * Install and uninstall functions as well as schema definition for the Mollom module.
   7   */
   8  
   9  /**
  10   * Implements hook_requirements().
  11   *
  12   * @param $check
  13   *   (optional) Boolean whether to re-check the module's installation and
  14   *   configuration status. Defaults to TRUE, as this argument is not passed for
  15   *   hook_requirements() by default. Passing FALSE allows other run-time code
  16   *   to re-generate requirements error messages to be displayed on other pages
  17   *   than the site's system status report page.
  18   *
  19   * @see mollom_init()
  20   * @see mollom_admin_settings()
  21   * @see _mollom_status()
  22   */
  23  function mollom_requirements($phase = 'runtime', $check = TRUE) {
  24    $requirements = array();
  25    if ($phase == 'runtime') {
  26      // This is invoked from both mollom_install() and mollom_init(); make sure
  27      // that mollom.module is loaded.
  28      drupal_load('module', 'mollom');
  29  
  30      $status = _mollom_status($check);
  31      // Immediately return if everything is in order.
  32      if ($status === TRUE) {
  33        return $requirements;
  34      }
  35      // If not, something is wrong; prepare the requirements entry and set
  36      // defaults for any yet unknown edge-cases.
  37      $requirements['mollom'] = array(
  38        'title' => 'Mollom API keys',
  39        'value' => '',
  40        'severity' => REQUIREMENT_ERROR,
  41      );
  42      // Append a link to the settings page to the error message on all pages,
  43      // except on the settings page itself. These error messages also need to be
  44      // shown on the settings page, since Mollom API keys can be entered later.
  45      $admin_message = '';
  46      if ($_GET['q'] != 'admin/settings/mollom/settings') {
  47        $admin_message = t('Visit the <a href="@settings-url">Mollom settings page</a> to configure your keys.', array(
  48          '@settings-url' => url('admin/settings/mollom/settings'),
  49        ));
  50      }
  51      // Generate an appropriate error message:
  52      // Missing API keys.
  53      if (!$status['keys']) {
  54        $requirements['mollom']['value'] = t('Not configured');
  55        $requirements['mollom']['description'] = t('The Mollom API keys are not configured yet. !admin-message', array(
  56          '!admin-message' => $admin_message,
  57        ));
  58      }
  59      // Invalid API keys.
  60      elseif ($status['keys valid'] === MOLLOM_ERROR) {
  61        $requirements['mollom']['value'] = t('Invalid');
  62        $requirements['mollom']['description'] = t('The configured Mollom API keys are invalid. !admin-message', array(
  63          '!admin-message' => $admin_message,
  64        ));
  65      }
  66      // Communication error.
  67      elseif ($status['keys valid'] === NETWORK_ERROR) {
  68        $requirements['mollom']['value'] = t('Network error');
  69        $requirements['mollom']['description'] = t('The Mollom servers could not be contacted. Please make sure that your web server can make outgoing HTTP requests.');
  70      }
  71    }
  72    return $requirements;
  73  }
  74  
  75  /**
  76   * Implements hook_schema().
  77   */
  78  function mollom_schema() {
  79    $schema['mollom'] = array(
  80      'description' => 'Stores Mollom responses for content.',
  81      'fields' => array(
  82        'entity' => array(
  83          'description' => 'Entity type of the content.',
  84          'type' => 'varchar',
  85          'length' => 32,
  86          'not null' => TRUE,
  87          'default' => '',
  88        ),
  89        'did' => array(
  90          'description' => 'Unique entity ID of the content.',
  91          'type' => 'varchar',
  92          'length' => 32,
  93          'not null' => TRUE,
  94          'default' => '',
  95        ),
  96        'session' => array(
  97          'description' => "Content author's session ID.",
  98          'type' => 'varchar',
  99          'length' => 255,
 100          'not null' => TRUE,
 101          'default' => '',
 102        ),
 103        'changed' => array(
 104          'description' => 'Unix timestamp when the data was changed.',
 105          'type' => 'int',
 106          'not null' => TRUE,
 107          'default' => 0,
 108        ),
 109        // Server response columns are NULL by default, because any default value
 110        // would have an unintended meaning. Also, values are stored in individual
 111        // columns, so as to be able to join and filter/sort on these values for
 112        // improved content moderation.
 113        'spam' => array(
 114          'description' => 'Text analysis spam check result.',
 115          'type' => 'int',
 116          'size' => 'tiny',
 117          'not null' => FALSE,
 118        ),
 119        'quality' => array(
 120          'description' => 'Text analysis quality check result.',
 121          'type' => 'float',
 122          'size' => 'tiny',
 123          'not null' => FALSE,
 124        ),
 125        'languages' => array(
 126          'description' => 'Text analysis language check result.',
 127          'type' => 'varchar',
 128          'length' => 255,
 129          'not null' => TRUE,
 130          'default' => '',
 131        ),
 132      ),
 133      'indexes' => array('session' => array('session')),
 134      'primary key' => array('entity', 'did'),
 135    );
 136  
 137    $schema['mollom_form'] = array(
 138      'description' => 'Stores configuration of forms protected by Mollom.',
 139      'fields' => array(
 140        'form_id' => array(
 141          'description' => 'The protected form ID.',
 142          'type' => 'varchar',
 143          'length' => 255,
 144          'not null' => TRUE,
 145          'default' => '',
 146        ),
 147        'mode' => array(
 148          'description' => 'Protection mode for the form.',
 149          'type' => 'int',
 150          'size' => 'tiny',
 151          'not null' => TRUE,
 152          'default' => 0,
 153        ),
 154        'checks' => array(
 155          'description' => 'Text analyis checks to perform.',
 156          'type' => 'text',
 157          'not null' => FALSE,
 158          'serialize' => TRUE,
 159        ),
 160        'enabled_fields' => array(
 161          'description' => 'Form elements to analyze.',
 162          'type' => 'text',
 163          'not null' => FALSE,
 164          'serialize' => TRUE,
 165        ),
 166        'module' => array(
 167          'description' => 'Module name owning the form.',
 168          'type' => 'varchar',
 169          'length' => 255,
 170          'not null' => TRUE,
 171          'default' => '',
 172        ),
 173      ),
 174      'primary key' => array('form_id'),
 175    );
 176  
 177    $schema['cache_mollom'] = drupal_get_schema_unprocessed('system', 'cache');
 178    $schema['cache_mollom']['description'] = 'Cache table for the Mollom module to store information for forms it protects.';
 179  
 180    return $schema;
 181  }
 182  
 183  /**
 184   * Implements hook_install().
 185   */
 186  function mollom_install() {
 187    drupal_install_schema('mollom');
 188  
 189    // Point the user to Mollom's settings page after installation.
 190    $requirements = mollom_requirements('runtime', FALSE);
 191    // When running tests in D6, hook_install() seems to be invoked very (too?)
 192    // early, leading to a potentially predefined valid module configuration from
 193    // the parent site, therefore throwing a PHP notice here.
 194    if (isset($requirements['mollom'])) {
 195      drupal_set_message($requirements['mollom']['description'], 'warning');
 196    }
 197  }
 198  
 199  /**
 200   * Implements hook_uninstall().
 201   */
 202  function mollom_uninstall() {
 203    db_query("DELETE FROM {variable} WHERE name LIKE 'mollom_%'");
 204    drupal_uninstall_schema('mollom');
 205  }
 206  
 207  /**
 208   * An update function to add the language field.
 209   */
 210  function mollom_update_1() {
 211    $ret = array();
 212    db_add_field($ret, 'mollom', 'languages', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
 213    return $ret;
 214  }
 215  
 216  /**
 217   * Create the cache_mollom table.
 218   */
 219  function mollom_update_2() {
 220    $ret = array();
 221    $schema = drupal_get_schema_unprocessed('system', 'cache');
 222    db_create_table($ret, 'cache_mollom', $schema);
 223    return $ret;
 224  }
 225  
 226  /**
 227   * Upgrade form protection storage.
 228   */
 229  function mollom_update_3() {
 230    // Load hook_mollom_form_info() implementations for mollom_form_list().
 231    foreach (module_list(FALSE, FALSE) as $module) {
 232      drupal_load('module', $module);
 233    }
 234    drupal_load('module', 'mollom');
 235  
 236    foreach (mollom_form_list() as $form_id => $info) {
 237      $name = 'mollom_' . $form_id;
 238      $mode = variable_get($name, NULL);
 239      // $mode was stored as 1; convert to MOLLOM_MODE_ANALYSIS.
 240      if (isset($mode)) {
 241        variable_set($name, MOLLOM_MODE_ANALYSIS);
 242      }
 243    }
 244  }
 245  
 246  /**
 247   * Add a reputation field to the mollom table.
 248   */
 249  function mollom_update_4() {
 250    // Unused. Removed in mollom_update_6114().
 251    $ret = array();
 252    return $ret;
 253  }
 254  
 255  /**
 256   * Add the {mollom_form} table.
 257   */
 258  function mollom_update_6105() {
 259    $ret = array();
 260    $schema = array(
 261      'fields' => array(
 262        'form_id' => array(
 263          'type' => 'varchar',
 264          'length' => 255,
 265          'not null' => TRUE,
 266          'default' => '',
 267        ),
 268        'mode' => array(
 269          'type' => 'int',
 270          'size' => 'tiny',
 271          'not null' => TRUE,
 272          'default' => 0,
 273        ),
 274        'enabled_fields' => array(
 275          'type' => 'text',
 276          'serialize' => TRUE,
 277        ),
 278        'module' => array(
 279          'type' => 'varchar',
 280          'length' => 255,
 281          'not null' => TRUE,
 282          'default' => '',
 283        ),
 284      ),
 285      'primary key' => array('form_id'),
 286    );
 287    db_create_table($ret, 'mollom_form', $schema);
 288  
 289    // Migrate form configuration for enabled, supported modules.
 290    foreach (module_list(FALSE, FALSE) as $module) {
 291      drupal_load('module', $module);
 292    }
 293    drupal_load('module', 'mollom');
 294  
 295    $form_list = mollom_form_list();
 296    $result = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'mollom_%%' AND name NOT IN ('mollom_servers', 'mollom_fallback', 'mollom_public_key', 'mollom_private_key')");
 297    while ($row = db_fetch_object($result)) {
 298      $form_id = substr($row->name, 7);
 299      $mode = unserialize($row->value);
 300      if (!empty($mode) && isset($form_list[$form_id])) {
 301        $info = $form_list[$form_id];
 302        $info += mollom_form_info($form_id, $info['module']);
 303        $info['enabled_fields'] = ($mode == MOLLOM_MODE_ANALYSIS ? array_keys($info['elements']) : array());
 304        db_query("INSERT INTO {mollom_form} (form_id, mode, enabled_fields, module) VALUES ('%s', %d, '%s', '%s')", array($form_id, $mode, serialize($info['enabled_fields']), $info['module']));
 305      }
 306      variable_del($row->name);
 307    }
 308    return $ret;
 309  }
 310  
 311  /**
 312   * Add the {mollom}.form_id column.
 313   *
 314   * @todo Rename 'did' column after mollom_set_data() and mollom_get_data() have
 315   *   been removed.
 316   */
 317  function mollom_update_6106() {
 318    $ret = array();
 319    // Add the 'entity' column.
 320    db_add_field($ret, 'mollom', 'entity', array(
 321      'type' => 'varchar',
 322      'length' => 32,
 323      'not null' => TRUE,
 324      'default' => '',
 325    ));
 326  
 327    // Change the primary key to prevent duplicate key errors in the following
 328    // data conversions.
 329    db_drop_primary_key($ret, 'mollom');
 330    db_add_primary_key($ret, 'mollom', array('entity', 'did'));
 331  
 332    // Migrate existing node data.
 333    $ret[] = update_sql("UPDATE {mollom} SET entity = 'node' WHERE did LIKE 'node-%'");
 334    $ret[] = update_sql("UPDATE {mollom} SET did = SUBSTR(did, 6) WHERE entity = 'node'");
 335    // Migrate existing comment data.
 336    $ret[] = update_sql("UPDATE {mollom} SET entity = 'comment' WHERE did LIKE 'comment-%'");
 337    $ret[] = update_sql("UPDATE {mollom} SET did = SUBSTR(did, 9) WHERE entity = 'comment'");
 338  
 339    // Decrease the size of the 'did' column.
 340    // @todo We do not change the type to 'int' here to still support named
 341    //   identifiers. Reconsider this.
 342    db_change_field($ret, 'mollom', 'did', 'did', array(
 343      'type' => 'varchar',
 344      'length' => 32,
 345      'not null' => TRUE,
 346      'default' => '',
 347    ));
 348  
 349    return $ret;
 350  }
 351  
 352  /**
 353   * Disable the privacy policy link for existing sites by default.
 354   */
 355  function mollom_update_6107() {
 356    $ret = array();
 357    variable_set('mollom_privacy_link', 0);
 358    $ret[] = array('success' => TRUE, 'query' => "Link to Mollom's privacy policy on forms protected via textual analysis has been disabled.");
 359    return $ret;
 360  }
 361  
 362  /**
 363   * Rename 'post with no checking' permission to 'bypass mollom protection'.
 364   */
 365  function mollom_update_6108() {
 366    $ret = array();
 367    $ret[] = update_sql("UPDATE {permission} SET perm = REPLACE(perm, 'post with no checking', 'bypass mollom protection')");
 368    return $ret;
 369  }
 370  
 371  /**
 372   * Rename 'fields' column to 'enabled_fields'; previously introduced in mollom_update_6105().
 373   *
 374   * 'fields' is a reserved keyword in MySQL.
 375   */
 376  function mollom_update_6109() {
 377    $ret = array();
 378    if (db_column_exists('mollom_form', 'fields')) {
 379      db_change_field($ret, 'mollom_form', 'fields', 'enabled_fields', array(
 380        'type' => 'text',
 381        'serialize' => TRUE,
 382      ));
 383    }
 384    return $ret;
 385  }
 386  
 387  /**
 388   * Automatically update the new 'mollom_status' variable.
 389   *
 390   * _mollom_status() was introduced in 6.x-1.11 to prevent Mollom from
 391   * interfering with forms when it is incorrectly configured. Sites updating from
 392   * previous versions should be correctly configured, so we just invoke it here.
 393   */
 394  function mollom_update_6110() {
 395    $ret = array();
 396    drupal_load('module', 'mollom');
 397    _mollom_status(TRUE);
 398    return $ret;
 399  }
 400  
 401  /**
 402   * Fix enabled_fields array for CAPTCHA-only protected forms.
 403   *
 404   * mollom_update_6105() incorrectly stored enabled_fields values for forms
 405   * protected by CAPTCHAs only.
 406   */
 407  function mollom_update_6111() {
 408    $ret = array();
 409    // @see update_sql()
 410    $sql = "UPDATE {mollom_form} SET enabled_fields = '%s' WHERE mode = %d";
 411    $result = db_query($sql, array(serialize(array()), 1));
 412    $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
 413    return $ret;
 414  }
 415  
 416  /**
 417   * Add {mollom}.changed column to auto-flush expired entries.
 418   */
 419  function mollom_update_6112() {
 420    $ret = array();
 421    if (!db_column_exists('mollom', 'changed')) {
 422      db_add_field($ret, 'mollom', 'changed', array(
 423        'type' => 'int',
 424        'not null' => TRUE,
 425        'default' => 0,
 426      ));
 427      db_query("UPDATE {mollom} SET changed = %d", array(time()));
 428    }
 429    return $ret;
 430  }
 431  
 432  /**
 433   * Replace {mollom_form}.data with {mollom_form}.checks.
 434   */
 435  function mollom_update_6113() {
 436    $ret = array();
 437    // Add {mollom_form}.checks.
 438    if (!db_column_exists('mollom_form', 'checks')) {
 439      db_add_field($ret, 'mollom_form', 'checks', array(
 440        'type' => 'text',
 441        'not null' => FALSE,
 442        'serialize' => TRUE,
 443      ));
 444      // Default all checks to 'spam', including CAPTCHA-only rows, so spam
 445      // checking is enabled by default when switching the protection mode.
 446      // @see update_sql()
 447      $sql = "UPDATE {mollom_form} SET checks = '%s'";
 448      $result = db_query($sql, array(serialize(array('spam'))));
 449      $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
 450    }
 451    // {mollom_form}.data did never exist in D6.
 452  
 453    return $ret;
 454  }
 455  
 456  /**
 457   * Clean up and complete server response columns in {mollom}.
 458   */
 459  function mollom_update_6114() {
 460    $ret = array();
 461    // Remove 'reputation' column introduced in mollom_update_4(); unused.
 462    if (db_column_exists('mollom', 'reputation')) {
 463      db_drop_field($ret, 'mollom', 'reputation');
 464    }
 465  
 466    // Change {mollom}.quality from varchar into float.
 467    db_change_field($ret, 'mollom', 'quality', 'quality', array(
 468      'type' => 'float',
 469      'size' => 'tiny',
 470      'not null' => FALSE,
 471    ));
 472  
 473    // Add {mollom}.spam.
 474    if (!db_column_exists('mollom', 'spam')) {
 475      db_add_field($ret, 'mollom', 'spam', array(
 476        'type' => 'int',
 477        'size' => 'tiny',
 478        'not null' => FALSE,
 479      ));
 480      // Fill {mollom}.spam with approximate values based on {mollom}.quality.
 481      // Note that this is just to have some values. 'quality' and 'spam' are
 482      // completely unrelated otherwise.
 483      // MOLLOM_ANALYSIS_SPAM
 484      $ret[] = update_sql("UPDATE {mollom} SET spam = 2 WHERE quality < 0.5");
 485      // MOLLOM_ANALYSIS_UNSURE
 486      $ret[] = update_sql("UPDATE {mollom} SET spam = 3 WHERE quality = 0.5");
 487      // MOLLOM_ANALYSIS_HAM
 488      $ret[] = update_sql("UPDATE {mollom} SET spam = 1 WHERE quality > 0.5");
 489    }
 490  
 491    // Addition of {mollom}.profanity deferred to D7.
 492  
 493    return $ret;
 494  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7