Ejemplo n.º 1
0
        private void SwitchToLanguage(string language)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            path += @"\WikiLive\" + language;
            Directory.CreateDirectory(path);

            _baseName = path + @"\WikiLive.db";
            if (!File.Exists(_baseName))
            {
                SQLiteConnection.CreateFile(_baseName);
            }

            if (_connection != null && _connection.State != ConnectionState.Closed)
            {
                _connection.Close();
            }

            SQLiteFactory factory = (SQLiteFactory)DbProviderFactories.GetFactory("System.Data.SQLite");

            _connection = (SQLiteConnection)factory.CreateConnection();
            _connection.ConnectionString = "Data Source = " + _baseName;
            _connection.Open();

            using (SQLiteCommand command = new SQLiteCommand(_connection))
            {
                command.CommandText = @"CREATE TABLE IF NOT EXISTS [edits] (
                        [id] integer PRIMARY KEY NOT NULL,
                        [timestamp] TEXT NOT NULL,
                        [user] TEXT NOT NULL,
                        [page] TEXT NOT NULL,
                        [namespace] INTEGER NOT NULL,
                        [flags] INTEGER NOT NULL,
                        [oldid] INTEGER NOT NULL,
                        [size] INTEGER NOT NULL,
                        [summary] TEXT NOT NULL
                    );";
                command.CommandType = CommandType.Text;
                command.ExecuteNonQuery();
            }

            using (SQLiteCommand command = new SQLiteCommand(_connection))
            {
                command.CommandText = @"CREATE TABLE IF NOT EXISTS [watched_pages] (
                        [id] integer PRIMARY KEY AUTOINCREMENT NOT NULL,
                        [page] TEXT NOT NULL,
                        [namespace] INTEGER NOT NULL
                    );";
                command.CommandType = CommandType.Text;
                command.ExecuteNonQuery();
            }

            if (!File.Exists(path + @"\namespaces.dat"))
            {
                Wiki wiki = new Wiki("http://" + language + ".wikipedia.org");
                ParameterCollection parameters = new ParameterCollection();
                parameters.Add("meta", "siteinfo");
                parameters.Add("siprop", "namespaces");
                XmlDocument xml        = wiki.Enumerate(parameters, true);
                XmlNodeList nodes      = xml.SelectNodes("//ns[@id > 0]");
                Serializer  serializer = new Serializer();
                serializer.Put(nodes.Count);
                foreach (XmlNode node in nodes)
                {
                    serializer.Put(node.Attributes["id"].Value);
                    serializer.Put(node.FirstChild.Value);
                }

                using (FileStream fs = new FileStream(path + @"\namespaces.dat", FileMode.CreateNew))
                    using (BinaryWriter streamWriter = new BinaryWriter(fs))
                    {
                        streamWriter.Write(serializer.ToArray());
                    }
            }
        }