function convert_dir($dir, $echo = false) { $converted_files = array(); if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != '.' && $entry != '..') { $full = $dir . '/' . $entry; if (is_dir($full)) { $converted_files = array_merge($converted_files, convert_dir($full, $echo)); } else { if (end(explode('.', $full)) == 'php') { if (convert_file($full)) { $converted_files[] = $full; if ($echo) { println("Converted: " . $full); } } } } } } closedir($handle); } return $converted_files; }
function process_dir($dir) { $srcdir = $dir; echo "processing dir : {$srcdir} <br>"; if ($curdir = opendir($srcdir)) { while ($file = readdir($curdir)) { if ($file != '.' && $file != '..') { $srcfile = $srcdir . '\\' . $file; if (is_file($srcfile)) { convert_file($srcfile); } else { if (is_dir($srcfile)) { process_dir($srcfile); } } } } } closedir($curdir); }
#!/usr/bin/env php <?php set_include_path(__DIR__ . '/../../src/' . PATH_SEPARATOR . get_include_path()); require 'phpMorphy.php'; $root_dir = PHPMORPHY_DIR . '/phpMorphy'; $out_dir = PHPMORPHY_DIR; phpMorphy_Util_Fs::applyToEachFile($root_dir, '/\\.php$/', function ($path) use($out_dir) { convert_file($path, $out_dir, 'logger'); }); phpMorphy_Util_Fs::deleteEmptyDirectories($root_dir, 'logger'); function convert_file($path, $outDir, $log) { global $out_dir; $lines = array_map('rtrim', file($path)); try { $descriptor = phpMorphy_Generator_PhpFileParser::parseFile($path); } catch (Exception $e) { throw new phpMorphy_Exception("Can`t parse '{$path}': " . $e->getMessage()); } if (count($descriptor->classes) < 1) { return; } $first_class = $descriptor->classes[0]; $first_significant_line = null === $first_class->phpDoc ? $first_class->startLine : $first_class->phpDoc->startLine; $header = array_slice($lines, 0, $first_significant_line - 1); $header = implode(PHP_EOL, $header); $out_files = array(); $classes_count = count($descriptor->classes); foreach ($descriptor->classes as $class_descriptor) { $class_name = $class_descriptor->name; $out_path = $outDir . DIRECTORY_SEPARATOR . phpMorphy_Loader::classNameToFilePath($class_name);
<?php # usage: php utils/convert-docs.php man-src/*.txt function convert_file($path) { $out = file_get_contents($path); // options to definition lists $out = preg_replace_callback('/\\n\\* (.+?):?\\n\\n\\t/', function ($matches) { $arg = str_replace('`', '', $matches[1]); return "\n{$arg}\n: "; }, $out); // fix indentation $out = preg_replace('/^ ([^ ]+)/m', "\t\\1", $out); $out = str_replace("\t", ' ', $out); // prepend docblock notation $out = preg_replace('/^/m', "\t * ", $out); file_put_contents($path, $out); } foreach (array_slice($argv, 1) as $arg) { convert_file($arg); }
function convert_directory($source_dir, $target_dir, $keep_mode = false) { global $conf; if (!($dp = opendir($source_dir))) { fprintf(STDERR, "Error:\t [%s] directory does not exists!%s", $source_dir, PHP_EOL); exit(-1); } $t_dir = array(); $t_file = array(); while (($entry = readdir($dp)) !== false) { if ($entry == "." || $entry == "..") { continue; } $new_keep_mode = $keep_mode; $source_path = "{$source_dir}/{$entry}"; $source_stat = @lstat($source_path); $target_path = "{$target_dir}/{$entry}"; $target_stat = @lstat($target_path); if ($source_stat === false) { fprintf(STDERR, "Error:\t cannot stat [%s] !%s", $source_path, PHP_EOL); exit(-1); } if (isset($conf->t_skip) && is_array($conf->t_skip) && in_array($source_path, $conf->t_skip)) { continue; } if (is_link($source_path)) { if ($target_stat !== false && is_link($target_path) && $source_stat['mtime'] <= $target_stat['mtime']) { continue; } if ($target_stat !== false) { if (is_dir($target_path)) { directory_remove($target_path); } else { if (unlink($target_path) === false) { fprintf(STDERR, "Error:\t cannot unlink [%s] !%s", $target_path, PHP_EOL); exit(-1); } } } @symlink(readlink($source_path), $target_path); // Do not warn on non existing symbolinc link target! if (strtolower(PHP_OS) == 'linux') { $x = `touch '{$target_path}' --no-dereference --reference='{$source_path}' `; } continue; } if (is_dir($source_path)) { if ($target_stat !== false) { if (!is_dir($target_path)) { if (unlink($target_path) === false) { fprintf(STDERR, "Error:\t cannot unlink [%s] !%s", $target_path, PHP_EOL); exit(-1); } } } if (!file_exists($target_path)) { mkdir($target_path, 0777, true); } if (isset($conf->t_keep) && is_array($conf->t_keep) && in_array($source_path, $conf->t_keep)) { $new_keep_mode = true; } convert_directory($source_path, $target_path, $new_keep_mode); continue; } if (is_file($source_path)) { if ($target_stat !== false && is_dir($target_path)) { directory_remove($target_path); } if ($target_stat !== false && $source_stat['mtime'] <= $target_stat['mtime']) { continue; } // do not process if source timestamp is not greater than target $extension = pathinfo($source_path, PATHINFO_EXTENSION); $keep = $keep_mode; if (isset($conf->t_keep) && is_array($conf->t_keep) && in_array($source_path, $conf->t_keep)) { $keep = true; } if (!in_array($extension, $conf->t_convert_php_extension)) { $keep = true; } if ($keep) { file_put_contents($target_path, file_get_contents($source_path)); } else { $converted_str = convert_file($source_path); if ($converted_str === null) { if (isset($conf->abort_on_error)) { fprintf(STDERR, "Aborting...%s", PHP_EOL); exit; } } file_put_contents($target_path, $converted_str . PHP_EOL); } if ($keep) { file_put_contents($target_path, file_get_contents($source_path)); } touch($target_path, $source_stat['mtime']); continue; } } closedir($dp); }
function convert_dir($dirname) { if (@is_dir("{$dirname}/en")) { convert_dir("{$dirname}/en"); } if ($dir = opendir($dirname)) { // for each file in dir while (($file = readdir($dir)) !== false) { if ($file[0] == ".") { continue; } // ignore hidden files if ($file == "CVS") { continue; } // ignore CVS information if (is_dir("{$dirname}/{$file}")) { // is directory? if ($file != "en" && $file != "reference") { convert_dir("{$dirname}/{$file}"); // recurse if not 'reference' } } else { if (ereg("xml\$", $file)) { // is XML file? if (strpos("{$dirname}/", "/functions/") > 0) { convert_file($dirname, $file); // process if in 'functions' } } } } closedir($dir); } }
} remove_directory("{$target_directory}/yakpro-mtm"); exit; } $parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative()); // $parser = new PhpParser\Parser(new PhpParser\Lexer); $traverser = new PhpParser\NodeTraverser(); if ($conf->prettyPrinter == 'YAK Pro') { $prettyPrinter = new myPrettyprinter(); } else { $prettyPrinter = new PhpParser\PrettyPrinter\Standard(); } $traverser->addVisitor(new MyNodeVisitor()); switch ($process_mode) { case 'file': $converted_str = convert_file($source_file); if ($converted_str === null) { exit; } if ($target_file === '') { echo $converted_str . PHP_EOL; exit; } file_put_contents($target_file, $converted_str . PHP_EOL); exit; case 'directory': if (isset($conf->t_skip) && is_array($conf->t_skip)) { foreach ($conf->t_skip as $key => $val) { $conf->t_skip[$key] = "{$source_directory}/{$val}"; } }
foreach ($tplarr as $u) { $convs[] = $droot . $v[0] . $u; } } //后台或会员中心用的js文件 $jsarr = findfiles($droot . 'include/js/', 'js'); foreach ($jsarr as $v) { $convs[] = $droot . 'include/js/' . $v; } //处理... foreach ($convs as $v) { if ($lan == 'tcutf8') { convert_file('gbk', 'big5', $v); convert_file('big5', 'utf-8', $v); } else { convert_file('gbk', $icharset, $v); } } //**************清除多余或开发用的文件及设置///////////////////////////////////////////////////////////// foreach (array('index.htm', 'index.html', 'google.xml', 'baidu.xml', 'sitemap.xml', 'init.php') as $k) { @unlink($droot . $k); } $delarr = array('aguides', 'amenus', 'btagnames', 'inscopy', 'inspack', 'langs', 'tablestr', 'test', 'certificate', 'atpl'); foreach ($delarr as $k) { @unlink($droot . "admina/{$k}.inc.php"); } amessage('安装包生成成功', '?entry=atpl_bk'); } function convert_file($scode, $tcode, $sfile = '') { //gbk,big5,utf-8
<form enctype="multipart/form-data" action="" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> <input type="file" name="uploadFile" /> <input type="submit" name="upload" value="Импортировать в БД" /> </form> <?php require 'temp/converter.php'; if (isset($_POST['upload'])) { $folder = 'temp/'; $uploadedFile = $folder . basename($_FILES['uploadFile']['name']); if (is_uploaded_file($_FILES['uploadFile']['tmp_name'])) { if (move_uploaded_file($_FILES['uploadFile']['tmp_name'], $uploadedFile)) { convert_file($_FILES['uploadFile']['name']); if (import_csv()) { echo "Загружено"; } } else { echo 'Во время загрузки файла произошла ошибка'; } } else { echo 'Файл не загружен'; } } ?> </div> <!-- далее обработка загруженного файла , отправка его в бд. -->