安装过程Profile
译者:老葛 ESKALATE 科技公司
当你安装Drupal时,一些模块被启用,一些特定的配置被选择,但是这些默认的可能并不是你所需要的。Drupal安装器使用了一个默认的安装过程profile,用来决定所有的这些配置。通过创建你自己的安装过程profile,你可以定制Drupal的初始安装,从而使你的站点带有你想要的模块和设置。可能你在为每一个高校工作,你想创建一个安装过程profile,从而能够启用一个与你高校的单点登录系统相绑定的定制模块,能够为站点管理员创建一个新的角色,能够在安装完成时向你发送e-mail。Drupal的安装器系统,允许你通过创建一个安装过程profile来定制安装时的各种操作。在本章你将学到如何做到这一点。
Profile的存放位置
你的Drupal站点已经包含了一个安装过程profile。它是Drupal自带的默认的安装过程profile,位于profiles/default/default.profile。我们想创建一个新的名为“university”(大学)的profile,所以我们将先在profiles/university/university.profile创建一个新的文件。先在,我们将向这个文件仅添加一个独立的函数:
<?php
// $Id$
/**
* Return a description of the profile for the initial installation screen.
*
* @return
* An array with keys 'name' and 'description' describing this profile.
*/
function university_profile_details() {
return array(
'name' => st('Drupal (Customized for Iowa State University)'),
'description' => st('Select this profile to enable settings typical for a
departmental website.')
);
}
注意,这里文件的名称与profile目录的名称相同,在文件名的后面使用了.profile后缀,文件university.profile中的所有函数都以前缀university_开头。我们还是用了函数st(),而不是通常用的t(),这是因为在安装器运行这段代码时,Drupal还没有运行完一个完整的引导指令,所以t()不可用。
安装过程Profile的工作原理
当Drupal的安装器启动时,它扫描profiles目录以查看有多少个可用的profile。如果他发现多于一个时,它将向用户提供所有的profile以备用户选择。例如,创建了我们的university.profile文件并向其添加了university_profile_details()函数以后,访问http://example.com/install.php,将会产生一个如图23-1所示的截图。
图23-1 Drupal展示了一个有哪些profile可用的选项
Drupal的安装器接下来将再度回到安装过程profile上。它将找出该profile想要启用哪些模块,在安装过程的最后,当安装器将执行交给安装过程profile时,它又回来了一次。在后面的之一阶段,更多的Drupal定制化完成了。流程的概貌如图23-2所示。
指示启用哪些模块
通过添加函数university_profile_modules(),我们告诉Drupal我们的安装过程profile想启用哪些模块(还有,我们知道这个函数的名称应该由我们的profile的名称加上_profile_modules合成)。
/**
* Return an array of the modules to be enabled when this profile is installed.
*
* @return
* An array of modules to be enabled.
*/
function university_profile_modules() {
return array(
// Enable required core modules.
'block', 'filter', 'help', 'node', 'system', 'user', 'watchdog',
// Enable optional core modules.
'color', 'help', 'taxonomy', 'throttle', 'search', 'statistics',
// Enable single signon by enabling a contributed module.
'pubcookie',
);
}
在启用这些模块以前,安装器会询问每一个模块,以查看正被安装的Drupal系统是否提供了该模块所有的必要条件。通过为每个模块调用hook_requirements('install')来完成检查。如果要求没有被满足,安装器将会失败,并报告缺少了哪些条件。
图23-2 安装器是如何与安装过程profile交互的
注意 必要条件钩子是一个可选的钩子,它允许模块在在进行安装以前测试所需环境是否准备好了。关于该钩子的更多信息,参看http://api.drupal.org/api/5/function/hook_requirements。
安装器在启用模块以前确保模块是存在的。它会在多个地方查找,这些位置在表23-1中列出来了。由于我们要启用pubcookie模块(不包含在Drupal内核中的模块),在运行我们的安装过程profile以前,我们需要确保能够在这些所列的目录中找到它。
Table 23-1. Drupal模块可以存放的目录
目录 存放的模块
modules Drupal 核心模块
sites/all/modules 第3方模块(适用于所有站点)
profiles/profilename/modules 位于安装过程profile中的模块
sites/*/modules 与你的settings.php文件位于同一目录的模块
安装器还会在放置你站点settings.php文件的目录下查找模块。如果settings.php位于sites/default,那么Drupal会在sites/default/modules下面找。类似的,如果settings.php位于sites/example.com,那么Drupal将在sites/example.com/modules下面寻找模块。
最后设置
安装系统将按照我们列出模块的次序来启用我们所需要的模块,然后再调用安装过程profile。这一次,安装系统将查找一个名为university_install()的函数。在我们的例子中,我们没有实现这个函数,这是因为我们想在函数university_profile_final()中完成所有的事情。但是在引导指令结束以前调用install()钩子,如果需要的话,使你能够在最后时刻修改所要设置的东西。
注意 在Drupal的下一个版本中,安装过程profile中的install()函数将被删除,如果兼容性对你很重要的话,推荐你完全彻底profile_final()钩子。
最后,安装器调用university_profile_final()。
/**
* Perform final installation tasks for this installation profile.
*/
function university_profile_final() {
// Define a node type, 'page'.
$node_type = array(
'type' => 'page',
'name' => st('Page'),
'module' => 'node',
'description' => st('A standard web page.'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
'has_title' => TRUE,
'has_body' => TRUE,
'orig_type' => 'page',
'is_new' => TRUE,
);
node_type_save((object) $node_type);
// Page node types should be published and create new revisions by default.
variable_set('node_options_page', array('status', 'revision'));
// If the administrator enables the comment module, we want
// to have comments disabled for pages.
variable_set('comment_page', COMMENT_NODE_DISABLED);
// Define a node type, 'newsitem'.
$node_type = array(
'type' => 'news',
'name' => st('News Item'),
'module' => 'node',
'description' => st('A news item for the front page.'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
'has_title' => TRUE,
'has_body' => TRUE,
'orig_type' => 'news',
'is_new' => TRUE,
);
node_type_save((object) $node_type);
// News items should be published and promoted to front page by default.
// News items should create new revisions by default.
variable_set('node_options_news', array('status', 'revision', 'promote'));
// If the administrator enables the comment module, we want
// to have comments enabled for news items.
variable_set('comment_news', COMMENT_NODE_READ_WRITE);
// Create a taxonomy so news can be classified.
$vocabulary = array(
'name' => t('News Categories'),
'description' => st('Select the appropriate audience for your news item.'),
'help' => st('You may select multiple audiences.'),
'nodes' => array('news' => st('News Item')),
'hierarchy' => 0,
'relations' => 0,
'tags' => 0,
'multiple' => 1,
'required' => 0,
);
taxonomy_save_vocabulary($vocabulary);
// Define some terms to categorize news items.
$terms = array(
st('Departmental News'),
st('Faculty News'),
st('Staff News'),
st('Student News'),
);
// Submit the "Add term" form programmatically for each term.
foreach ($terms as $name) {
drupal_execute('taxonomy_form_term', array('name' => $name), $vid);
}
// Add a role.
db_query("INSERT INTO {role} (name) VALUES ('%s')", 'site administrator');
// Configure the pubcookie module.
variable_set('pubcookie_login_dir', 'login');
variable_set('pubcookie_id_is_email', 1);
// ...other settings go here
// Report by email that a new Drupal site has been installed.
$to = 'administrator@example.com';
$from = ini_get('sendmail_from');
$subject = st('New Drupal site created!');
$body = st('A new Drupal site was created: @site', array('@site' => base_path()));
drupal_mail('university-profile', $to, $subject, $body, $from);
}
如前面的例子所示,安装过程profile需要完成多个常用任务。安装器在调用profile_final()钩子以前,先完成一个完整的Drupal引导指令,所以在该钩子中可以使用所有的Drupal函数了。
注意 在安装过程profile中,我们使用st()来代替t(),从而使得整个安装过程profile可被翻译,如果有任何翻译的话,将被存放在一个安装过程profile的翻译文件中。这是一个与安装过程profile位于同一个目录下的.po文件。更多关于.po文件的信息,参看第18章。
设置Drupal变量
通过简单的调用variable_set()来设置Drupal变量:
variable_set('pubcookie_login_dir', 'login');
创建初始节点类型
如果你需要使用Drupal内置的内容类型系统创建新的节点类型的话,,你只需要创建一个节点类型定义对象并并将其传递给node_type_save()就可以了。在前面的profile例子中,我们创建了两个节点类型:“page”用于普通网页,而“news”用于新闻项。我们接着使用了variable_set()来设置节点的默认选项,这样发布的新闻项将会出现在首页,而“page”则不。
如果你启用了提供节点类型的模块,根据这些模块中的node_info()钩子,Drupal就可以调用里面的节点类型了。
将信息保存到数据库中
一个安装过程profile可能想修改一些数据库设置。由于现在数据库连接已经可用,可以使用db_query()来修改数据库。在我们的例子中,我们为该Drupal站点添加了一个角色。在你的profile中,你可做更多修改,比如向
permission表中插入一些权限。
一个简单的获取合适的查询的方法是,先按照默认设置安装好Drupal,接着完全按照你想要的方式来配置它。这甚至可以包括创建一些节点,完成URL别名的设置。本章例子中的安装过程profile可能想要一个About页面,一个Courses Taught页面,等等。当完成了这一配置过程以后,你可以使用你的数据库工具来将你站点的数据库完全导入到一个SQL脚本中。然后在SQL脚本中挑出你想要的INSERT SQL命令,并将它们包含到你的安装过程profile中。
提交表单
由于Drupal支持通过程序的方式提交表单,如果你要与网站交互的话,你可以使用drupal_execute()来提交表单。在前面的例子中,我们使用这种方式来为站点添加分类词语。关于drupal_execute()的更多信息,参看第10章。
总结
在本章,你学到了以下几点:
• 什么是安装过程profile
• 安装过程profile的存放位置
• 如何创建一个基本的安装过程profile
• 在最后安装阶段如何操纵Drupal