SearchSkel.php 6.0 KB

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