drupal 管理界面是否有休息 api(例如,用于清除缓存)

Is there a rest api for the drupal admin interface (e.g. for clearing the cache)

我正在使用基于云的持续集成将我的主题部署到我的 drupal 站点,我希望能够在主题文件复制到位后自动清除缓存。

有没有办法远程执行此操作?

我想如果能休息一下就好了api做这样的事情,所以我可以做例如:

curl http://mysite.example.com/admin-api/clear-cache?key=<secret>

这将通过脚本远程完成。我认为这是对 drush 的补充,一种 "remote drush" 适用于您无法通过 ssh 访问服务器但仍希望自动化的情况。

有这方面的模块吗?或者其他策略?

您可以编写一个挂接到 cron 的小模块:

// function that will be triggered on next cron run
function <MODULE>_cron() {
   // clear all caches
   drupal_flush_all_caches();
}

然后你可以简单地 运行 cron:

curl http://mysite.example.com/cron.php?cron_key=<YOUR_CRON_KEY>

这又会触发 <MODULE>_cron() 清除缓存。

另一种方法是创建一个小模块,通过 hook_menu():

添加回调
function <MODULE>_menu() {
  $menu['cache/clear/%'] = array(
    'page callback' => '<MODULE>_clear_cache',
    'access arguments' => array('access content'),
    'page arguments' => array(2)
  );
  return $menu;
}

function <MODULE>_clear_cache($cache) {
  // check so we have a valid access_token
  $access_token = variable_get('cache_access_token');
  $token = filter_input(INPUT_GET, 'access_token');
  if($token == $access_token) {
    switch($cache) {
      case 'all':
        drupal_flush_all_caches();
        break;
      case 'theme':
        cache_clear_all('theme_registry', 'cache', TRUE);
        break;
    }
  }
}

function <MODULE>_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'system_site_information_settings') {
    // add a cache access token field under site information
    $form['cache_access_token'] = array(
        '#type' => 'textfield',
        '#title' => t('Access Token'),
        '#description' => t('Token used to access clear cache remotely'),
        '#default_value' => variable_get('cache_access_token')
    );
  }
}

启用该模块,您应该可以通过 运行ning:

清除缓存
curl http://mysite.example.com/cache/clear/all?access_token=<YOUR_ACCESS_TOKEN>

如果您只想清除主题缓存,您可以这样做:

curl http://mysite.example.com/cache/clear/theme?access_token=<YOUR_ACCESS_TOKEN>

还有一个名为 Clear Cache Remotely 的模块,我认为它可以满足您的需求。