Source for file dftk_output_oneshot.php
Documentation is available at dftk_output_oneshot.php
1 <?php 2 /* 3 4 Copyright (c)2003 DuckCorp(tm) and RtpNet(tm) 5 6 7 8 This file is part of DFTK. 9 10 DFTK is free software; you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation; either version 2 of the License, or 13 (at your option) any later version. 14 15 DFTK is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with DFTK; if not, write to the Free Software 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 */ 24 25 26 /** 27 * @package dftk 28 * @author Duck <duck@DuckCorp.org> 29 * @author Rtp <rtp@rtp-net.org> 30 * @copyright Copyright (c)2003 DuckCorp(tm) and RtpNet(tm) 31 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 32 * @version 0.4.0 33 */ 34 35 36 /** 37 * DFTK Stdout Output Class 38 * 39 * @package dftk 40 * @subpackage dftk-output 41 * @author Duck <duck@DuckCorp.org> 42 * 43 * @access public 44 */ 45 class DftkOutputOneshot extends DftkOutput 46 { 47 /** 48 * Filename for old output backup 49 * 50 * @access private 51 * @var string 52 */ 53 var $_filename; 54 55 /** 56 * Output object 57 * 58 * @access private 59 * @var object DftkOutput 60 */ 61 var $_output; 62 63 /** 64 * Constructor 65 * 66 * @access public 67 * @param object DftkDaTraceManager &$tracemgr Language Manager 68 * @param string $filename Log file name 69 * @param object DftkOutput $output Output object 70 */ 71 function DftkOutputOneshot(&$tracemgr, $filename, &$output) 72 { 73 DftkOutput::DftkOutput(&$tracemgr); 74 75 $this->_filename = $filename; 76 $this->_output =& $output; 77 78 register_shutdown_function(array(&$this, "_DftkOutputOneshot")); 79 } 80 81 /** 82 * Destructor 83 * 84 * @access private 85 */ 86 function _DftkOutputOneshot() 87 { 88 } 89 90 /** 91 * Flush output 92 * 93 * Result : 94 * + 'output_changed' : true if output has changed since last call 95 * 96 * @access public 97 * @return object DftkDaTrace $r Trace 98 */ 99 function &flush_output() 100 { 101 $r =& $this->_tracemgr->create_trace(); 102 103 if (file_exists($this->_filename)) 104 { 105 if (!function_exists('md5_file')) 106 $old_md5 = md5(implode("", file($this->_filename))); 107 else 108 $old_md5 = md5_file($this->_filename); 109 } 110 else 111 $old_md5=""; 112 113 $content = $this->_header.$this->_content.$this->_footer; 114 $md5 = md5($content); 115 116 if ($md5 != $old_md5) 117 { 118 $r2 =& $this->_save_output(); 119 $r->merge_traces($r2); 120 121 if ($content) 122 { 123 $this->_output->set_header($this->_header); 124 $this->_output->clear_content(); 125 $this->_output->add_content($this->_content); 126 $this->_output->set_footer($this->_footer); 127 128 $this->_output->flush_output(); 129 } 130 131 $r->set_result('output_changed', true); 132 } 133 else 134 $r->set_result('output_changed', false); 135 136 $r2 =& DftkOutput::flush_output(); 137 $r->merge_traces($r2); 138 139 return $r; 140 } 141 142 /** 143 * Save output 144 * 145 * @access private 146 * @return object DftkDaTrace $r Trace 147 */ 148 function &_save_output() 149 { 150 $r =& $this->_tracemgr->create_trace(); 151 152 $fp = fopen($this->_filename, "w"); 153 if (!$fp) 154 $r->add_event('dftk-output_ioerr'); 155 156 if (!$r->has_error()) 157 { 158 if (!fwrite($fp, $this->_header.$this->_content.$this->_footer)) 159 $r->add_event('dftk-output_ioerr'); 160 161 if (!$r->has_error()) 162 { 163 if(!fclose($fp)) 164 $r->add_event('dftk-output_ioerr'); 165 } 166 } 167 168 return $r; 169 } 170 } 171 172 ?>
|