用PHP写的一个web升格脚本
用PHP写的一个web升级脚本
管理web项目一直是件很困难的事情,程序使用时间越长里面的spam文件也越多。如何保证web目录的清洁是个重要的工作,结合这个脚本和subversion的管理功能,可以方便你的日常升级管理。
<?php /** * Website upgrade script * Author: victorwmh * Email: victorwmh@gmail.com * Date: 2011-08-23 * * Description: * $argc and $argv is default variables of CLI mode. * $argc is int, $argv is array. $argv[0] is self file name. */ $ublocal = "/home/df3c/upgrade/bundles/"; $uapplocal = "/home/df3c/web"; if ($argc < 2) exit('no argument.'); $ubname = $argv[1]; $ufox = new UpgradeFox(); $ufox->init($uapplocal, $ublocal, $ubname); $ufox->upgrade(); /** * Upgrade-fox class. * @author victorwmh * */ class UpgradeFox { /* update bundle name */ private $bname = ''; /* update bundle location */ private $blocal = ''; /* application location for upgrade */ private $applocal = ''; /* bundle extract directory */ private $extdir = ''; /* all directory of boundle */ private $directories = array(); /* all file of boundle */ private $files = array(); /** * Initialization basic information. * @param string $uapplocal * @param string $ublocal * @param string $ubname */ public function init($uapplocal, $ublocal, $ubname) { $this->applocal = rtrim($uapplocal, '/'); $this->bname = $ubname; $this->blocal = rtrim($ublocal, '/'); $this->extdir = rtrim($this->blocal . '/' . $this->bname, '.zip'); } /** * Update files in the bundle and delete files in the list for dellist.txt. */ public function upgrade() { ob_start(); echo "Upgrade processing...\n"; echo "Upgrade will to overwrite or remove a files.\n"; echo "Are you want to continue? press (y or n):"; ob_flush(); if (trim(fgets(STDIN)) != 'y') { exit("Canceled.\n\n"); }; echo "Check: "; ob_flush(); if ($this->check() === FALSE) { exit("bundle not exist.\nUpgrade failure.\n\n"); } elseif($this->check() == 'zip') { echo "Ok!\nExtract bundle: "; ob_flush(); $this->extract(); } $this->uverify(); echo "Ok!\nUpdate files: "; ob_flush(); $this->update(); echo "Ok!\nClean files: "; ob_flush(); $this->clean(); echo "Ok!\nFinished.\n\n"; ob_end_flush(); } /** * Check upgrade bundle. */ private function check() { if (file_exists($this->blocal . '/' . $this->bname)) { if (preg_match('/(\.zip)+$/', $this->bname)) { return 'zip'; } elseif(is_dir($this->blocal . '/' . $this->bname)) { return 'dir'; } else { return FALSE; } } else { return FALSE; } } /** * Extract upgrade bundle. */ private function extract() { $zip = new ZipArchive(); $zip->open($this->blocal . '/' . $this->bname); if (! $zip->extractTo($this->blocal)) { $zip->close(); exit("extract is failure.\nUpgrade failure.\n\n"); } } /** * Update files in the bundle. */ private function update() { $this->sortDirFile($this->extdir); $this->mkDirAppend(); $this->uoverwrite(); $this->udelete(); } /** * Verify bundles. */ private function uverify() { $ufoxfile = $this->extdir . '/ufox-clean.ini'; if ( ! file_exists($ufoxfile)) { exit("bundle rootdir is not ufox-clean.ini\n"); } } /** * Delete trash files of applocal. */ private function udelete() { $ufoxfile = $this->extdir . '/ufox-clean.ini'; $delfiles = $this->getFileContent($ufoxfile); foreach ($delfiles as $file) { $file = preg_replace('/\s.*/', '', $file); $tmp = $this->applocal . '/' . $file; if ('' == $file) { continue; } elseif (file_exists($tmp)) { is_dir($tmp) ? $this->rmdir($tmp) : unlink($tmp); } } unlink($this->applocal . '/ufox-clean.ini'); } /** * add or overwrite files. */ private function uoverwrite() { $bdirname = rtrim($this->bname, '.zip'); foreach ($this->files as $file) { $tmp = $this->applocal . '/' . substr($file, strpos($file, $bdirname)+strlen($bdirname)); copy($file, $tmp); } } /** * Clean extract files. */ private function clean() { $this->rmdir($this->extdir); } /** * Get contents of the file. * @param string $file * @return array */ private function getFileContent($file) { /* Line breaks for each OS. win:“\r\n”,0x0D0A; linux:“\n”,0x0A; mac:"\r",0x0D; */ if (file_exists($file)) { $contents = file_get_contents($file); if (strpos($contents, "\r\n") > 0) { $separator = "\r\n"; } elseif (strpos($contents, "\n") > 0) { $separator = "\n"; } elseif (strpos($contents, "\r") > 0) { $separator = "\r"; } return explode($separator, $contents); } else { return FALSE; } } /** * sort all file and sub directory of dir. * @param string $dir */ private function sortDirFile($dir) { $handle = opendir($dir); if ($handle) { while (($name = readdir($handle)) !== false) { if ($name === '.' || $name === '..') continue; $tmp = realpath($dir . '/' . $name ); if (is_dir($tmp)) { $this->directories[] = $tmp; $this->sortDirFile($tmp); } else { $this->files[] = $tmp; } } closedir($handle); } } /** * mk appended sub directories of bundle. */ private function mkDirAppend() { $bdirname = rtrim($this->bname, '.zip'); foreach ($this->directories as $dir) { $dir = substr($dir, strpos($dir, $bdirname)+strlen($bdirname)); $tmp = $this->applocal . '/' . $dir; if (! file_exists($tmp)) { mkdir($tmp); } } } /** * Remove directory. * @param string $dir */ private function rmdir($dir) { if (! is_dir($dir)) return false; $handle = opendir($dir); while (($file = readdir($handle)) !== false) { if ($file !== '.' && $file !== '..') { $tmp = $dir . '/' . $file; is_dir($tmp) ? $this->rmdir($tmp) : @unlink($tmp); } } closedir($handle); return rmdir($dir); } }