You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

114 lines
2.4 KiB

  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(__DIR__ . '/../') . '/');
  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. {
  13. private $quiet = false;
  14. private $clear = false;
  15. /**
  16. * Register options and arguments on the given $options object
  17. *
  18. * @param Options $options
  19. * @return void
  20. */
  21. protected function setup(Options $options)
  22. {
  23. $options->setHelp(
  24. 'Updates the searchindex by indexing all new or changed pages. When the -c option is ' .
  25. 'given the index is cleared first.'
  26. );
  27. $options->registerOption(
  28. 'clear',
  29. 'clear the index before updating',
  30. 'c'
  31. );
  32. $options->registerOption(
  33. 'quiet',
  34. 'don\'t produce any output',
  35. 'q'
  36. );
  37. }
  38. /**
  39. * Your main program
  40. *
  41. * Arguments and options have been parsed when this is run
  42. *
  43. * @param Options $options
  44. * @return void
  45. */
  46. protected function main(Options $options)
  47. {
  48. $this->clear = $options->getOpt('clear');
  49. $this->quiet = $options->getOpt('quiet');
  50. if ($this->clear) $this->clearindex();
  51. $this->update();
  52. }
  53. /**
  54. * Update the index
  55. */
  56. protected function update()
  57. {
  58. global $conf;
  59. $data = [];
  60. $this->quietecho("Searching pages... ");
  61. search($data, $conf['datadir'], 'search_allpages', ['skipacl' => true]);
  62. $this->quietecho(count($data) . " pages found.\n");
  63. foreach ($data as $val) {
  64. $this->index($val['id']);
  65. }
  66. }
  67. /**
  68. * Index the given page
  69. *
  70. * @param string $id
  71. */
  72. protected function index($id)
  73. {
  74. $this->quietecho("$id... ");
  75. idx_addPage($id, !$this->quiet, $this->clear);
  76. $this->quietecho("done.\n");
  77. }
  78. /**
  79. * Clear all index files
  80. */
  81. protected function clearindex()
  82. {
  83. $this->quietecho("Clearing index... ");
  84. idx_get_indexer()->clear();
  85. $this->quietecho("done.\n");
  86. }
  87. /**
  88. * Print message if not supressed
  89. *
  90. * @param string $msg
  91. */
  92. protected function quietecho($msg)
  93. {
  94. if (!$this->quiet) echo $msg;
  95. }
  96. }
  97. // Main
  98. $cli = new IndexerCLI();
  99. $cli->run();