/ -> dumpcontent.php
1 <?php
2
3 // dumpcontent.php - Dumps table content in tab-delimited file
4 // dumpcontent.php - author: Nico Stuurman<nicost@sourceforge.net>
5
6 /***************************************************************************
7 * Dumps table content in tab-delimited file *
8 * Takes 'tablename' as a get variable *
9 * *
10 * Copyright (c) 2003 by Nico Stuurman *
11 * ------------------------------------------------------------------------ *
12 * This program is free software; you can redistribute it and/or modify it *
13 * under the terms of the GNU General Public License as published by the *
14 * Free Software Foundation; either version 2 of the License, or (at your *
15 * option) any later version. *
16 \**************************************************************************/
17 // this may take a long time. Simply kill me if I hang
18 ini_set('max_execution_time','0');
19
20 require ('include.php');
21 require ('includes/db_inc.php');
22 require ('includes/general_inc.php');
23
24 printheader($httptitle,false);
25 navbar ($USER['permissions']);
26
27 if (!$USER['permissions'] & $SUPER) {
28 echo "<h3 align='center'>Sorry, this page is not for you.</h3>\n";
29 printfooter($db, $USER);
30 exit();
31 }
32
33 // Three GET variables are useful:
34 // tablename
35 // fields - let's you export only a subset of columns
36 // valuesOnly - switch wether or not values should be 'expanded'
37
38 $tablename=$HTTP_GET_VARS['tablename'];
39 $tableid=get_cell($db,'tableoftables','id','tablename',$tablename);
40 if (!$tableid) {
41 echo "<h3>This script will dump the contents of a table in a tab-delimited file. The contents of that file can be imported into phplabware or any other database program.</h3>";
42 $r=$db->execute("SELECT id,tablename FROM tableoftables");
43 if ($r) {
44 echo "<table align='center'>\n";
45 echo "<tr><td><h3>Select one of the following tables:</h3></td></tr>\n";
46 while ($r && !$r->EOF) {
47 echo "<tr><td><a href='dumpcontent.php?tablename=".$r->fields[1]."'>".$r->fields[1]."</a></td></tr>\n";
48 $r->MoveNext();
49 }
50 }
51 printfooter($d, $USER);
52 exit();
53 }
54
55 $tableinfo=new tableinfo($db);
56
57 if (! ($HTTP_GET_VARS['fields'] || $HTTP_POST_VARS['selectfields'])) {
58 $rfields=$db->Execute("SELECT columnname FROM {$tableinfo->desname}");
59 $tr=$rfields->recordCount();
60 $menu=$rfields->GetMenu('selectfields'," ",false,true,$tr);
61 echo "<table align='center'>\n<tr><td>\n";
62 echo "<form method='post' action='$PHP_SELF?tablename={$tableinfo->name}'>\n";
63 echo "<h3>Select the fields you want to export: </h3><br>$menu<br>";
64 echo "<input type='submit' name='submit' value='submit'>\n";
65 echo "</form>\n</td></tr></table>\n";
66 printfooter($d,$USER);
67 exit();
68 }
69 if ($HTTP_POST_VARS['selectfields']) {
70 foreach($HTTP_POST_VARS['selectfields'] as $field)
71 $fields.=$field.",";
72 // strip the last comma
73 $fields=substr($fields,0,-1);
74 //print_r($HTTP_POST_VARS['selectfields']);
75 }
76
77 if (!$tableinfo->id) {
78 echo "<h3 align='center'>Table <i>$tablename</i> does not exist.</h3>\n";
79 printfooter($d, $USER);
80 exit();
81 }
82
83 $pre_seperator="";
84 $post_seperator="\t";
85
86 // open file to write output to
87 $tmpdir=$system_settings['tmpdir'].'/phplabwaredump';
88 if (!file_exists($tmpdir))
89 mkdir ($tmpdir);
90 $outfile=$tmpdir.'/dumpcontent.txt';
91 $filedir=$tmpdir.'/files/';
92 $filetable=$tmpdir.'/files.txt';
93 // since it is a tmp dir we can destroy all content (right?)
94 if (file_exists($filedir))
95 `rm -rf {$filedir}*`;
96 else
97 mkdir ($filedir);
98
99 $fp=fopen($outfile,'w');
100 if (!$fp) {
101 echo "<h3 align='center'>Failed to open <i>$outfile</i> for output</h3>\n";
102 printfooter($db, $USER);
103 }
104 $ff=fopen($filetable,'w');
105 if (!$ff) {
106 echo "<h3 align='center'>Failed to open <i>$filetable</i> for output</h3>\n";
107 printfooter($db, $USER);
108 }
109 // write the column headers for the temp file table
110 fwrite ($ff,"id\tname\tmime\tsize\ttype\n");
111
112 if (!$fields) {
113 if ($HTTP_GET_VARS['fields'])
114 $fields='id,'.$HTTP_GET_VARS['fields'];
115 else
116 $fields='id,'.comma_array_SQL($db,$tableinfo->desname,'columnname');
117 }
118 else
119 $fields='id,'.$fields;
120 // we might have accidentaly added an extra id, delete that here:
121 $fields=preg_replace('/,id/','',$fields);
122
123 $headers=getvalues($db,$tableinfo,$fields);
124 if ($HTTP_GET_VARS['valuesOnly'])
125 $valuesOnly=true;
126
127 foreach ($headers as $header) {
128 if ($header['label'])
129 fwrite ($fp,$pre_seperator.$header['label'].$post_seperator);
130 else
131 fwrite ($fp,$pre_seperator.'id'.$post_seperator);
132 }
133 fwrite ($fp,"\n");
134
135 $r=$db->Execute("SELECT $fields FROM ".$tableinfo->realname);
136 while ($r->fields['id'] && !$r->EOF) {
137 $rowvalues=getvalues($db,$tableinfo,$fields,'id',$r->fields['id']);
138 foreach ($rowvalues as $row) {
139 if (is_array($row)) {
140 // files will be exported to the directory files
141 if ($row['datatype']=='file') {
142 $files=get_files($db,$tableinfo->name,$row['recordid'],$row['columnid'],0);
143 fwrite ($fp,$pre_seperator);
144 for ($i=0;$i<sizeof($files);$i++) {
145 $filecounter++;
146 // write a temp table with all the file info,and provide an comma separated list of ids to that table here
147 fwrite ($ff,++$filenr."\t".$files[$i]['name']."\t".$files[$i]['mime']."\t".$files[$i]['size']."\t".$files[$i]['type']."\n");
148 fwrite ($fp,$filenr.',');
149 $path=file_path($db,$files[$i]['id']);
150 // $cpstr="cp $path {$filedir}{$filenr}_{$files[$i]['name']}";
151 //`$cpstr`;
152 copy ($path,"{$filedir}{$filenr}_{$files[$i]['name']}");
153 }
154 fwrite ($fp,$post_seperator);
155 }
156 else {
157
158 if ($valuesOnly) {
159 fwrite ($fp,$pre_seperator.$row['values'].$post_seperator);
160 }
161 else {
162 if ($row['datatype']=='textlong') {
163 // strip every kind of new line tag first
164 $row['values']=strtr($row['values'],"\t\n\r\m",' ');
165 fwrite ($fp,$pre_seperator.$row['values'].$post_seperator);
166 }
167 else
168 fwrite ($fp,$pre_seperator.$row['text'].$post_seperator);
169 }
170 }
171 }
172 else
173 fwrite ($fp,$pre_seperator.$row.$post_seperator);
174 }
175 fwrite ($fp,"\n");
176 $counter++;
177 $r->MoveNext();
178 }
179
180
181 fclose($fp);
182
183 echo "<h3>Wrote script to $outfile.</h3>";
184 echo "<h3>Wrote $counter records.</h3>";
185 if ($filecounter)
186 echo "<h3>Saved $filecounter files in $filedir.</h3>";
187
188 printfooter($db, $USER);
189
190 ?>