summaryrefslogtreecommitdiff
path: root/htroot/IndexControl_p.java
blob: f52a4c135bbb194f61d9967d2e49687ca34b957f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// IndexControl_p.java 
// -----------------------
// part of the AnomicHTTPD caching proxy
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
// last change: 02.05.2004
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.

// you must compile this file with
// javac -classpath .:../Classes IndexControl_p.java
// if the shell's current path is HTROOT

import java.util.*;
import java.net.*;
import java.io.*;
import de.anomic.tools.*;
import de.anomic.server.*;
import de.anomic.plasma.*;
import de.anomic.http.*;
import de.anomic.htmlFilter.*;
import de.anomic.yacy.*;

public class IndexControl_p {

    public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch env) {
	// return variable that accumulates replacements
        plasmaSwitchboard switchboard = (plasmaSwitchboard) env;
	serverObjects prop = new serverObjects();

        if ((post == null) || (env == null)) {
            prop.put("keystring", "");
            prop.put("keyhash", "");
            prop.put("urlstring", "");
            prop.put("urlhash", "");
            prop.put("result", "");
            prop.put("wcount", "" + switchboard.wordIndex.sizeMin());
            prop.put("ucount", "" + switchboard.loadedURL.size());
            prop.put("otherHosts", "");
            prop.put("indexDistributeChecked", (switchboard.getConfig("allowDistributeIndex", "true").equals("true")) ? "checked" : "");
            prop.put("indexReceiveChecked", (switchboard.getConfig("allowReceiveIndex", "true").equals("true")) ? "checked" : "");
            return prop; // be save
        }
        
        // default values
        String keystring = ((String) post.get("keystring")).trim();
        String keyhash = ((String) post.get("keyhash")).trim();
        String urlstring = ((String) post.get("urlstring")).trim();
        String urlhash = ((String) post.get("urlhash")).trim();
        
        if (!(urlstring.startsWith("http://"))) urlstring = "http://" + urlstring;
        
        prop.put("keystring", keystring);
        prop.put("keyhash", keyhash);
        prop.put("urlstring", urlstring);
        prop.put("urlhash", urlhash);
        prop.put("result", "");
        
        // read values from checkboxes
        String[] urlx = post.getAll("urlhx.*");
        boolean delurl    = post.containsKey("delurl");
        boolean delurlref = post.containsKey("delurlref");
        //System.out.println("DEBUG CHECK: " + ((delurl) ? "delurl" : "") + " " + ((delurlref) ? "delurlref" : ""));
        
        if (post.containsKey("setIndexDistribute")) {
            boolean allowDistributeIndex = ((String) post.get("indexDistribute", "")).equals("on");
            switchboard.setConfig("allowDistributeIndex", (allowDistributeIndex) ? "true" : "false");
        }
        
        if (post.containsKey("setIndexReceive")) {
            boolean allowReceiveIndex = ((String) post.get("indexReceive", "")).equals("on");
            switchboard.setConfig("allowReceiveIndex", (allowReceiveIndex) ? "true" : "false");
            yacyCore.seedDB.mySeed.setFlagAcceptRemoteIndex(allowReceiveIndex);
        }
        
        if (post.containsKey("keyhashdeleteall")) {
            try {
                if ((delurl) || (delurlref)) {
                    // generate an urlx array
                    try {
                        HashSet keyhashes = new HashSet();
                        keyhashes.add(keyhash);
                        plasmaWordIndexEntity index = switchboard.searchManager.searchHashes(keyhashes, 10000);
                        Enumeration en = index.elements(true);
                        int i = 0;
                        urlx = new String[index.size()];
                        while (en.hasMoreElements()) urlx[i++] = ((plasmaWordIndexEntry) en.nextElement()).getUrlHash();
                    } catch (IOException e) {
                        urlx = new String[0];
                    }
                }
                if (delurlref) for (int i = 0; i < urlx.length; i++) switchboard.removeAllUrlReferences(urlx[i], true);
                if ((delurl) || (delurlref)) for (int i = 0; i < urlx.length; i++) switchboard.loadedURL.remove(urlx[i]);
                switchboard.wordIndex.deleteComplete(keyhash);
            } catch (IOException e) {}
            post.remove("keyhashdeleteall");
            if ((keystring.length() > 0) && (plasmaWordIndexEntry.word2hash(keystring).equals(keyhash)))
                post.put("keystringsearch", "generated");
            else
                post.put("keyhashsearch", "generated");
        }
        
        if (post.containsKey("keyhashdelete")) {
            try {
                if (delurlref) for (int i = 0; i < urlx.length; i++) switchboard.removeAllUrlReferences(urlx[i], true);
                if ((delurl) || (delurlref)) for (int i = 0; i < urlx.length; i++) switchboard.loadedURL.remove(urlx[i]);
                switchboard.wordIndex.removeEntries(keyhash, urlx, true);
            } catch (IOException e) {}
            // this shall lead to a presentation of the list; so handle that the remaining program
            // thinks that it was called for a list presentation
            post.remove("keyhashdelete");
            if ((keystring.length() > 0) && (plasmaWordIndexEntry.word2hash(keystring).equals(keyhash)))
                post.put("keystringsearch", "generated");
            else
                post.put("keyhashsearch", "generated");
            //prop.put("result", "Delete of relation of url hashes " + result + " to key hash " + keyhash);
        }
        
        if (post.containsKey("urlhashdeleteall")) {
            int i = switchboard.removeAllUrlReferences(urlhash, true);
            prop.put("result", "Deleted URL and " + i + " references from " + i + " word indexes.");
        }
        
        if (post.containsKey("urlhashdelete")) {
            plasmaCrawlLURL.entry entry = switchboard.loadedURL.getEntry(urlhash);
            URL url = entry.url();
            if (url == null) {
                prop.put("result", "No Entry for url hash " + urlhash + "; nothing deleted.");
            } else {
                urlstring = htmlFilterContentScraper.urlNormalform(url);
                prop.put("urlstring", "");
                switchboard.loadedURL.remove(urlhash);
                prop.put("result", "Removed URL " + urlstring);
            }
        }

        if (post.containsKey("keystringsearch")) {
            keyhash = plasmaWordIndexEntry.word2hash(keystring);
            prop.put("keyhash", keyhash);
            prop.put("urlstring", "");
            prop.put("urlhash", "");
            prop.put("result", genUrlList(switchboard, keyhash, keystring));
        }
        
        if (post.containsKey("keyhashsearch")) {
            if ((keystring.length() == 0) || (!(plasmaWordIndexEntry.word2hash(keystring).equals(keyhash))))
                prop.put("keystring", "<not possible to compute word from hash>");
            prop.put("urlstring", "");
            prop.put("urlhash", "");
            prop.put("result", genUrlList(switchboard, keyhash, ""));
        }
        
        if (post.containsKey("keyhashtransfer")) {
            if ((keystring.length() == 0) || (!(plasmaWordIndexEntry.word2hash(keystring).equals(keyhash))))
                prop.put("keystring", "<not possible to compute word from hash>");
            prop.put("urlstring", "");
            prop.put("urlhash", "");
            plasmaWordIndexEntity[] indexes = new plasmaWordIndexEntity[1];
            String result;
            long starttime = System.currentTimeMillis();
            try {indexes[0] = switchboard.wordIndex.getEntity(keyhash, true);
                result = yacyClient.transferIndex(yacyCore.seedDB.getConnected(post.get("hostHash", "")), indexes, switchboard.loadedURL);
            } catch (IOException e) {
                result = "IOException: " + e.getMessage();
            }
            prop.put("result", (result == null) ? ("Successfully transferred " + indexes[0].size() + " words in " + ((System.currentTimeMillis() - starttime) / 1000) + " seconds") : result);
        }
        
	if (post.containsKey("keyhashsimilar")) {
                Iterator hashIt = switchboard.wordIndex.hashIterator(keyhash, true, true, true);
                String result = "Sequential List of Word-Hashes:<br>";
                String hash;
                int i = 0;
                while (hashIt.hasNext()) {
                    hash = (String) hashIt.next();
                    result += "<a href=\"/IndexControl_p.html?" + 
                    "keystring=" +
                    "&keyhash=" + hash +
                    "&urlhash=" +
                    "&urlstring=" +
                    "&keyhashsearch=" +
                    "\" class=\"tt\">" + hash + "</a> " + (((i + 1) % 8 == 0) ? "<br>" : "");
                    i++;
                }
                prop.put("result", result);
        }
        
	if (post.containsKey("urlstringsearch")) {
            try {
                URL url = new URL(urlstring);
                urlhash = plasmaURL.urlHash(url);
                prop.put("urlhash", urlhash);
                plasmaCrawlLURL.entry entry = switchboard.loadedURL.getEntry(urlhash);
                prop.put("result", genUrlProfile(switchboard, entry, urlhash));
            } catch (MalformedURLException e) {
                prop.put("urlstring", "wrong url: " + urlstring);
                prop.put("urlhash", "");
            }
        }
        
        if (post.containsKey("urlhashsearch")) {
            plasmaCrawlLURL.entry entry = switchboard.loadedURL.getEntry(urlhash);
            URL url = entry.url();
            if (url == null) {
                prop.put("result", "No Entry for url hash " + urlhash);
            } else {
                urlstring = url.toString();
                prop.put("urlstring", urlstring);
                prop.put("result", genUrlProfile(switchboard, entry, urlhash));
            }
        }

	if (post.containsKey("urlhashsimilar")) {
            try {
                Iterator hashIt = switchboard.loadedURL.urlHashes(urlhash, true);
                String result = "Sequential List of URL-Hashes:<br>";
		String hash;
		int i = 0;
                while (hashIt.hasNext()) {
		    hash = (String) hashIt.next();
                    result += "<a href=\"/IndexControl_p.html?" + 
                    "keystring=" +
                    "&keyhash=" +
                    "&urlhash=" + hash +
                    "&urlstring=" + 
                    "&urlhashsearch=" +
                    "\" class=\"tt\">" + hash + "</a> " + (((i + 1) % 8 == 0) ? "<br>" : "");
                    i++;
		}
                prop.put("result", result);
            } catch (IOException e) {
                prop.put("result", "Error: " + e.getMessage());
            }
        }

        //List known hosts
	yacySeed seed;
        int hc = 0;
	if ((yacyCore.seedDB != null) && (yacyCore.seedDB.sizeConnected() > 0)) {
	    Enumeration e = yacyCore.dhtAgent.getAcceptRemoteIndexSeeds(keyhash);
	    while (e.hasMoreElements()) {
		seed = (yacySeed) e.nextElement();
                if (seed != null) {
                    prop.put("hosts_" + hc + "_hosthash", seed.hash);
                    prop.put("hosts_" + hc + "_hostname", /*seed.hash + " " +*/ seed.get("Name", "nameless"));
                    hc++;
                }
	    }
            prop.put("hosts", "" + hc);
	} else {
            prop.put("hosts", "0");
	}
        
        // insert constants
        prop.put("wcount", "" + switchboard.wordIndex.sizeMin());
        prop.put("ucount", "" + switchboard.loadedURL.size());
	prop.put("indexDistributeChecked", (switchboard.getConfig("allowDistributeIndex", "true").equals("true")) ? "checked" : "");
        prop.put("indexReceiveChecked", (switchboard.getConfig("allowReceiveIndex", "true").equals("true")) ? "checked" : "");
        // return rewrite properties
	return prop;
    }

    public static String genUrlProfile(plasmaSwitchboard switchboard, plasmaCrawlLURL.entry entry, String urlhash) {
        if (entry == null) return "No entry found for url-hash " + urlhash;
        URL url = entry.url();
        if (url == null) return "No entry found for url-hash " + urlhash;
        String result = "<table>" +
        "<tr><td class=\"small\">URL String</td><td class=\"tt\">" + htmlFilterContentScraper.urlNormalform(url) + "</td></tr>" +
        "<tr><td class=\"small\">Hash</td><td class=\"tt\">" + urlhash + "</td></tr>" +
        "<tr><td class=\"small\">Description</td><td class=\"tt\">" + entry.descr() + "</td></tr>" +
        "<tr><td class=\"small\">Modified-Date</td><td class=\"tt\">" + entry.moddate() + "</td></tr>" +
        "<tr><td class=\"small\">Loaded-Date</td><td class=\"tt\">" + entry.loaddate() + "</td></tr>" +
        "<tr><td class=\"small\">Referrer</td><td class=\"tt\">" + switchboard.loadedURL.getEntry(entry.referrerHash()).url() + "</td></tr>" +
        "<tr><td class=\"small\">Doctype</td><td class=\"tt\">" + entry.doctype() + "</td></tr>" +
        "<tr><td class=\"small\">Copy-Count</td><td class=\"tt\">" + entry.copyCount() + "</td></tr>" +
        "<tr><td class=\"small\">Local-Flag</td><td class=\"tt\">" + entry.local() + "</td></tr>" +
        "<tr><td class=\"small\">Quality</td><td class=\"tt\">" + entry.quality() + "</td></tr>" +
        "<tr><td class=\"small\">Language</td><td class=\"tt\">" + entry.language() + "</td></tr>" +
        "<tr><td class=\"small\">Size</td><td class=\"tt\">" + entry.size() + "</td></tr>" +
        "<tr><td class=\"small\">Words</td><td class=\"tt\">" + entry.wordCount() + "</td></tr>" +
        "</table><br>";
        result +=
        "<form action=\"IndexControl_p.html\" method=\"post\" enctype=\"multipart/form-data\">" +
        "<input type=\"hidden\" name=\"keystring\" value=\"\">" +
        "<input type=\"hidden\" name=\"keyhash\" value=\"\">" +
        "<input type=\"hidden\" name=\"urlstring\" value=\"\">" +
        "<input type=\"hidden\" name=\"urlhash\" value=\"" + urlhash + "\">" +
        "<input type=\"submit\" value=\"Delete URL\" name=\"urlhashdelete\"><br>" +
        "<span class=\"small\">&nbsp;this may produce unresolved references at other word indexes but they do not harm</span><br><br>" +
        "<input type=\"submit\" value=\"Delete URL and remove all references from words\" name=\"urlhashdeleteall\"><br>" +
        "<span class=\"small\">&nbsp;delete the reference to this url at every other word where the reference exists (very extensive, but prevents unresolved references)</span><br>" +
        "</form>";       
        return result;
    }
    
    public static String genUrlList(plasmaSwitchboard switchboard, String keyhash, String keystring) {
        // search for a word hash and generate a list of url links
        try {
            HashSet keyhashes = new HashSet();
            keyhashes.add(keyhash);
            plasmaWordIndexEntity index = switchboard.searchManager.searchHashes(keyhashes, 10000);
            String result = "";
            if (index.size() == 0) {
                result = "No URL entries related to this word hash <span class=\"tt\">" + keyhash + "</span>.";
            } else {
                Enumeration en = index.elements(true);
                plasmaWordIndexEntry ie;
                result = "URL entries related to this word hash <span class=\"tt\">" + keyhash + "</span>:<br>";
                result += "<form action=\"IndexControl_p.html\" method=\"post\" enctype=\"multipart/form-data\">";
                String us, uh;
                int i = 0;
                while (en.hasMoreElements()) {
                    ie = (plasmaWordIndexEntry) en.nextElement();
                    uh = ie.getUrlHash();
                    result +=
                    "<input type=\"checkbox\" name=\"urlhx" + i++ + "\" value=\"" + uh + "\" align=\"top\">";
                    if (switchboard.loadedURL.exists(uh)) {
                        us = switchboard.loadedURL.getEntry(uh).url().toString();
                        result +=
                        "<a href=\"/IndexControl_p.html?" + "keystring=" + keystring +
                        "&keyhash=" + keyhash + "&urlhash=" + uh + "&urlstringsearch=" + "&urlstring=" + us +
                        "\" class=\"tt\">" + uh + "</a><span class=\"tt\">&nbsp;" + us + "</span><br>";
                    } else {
                        result +=
                        "<span class=\"tt\">" + uh + "&nbsp;&lt;unresolved URL Hash&gt;</span><br>";
                    }
                }
                result +=
                "<input type=\"hidden\" name=\"keystring\" value=\"" + keystring + "\">" +
                "<input type=\"hidden\" name=\"keyhash\" value=\"" + keyhash + "\">" +
                "<input type=\"hidden\" name=\"urlstring\" value=\"\">" +
                "<input type=\"hidden\" name=\"urlhash\" value=\"\">" +
                "<br><fieldset><legend>Reference Deletion</legend><table border=\"0\" cellspacing=\"5\" cellpadding=\"5\"><tr valign=\"top\"><td><br><br>" +
                "<input type=\"submit\" value=\"Delete reference to selected URL's\" name=\"keyhashdelete\"><br><br>" +
                "<input type=\"submit\" value=\"Delete reference to ALL URL's\" name=\"keyhashdeleteall\"><span class=\"small\"><br>&nbsp;&nbsp;(= delete Word)</span>" +
                "</td><td width=\"150\">" +
                "<center><input type=\"checkbox\" name=\"delurl\" value=\"\" align=\"top\" checked></center><br>" +
                "<span class=\"small\">delete also the referenced URL itself (reasonable and recommended, may produce unresolved references at other word indexes but they do not harm)</span>" +
                "</td><td width=\"150\">" +
                "<center><input type=\"checkbox\" name=\"delurlref\" value=\"\" align=\"top\"></center><br>" +
                "<span class=\"small\">for every resolveable and deleted URL reference, delete the same reference at every other word where the reference exists (very extensive, but prevents further unresolved references)</span>" +
                "</td></tr></table></fieldset></form>";
            }
            return result;
        }  catch (IOException e) {
            return "";
        }
    }
    
}