clockwerk-tools – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 1 #!/usr/bin/php
2 <?php
3  
4 /////////////////////////////////////////////////////////////
5 // Wizardry and Steamworks (c) was.fm - 2013, License: MIT //
6 // //
7 // Permission is hereby granted, free of charge, to any //
8 // person obtaining a copy of this software and associated //
9 // documentation files (the "Software"), to deal in the //
10 // Software without restriction, including without //
11 // limitation the rights to use, copy, modify, merge, //
12 // publish, distribute, sublicense, and/or sell copies of //
13 // the Software, and to permit persons to whom the //
14 // Software is furnished to do so, subject to the //
15 // following conditions: //
16 // //
17 // The above copyright notice and this permission notice //
18 // shall be included in all copies or substantial portions //
19 // of the Software. //
20 // //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF //
22 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT //
23 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS //
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO //
25 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE //
26 // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER //
27 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR //
29 // THE USE OR OTHER DEALINGS IN THE SOFTWARE. //
30 /////////////////////////////////////////////////////////////
31  
32 /////////////////////////////////////////////////////////////
33 // CONFIGURATION //
34 /////////////////////////////////////////////////////////////
35  
36 // Hostname or IP of your OpenSim MySQL server.
37 define("MYSQL_HOSTNAME", "127.0.0.1");
38 // Username of the OpenSim MySQL user.
39 define("MYSQL_USERNAME", "opensim");
40 // Password of the OpenSim MySQL user.
41 define("MYSQL_PASSWORD", "***");
42 // Name of the OpenSim database on the MySQL server.
43 define("MYSQL_DATABASE", "opensim");
44  
45 /////////////////////////////////////////////////////////////
46 // INTERNALS //
47 /////////////////////////////////////////////////////////////
48  
49 if(!defined('STDIN')) {
50 print 'This script is meant to be run on the command line.'."\n";
51 return 1;
52 }
53 if($argc < 2) {
54 print 'ERROR: Please specify OARs and IARs to filter on the command line.'."\n";
55 print 'Syntax: php '.$argv[0]. ' <OAR|IAR|...>'."\n";
56 return 1;
57 }
58  
59 /*
60 * Connect to the database and grab all the texture UUIDs
61 * and store them in an array for later filtering.
62 */
63 $connection_ok = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
64 if(!$connection_ok) {
65 print 'Could not connect to the OpenSim database. Please edit the script and make sure the credentials are correct.'."\n";
66 return 1;
67 }
68 $db_selected = mysql_select_db(MYSQL_DATABASE);
69 if(!$db_selected) {
70 print 'Could not select the opensim database. Please edit this script and make sure the credentials are correct.'."\n";
71 return 1;
72 }
73 // Now we can get rid of the script name.
74 array_shift($argv);
75  
76 /*
77 * First thing to do is search all scripts for references to assets. This will ensure that assets
78 * that are referenced inside scripts and that are deleted from an avatar's inventory or not
79 * displayed in-world will not be deleted from the database.
80 */
81 $scriptReferences = array();
82 foreach($argv as $arg) {
83 if(!file_exists($arg)) {
84 print 'Archive: '.$arg.' does not exist.'."\n";
85 return 1;
86 }
87 $files = array();
88 exec('tar -tzf '.$arg.' 2>/dev/null', $files, $ret);
89 foreach($files as $file) {
90 if(!preg_match('/_script/i', $file)) continue;
91 $data = array();
92 exec('tar -zf '.$arg.' -xO '.$file.' 2>/dev/null', $data, $ret);
93 if($ret != 0) {
94 print 'Could not process script: '.$file."\n";
95 return 1;
96 }
97 preg_match_all("/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/i", implode($data), $referencesInScripts);
98 foreach($referencesInScripts[1] as $uuid) {
99 array_push($scriptReferences, $uuid);
100 }
101 }
102 }
103  
104 /*
105 * Now start cleaning assets.
106 */
107 $assets = array( "texture" => 0, "sound" => 1, "clothing" => 5, "script" => 10, "bodypart" => 13, "animation" => 20 );
108 foreach($assets as $assetName => $assetValue) {
109 $query = 'SELECT * FROM assets WHERE assetType='.$assetValue;
110 $result = mysql_query($query);
111  
112 $allAssets = array();
113 while($row = mysql_fetch_array($result)) {
114 array_push($allAssets, $row['id']);
115 }
116  
117 /*
118 * Now grab all the texture UUIDs from the OARs and IARs
119 * supplied on the command line for an exclusion list.
120 */
121 $liveAssets = array();
122 foreach($argv as $arg) {
123 if(!file_exists($arg)) {
124 print 'Archive: '.$arg.' does not exist.'."\n";
125 return 1;
126 }
127 $files = array();
128 exec('tar -tzf '.$arg.' 2>/dev/null', $files, $ret);
129 if($ret != 0) {
130 print 'Could not process archive: '.$arg."\n";
131 return 1;
132 }
133 preg_match_all("/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})_$assetName/i", implode($files), $dumpAssets);
134 foreach($dumpAssets[1] as $uuid) {
135 array_push($liveAssets, $uuid);
136 }
137 }
138 // Now add the script references to the pool.
139 array_push($liveAssets, $scriptReferences);
140  
141 /*
142 * We consider all the assets in the OARs and IARs
143 * supplied on the command line to be live assets.
144 * Everything else is considered unreferenced and
145 * orphaned.
146 *
147 * ORPHANED_ASSETS = (ALL_ASSETS \ LIVE_ASSETS) \ SCRIPT_REFERENCES_TO_ASSETS
148 *
149 * The same applies to all other asset types.
150 */
151 $orphanedAssets = array_values(array_diff($allAssets, $liveAssets));
152 if(sizeof($orphanedAssets) == 0) {
153 print 'Congratulations! Your OpenSim database contains no unreferenced '.$assetName."s.\n";
154 continue;
155 }
156  
157 /*
158 * List assets to be deleted.
159 */
160 print '-------------------- REMOVE -----------------'."\n";
161 foreach($orphanedAssets as $asset) {
162 $query = 'SELECT * FROM assets WHERE assetType='.$assetValue.' AND id=\''.$asset.'\'';
163 $result = mysql_query($query);
164 while($row = mysql_fetch_array($result)) {
165 print $row['name'].' '.$asset."\n";
166 }
167 }
168 print '---------------------------------------------'."\n";
169  
170 /*
171 * Perform some minor liability dumping. Last chance to quit.
172 */
173 ASK:
174 print sizeof($orphanedAssets).' '.$assetName.'s will be removed from the database. Are you sure? (y/n) : ';
175 $in=trim(fgets(STDIN));
176 if($in == 'y') goto GO;
177 if($in == 'n') {
178 print 'Bailing out. Nothing was deleted.'."\n";
179 continue;
180 }
181 print 'Please type either y to proceed or n to quit without deleting anything.'."\n";
182 goto ASK;
183  
184 /*
185 * Start to delete unreferenced assets from the database.
186 */
187 GO:
188 foreach($orphanedAssets as $uuid) {
189 $query = 'DELETE FROM assets WHERE assetType='.$assetValue.' AND id=\''.$uuid.'\'';
190 $result = mysql_query($query);
191 if($result) {
192 print '-';
193 continue;
194 }
195 print 'e';
196 }
197  
198 print "\n";
199 print 'Finished deleting '.$assetName."s.\n";
200 }
201  
202 print "\n";
203 print 'All operations completed!'."\n";