indexer.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env php
  2. <?php
  3. use splitbrain\phpcli\CLI;
  4. use splitbrain\phpcli\Options;
  5. if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
  6. define('NOSESSION', 1);
  7. require_once(DOKU_INC . 'inc/init.php');
  8. /**
  9. * Update the Search Index from command line
  10. */
  11. class IndexerCLI extends CLI {
  12. private $quiet = false;
  13. private $clear = false;
  14. /**
  15. * Register options and arguments on the given $options object
  16. *
  17. * @param Options $options
  18. * @return void
  19. */
  20. protected function setup(Options $options) {
  21. $options->setHelp(
  22. 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
  23. 'given the index is cleared first.'
  24. );
  25. $options->registerOption(
  26. 'clear',
  27. 'clear the index before updating',
  28. 'c'
  29. );
  30. $options->registerOption(
  31. 'quiet',
  32. 'don\'t produce any output',
  33. 'q'
  34. );
  35. }
  36. /**
  37. * Your main program
  38. *
  39. * Arguments and options have been parsed when this is run
  40. *
  41. * @param Options $options
  42. * @return void
  43. */
  44. protected function main(Options $options) {
  45. $this->clear = $options->getOpt('clear');
  46. $this->quiet = $options->getOpt('quiet');
  47. if($this->clear) $this->clearindex();
  48. $this->update();
  49. }
  50. /**
  51. * Update the index
  52. */
  53. protected function update() {
  54. global $conf;
  55. $data = array();
  56. $this->quietecho("Searching pages... ");
  57. search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true));
  58. $this->quietecho(count($data) . " pages found.\n");
  59. foreach($data as $val) {
  60. $this->index($val['id']);
  61. }
  62. }
  63. /**
  64. * Index the given page
  65. *
  66. * @param string $id
  67. */
  68. protected function index($id) {
  69. $this->quietecho("$id... ");
  70. idx_addPage($id, !$this->quiet, $this->clear);
  71. $this->quietecho("done.\n");
  72. }
  73. /**
  74. * Clear all index files
  75. */
  76. protected function clearindex() {
  77. $this->quietecho("Clearing index... ");
  78. idx_get_indexer()->clear();
  79. $this->quietecho("done.\n");
  80. }
  81. /**
  82. * Print message if not supressed
  83. *
  84. * @param string $msg
  85. */
  86. protected function quietecho($msg) {
  87. if(!$this->quiet) echo $msg;
  88. }
  89. }
  90. // Main
  91. $cli = new IndexerCLI();
  92. $cli->run();