public function execute() { $limit = (int) $this->getOption('limit', PHP_INT_MAX); $langEn = Language::factory('en'); $infile = $this->getArg(0); if ($infile === '-') { $infile = 'php://stdin'; } $outfile = $this->getArg(1); if (!is_readable($infile) && $infile !== 'php://stdin') { $this->error("Cannot open input file {$infile} for reading", 1); } $file = fopen($infile, 'r'); if ($file === false) { $this->error("Cannot read input file {$infile}", 1); } try { $db = \Cdb\Writer::open($outfile); $alreadyWritten = array(); $skipped = 0; for ($i = 0; $i - $skipped < $limit; $i++) { if (feof($file)) { break; } $rawLine = fgets($file); if ($rawLine === false) { $this->error("Error reading input file"); break; } if (substr($rawLine, -1) !== "\n" && !feof($file)) { // We're assuming that this just won't happen. $this->error("fgets did not return whole line at {$i}??"); } $line = $langEn->lc(trim($rawLine)); if ($line === '') { $this->error("Line number " . ($i + 1) . " is blank?"); $skipped++; continue; } if (isset($alreadyWritten[$line])) { $this->output("Password '{$line}' already written (line " . ($i + 1) . ")\n"); $skipped++; continue; } $alreadyWritten[$line] = true; $db->set($line, $i + 1 - $skipped); } // All caps, so cannot conflict with potential password $db->set('_TOTALENTRIES', $i - $skipped); $db->close(); $this->output("Successfully wrote " . ($i - $skipped) . " (out of {$i}) passwords to {$outfile}\n"); } catch (\Cdb\Exception $e) { $this->error("Error writing cdb file: " . $e->getMessage(), 2); } }
private function populateCDB($thisSite, $local, $global) { $cdbFile = tempnam(wfTempDir(), 'MW-ClassicInterwikiLookupTest-') . '.cdb'; $cdb = \Cdb\Writer::open($cdbFile); $hash = $this->populateHash($thisSite, $local, $global); foreach ($hash as $key => $value) { $cdb->set($key, $value); } $cdb->close(); return $cdbFile; }
public function set($key, $value) { if (is_null($this->writer)) { throw new MWException(__CLASS__ . ': must call startWrite() before calling set()'); } try { $this->writer->set($key, serialize($value)); } catch (Exception $e) { throw new MWException($e->getMessage()); } }
/** * @since 0.1 * * @param string $location * @param string $language * * @return boolean */ public static function createCdbByLanguage($location, $language) { $language = strtolower($language); $source = $location . $language . '.json'; if (!file_exists($source)) { throw new RuntimeException("{$source} is not available."); } $contents = json_decode(file_get_contents($source), true); if (!isset($contents['list'])) { throw new RuntimeException("JSON is missing the `list` index."); } $writer = Writer::open(self::getTargetByLanguage($language)); foreach ($contents['list'] as $words) { $writer->set(trim($words), true); } $writer->close(); return true; }