/// <summary>
        /// Deletes the outgoing links of a page and all the target links that include the page.
        /// </summary>
        /// <param name="page">The full name of the page.</param>
        /// <returns><c>true</c> if the links are deleted, <c>false</c> otherwise.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="page"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If <paramref name="page"/> is empty.</exception>
        public bool DeleteOutgoingLinks(string page)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }
            if (page.Length == 0)
            {
                throw new ArgumentException("page");
            }

            try {
                var query = (from e in _context.CreateQuery <OutgoingLinkEntity>(OutgoingLinksTable).AsTableServiceQuery()
                             where e.PartitionKey.Equals(_wiki) && (e.SourcePage.Equals(page) || e.DestinationPage.Equals(page))
                             select e).AsTableServiceQuery();
                var entities = QueryHelper <OutgoingLinkEntity> .All(query);

                if (entities == null || entities.Count == 0)
                {
                    return(false);
                }
                foreach (var entity in entities)
                {
                    _context.DeleteObject(entity);
                }
                _context.SaveChangesStandard();

                return(true);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// Gets the outgoing links of a page.
        /// </summary>
        /// <param name="page">The full name of the page.</param>
        /// <returns>The outgoing links.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="page"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If <paramref name="page"/> is empty.</exception>
        public string[] GetOutgoingLinks(string page)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }
            if (page.Length == 0)
            {
                throw new ArgumentException("page");
            }

            try {
                var query = (from e in _context.CreateQuery <OutgoingLinkEntity>(OutgoingLinksTable).AsTableServiceQuery()
                             where e.PartitionKey.Equals(_wiki) && e.SourcePage.Equals(page)
                             select e).AsTableServiceQuery();
                var entities = QueryHelper <OutgoingLinkEntity> .All(query);

                List <string> outgoingLinks = new List <string>(entities.Count);
                foreach (var entity in entities)
                {
                    outgoingLinks.Add(entity.DestinationPage);
                }
                return(outgoingLinks.ToArray());
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// Gets all the outgoing links stored.
        /// </summary>
        /// <returns>The outgoing links, in a dictionary in the form page-&gt;outgoing_links.</returns>
        public IDictionary <string, string[]> GetAllOutgoingLinks()
        {
            try {
                var query = (from e in _context.CreateQuery <OutgoingLinkEntity>(OutgoingLinksTable).AsTableServiceQuery()
                             where e.PartitionKey.Equals(_wiki)
                             select e).AsTableServiceQuery();
                IList <OutgoingLinkEntity> outgoingLinksEntities = QueryHelper <OutgoingLinkEntity> .All(query);

                Dictionary <string, List <string> > outgoingLinksWithList = new Dictionary <string, List <string> >(outgoingLinksEntities.Count);
                foreach (OutgoingLinkEntity entity in outgoingLinksEntities)
                {
                    if (!outgoingLinksWithList.ContainsKey(entity.SourcePage))
                    {
                        outgoingLinksWithList[entity.SourcePage] = new List <string>();
                    }
                    outgoingLinksWithList[entity.SourcePage].Add(entity.DestinationPage);
                }
                Dictionary <string, string[]> outgoingLinks = new Dictionary <string, string[]>(outgoingLinksWithList.Count);
                foreach (var outgoingLink in outgoingLinksWithList)
                {
                    outgoingLinks.Add(outgoingLink.Key, outgoingLink.Value.ToArray());
                }
                return(outgoingLinks);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// Renames a ACL resource.
        /// </summary>
        /// <param name="resource">The resource to rename.</param>
        /// <param name="newName">The new name of the resource.</param>
        /// <returns><c>true</c> if one or more entries weere updated, <c>false</c> otherwise.</returns>
        private bool RenameAclResource(string resource, string newName)
        {
            try {
                var query = (from e in _context.CreateQuery <AclEntriesEntity>(AclEntriesTable).AsTableServiceQuery()
                             where e.PartitionKey.Equals(_wiki) && e.Resource.Equals(resource)
                             select e).AsTableServiceQuery();
                IList <AclEntriesEntity> aclEntriesEntities = QueryHelper <AclEntriesEntity> .All(query);

                if (aclEntriesEntities == null || aclEntriesEntities.Count == 0)
                {
                    return(false);
                }

                foreach (AclEntriesEntity entity in aclEntriesEntities)
                {
                    entity.Resource = newName;
                    _context.UpdateObject(entity);
                }
                _context.SaveChangesStandard();
                return(true);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// Updates all outgoing links data for a page rename.
        /// </summary>
        /// <param name="oldName">The old page name.</param>
        /// <param name="newName">The new page name.</param>
        /// <returns><c>true</c> if the data is updated, <c>false</c> otherwise.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="oldName"/> or <paramref name="newName"/> are <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If <paramref name="oldName"/> or <paramref name="newName"/> are empty.</exception>
        public bool UpdateOutgoingLinksForRename(string oldName, string newName)
        {
            if (oldName == null)
            {
                throw new ArgumentNullException("oldName");
            }
            if (newName == null)
            {
                throw new ArgumentNullException("newName");
            }
            if (oldName.Length == 0)
            {
                throw new ArgumentException("oldName");
            }
            if (newName.Length == 0)
            {
                throw new ArgumentException("newName");
            }

            try {
                bool updateExecuted = false;
                var  query          = (from e in _context.CreateQuery <OutgoingLinkEntity>(OutgoingLinksTable).AsTableServiceQuery()
                                       where e.PartitionKey.Equals(_wiki) && e.SourcePage.Equals(oldName)
                                       select e).AsTableServiceQuery();
                var entities = QueryHelper <OutgoingLinkEntity> .All(query);

                if (entities != null && entities.Count > 0)
                {
                    updateExecuted = true;
                    foreach (var entity in entities)
                    {
                        entity.SourcePage = newName;
                        _context.UpdateObject(entity);
                    }
                    _context.SaveChangesStandard();
                }

                query = (from e in _context.CreateQuery <OutgoingLinkEntity>(OutgoingLinksTable).AsTableServiceQuery()
                         where e.PartitionKey.Equals(_wiki) && e.DestinationPage.Equals(oldName)
                         select e).AsTableServiceQuery();
                entities = QueryHelper <OutgoingLinkEntity> .All(query);

                if (entities != null && entities.Count > 0)
                {
                    updateExecuted = true;
                    foreach (var entity in entities)
                    {
                        entity.DestinationPage = newName;
                        _context.UpdateObject(entity);
                    }
                    _context.SaveChangesStandard();
                }

                return(updateExecuted);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        private IList <GlobalSettingsEntity> GetGlobalSettingsEntities()
        {
            var query = (from e in _context.CreateQuery <GlobalSettingsEntity>(GlobalSettingsTable).AsTableServiceQuery()
                         where e.PartitionKey.Equals("0")
                         select e).AsTableServiceQuery();

            return(QueryHelper <GlobalSettingsEntity> .All(query));
        }
        private IList <AclEntriesEntity> GetAclEntriesEntities(string wiki)
        {
            var query = (from e in _context.CreateQuery <AclEntriesEntity>(AclEntriesTable).AsTableServiceQuery()
                         where e.PartitionKey.Equals(wiki)
                         select e).AsTableServiceQuery();

            return(QueryHelper <AclEntriesEntity> .All(query));
        }
Esempio n. 8
0
        private IList <UserDataEntity> GetUserDataEntities(string wiki, string username)
        {
            var userGroupsQuery = (from e in _context.CreateQuery <UserDataEntity>(UserDataTable).AsTableServiceQuery()
                                   where e.PartitionKey.Equals(wiki + "|" + username)
                                   select e).AsTableServiceQuery();

            return(QueryHelper <UserDataEntity> .All(userGroupsQuery));
        }
        /// <summary>
        /// Stores the outgoing links of a page, overwriting existing data.
        /// </summary>
        /// <param name="page">The full name of the page.</param>
        /// <param name="outgoingLinks">The full names of the pages that <b>page</b> links to.</param>
        /// <returns><c>true</c> if the outgoing links are stored, <c>false</c> otherwise.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="page"/> or <paramref name="outgoingLinks"/> are <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If <paramref name="page"/> or <paramref name="outgoingLinks"/> are empty.</exception>
        public bool StoreOutgoingLinks(string page, string[] outgoingLinks)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }
            if (outgoingLinks == null)
            {
                throw new ArgumentNullException("outgoingLinks");
            }
            if (page.Length == 0)
            {
                throw new ArgumentException("page");
            }

            try {
                var query = (from e in _context.CreateQuery <OutgoingLinkEntity>(OutgoingLinksTable).AsTableServiceQuery()
                             where e.PartitionKey.Equals(_wiki) && e.SourcePage.Equals(page)
                             select e).AsTableServiceQuery();
                var entities = QueryHelper <OutgoingLinkEntity> .All(query);

                foreach (var entity in entities)
                {
                    _context.DeleteObject(entity);
                }
                _context.SaveChangesStandard();

                foreach (string outgoingLink in outgoingLinks)
                {
                    if (outgoingLink == null)
                    {
                        throw new ArgumentNullException("outgoingLinks", "Null element in outgoing links array");
                    }
                    if (outgoingLink.Length == 0)
                    {
                        throw new ArgumentException("Elements in outgoing links cannot be empty", "outgoingLinks");
                    }

                    OutgoingLinkEntity outgoingLinksEntity = new OutgoingLinkEntity()
                    {
                        PartitionKey    = _wiki,
                        RowKey          = Guid.NewGuid().ToString("N"),
                        SourcePage      = page,
                        DestinationPage = outgoingLink
                    };
                    _context.AddObject(OutgoingLinksTable, outgoingLinksEntity);
                }
                _context.SaveChangesStandard();
                return(true);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// Gets all the Log Entries, sorted by date/time (oldest to newest).
        /// </summary>
        /// <returns>The Log Entries.</returns>
        public LogEntry[] GetLogEntries()
        {
            var query = (from e in _context.CreateQuery <LogEntity>(LogsTable).AsTableServiceQuery()
                         where e.PartitionKey.Equals("0")
                         select e).Take((int)(int.Parse(_host.GetGlobalSettingValue(GlobalSettingName.MaxLogSize)) * 9)).AsTableServiceQuery();
            IList <LogEntity> logEntities = QueryHelper <LogEntity> .All(query);

            List <LogEntry> logEntries = new List <LogEntry>();

            foreach (LogEntity entity in logEntities)
            {
                logEntries.Add(new LogEntry(EntryTypeParse(entity.Type), new DateTime(entity.DateTime.Ticks, DateTimeKind.Utc), entity.Message, entity.User, entity.Wiki));
            }
            return(logEntries.ToArray());
        }
        /// <summary>
        /// Gets the recent changes of the Wiki.
        /// </summary>
        /// <returns>The recent Changes, oldest to newest.</returns>
        public RecentChange[] GetRecentChanges()
        {
            try {
                var query = (from e in _context.CreateQuery <RecentChangesEntity>(RecentChangesTable).AsTableServiceQuery()
                             where e.PartitionKey.Equals(_wiki)
                             select e).Take((int)(int.Parse(_host.GetSettingValue(SettingName.MaxRecentChanges)) * 0.90)).AsTableServiceQuery();
                IList <RecentChangesEntity> recentChangesEntities = QueryHelper <RecentChangesEntity> .All(query);

                List <RecentChange> recentChanges = new List <RecentChange>(recentChangesEntities.Count);
                foreach (RecentChangesEntity entity in recentChangesEntities)
                {
                    recentChanges.Add(new RecentChange(entity.Page, entity.Title, entity.MessageSubject + "", new DateTime(entity.DateTime.Ticks, DateTimeKind.Utc), entity.User, GetChange(entity.Change), entity.Description + ""));
                }
                return(recentChanges.ToArray());
            }
            catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// Reduces the size of the Log to the specified size (or less).
        /// </summary>
        /// <param name="size">The size to shrink the log to (in bytes).</param>
        private void CutLog(int size)
        {
            size = size * 1024;
            var query = (from e in _context.CreateQuery <LogEntity>(LogsTable).AsTableServiceQuery()
                         where e.PartitionKey.Equals("0")
                         select e).AsTableServiceQuery();

            LogEntity[] logEntities = QueryHelper <LogEntity> .All(query).ToArray();

            int estimatedSize = logEntities.Length * EstimatedLogEntrySize;

            if (size < estimatedSize)
            {
                int difference      = estimatedSize - size;
                int entriesToDelete = difference / EstimatedLogEntrySize;
                // Add 10% to avoid 1-by-1 deletion when adding new entries
                entriesToDelete += entriesToDelete / 10;

                if (entriesToDelete > 0)
                {
                    int count = 0;
                    for (int i = 0; i < entriesToDelete; i++)
                    {
                        _context.DeleteObject(logEntities[i]);
                        if (count > 98)
                        {
                            _context.SaveChangesStandard();
                            count = 0;
                        }
                        else
                        {
                            count++;
                        }
                    }
                    _context.SaveChangesStandard();
                }
            }
        }
        /// <summary>
        /// Clear the Log.
        /// </summary>
        public void ClearLog()
        {
            var query = (from e in _context.CreateQuery <LogEntity>(LogsTable).AsTableServiceQuery()
                         where e.PartitionKey.Equals("0")
                         select e).AsTableServiceQuery();
            IList <LogEntity> logEntities = QueryHelper <LogEntity> .All(query);

            int count = 0;

            foreach (LogEntity logEntity in logEntities)
            {
                _context.DeleteObject(logEntity);
                if (count > 98)
                {
                    _context.SaveChangesStandard();
                    count = 0;
                }
                else
                {
                    count++;
                }
            }
            _context.SaveChangesStandard();
        }
Esempio n. 14
0
        private IList <UserEntity> GetUsersEntities(string wiki)
        {
            if (!(allUsersRetrieved && _users != null))
            {
                allUsersRetrieved = true;
                var usersQuery = (from e in _context.CreateQuery <UserEntity>(UsersTable).AsTableServiceQuery()
                                  where e.PartitionKey.Equals(wiki)
                                  select e).AsTableServiceQuery();
                var entities = QueryHelper <UserEntity> .All(usersQuery);

                _users = new Dictionary <string, UserEntity>();
                foreach (UserEntity entity in entities)
                {
                    _users[entity.RowKey] = entity;
                }
            }
            IList <UserEntity> usersEntities = new List <UserEntity>();

            foreach (var user in _users.Values)
            {
                usersEntities.Add(user);
            }
            return(usersEntities);
        }