SearchSkel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Xunsearch PHP-SDK 搜索骨架代码生成工具
  5. *
  6. * @author hightman
  7. * @link http://www.xunsearch.com/
  8. * @copyright Copyright &copy; 2011 HangZhou YunSheng Network Technology Co., Ltd.
  9. * @license http://www.xunsearch.com/license/
  10. * @version $Id$
  11. */
  12. require_once dirname(__FILE__) . '/../lib/XS.php';
  13. require_once dirname(__FILE__) . '/XSUtil.class.php';
  14. // check arguments
  15. XSUtil::parseOpt(array('p', 'o', 'project', 'output'));
  16. $project = XSUtil::getOpt('p', 'project', true);
  17. if (XSUtil::getOpt('h', 'help') !== null || !is_string($project)) {
  18. $version = PACKAGE_NAME . '/' . PACKAGE_VERSION;
  19. echo <<<EOF
  20. SearchSkel - 搜索骨架代码生成工具 ($version)
  21. 用法
  22. {$_SERVER['argv'][0]} [options] [-p|--project] <project> [[-o|--output] <dir>]
  23. 选项说明
  24. --project=<name|ini>
  25. -p <project> 用于指定要搜索的项目名称或项目配置文件的路径,
  26. 如果指定的是名称,则使用 ../app/<name>.ini 作为配置文件
  27. --output=<dir>
  28. -o <dir> 指定生成的骨架代码存储位置,默认为当前目录
  29. -h|--help 显示帮助信息
  30. EOF;
  31. exit(0);
  32. }
  33. // output dir
  34. $output = XSUtil::getOpt('o', 'output', true);
  35. if ($output === null) {
  36. $output = '.';
  37. }
  38. if (!is_dir($output)) {
  39. echo "错误:输出目录 ($output) 不是一个有效的目录。\n";
  40. exit(-1);
  41. }
  42. if (!is_writable($output)) {
  43. echo "错误:输出目录 ($output) 不可写入,请注意检查权限。\n";
  44. exit(-1);
  45. }
  46. // execute the search
  47. try {
  48. // create xs object
  49. echo "初始化项目对象 ...\n";
  50. $xs = new XS($project);
  51. // generate varialbes
  52. echo "解析字段,生成变量清单 ...\n";
  53. $vars = array();
  54. // timezone
  55. if (!ini_get('date.timezone')) {
  56. date_default_timezone_set('Asia/Chongqing');
  57. }
  58. // basic
  59. $vars['@project@'] = is_file($project) ? realpath($project) : $project;
  60. $vars['@charset@'] = $xs->getDefaultCharset();
  61. if ($vars['@charset@'] !== 'GB2312' && $vars['@charset@'] !== 'GBK') {
  62. $vars['@charset@'] = 'UTF-8';
  63. }
  64. $vars['@xs_lib_root@'] = XS_LIB_ROOT;
  65. $vars['@date_time@'] = date('Y-m-d H:i:s');
  66. $vars['@project_name@'] = ucfirst($xs->name);
  67. $vars['@package_name@'] = PACKAGE_NAME;
  68. $vars['@package_version@'] = PACKAGE_VERSION;
  69. // fields
  70. $vars['@set_filter@'] = '';
  71. $vars['@set_sort@'] = '';
  72. $vars['@field_id@'] = $xs->getFieldId()->name;
  73. if (($field = $xs->getFieldTitle()) !== false) {
  74. $vars['@field_title@'] = $field->name;
  75. }
  76. if (($field = $xs->getFieldBody()) !== false) {
  77. $vars['@field_body@'] = $field->name;
  78. }
  79. $vars['@field_info@'] = '';
  80. foreach ($xs->getAllFields() as $field) /* @var $field XSFieldMeta */ {
  81. if ($field->hasIndexSelf() && $field->type != XSFieldMeta::TYPE_BODY && !$field->isBoolIndex()) {
  82. $vars['@set_filter@'] .= "\t\t\t<label class=\"radio inline\"><input type=\"radio\" name=\"f\" value=\"{$field->name}\" <?php echo \$f_{$field->name}; ?> />" . ucfirst($field->name) . "</label>\n";
  83. }
  84. if ($field->isNumeric()) {
  85. $vars['@set_sort@'] .= "\t\t\t\t\t<option value=\"" . $field->name . "_DESC\" <?php echo \$s_{$field->name}_DESC; ?>>" . ucfirst($field->name) . "从大到小</option>\n";
  86. $vars['@set_sort@'] .= "\t\t\t\t\t<option value=\"" . $field->name . "_ASC\" <?php echo \$s_{$field->name}_ASC; ?>>" . ucfirst($field->name) . "从小到大</option>\n";
  87. }
  88. if ($field->isSpeical()) {
  89. continue;
  90. }
  91. if ($field->type == XSFieldMeta::TYPE_STRING) {
  92. if (!isset($vars['@field_title@'])) {
  93. $vars['@field_title@'] = $field->name;
  94. continue;
  95. }
  96. if (!isset($vars['@field_body@'])) {
  97. $vars['@field_body@'] = $field->name;
  98. continue;
  99. }
  100. }
  101. $vars['@field_info@'] .= "\t\t\t\t<span><strong>" . ucfirst($field->name) . ":</strong><?php echo htmlspecialchars(\$doc->" . $field->name . "); ?></span>\n";
  102. }
  103. $vars['@set_filter@'] = trim($vars['@set_filter@']);
  104. $vars['@set_sort@'] = trim($vars['@set_sort@']);
  105. $vars['@field_info@'] = trim($vars['@field_info@']);
  106. if (!isset($vars['@field_title@'])) {
  107. $vars['@field_title@'] = 'title';
  108. }
  109. if (!isset($vars['@field_body@'])) {
  110. $vars['@field_body@'] = 'body';
  111. }
  112. // output dir
  113. echo "检测并创建输出目录 ...\n";
  114. $output .= '/' . $xs->name;
  115. if (!is_dir($output) && !mkdir($output)) {
  116. throw new XSException('Failed to create output directory: ' . $output);
  117. }
  118. // loop to write-in files
  119. $input = dirname(__FILE__) . '/skel';
  120. $dir = dir($input);
  121. while (($entry = $dir->read()) !== false) {
  122. if ($entry === '.' || $entry === '..') {
  123. continue;
  124. }
  125. if (substr($entry, -3) === '.in') {
  126. echo "正在生成 " . substr($entry, 0, -3) . " ...\n";
  127. $file = $output . '/' . substr($entry, 0, -3);
  128. if (file_exists($file)) {
  129. copy($file, $file . '.bak');
  130. }
  131. $content = file_get_contents($input . '/' . $entry);
  132. $content = strtr($content, $vars);
  133. if ($vars['@charset@'] !== 'UTF-8') {
  134. $content = XS::convert($content, $vars['@charset@'], 'UTF-8');
  135. }
  136. file_put_contents($file, $content);
  137. } else {
  138. echo "正在复制 " . $entry . " ...\n";
  139. $file = $output . '/' . $entry;
  140. if (is_dir($input . '/' . $entry)) {
  141. XSUtil::copyDir($input . '/' . $entry, $file);
  142. } else {
  143. if (file_exists($file)) {
  144. copy($file, $file . '.bak');
  145. }
  146. copy($input . '/' . $entry, $file);
  147. }
  148. }
  149. }
  150. $dir->close();
  151. echo "完成,请将 `$output` 目录转移到 web 可达目录,然后访问 search.php 即可。\n";
  152. } catch (XSException $e) {
  153. // Exception
  154. $start = dirname(dirname(__FILE__)) . '/';
  155. $relative = substr(XSException::getRelPath($start), 0, -1);
  156. $traceString = $e->getTraceAsString();
  157. $traceString = str_replace(dirname(__FILE__) . '/', '', $traceString);
  158. $traceString = str_replace($start, $relative, $traceString);
  159. echo $e . "\n" . $traceString . "\n";
  160. }