ubuntu中用php按时间段导出netbeans6.9更改过的文件!

ubuntu中用php按时间段导出netbeans6.9更改过的文件!


用netbeans改了哪些文件,你还记得吗?一个一个地记,也太麻烦了。好在netbeans已经为你记住了,你可以在netbeans修改一下文件历史时间,默认是7天 。工具->选项->其它->本地历史记录。在“保存本地历史文件[30]天数”这里,我填写为30天。如果你的项目比较长,可以把天数改大一点。

您很可能需要安装unzip。如果你使用windows,你可能要改一下function ISO_8859_1_To_ANSI( $src)里面的内容,因为linux的目录是这样的"/linux-apps/www",windows是这样的"C:\www"


三个目录说明:

/media/linux-apps/www/wudimei.com    假如这是我的网站的项目文件夹,里面是我的源代码

/media/linux-apps/www/wudimei.com_update  这个是要放我修改过的文件。

/home/rong/.netbeans/6.9/var/filehistory   这个是netbeans的修改记录文件

我的目的就是要把修改过的文件放到wudimei.com_update这个文件夹中。


projectDirectory = $projectDirectory;
        $this->destDirectory = $destDirectory;
        $this->historyLocation = $historyLocation;
        $this->scanHistoryFolders();
    }

    public function scanHistoryFolders()
    {
        $dirObj = dir( $this->historyLocation );
        while( ( $file = $dirObj->read() )  != false )
        {
            if( $file != "." && $file != ".." )
            {
                $subPath = $this->historyLocation . "/" . $file ;
                if( is_dir( $subPath ) )
                {
                    $dirObj2 = dir( $subPath );
                    while( ( $file2 = $dirObj2->read() ) != false )
                    {
                        if( $file2 != "." && $file2 != ".." )
                        {
                            $subPath2 =   $subPath . "/" . $file2  ;


                            $dataPath = @file_get_contents( $subPath2 . "/data");
                            $dataPath = $this->ISO_8859_1_To_ANSI( $dataPath );

                            file_put_contents( $subPath2 . "/data2" , $dataPath );
                            
                            $this->data[] = array(
                                "md5_key" => $file2 ,
                                "src_path" => $dataPath,
                                "history_dir" => $subPath2,
                                "history_files" =>  $this->readHistoryFileNames( $subPath2)
                            );
                        }
                    }
                    $dirObj2->close();
                }
            }
        }
          $dirObj->close();
    }

    public function  readHistoryFileNames($dir )
    {
        $dirObj = dir( $dir );
        $files = array();
        while( ( $file = $dirObj->read() )  != false )
        {
            if( $file != "." && $file != ".." )
            {
                if( preg_match( "/^[0-9]+$/i" , $file ) )
                {
                    $files[$file ] = $dir . "/" . $file ;
                }
            }
        }
        $dirObj->close();
        return $files;
    }

    function ISO_8859_1_To_ANSI( $src){
        $src = substr( $src , strpos( $src ,"/") );
        $newSrc = "";
        for( $i=0; $i< strlen( $src); $i++ )
        {
            if( ord( $src[$i])> 0 )
            {
                $newSrc .= $src[$i];
            }
        }
        //echo strpos($newSrc , "/linux") . " ";
        return $newSrc;
    }

    
    /**
     *
     * @param string $projectDirectory  /media/linux-apps/www/wudimei.com
     * @param string $destDirectory  /media/linux-apps/www/wudimei.com_update
     * @param string $historyLocation   /home/rong/.netbeans/6.9/var/filehistory
     */
    public  function export( $startDate, $endDate =null )
    {
        $startDateTimestamp = strtotime( $startDate ) ;
        $endDateTimestamp = strtotime( $endDate ) ;
         for( $i=0; $i< count( $this->data ); $i++ )
         {
             $r = $this->data[$i];
             $src_path = $r["src_path"];
             $history_files = $r["history_files"];
             
             if( strpos( $src_path , $this->projectDirectory ) !== false )
             {
                 $destFile = $this->destDirectory .  str_replace($this->projectDirectory, "", $src_path  );
                 $destDir = dirname( $destFile );
                list( $fileName,  $lastSrcFile )= $this->getLastFile( $history_files , $startDateTimestamp ,$endDateTimestamp );
                 
                 if( trim( $lastSrcFile ) != "" && file_exists( $lastSrcFile))
                 {
                     if( !is_dir( $destDir ) )
                     {
                         mkdir($destDir,0777, true);
                     }
                     copy( $lastSrcFile, $destFile.".2" );
                     system("unzip " . $destFile.".2 -d " . dirname( $destFile) );
                     unlink( $destFile . ".2" );
                     copy( dirname( $destFile )  . "/" . $fileName , $destFile );
                     unlink(dirname( $destFile )  . "/" . $fileName );
                 }
             }
         }
    }

    public function getLastFile( $files, $startDateTimestamp , $endDateTimestamp )
    {
        $maxTime =0;
        $files_in_period = array();
        
        if( !empty( $files ) )
        {
            foreach( $files as $k => $v )
            {
                $time = $k /1000; //the filename is millisecond,so i divide 1000
                if( $startDateTimestamp <= $time && $time<= $endDateTimestamp )
                {
                    $files_in_period[$k] = $v;
                }
            }
            foreach( $files_in_period as $k => $v )
            {
                if( $k> $maxTime )
                {
                    $maxTime = $k;
                }
            }
        }
        return array( $maxTime , @$files_in_period[$maxTime] );
    }


}

$FHE = new FileHistoryExport(
        "/media/linux-apps/www/wudimei.com",  /* +++++++CHANGE ME!path to your web project's root+++++++++*/
         "/media/linux-apps/www/wudimei.com_update",/* +++++++CHANGE ME!the place to export +++++++++*/
         "/home/rong/.netbeans/6.9/var/filehistory" /* +++++++CHANGE ME!netbeans's filehistory+++++++++*/
        );

$startTime ="";
$endTime = "";

echo "===========File History Export===========\npowwer by wudimei.com\n";
echo "enter start time(eg:2012-08-2 00:00:00):";
$startTime = read();
echo "enter end time(eg:2012-09-06 23:00:00):";
$endTime = read();
//echo $startTime . " " . $endTime ; exit();
sleep(1);
$FHE->export(
         $startTime, $endTime
        );

echo "Press any key to exit:";
$k = read();
fclose( $GLOBALS['StdinPointer'] );
?>


然后写一个export.sh ,主要功能是删除更新文件夹中的旧文件,再把改动过的文件拷过去。

sudo rm  -rf /media/linux-apps/www/wudimei.com_update/*
sudo /d/app/php5/bin/php -c /d/app/php5/etc/php.ini /media/linux-apps/app/wudimei/FileHistoryExport/FileHistoryExport.php


File History Export
powwer by wudimei.com
enter start time(eg:2012-08-2 00:00:00):2012-08-2 00:00:00 #输入你开始修改的时间
enter end time(eg:2012-09-06 23:00:00):2012-09-06 23:00:00 #输入结束的时间,可以是未来

你可能感兴趣的:(ubuntu中用php按时间段导出netbeans6.9更改过的文件!)