[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/module_builder/includes/ -> update.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Module builder file downloading.
   6   * 
   7   * Fetches files from locations specified by hook_module_builder_info 
   8   * implementations (including our own).
   9   *
  10   * The main function here is module_builder_update_documentation().
  11   * Other functions (beginning with underscores) are internal to this file.
  12   * 
  13   * The data that gets passed around is an array keyed by filename. Filenames
  14   * thus be unique; if there is a possibility of filename clash these must 
  15   * be rendered safe, for example by prefixing the module name.
  16   * The keys to each item are:
  17   *  - path: the full path to this file
  18   *  - url: (internal to this file) URL to download this file from.
  19   *  - destination: the module code file where the hooks from this hook data file
  20   *    should be saved by code generation
  21   *  - hook_destinations: an array of destinations for specific hooks. Each key
  22   *    is a full hook name, each value is a destination. This is converted from
  23   *    the data structure hook_module_builder_info() returns.
  24   *  - group: the group this file's hooks should be shown in the Drupal UI.
  25   * Example:
  26   *  [system.core.php] => array(
  27   *    [path]        => /Users/you/data/drupal_hooks/6/system.core.php
  28   *    [url]         => http://cvs.drupal.org/viewvc.py/drupal/contributions/docs/developer/hooks/core.php?view=co&pathrev=DRUPAL-6--1
  29   *    [destination] => %module.module
  30   *    [group]       => core
  31   */
  32  
  33  /**
  34   * Updates hook documentation files.
  35   *
  36   * This function should be called after all settings have been checked.
  37   * It ensures hook documentation files are available (on Drupal 5 and 6 by
  38   * downloading them).
  39   *
  40   * After calling this function, you probably want to pass the returned list
  41   * of files to module_builder_process_hook_data().
  42   * Though really, instead of this function you probably want module_builder_update_data().
  43   * Just saying.
  44   *
  45   * @return
  46   *  Array of hook files suitable for passing to module_builder_process_hook_data().
  47   *  See file documentation for details.
  48   */
  49  function module_builder_update_documentation() {
  50    $directory = _module_builder_get_hooks_directory();
  51    //print_r($directory);
  52  
  53    // Fetch data about the files we need to download.
  54    $hook_files = _module_builder_get_hook_file_urls();
  55    //print_r($hook_files);
  56    
  57    // For testing only: skip downloading, just process.
  58    /*
  59    module_builder_process_hook_data($hook_files);
  60    return $hook_files;
  61    */
  62    
  63    // Retrieve each file and store it in the hooks directory, overwriting what's currently there
  64    foreach ($hook_files as $file_name => $data) {
  65      $file_contents = drupal_http_request($data['url']);
  66      
  67      _module_builder_drush_print("writing $directory/$file_name", 2);
  68      file_put_contents("$directory/$file_name", $destination . $file_contents->data);
  69    }
  70    
  71    // inform that hook documentation has been downloaded.
  72    drupal_set_message(t("Module Builder has just downloaded hook documentation to your %dir directory from CVS. This documentation contains detailed descriptions and usage examples of each of Drupal's hooks. Please view the files for more information, or view them online at the <a href=\"!api\">Drupal API documentation</a> site.", array('%dir' => 'files/'. variable_get('module_builder_hooks_directory', 'hooks'), '!api' => url('http://api.drupal.org/'))));
  73    
  74    return $hook_files;
  75  }
  76  
  77  /**
  78   * Get list of hook file URLS from any modules that declare them.
  79   *
  80   * @return
  81   *   An array of data about the files to download, keyed by (safe) filename:
  82      [system.core.php] => Array
  83        [path] => the full path this file should be saved to
  84        [url] => URL
  85        [destination] => %module.module
  86        [group] => core
  87   */
  88  function _module_builder_get_hook_file_urls() {
  89    // Get data by invoking our hook.
  90    $data = _module_builder_invoke_hook();
  91      
  92    foreach ($data as $module => $module_data) {
  93      $branch = $module_data['branch'];
  94      foreach ($module_data['hook_files'] as $hook_file => $destination) {
  95        $url = str_replace(
  96          array('%file', '%branch'),
  97          array($hook_file, $branch),
  98          $module_data['url']
  99        );
 100        // Create our own safe filename with module prefix.
 101        $hook_file_safe_name = "$module.$hook_file";
 102        $directory = _module_builder_get_hooks_directory();      
 103        $urls[$hook_file_safe_name]['path'] = $directory . '/' . $hook_file_safe_name;  
 104        $urls[$hook_file_safe_name]['url'] = $url;
 105        $urls[$hook_file_safe_name]['destination'] = $destination;
 106        if (isset($module_data['hook_destinations'])) {
 107          $urls[$hook_file_safe_name]['hook_destinations'] = array();
 108          foreach ($module_data['hook_destinations'] as $destination => $hooks) {
 109            $urls[$hook_file_safe_name]['hook_destinations'] += array_fill_keys($hooks, $destination);
 110          }
 111        }
 112        if ($module_data['group'] == '#filenames') {
 113          $urls[$hook_file_safe_name]['group'] = str_replace('.php', '', $hook_file);
 114        }
 115        else {
 116          $urls[$hook_file_safe_name]['group'] = $module_data['group'];
 117        }
 118      }
 119    }
 120    
 121    //print_r($urls);
 122    
 123    return $urls;
 124  }
 125  
 126  /**
 127   * Helper function for building URLs.
 128   *
 129   * Get the branch tag, given the minor revision.
 130   * Gets the major revision from the current Drupal install.
 131   */
 132  function _module_builder_get_cvs_branch($minor) {
 133    list($major, ) = explode('.', VERSION); //
 134    $cvs_version = "DRUPAL-$major--$minor";
 135    return $cvs_version;
 136  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7