Tag: cli
SVN folder cleaning (svn wipe)
by z3n on Dec.04, 2010, under Coding
Problem:
You did a svn checkout on this nice project but now you don’t want that .svn folders all over the place, after all you won’t be commiting it. Suddenly you find out that there’s thousands of folders with thousands of files.
Solution:
I wrote this little php cli script to fix this:
<?php
// (c) z3n - R1V1@101203 - www.overflow.biz - rodrigo.orph@gmail.com
function _o($msg) {
echo "\r" . substr($msg,0, 79) . str_repeat(" ", 79 - strlen($msg));
}
function rddir($dir) { /* v2.17 */
global $tfile,$tdir;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
if (is_dir($dir . $file))
$tdir[] = $dir . $file;
else
$tfile[] = $file;
}
}
closedir($dh);
}
}
}
if (!isset($argv[1]))
die("Usage: " . $_SERVER['PHP_SELF'] . " <path/to/svn_root/>");
// browse dirs searching for .svn
for (
$tdir = array($argv[1]),
$tfile = array(),
$svn = array(),
$i = 0;$i < count($tdir);$i++)
{
rddir($tdir[$i]);
_o("Reading: ".$tdir[$i]." ...");
foreach ($tfile as $entry) {
if ($entry != "." && $entry != "..") {
$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
if ($entry == ".svn" && !in_array($fn, $svn))
$svn[] = $fn;
if (is_dir($fn) && $entry != ".svn")
$tdir[] = $fn;
}
}
$tfile = array();
}
_o("Found: " . count($svn) . " .svn folders, collecting files...");
$files = array();
$dirs = array();
$t = 0;
// collect files from .svn folders
foreach ($svn as $dir) {
for ($tdir = array($dir), $tfile = array(),$i = 0;$i < count($tdir);$i++) {
rddir($tdir[$i]);
_o("[" . $t . "] - Collecting: ". $tdir[$i] ."...");
foreach ($tfile as $entry) {
if ($entry != "." && $entry != "..") {
$fn = $tdir[$i] . (PHP_OS == "WINNT" ? "\\" : "/") . $entry;
$t++;
if (is_dir($fn)) {
$tdir[] = $fn;
$dirs[] = $fn;
} else {
$files[] = $fn;
}
}
}
}
}
$i = count($files) + count($dirs) + count($svn);
// delete files
foreach ($files as $file) {
_o("[" . $i . "] Deleting Files: " . $file);
unlink($file);
$i--;
}
// delete subfolders
rsort($dirs);
foreach ($dirs as $dir) {
_o("[" . $i . "] Deleting Dir: " . $dir);
rmdir($dir);
$i--;
}
// delete root .svn folders
foreach ($svn as $dir) {
_o("[" . $i . "] Deleting Roots: " . $dir);
rmdir($dir);
$i--;
}
echo "\nDone!";
?>
Display MySQL tables detailed information
by z3n on Jun.18, 2009, under Coding, Tips & Hints
Problem:
How to display detailed information about each table on a database?
Solution:
show table status;
Source:
md5 File Comparsion PHP Script
by z3n on Jun.05, 2009, under Coding, Tips & Hints
Problem:
I had a bunch of files but i didn’t knew which ones were doubles and which ones were unique, they had different names and belong to different subfolders.
Solution:
A PHP script that reads and do a md5 hash on every file then do a comparsion.
-
-
// (c) z3n – R1V1@090605 – z3n666@gmail.com – www.overflow.biz
-
-
define("is_debug",1);
-
define("_dbp","_debug.log");
-
define('_bf',512288); // debug buffer
-
define('_dw',250); // maximum debug file size, _bf*_dw bytes
-
define('_cr',chr(13).chr(10)); // CRLF definition
-
define('_dt_sml','Y-m-d h:i:s'); // date small format (for debug)
-
ini_set("memory_limit","128M"); // avoid happyness
-
-
// functions
-
-
function _o($x) { if (is_debug) { _dbg($x); } echo $x."\n"; }
-
function _fw($fn,$txt,$m) { /* v1.5 with auto mkdir */ global $__fw,$__fwb;$__fw++;$__fwb+=strlen($txt);$x=1;$fn=str_replace("\\","/",$fn);if (strpos($fn,"/") !== false) { $p=substr($fn,0,strrpos($fn,"/"));if (!is_dir($p)) { _o("Mkdir: ".$p);mkdir($p,777,true); } } if ((is_writable($fn)) || (!file_exists($fn))) { if (!$fp=fopen($fn,$m)) { $x=0;break; } if (!fwrite($fp,$txt)) { $x=0;break; } fclose($fp); } _o("Wrote: ".$fn);return $x; }
-
function _dbg($x,$f=0) { /* v1.9 */ global $_bg,$_dw;if ((is_debug) && ($_dw < _dw)) { if ($x != "") { $_bg.=date(_dt_sml).":".$x._cr; } if ((strlen($_bg) > _bf) || ($f)) { $x=$_bg;$_dw++;$_bg="";_fw(_dbp,$x,"a"); } } }
-
function rddir($dir) { /* v2.11 */
-
global $tfile,$tdir,$ij;$i=0;$j=count($tdir);
-
if (is_dir($dir)) {
-
if ($dh=opendir($dir)) {
-
while (($file=readdir($dh)) !== false) {
-
if (is_dir($dir.$file)) {
-
if (($file != ".") && ($file != "..")) {
-
$tdir[$j]=$dir.$file;$j++;
-
} } else { $tfile[$i]=$file;$i++;
-
} } closedir($dh); } } $ij=$i; }
-
-
// main
-
-
$_bg="";$_dw=$__fw=$__fwb=0;$_x=array();$_f=array();
-
-
if (!isset($argv[1])) { _o("Usage: ".$_SERVER['PHP_SELF']." <path/to/>");die; }
-
-
for ($tdir[0]=$argv[1],$k=$i=0,rddir($tdir[$i]);$i<count($tdir);$i++) {
-
$tdir[$i].="/";
-
for ($j=0,$m=count($tfile),_o("Found: ".$m." files @ ".$tdir[$i]."\nBuilding md5 hash…");$j<$m;$j++) {
-
if (($tfile[$j] != ".") && ($tfile[$j] != "..")) {
-
$h=md5(file_get_contents($tdir[$i].$tfile[$j]));
-
$_x[$k]=$h;
-
$_f[$k]=$tdir[$i].$tfile[$j];
-
$x=array_keys($_x,$h);
-
for ($o=0,$p=count($x);$o<$p;$o++) {
-
if ($x[$o] != $k) {
-
_o($_f[$o]." = ".$_f[$k]." (".$h." – ".$j.")");
-
}
-
}
-
$k++;
-
}
-
}
-
}
-
-
_dbg("",1);_o("Done!");
-
-
?>
Testing email server with telnet
by z3n on Apr.28, 2009, under Tips & Hints
I keep forgeting about this so i decided to write a post.
Problem:
An easy way to test a pop3 email server.
Solution:
telnet mail.server.com 110
user username
pass yourpassword
list to list messages
stat to show status of your mailbox
retr # to show the message
dele # to delete a message
Source:
JS and CSS Packer in PHP
by z3n on Apr.03, 2009, under Coding
Problem:
No decent css/js packers in php
Solution:
This CLI based script will compress the input files detecting their formats and placing on a defined folder, easying the automation of multiple files compression.
have fun!