SearchSkel.php 5.3 KB

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