Location: PHPKode > projects > Program E > docs/developer/__filesource/fsource_application__cmsbackup.php.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<!-- template designed by Marco Von Ballmoos  -->
			<title>File Source for backup.php</title>
			<link rel="stylesheet" href="../media/stylesheet.css" />
											</head>
		<body>
						
<h1>Source for file backup.php</h1>
<p>Documentation is available at <a href="../application/_cms_backup_php.html">backup.php</a></p>
<div class="src-code">
<pre><ol><li><a name="a1"></a><span class="src-php">&lt;?php</span></li>
<li><a name="a2"></a>&nbsp;</li>
<li><a name="a3"></a><span class="src-comm">/*</span></li>
<li><a name="a4"></a><span class="src-comm">    Program E related AIML knowledgebase tools</span></li>
<li><a name="a5"></a><span class="src-comm">    Copyright 2004, Anne Kootstra [hide@address.com]</span></li>
<li><a name="a6"></a><span class="src-comm">    http://www.AIML.info</span></li>
<li><a name="a7"></a>    </li>
<li><a name="a8"></a><span class="src-comm">    Program E is free software; you can redistribute it and/or modify</span></li>
<li><a name="a9"></a><span class="src-comm">    it under the terms of the GNU General Public License as published by</span></li>
<li><a name="a10"></a><span class="src-comm">    the Free Software Foundation; either version 2 of the License, or</span></li>
<li><a name="a11"></a><span class="src-comm">    (at your option) any later version.</span></li>
<li><a name="a12"></a>&nbsp;</li>
<li><a name="a13"></a><span class="src-comm">    Program E is distributed in the hope that it will be useful,</span></li>
<li><a name="a14"></a><span class="src-comm">    but WITHOUT ANY WARRANTY; without even the implied warranty of</span></li>
<li><a name="a15"></a><span class="src-comm">    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></li>
<li><a name="a16"></a><span class="src-comm">    GNU General Public License for more details.</span></li>
<li><a name="a17"></a>&nbsp;</li>
<li><a name="a18"></a><span class="src-comm">    You should have received a copy of the GNU General Public License</span></li>
<li><a name="a19"></a><span class="src-comm">    along with Program E; if not, write to the Free Software</span></li>
<li><a name="a20"></a><span class="src-comm">    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</span></li>
<li><a name="a21"></a><span class="src-comm">*/</span></li>
<li><a name="a22"></a>&nbsp;</li>
<li><a name="a23"></a><span class="src-comm">// increasing the maximum script time from 30 seconds to 24 hours.</span></li>
<li><a name="a24"></a><a href="http://www.php.net/set_time_limit">set_time_limit</a><span class="src-sym">(</span><span class="src-num">86400</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a25"></a>&nbsp;</li>
<li><a name="a26"></a>&nbsp;</li>
<li><a name="a27"></a><span class="src-doc">/**</span></li>
<li><a name="a28"></a><span class="src-doc">* start of the timer sequence of the chatterbot. This will tell you at the</span></li>
<li><a name="a29"></a><span class="src-doc">* end how much time it took to process the log file</span></li>
<li><a name="a30"></a><span class="src-doc">*/</span></li>
<li><a name="a31"></a><span class="src-var">$t_start </span>= <a href="http://www.php.net/array_sum">array_sum</a><span class="src-sym">(</span><a href="http://www.php.net/explode">explode</a><span class="src-sym">(</span><span class="src-str">' '</span><span class="src-sym">, </span><a href="http://www.php.net/microtime">microtime</a><span class="src-sym">(</span><span class="src-sym">)))</span><span class="src-sym">;</span></li>
<li><a name="a32"></a>&nbsp;</li>
<li><a name="a33"></a><span class="src-comm">// Required files with PHP functions</span></li>
<li><a name="a34"></a><span class="src-inc">require_once </span><span class="src-str">&quot;../admin/dbprefs.php&quot;</span><span class="src-sym">;</span></li>
<li><a name="a35"></a><span class="src-inc">require_once </span><span class="src-str">&quot;include/retrieve.inc.php&quot;</span><span class="src-sym">;</span></li>
<li><a name="a36"></a><span class="src-inc">require_once </span><span class="src-str">&quot;include/common.inc.php&quot;</span><span class="src-sym">;</span></li>
<li><a name="a37"></a><span class="src-inc">require_once </span><span class="src-str">&quot;include/backup.inc.php&quot;</span><span class="src-sym">;</span></li>
<li><a name="a38"></a>&nbsp;</li>
<li><a name="a39"></a><span class="src-comm">// This is the http_post/get_var </span></li>
<li><a name="a40"></a><a href="../application/_cms_include_common_inc_php.html#functiongetRequest">getRequest</a><span class="src-sym">(</span><span class="src-str">&quot;botid&quot;</span><span class="src-sym">, </span><span class="src-num">0</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a41"></a>&nbsp;</li>
<li><a name="a42"></a><span class="src-comm">// Number of templates to select and process per round.</span></li>
<li><a name="a43"></a><span class="src-var">$templatesToProcess </span>= <span class="src-num">3000</span><span class="src-sym">;</span></li>
<li><a name="a44"></a>&nbsp;</li>
<li><a name="a45"></a><span class="src-comm">// Name of the file in which the backup will be made.</span></li>
<li><a name="a46"></a><span class="src-var">$backupfile </span>= <span class="src-str">&quot;backup.aiml&quot;</span><span class="src-sym">;</span></li>
<li><a name="a47"></a>&nbsp;</li>
<li><a name="a48"></a><span class="src-comm">// Setting certain needed variables to zero.</span></li>
<li><a name="a49"></a><span class="src-var">$pid </span>= <span class="src-num">0</span><span class="src-sym">;</span></li>
<li><a name="a50"></a><span class="src-var">$categories </span>= <span class="src-num">0</span><span class="src-sym">;</span></li>
<li><a name="a51"></a>&nbsp;</li>
<li><a name="a52"></a>&nbsp;</li>
<li><a name="a53"></a><span class="src-comm">// If not botid is known, present the user with a botselection page</span></li>
<li><a name="a54"></a><span class="src-key">if</span><span class="src-sym">(</span><span class="src-sym">!</span><span class="src-var">$botid</span><span class="src-sym">) </span><span class="src-sym">{</span></li>
<li><a name="a55"></a>    <span class="src-comm">// Get an array of bot names and botid's from the database</span></li>
<li><a name="a56"></a>    <span class="src-var">$botnames </span>= <a href="../application/_cms_include_common_inc_php.html#functiongetbotnames">getbotnames</a><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a57"></a>    </li>
<li><a name="a58"></a>    <span class="src-comm">// Put the names and the ID's into a selection form box</span></li>
<li><a name="a59"></a>    <span class="src-key">while</span><span class="src-sym">(</span>list<span class="src-sym">(</span><span class="src-var">$key</span><span class="src-sym">, </span><span class="src-var">$val</span><span class="src-sym">) </span>= <a href="http://www.php.net/each">each</a><span class="src-sym">(</span><span class="src-var">$botnames</span><span class="src-sym">)) </span><span class="src-sym">{</span></li>
<li><a name="a60"></a>        <span class="src-key">if</span><span class="src-sym">(</span><span class="src-var">$botid </span>== <span class="src-var">$key</span><span class="src-sym">)</span><span class="src-sym">{</span></li>
<li><a name="a61"></a>           <span class="src-var">$bots </span>.= <span class="src-str">&quot;</span>&lt;<span class="src-id">option value</span>=\&quot;<span class="src-var">$key</span>\&quot; <span class="src-id">selected</span>&gt;<span class="src-var">$val</span>&lt;/<span class="src-id">option</span>&gt;<span class="src-str">&quot;</span><span class="src-sym">;</span></li>
<li><a name="a62"></a>        <span class="src-sym">}</span><span class="src-key">else</span><span class="src-sym">{</span></li>
<li><a name="a63"></a>              <span class="src-var">$bots </span>.= <span class="src-str">&quot;</span>&lt;<span class="src-id">option value</span>=\&quot;<span class="src-var">$key</span>\&quot;&gt;<span class="src-var">$val</span>&lt;/<span class="src-id">option</span>&gt;<span class="src-str">&quot;</span><span class="src-sym">;</span></li>
<li><a name="a64"></a>        <span class="src-sym">}</span></li>
<li><a name="a65"></a>    <span class="src-sym">}</span></li>
<li><a name="a66"></a>    </li>
<li><a name="a67"></a>    <span class="src-comm">// Assign a placeholder in the HTML template to a variable.</span></li>
<li><a name="a68"></a>    <span class="src-var">$page</span><span class="src-sym">[</span><span class="src-str">'{{bots}}'</span><span class="src-sym">] </span>= <span class="src-var">$bots</span><span class="src-sym">;</span></li>
<li><a name="a69"></a>    </li>
<li><a name="a70"></a>    <span class="src-comm">// Select the HTML template and return the completed page to the user's browser</span></li>
<li><a name="a71"></a>    echo <a href="../application/_cms_include_common_inc_php.html#functionuseTemplate">useTemplate</a><span class="src-sym">(</span><span class="src-str">'botselect'</span><span class="src-sym">,</span><span class="src-var">$page</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a72"></a>&nbsp;</li>
<li><a name="a73"></a>    <span class="src-comm">// In this case the PHP script ends processing here.</span></li>
<li><a name="a74"></a>    <span class="src-key">return</span><span class="src-sym">;</span></li>
<li><a name="a75"></a>&nbsp;</li>
<li><a name="a76"></a><span class="src-sym">}</span></li>
<li><a name="a77"></a>&nbsp;</li>
<li><a name="a78"></a><span class="src-comm">// Open and wipe an existing file or create a new one.</span></li>
<li><a name="a79"></a><span class="src-var">$fd </span>= <a href="http://www.php.net/fopen">fopen</a><span class="src-sym">(</span><span class="src-var">$backupfile</span><span class="src-sym">, </span><span class="src-str">&quot;w+&quot;</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a80"></a>&nbsp;</li>
<li><a name="a81"></a><span class="src-comm">// write the AIML file header.</span></li>
<li><a name="a82"></a><span class="src-var">$fout </span>= <a href="http://www.php.net/fwrite">fwrite</a><span class="src-sym">(</span><span class="src-var">$fd</span><span class="src-sym">, </span><span class="src-str">'&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;'</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a83"></a><span class="src-var">$fout </span>= <a href="http://www.php.net/fwrite">fwrite</a><span class="src-sym">(</span><span class="src-var">$fd</span><span class="src-sym">, </span><span class="src-str">&quot;\n&lt;aiml&gt;\n&quot;</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a84"></a>&nbsp;</li>
<li><a name="a85"></a><span class="src-comm">// Count the number of templates in the templates database.</span></li>
<li><a name="a86"></a><span class="src-var">$templateCount </span>= <a href="../application/_cms_include_backup_inc_php.html#functioncount_IDs">count_IDs</a><span class="src-sym">(</span><span class="src-var">$botid</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a87"></a>&nbsp;</li>
<li><a name="a88"></a><span class="src-comm">// Calculate the number of processing cycles.</span></li>
<li><a name="a89"></a><span class="src-var">$templateCycles </span>= <a href="http://www.php.net/ceil">ceil</a><span class="src-sym">(</span><span class="src-var">$templateCount</span>/<span class="src-var">$templatesToProcess</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a90"></a>&nbsp;</li>
<li><a name="a91"></a><span class="src-comm">// Untill the last processing cycle has been reached ...</span></li>
<li><a name="a92"></a><span class="src-key">while</span><span class="src-sym">(</span><span class="src-var">$pid </span>&lt; <span class="src-var">$templateCycles</span><span class="src-sym">) </span><span class="src-sym">{</span></li>
<li><a name="a93"></a>&nbsp;</li>
<li><a name="a94"></a>    <span class="src-comm">// Get the necessary template ID's and their contents.</span></li>
<li><a name="a95"></a>    <span class="src-var">$templateIDs </span>= <a href="../application/_cms_include_backup_inc_php.html#functiongetTemplateIDs">getTemplateIDs</a><span class="src-sym">(</span><span class="src-var">$botid</span><span class="src-sym">, </span><span class="src-var">$pid</span><span class="src-sym">, </span><span class="src-var">$templatesToProcess</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a96"></a>&nbsp;</li>
<li><a name="a97"></a>    <span class="src-comm">// Process every template seperately.</span></li>
<li><a name="a98"></a>    <span class="src-key">while </span><span class="src-sym">(</span>list<span class="src-sym">(</span><span class="src-var">$key</span><span class="src-sym">, </span><span class="src-var">$val</span><span class="src-sym">) </span>= <a href="http://www.php.net/each">each</a><span class="src-sym">(</span><span class="src-var">$templateIDs</span><span class="src-sym">)) </span><span class="src-sym">{</span></li>
<li><a name="a99"></a>        </li>
<li><a name="a100"></a>        <span class="src-comm">// Retrieve the AIML pattern (topic, pattern, that) from the pattern table.</span></li>
<li><a name="a101"></a>        <span class="src-var">$category </span>= <a href="../application/_cms_include_retrieve_inc_php.html#functionfindCategoryPattern">findCategoryPattern</a><span class="src-sym">(</span><span class="src-var">$botid</span><span class="src-sym">, </span><span class="src-var">$key</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a102"></a>        </li>
<li><a name="a103"></a>        <span class="src-comm">// Build the actual AIML category.</span></li>
<li><a name="a104"></a>        <span class="src-var">$aiml_cat </span>= <a href="../application/_cms_include_backup_inc_php.html#functionmakeCategory">makeCategory</a><span class="src-sym">(</span><span class="src-var">$category</span><span class="src-sym">[</span><span class="src-str">'topic'</span><span class="src-sym">]</span><span class="src-sym">, </span><span class="src-var">$category</span><span class="src-sym">[</span><span class="src-str">'that'</span><span class="src-sym">]</span><span class="src-sym">, </span><span class="src-var">$category</span><span class="src-sym">[</span><span class="src-str">'pattern'</span><span class="src-sym">]</span><span class="src-sym">, </span><span class="src-var">$val</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a105"></a>        </li>
<li><a name="a106"></a>        <span class="src-comm">// Write the AIML category to the file</span></li>
<li><a name="a107"></a>        <span class="src-var">$fout </span>= <a href="http://www.php.net/fwrite">fwrite</a><span class="src-sym">(</span><span class="src-var">$fd</span><span class="src-sym">, </span><span class="src-var">$aiml_cat</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a108"></a>        </li>
<li><a name="a109"></a>        <span class="src-comm">// category counter.</span></li>
<li><a name="a110"></a>        <span class="src-var">$categories</span>++<span class="src-sym">;</span></li>
<li><a name="a111"></a>&nbsp;</li>
<li><a name="a112"></a>    <span class="src-sym">}</span></li>
<li><a name="a113"></a>    </li>
<li><a name="a114"></a>    <span class="src-comm">// Increase the Processing ID, to process the next bunch of templates.</span></li>
<li><a name="a115"></a>    <span class="src-var">$pid</span>++<span class="src-sym">;</span></li>
<li><a name="a116"></a><span class="src-sym">}</span></li>
<li><a name="a117"></a>&nbsp;</li>
<li><a name="a118"></a><span class="src-comm">// Write the AIML file ending and close it.</span></li>
<li><a name="a119"></a><span class="src-var">$fout </span>= <a href="http://www.php.net/fwrite">fwrite</a><span class="src-sym">(</span><span class="src-var">$fd</span><span class="src-sym">, </span><span class="src-str">&quot;\n&lt;/aiml&gt;\n&quot;</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a120"></a><a href="http://www.php.net/fclose">fclose</a><span class="src-sym">(</span><span class="src-var">$fd</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a121"></a>&nbsp;</li>
<li><a name="a122"></a><span class="src-comm">// calculation of the total time needed to process the script. </span></li>
<li><a name="a123"></a><span class="src-var">$exec_time </span>= <a href="http://www.php.net/array_sum">array_sum</a><span class="src-sym">(</span><a href="http://www.php.net/explode">explode</a><span class="src-sym">(</span><span class="src-str">' '</span><span class="src-sym">, </span><a href="http://www.php.net/microtime">microtime</a><span class="src-sym">(</span><span class="src-sym">))) </span>- <span class="src-var">$t_start</span><span class="src-sym">;</span></li>
<li><a name="a124"></a>&nbsp;</li>
<li><a name="a125"></a><span class="src-comm">// match the placeholders in the template to </span></li>
<li><a name="a126"></a><span class="src-comm">// a specific PHP variable.</span></li>
<li><a name="a126"></a>&nbsp;</li>
<li><a name="a127"></a><span class="src-var">$page</span><span class="src-sym">[</span><span class="src-str">'{{bot_id}}'</span><span class="src-sym">] </span>= <span class="src-var">$botid</span><span class="src-sym">;</span></li>
<li><a name="a128"></a><span class="src-var">$page</span><span class="src-sym">[</span><span class="src-str">'{{templatesToProcess}}'</span><span class="src-sym">] </span>= <span class="src-var">$templatesToProcess</span><span class="src-sym">;</span></li>
<li><a name="a129"></a><span class="src-var">$page</span><span class="src-sym">[</span><span class="src-str">'{{categories}}'</span><span class="src-sym">] </span>= <span class="src-var">$categories</span><span class="src-sym">;</span></li>
<li><a name="a130"></a><span class="src-var">$page</span><span class="src-sym">[</span><span class="src-str">'{{exec_time}}'</span><span class="src-sym">] </span>= <span class="src-var">$exec_time</span><span class="src-sym">;</span></li>
<li><a name="a131"></a>&nbsp;</li>
<li><a name="a132"></a>&nbsp;</li>
<li><a name="a133"></a><span class="src-comm">// Select the HTML template and return the completed page to the user's browser</span></li>
<li><a name="a134"></a>echo <a href="../application/_cms_include_common_inc_php.html#functionuseTemplate">useTemplate</a><span class="src-sym">(</span><span class="src-str">'backup'</span><span class="src-sym">,</span><span class="src-var">$page</span><span class="src-sym">)</span><span class="src-sym">;</span></li>
<li><a name="a135"></a><span class="src-php">?&gt;</span></li>
</ol></pre>
</div>
	<p class="notes" id="credit">
		Documentation generated on Tue, 11 Jan 2005 18:40:58 +0100 by <a href="http://www.phpdoc.org" target="_blank">phpDocumentor 1.3.0RC3</a>
	</p>
	</body>
</html>
Return current item: Program E