$return = false; } if (is_dir($conf['lockdir'] . '/_indexer.lock') && !rmdir($conf['lockdir'] . '/_indexer.lock')) { $this->error('failed to remove ' . $conf['lockdir'] . '/_indexer.lock something is wrong'); $return = false; } $this->quietecho('done.\\n'); return $return; } public function sigInt() { $this->exit = true; } } // Main $cli = new EnhancedIndexerCLI(); if (function_exists('pcntl_signal')) { // ensure things exit cleanly with ctrl+c declare (ticks=10); pcntl_signal(SIGINT, array($cli, 'sigInt')); pcntl_signal(SIGTERM, array($cli, 'sigInt')); } $conf['cachetime'] = 60 * 60; // default is -1 which means cache isnt' used :( $cli->run(); function return_bytes($size_str) { switch (substr($size_str, -1)) { case 'M': case 'm': return (int) $size_str * 1048576;
/** * Update the index */ function update() { global $conf; // using a lock to prevent the indexer from running multiple instances if ($this->lock() == false) { $this->error('unable to get lock, bailing'); exit(1); } // are we indexing a single namespace or all files? if ($this->namespace) { $dir = $conf['datadir'] . DS . str_replace(':', DS, $this->namespace); $idPrefix = $this->namespace . ':'; } else { $dir = $conf['datadir']; $idPrefix = ''; } // get a temp file name to store the list of files to index if (empty(self::$tempFileName)) { self::$tempFileName = sys_get_temp_dir() . '/EnhancedIndexer-' . microtime(true); if (file_exists(self::$tempFileName)) { self::$tempFileName .= 'b'; } $this->quietecho("Searching pages... "); // we aren't going to use $data, but the search function needs it $data = array(); search($data, $dir, 'EnhancedIndexerCLI::save_search_allpages', array('skipacl' => true)); $this->quietecho(self::$totalPagesToIndex . " pages found.\n"); } $cnt = 0; try { // we are using the SplFileObject so we can read one line without loading the whole file $this->temp_file = new SplFileObject(self::$tempFileName); // this flag tells the SplFileObject to remove the \n from the end of each line it reads $this->temp_file->setFlags(SplFileObject::DROP_NEW_LINE); for ($i = $this->startOffset; $i < self::$totalPagesToIndex; $i++) { // make sure the file handle is still open if (!$this->temp_file->valid()) { break; } // move to the next line and read the page id $this->temp_file->seek($i); $pageId = $this->temp_file->current(); // index this page, if not done already if ($this->index($idPrefix . $pageId, $i + 1, self::$totalPagesToIndex)) { $cnt++; $this->clean = false; } // used to exit cleanly if ctrl+c is detected if ($this->exit) { break; } // restart when memory usage exceeds 256M if (memory_get_usage() > ONE_MEGABYTE * 256) { $this->error('Memory almost full, resetting'); $this->restart($i + 1); } if ($this->maxRuns && $cnt >= $this->maxRuns) { $this->error('Max runs reached ' . $cnt . ', restarting'); $this->restart($i + 1); } } // release the temp file if (!empty($this->temp_file)) { $this->temp_file = null; unset($this->temp_file); } } catch (Exception $e) { $this->error("\n" . $e->getMessage()); } // remove the temp file if (is_file(self::$tempFileName)) { $this->quietecho("Removing temp file... "); unlink(self::$tempFileName); $this->quietecho("done\n"); } }