41
41
public static void Main(string[] args)
43
43
SetProcessName("grub-urlsdb");
44
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MainUnhandledException);
45
44
//If no arguments for program, show help
46
45
if (args.Length == 0)
48
Console.WriteLine("Syntax: mono urlsdb.exe [OPTION] [COMMAND]");
49
Console.WriteLine("Options:");
50
Console.WriteLine("--quiet - no console output (quiet mode, only error messages)");
51
Console.WriteLine("Commands:");
52
47
Console.WriteLine("createconf - create configuration file for program");
53
48
Console.WriteLine("updateconf - update configuration file for program");
54
49
Console.WriteLine("createdb - create database");
55
50
Console.WriteLine("updatedb - update database");
56
51
Console.WriteLine("filldb [FILENAME] - put URL's to database from FILENAME file");
57
Console.WriteLine("fastfilldb [FILENAME] - put URL's to database from FILENAME file. For difference with filldb look at README.txt");
58
52
Console.WriteLine("backup [FILENAME] - create database backup to FILENAME file (only URL's)");
59
53
Console.WriteLine("addurl [URL] - add URL to database");
60
54
Console.WriteLine("delurl [URL] - delete URL from database and index");
61
55
Console.WriteLine("delurls [FILENAME] - delete URL's (list from FILENAME) from database and index");
62
56
Console.WriteLine("createwu - create workunits from URL's from database.");
63
Console.WriteLine("maintain - delete URL's from database (with HTTP status codes: 204, 301, 400-408, 500, 503), optimize database and Solr, create compressed backup of URL's");
57
Console.WriteLine("maintain - delete URL's from database (with HTTP status codes: 301 and 400-406), optimize tables and optimize Solr");
64
58
Console.WriteLine("optimizedb - optimize and flush database");
65
59
Console.WriteLine("optimizesolr - optimize Solr index (if Solr is enabled)");
72
if (args[0] != "--quiet")
82
util = new Utils(quiet);
83
62
//Create configuration file
84
if (command == "createconf")
63
if (args[0] == "createconf")
86
util.Message("Creating configuration file ... ", false);
87
Config.CreateConfig();
88
util.Message("done.", true);
89
util.Message("Please now edit it for set correct settings", true);
65
Console.Write("Creating configuration file ... ");
66
Config.CreateConfig();
67
Console.WriteLine("done.");
68
Console.WriteLine("Please now edit it for set correct settings");
92
71
//Update configuration file
93
if (command == "updateconf")
72
if (args[0] == "updateconf")
95
util.Message("Updating configuration file ... ", false);
74
Console.Write("Updating configuration file ... ");
96
75
Config.UpdateConfig();
97
util.Message("done.", true);
98
util.Message("Please now edit it for set correct settings", true);
76
Console.WriteLine("done.");
77
Console.WriteLine("Please now edit it for set correct settings");
102
if (command == "createdb")
81
if (args[0] == "createdb")
104
util.Message("Creating database ... ", false);
83
Console.Write("Creating database ... ");
105
84
Database.CreateDB();
106
util.Message("done.", true);
85
Console.WriteLine("done.");
109
88
Database db = new Database();
111
if (command == "updatedb")
91
if (args[0] == "updatedb")
113
util.Message("Updating database ... ", false);
93
Console.Write("Updating database ... ");
115
util.Message("done.", true);
95
Console.WriteLine("done.");
118
99
//Backup URL's to plain text file
119
Files file = new Files(quiet);
120
if (command == "backup")
100
if (args[0] == "backup")
122
if (args.Length == pos)
102
if (args.Length == 1)
124
104
Console.WriteLine("Please enter backup file name.");
127
file.Backup(args[pos]);
107
Console.Write("Creating database backup ... ");
110
StreamWriter writer = new StreamWriter(args[1]);
111
StringBuilder urls2 = new StringBuilder();
114
urls2.Append(db.SelectURLs(offset));
115
if (urls2.Length == 0)
119
urls2.Append(Environment.NewLine);
122
urls2.Remove(0, urls2.Length);
127
Console.WriteLine("done.");
129
129
ParseURLs parseurl = new ParseURLs();
131
131
string record, hash1, hash2 = String.Empty;
132
//Check if selected file exists
133
if ((command == "filldb") || (command == "fastfilldb") || (command == "delurls"))
135
if (args.Length == 1)
137
Console.WriteLine("Please enter file name with urls.");
140
if (!File.Exists(args[pos]))
142
Console.WriteLine("File {0} not exist.", args[pos]);
146
132
//Insert URL's from file to database
147
if ((command == "filldb") || (command == "fastfilldb"))
133
if (args[0] == "filldb")
149
if (command == "filldb")
151
file.AddURLs(args[pos], false);
155
file.AddURLs(args[pos], true);
135
if (args.Length == 1)
137
Console.WriteLine("Please enter file name with urls.");
140
if (!File.Exists(args[1]))
142
Console.WriteLine("File {0} not exist.", args[1]);
145
StreamReader reader = new StreamReader(args[1]);
147
while (!reader.EndOfStream)
149
record = ParseURLs.ParseURL(reader.ReadLine());
150
if (record.Length > 0)
152
hash1 = parseurl.GetHash(record);
153
if (!record.StartsWith("www."))
155
hash2 = parseurl.GetHash("www." + record);
159
hash2 = parseurl.GetHash(record.Substring(4));
161
amount += db.InstertURL(hash1, record, hash2);
167
Console.WriteLine("{0} URL's was inserted from {1} URL's.", amount.ToString(), i.ToString());
158
169
//Add single URL to database
159
if (command == "addurl")
170
if (args[0] == "addurl")
161
172
if (args.Length == 1)
163
174
Console.WriteLine("Please enter URL to add.");
166
record = ParseURLs.ParseURL(args[pos]);
177
record = ParseURLs.ParseURL(args[1]);
167
178
if (record.Length > 0)
169
180
hash1 = parseurl.GetHash(record);
192
203
solrenabled = true;
194
205
//Delete single URL from database
195
if (command == "delurl")
206
if (args[0] == "delurl")
197
208
if (args.Length == 1)
199
210
Console.WriteLine("Please enter URL to delete.");
202
amount = db.DeleteURL(parseurl.GetHash(args[pos]));
213
amount = db.DeleteURL(parseurl.GetHash(args[1]));
205
string solrcommand = "<delete><id>http://" +
206
System.Security.SecurityElement.Escape(ParseURLs.ParseURL(args[pos])) +
216
string solrcommand = "<delete><id>http://" + ParseURLs.ParseURL(args[1]) + "</id></delete>";
208
217
solrcommand = Regex.Replace(solrcommand, @"[\p{IsC}]", String.Empty);
209
MainClass.SendCommand(solrcommand, "Solr");
218
MainClass.SendToSolr(solrcommand);
210
219
solrcommand = "<commit/>";
211
MainClass.SendCommand(solrcommand, "Solr");
220
MainClass.SendToSolr(solrcommand);
215
util.Message("URL not exist in database.", true);
224
Console.WriteLine("URL not exist in database.");
219
util.Message("URL deleted from database.", true);
228
Console.WriteLine("URL deleted from database.");
223
231
//Delete URL's from file from database
224
if (command == "delurls")
232
if (args[0] == "delurls")
226
util.Message("Deleting URL's from database ... ", false);
227
FileInfo finfo = new FileInfo(args[pos]);
228
int length = (int)finfo.Length;
230
StreamReader reader = new StreamReader(args[pos]);
231
StringBuilder solrcommand = new StringBuilder();
232
solrcommand.Append("<delete>");
233
int i = 0, amount2 = 0;
234
string scommand = String.Empty;
235
System.Collections.Generic.List<string> records = new System.Collections.Generic.List<string>();
236
util.ProgressStart(length, String.Empty);
234
if (args.Length == 1)
236
Console.WriteLine("Please enter file name with urls.");
239
if (!File.Exists(args[1]))
241
Console.WriteLine("File {0} not exist.", args[1]);
244
StreamReader reader = new StreamReader(args[1]);
245
string solrcommand = "<delete>";
237
247
while (!reader.EndOfStream)
239
for (int j = 0; j < 25000; j++)
241
record = reader.ReadLine();
248
amount += db.DeleteURLs(records);
249
foreach (string record3 in records)
253
solrcommand.Append("<id>http://");
254
solrcommand.Append(System.Security.SecurityElement.Escape(ParseURLs.ParseURL(record3)));
255
solrcommand.Append("</id>");
259
solrcommand.Append("</delete>");
260
scommand = Regex.Replace(solrcommand.ToString(), @"[\p{IsC}]", String.Empty);
261
MainClass.SendCommand(scommand, "Solr");
262
MainClass.SendCommand("<commit/>", "Solr");
263
solrcommand.Remove(0, solrcommand.Length);
264
solrcommand.Append("<delete>");
268
util.Curamount += record3.Length;
272
records.TrimExcess();
249
record = reader.ReadLine();
250
amount += db.DeleteURL(parseurl.GetHash(record));
251
solrcommand += "<id>http://" + ParseURLs.ParseURL(record) + "</id>";
276
255
reader.Dispose();
279
solrcommand.Append("</delete>");
280
scommand = Regex.Replace(solrcommand.ToString(), @"[\p{IsC}]", String.Empty);
281
MainClass.SendCommand(scommand, "Solr");
282
MainClass.SendCommand("<commit/>", "Solr");
258
solrcommand += "</delete>";
259
solrcommand = Regex.Replace(solrcommand, @"[\p{IsC}]", String.Empty);
260
MainClass.SendToSolr(solrcommand);
261
solrcommand = "<commit/>";
262
MainClass.SendToSolr(solrcommand);
285
util.Message(String.Empty, true);
286
util.Message(amount.ToString() + " URL's was deleted from " + i.ToString() + " URL's.", true);
264
Console.WriteLine("{0} URL's was deleted from {1} URL's.", amount.ToString(), i.ToString());
288
266
//Create workunits from URL's from database
289
if (command == "createwu")
267
if (args[0] == "createwu")
291
util.Message("Creating workunits ... ", false);
292
269
string urls, path, host;
293
270
string wupass = Config.ReadConfig("/configuration/workunitspassword");
294
271
string useragent = Config.ReadConfig("/configuration/useragent");
377
354
writer.Dispose();
378
355
File.Delete(tempwu);
379
util.Message("done.", true);
381
358
//Maintenance work on URL's database and Solr
382
if ((command == "maintain") || (command == "optimizedb") || (command == "optimizesolr"))
384
MainClass.SendCommand("serverenabled=N,enableupload=N", "Upload server");
386
if (command == "maintain")
388
string[] codes = new string[] {"204", "301", "400", "401", "402", "403", "404", "405", "406", "408", "500", "503"};
359
if (args[0] == "maintain")
361
string[] codes = new string[] {"204", "301", "400", "401", "402", "403", "404", "405", "406", "408", "503"};
389
362
foreach (string code in codes)
391
util.Message("Deleting URL's with HTTP status code " + code, true);
364
Console.WriteLine("Deleting URL's with HTTP status code {0}", code);
392
366
amount = db.CleanURLs(code, solrenabled);
393
util.Message(amount.ToString() + " URL's was deleted.", true);
368
Console.WriteLine("{0} URL's was deleted.", amount);
397
if ((command == "optimizedb") || (command == "maintain"))
371
//Optimize and flush database
372
if ((args[0] == "optimizedb") || (args[0] == "maintain"))
399
util.Message("Optimizing database ... ", false);
374
Console.Write("Optimizing database ... ");
401
util.Message("done.", true);
378
Console.WriteLine("done.");
403
380
//Optimize Solr index
404
if ((command == "optimizesolr") || (command == "maintain"))
381
if ((args[0] == "optimizesolr") || (args[0] == "maintain"))
406
383
if (!solrenabled)
410
util.Message("Optimizing Solr ... ", false);
411
MainClass.SendCommand("<optimize/>", "Solr");
412
util.Message("done.", true);
414
//Create database backup during maintenance work
415
if (command == "maintain")
417
file.Backup(DateTime.UtcNow.ToString("yyyy" + "MM" + "dd") + ".txt");
419
if ((command == "maintain") || (command == "optimizedb") || (command == "optimizesolr"))
421
MainClass.SendCommand("serverenabled=Y,enableupload=Y", "Upload server");
387
Console.Write("Optimizing Solr ... ");
388
MainClass.SendToSolr("<optimize/>");
389
Console.WriteLine("done.");
451
/// Function send XML Update command to remote server
418
/// Function send XML Update command to Solr
453
420
/// <param name="command">
454
/// A <see cref="System.String"/> command to send.
421
/// A <see cref="System.String"/> XML Update command to send
456
/// <param name="destination">
457
/// A <see cref="System.String"/> destination for command (Solr, upload server).
458
public static void SendCommand(string command, string destination)
423
public static void SendToSolr(string command)
460
HttpWebRequest request;
461
if (destination == "Solr")
463
request = (HttpWebRequest)WebRequest.Create(Config.ReadConfig("/configuration/solraddress"));
464
request.Credentials = new NetworkCredential(Config.ReadConfig("/configuration/solrusername"),
465
Config.ReadConfig("configuration/solrpassword"));
466
request.PreAuthenticate = true;
467
request.ContentType = "text/xml";
471
request = (HttpWebRequest)WebRequest.Create(Config.ReadConfig("/configuration/uploadaddress"));
472
command = "config," + Config.ReadConfig("/configuration/uploadusername") + "," +
473
Config.ReadConfig("/configuration/uploadpassword") + ",set," + command;
425
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Config.ReadConfig("/configuration/solraddress"));
475
426
request.Proxy = null;
427
request.Credentials = new NetworkCredential(Config.ReadConfig("/configuration/solrusername"),
428
Config.ReadConfig("configuration/solrpassword"));
429
request.PreAuthenticate = true;
430
request.ContentType = "text/xml";
476
431
request.Method = "POST";
477
432
request.Timeout = 1000000000;
433
Stream streamw1 = request.GetRequestStream();
478
434
byte[] buffer = Encoding.UTF8.GetBytes(command);
479
using (Stream streamw1 = request.GetRequestStream())
481
streamw1.Write(buffer, 0, buffer.Length);
435
streamw1.Write(buffer, 0, buffer.Length);
486
440
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
494
448
if (e.Response != null)
496
using (Stream stream = e.Response.GetResponseStream())
450
Stream stream = e.Response.GetResponseStream();
451
FileStream errorfile = new FileStream("error.log", FileMode.Append);
452
string tdate = DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss");
453
buffer = Encoding.UTF8.GetBytes(tdate + " Solr error:" + Environment.NewLine);
454
errorfile.Write(buffer, 0, buffer.Length);
456
buffer = new byte[1024];
458
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
498
using (FileStream errorfile = new FileStream("error.log", FileMode.Append))
500
string tdate = DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss");
501
buffer = Encoding.UTF8.GetBytes(tdate + " " + destination + " error:" + Environment.NewLine);
502
errorfile.Write(buffer, 0, buffer.Length);
504
buffer = new byte[1024];
506
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
508
errorfile.Write(buffer, 0, bytesRead);
460
errorfile.Write(buffer, 0, bytesRead);
515
467
e.Response.Close();
521
/// Main function for catch unhandled exceptions. Write informations about exception to error.log.
523
/// <param name="sender">
524
/// A <see cref="System.Object"/> unused.
526
/// <param name="args">
527
/// A <see cref="UnhandledExceptionEventArgs"/> provide informations about error (source, stacktrace,
528
/// general info about error)
530
static void MainUnhandledException(object sender, UnhandledExceptionEventArgs args)
536
Exception e = (Exception)args.ExceptionObject;
537
string tdate = DateTime.Now.ToString("yyyy/MM/dd/HH:mm:ss");
538
using (FileStream errorlog = File.Open("error.log", FileMode.Append))
540
using (StreamWriter logstream = new StreamWriter(errorlog))
542
logstream.WriteLine(tdate + " Caught: " + e.GetType().ToString() + " " + e.Message);
543
logstream.WriteLine(" Source: " + e.Source);
544
logstream.WriteLine(" StackTrace: " + e.StackTrace);
545
logstream.WriteLine(" TargetSite: " + e.TargetSite);