/// <summary>
 /// Write Cache
 /// </summary>
 /// <param name="wfi"><see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo input</param>
 /// <param name="cacheid">string Cache ID</param>
 /// <returns><see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo</returns>
 internal void _WikiFileCacheWrite(WikiFolderInfo wfi, string cacheid)
 {
     try
     {
         string cachePath = _WikiFileCacheFilePath(cacheid);
         if (!Directory.Exists(Path.GetDirectoryName(cachePath)))
         {
             Directory.CreateDirectory(Path.GetDirectoryName(cachePath));
         }
         if (this._lockCache.TryEnterWriteLock(waitWriteFsOnProcess))
         {
             try
             {
                 BinaryFormatter formatter = new BinaryFormatter();
                 using (FileStream fs = new FileStream(cachePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                 {
                     formatter.Serialize(fs, wfi);
                     fs.Close();
                 }
             }
             finally
             {
                 this._lockCache.ExitWriteLock();
             }
         }
     }
     catch (Exception e)
     {
         this.Fire_ProcessError(new WikiErrorEventArgs(e));
     }
 }
        /// <summary>
        /// internal find Namespace folder, iteration recursive
        /// </summary>
        /// <param name="wfo">ref <see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo, out data</param>
        /// <param name="wfi"><see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo, input data</param>
        /// <param name="key">Start key to search from Dictionary, its directory name</param>
        /// <returns>Bolean</returns>
        internal bool __WikiFilesFindNamespaceFiles(ref WikiFolderInfo wfo, WikiFolderInfo wfi, string key)
        {
            bool isUpdate = false;

            if (wfi == null)
            {
                return(isUpdate);
            }
            if (wfi.Files.Count > 0)
            {
                bool           isStrongSearch = wfo.SearchStrong;
                string         search         = wfo.SearchPatern;
                WikiFolderInfo nwfo           = new WikiFolderInfo();
                nwfo.NameSpace = wfi.NameSpace;

                wfi.Files.ForEach(o =>
                {
                    if (
                        ((isStrongSearch) && (o.FileName.Equals(search))) ||
                        ((!isStrongSearch) && (o.FileName.Contains(search)))
                        )
                    {
                        WikiFileInfo nwfi = (WikiFileInfo)o.Clone();
                        nwfi.NameSpace    = nwfo.NameSpace;
                        nwfo.Files.Add(nwfi);
                        isUpdate = true;
                    }
                });
                if (isUpdate)
                {
                    wfo.Dirs.Add(key, nwfo);
                    if (isStrongSearch)
                    {
                        return(isUpdate);
                    }
                }
            }
            if (wfi.Dirs.Count > 0)
            {
                foreach (KeyValuePair <string, WikiFolderInfo> item in wfi.Dirs)
                {
                    bool ret = __WikiFilesFindNamespaceFiles(ref wfo, ((WikiFolderInfo)item.Value), ((string)item.Key));
                    isUpdate = ((isUpdate) ? isUpdate : ret);
                }
            }
            return(isUpdate);
        }
 /// <summary>
 /// internal find Namespace folder, iteration recursive
 /// </summary>
 /// <param name="wfi">WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo input</param>
 /// <param name="search">serch patern string</param>
 /// <returns><see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo</returns>
 internal WikiFolderInfo __WikiFilesFindNamespaceFolder(WikiFolderInfo wfi, string search)
 {
     if (
         (wfi == null) ||
         (wfi.Dirs.Count == 0) ||
         (string.IsNullOrWhiteSpace(search))
         )
     {
         return(null);
     }
     return(wfi.Dirs.Where(o => o.Key.Equals(search))
            .Select(o =>
     {
         WikiFolderInfo wfo = (WikiFolderInfo)((WikiFolderInfo)o.Value).Clone();
         return wfo;
     })
            .FirstOrDefault <WikiFolderInfo>());
 }
 internal void _CreateFilesList()
 {
     try
     {
         if (this._lockFs.TryEnterWriteLock((this._isOnstart) ? waitWriteFsOnStart : waitWriteFsOnProcess))
         {
             try
             {
                 DateTime dtStart = DateTime.Now;
                 this._wikiFSDict.Clear();
                 this._rootTree.ForEach(r =>
                 {
                     WikiFolderInfo wfi = new WikiFolderInfo();
                     WikiFileType type  = DokuUtil.WikiFileStringToType(WikiRequestType.None, r);
                     wfi.Files          = this._WikiFileCreateFilesList(Path.Combine(this._rootPath, r), type);
                     wfi.Dirs           = this._WikiFileCreateDictFolders(Path.Combine(this._rootPath, r), type);
                     this._wikiFSDict.Add(r, wfi);
                 });
                 this._isFsModify         = true;
                 this.waitReadFsOnProcess = ((DateTime.Now.Millisecond - dtStart.Millisecond) + 1000);
                 this.Fire_WikiFSChange(new WikiFSChangeEventArgs(this, this._wikiFSDict.Count, (this.waitReadFsOnProcess - 1000)));
             }
             finally
             {
                 try
                 {
                     this._lockFs.ExitWriteLock();
                 }
                 catch (LockRecursionException e)
                 {
                     this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                 }
                 catch (SynchronizationLockException e)
                 {
                     this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                 }
             }
         }
     }
     catch (Exception e)
     {
         this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
     }
 }
        /// <summary>
        /// ToString method from <see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo to MarkDown
        /// Page/Media/Attic List
        /// </summary>
        /// <param name="wfi"><see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo input</param>
        /// <param name="sb">StringBuilder instance or null</param>
        /// <param name="type"><see cref="WikiEngine.WikiFileType"/>WikiEngine.WikiFileType, if this equals WikiFileType.FileReadAttic, return only date from attic</param>
        /// <returns>String or String.Empty</returns>
        public string ResourceListToMdString(WikiFolderInfo wfi, StringBuilder sb = null, WikiFileType type = WikiFileType.None)
        {
            Action         act;
            WikiFileAction wfa = new WikiFileAction(sb);

            wfa.IsAllFiles = true;

            switch (type)
            {
            // Attic date and time list to Md string
            case WikiFileType.FileReadAttic:
            case WikiFileType.FileWriteAttic:
            {
                act = (Action)(() => this._WikiFileActionRecursive(wfi, wfa, this._ActionAtticDateList));
                break;
            }

            default:
            {
                act = (Action)(() => this._WikiFileActionRecursive(wfi, wfa, this._ActionResourceList));
                break;
            }
            }
            string sOut = this._ToMd(
                (object)new Object(),
                act,
                wfa.IsSBuilder,
                wfa.SBuilder
                );

            return(string.Format(
                       Properties.ResourceWikiEngine.ListFormatPageStatistic,
                       sOut,
                       ((string.IsNullOrWhiteSpace(wfi.SearchPatern)) ? wfi.NameSpace : wfi.SearchPatern),
                       wfa.ConutDirs,
                       wfa.ConutFiles,
                       Environment.NewLine
                       ));
        }
        internal Dictionary <string, WikiFolderInfo> _WikiFileCreateDictFolders(string dir, WikiFileType type)
        {
            Dictionary <string, WikiFolderInfo> foldrDic = new Dictionary <string, WikiFolderInfo>();

            foreach (string d in Directory.GetDirectories(dir))
            {
                WikiFolderInfo wfi = new WikiFolderInfo();
                wfi.Files = _WikiFileCreateFilesList(d, type);
                wfi.Dirs  = _WikiFileCreateDictFolders(d, type);
                d.Substring(this._rootPath.Length, (d.Length - this._rootPath.Length))
                .Split(new string[] { WikiFile.wikiDefaultSeparator }, StringSplitOptions.RemoveEmptyEntries)
                .ToArray <string>()
                .Skip(1)
                .ToList <string>()
                .ForEach(o =>
                {
                    wfi.NameSpace = String.Concat(wfi.NameSpace, o, ":");
                });

                foldrDic.Add(Path.GetFileName(d), wfi);
            }
            return(foldrDic);
        }
        /// <summary>
        /// Recursive Action, use in:
        /// <see cref="_SetFileLock(WikiEngine.WikiData, bool)"/>
        /// </summary>
        /// <param name="wfi"><see cref="WikiEngine.WikiFolderInfo"/></param>
        /// <param name="wfa"><see cref="WikiEngine.WikiFileAction"/></param>
        /// <param name="act">Action&lt;WikiEngine.WikiFileAction&gt;</param>
        /// <returns>Bolean</returns>
        internal bool _WikiFileActionRecursive(
            WikiFolderInfo wfi,
            WikiFileAction wfa,
            Action <WikiFileAction> act
            )
        {
            bool isUpdate = false;

            if (
                (act == null) ||
                (wfa == null) ||
                (wfi == null)
                )
            {
                return(false);
            }
            if (wfi.Files.Count > 0)
            {
                wfa.ConutFile   = 0;
                wfa.NameSpace   = wfi.NameSpace;
                wfa.ConutFiles += wfi.Files.Count;

                wfi.Files.ForEach(o =>
                {
                    if (
                        (wfa.IsAllFiles) ||
                        (o.FileName.Equals(wfa.Search))
                        )
                    {
                        wfa.FileInfo = o;
                        wfa.ConutFile++;
                        act(wfa);
                        isUpdate = true;
                        if (!wfa.IsAllFiles)
                        {
                            return;
                        }
                    }
                });
                if (
                    (!wfa.IsAllFiles) &&
                    (isUpdate)
                    )
                {
                    return(isUpdate);
                }
            }
            if (wfi.Dirs.Count > 0)
            {
                wfa.ConutDirs += wfi.Dirs.Count;

                foreach (KeyValuePair <string, WikiFolderInfo> item in wfi.Dirs)
                {
                    wfa.Key = ((string)item.Key);

                    bool ret = _WikiFileActionRecursive(
                        ((WikiFolderInfo)item.Value),
                        wfa,
                        act
                        );
                    isUpdate = ((isUpdate) ? isUpdate : ret);
                }
            }
            return(isUpdate);
        }
        /// <summary>
        /// internal find Namespace
        /// </summary>
        /// <param name="wfp"><see cref="WikiFile.WikiFileParse"/>WikiFile.WikiFileParse data</param>
        /// <returns><see cref="WikiEngine.WikiFolderInfo"/>WikiEngine.WikiFolderInfo</returns>
        internal WikiFolderInfo __WikiFilesFindNamespace(WikiFileParse wfp)
        {
            WikiFolderInfo wfi = null;

            try
            {
                if (this._taskFilesList.Status == TaskStatus.Running)
                {
                    this._taskFilesList.Wait();
                    if (this._taskFilesList.Exception != null)
                    {
                        throw this._taskFilesList.Exception;
                    }
                }
                if (this._lockFs.TryEnterReadLock((this._isOnstart) ? waitReadFsOnStart : waitReadFsOnProcess))
                {
                    try
                    {
                        if (
                            (this._wikiFSDict == null) ||
                            (this._wikiFSDict.Count == 0)
                            )
                        {
                            throw new WikiEngineInternalSearchErrorException("FS base is empty");
                        }
                        if ((wfi =
                                 this._wikiFSDict.Where(o => o.Key.Equals(DokuUtil.WikiFileTypeToString(wfp.FolderType)))
                                 .Select(o =>
                        {
                            return((WikiFolderInfo)o.Value);
                        })
                                 .FirstOrDefault <WikiFolderInfo>()) == null)
                        {
                            throw new WikiEngineInternalSearchErrorException();
                        }
                        if (
                            (wfp.UriPart == null) ||
                            (wfp.UriPart.Count == 0)
                            )
                        {
                            throw new WikiEngineInternalSearchOkException();
                        }

                        int i = 0, count = ((wfp.IsNameSpaceOnly) ? wfp.UriPart.Count : (wfp.UriPart.Count - 1));

                        wfp.UriPart.ForEach(fpart =>
                        {
                            if (i == count)
                            {
                                return;
                            }
                            if ((wfi = this.__WikiFilesFindNamespaceFolder(wfi, fpart)) == null)
                            {
                                return;
                            }
                            i++;
                        });
                        if (wfi != null)
                        {
                            wfi.SearchPatern = ((wfp.IsNameSpaceOnly) ? String.Empty : wfp.UriPart[count]);
                            throw new WikiEngineInternalSearchOkException();
                        }
                        else
                        {
                            throw new WikiEngineInternalSearchErrorException();
                        }
                    }
                    finally
                    {
                        try
                        {
                            this._lockFs.ExitReadLock();
                        }
                        catch (LockRecursionException e)
                        {
                            this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                        }
                        catch (SynchronizationLockException e)
                        {
                            this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                        }
                    }
                }
                throw new WikiEngineInternalSearchErrorException();
            }
            catch (WikiEngineInternalSearchOkException)
            {
                return(wfi);
            }
            catch (WikiEngineInternalSearchErrorException)
            {
                return(null);
            }
            catch (Exception e)
            {
                this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                return(null);
            }
        }
        /// <summary>
        /// internal Wiki find files
        /// </summary>
        /// <param name="wfp"><see cref="WikiFile.WikiFileParse"/>WikiFile.WikiFileParse data</param>
        /// <returns>WikiFolderInfo data</returns>
        internal WikiFolderInfo __WikiFilesFindFiles(WikiFileParse wfp)
        {
            WikiFolderInfo wfi = null, wfo = null;
            string         key = String.Empty;

            try
            {
                if (this._taskFilesList.Status == TaskStatus.Running)
                {
                    this._taskFilesList.Wait();
                    if (this._taskFilesList.Exception != null)
                    {
                        throw new WikiEngineInternalSearchErrorException(this._taskFilesList.Exception.Message);
                    }
                }
                if (this._lockFs.TryEnterReadLock((this._isOnstart) ? waitReadFsOnStart : waitReadFsOnProcess))
                {
                    try
                    {
                        if (
                            (this._wikiFSDict == null) ||
                            (this._wikiFSDict.Count == 0)
                            )
                        {
                            throw new WikiEngineInternalSearchErrorException("FS base is empty");
                        }
                        key = DokuUtil.WikiFileTypeToString(wfp.FolderType);
                        if ((wfi =
                                 this._wikiFSDict.Where(o => o.Key.Equals(key))
                                 .Select(o =>
                        {
                            return((WikiFolderInfo)o.Value);
                        })
                                 .FirstOrDefault <WikiFolderInfo>()) == null)
                        {
                            throw new WikiEngineInternalSearchEmptyException();
                        }
                        if (
                            (wfp.UriPart != null) &&
                            (wfp.UriPart.Count > 0)
                            )
                        {
                            wfp.UriPart.ForEach(fpart =>
                            {
                                if ((wfi = this.__WikiFilesFindNamespaceFolder(wfi, fpart)) == null)
                                {
                                    return;
                                }
                                key = fpart;
                            });
                        }
                        if (wfi == null)
                        {
                            throw new WikiEngineInternalSearchEmptyException();
                        }

                        wfo = new WikiFolderInfo();
                        wfo.SearchPatern = wfp.SearchPatern;
                        wfo.SearchStrong = wfp.IsStrongSearch;

                        if (this.__WikiFilesFindNamespaceFiles(ref wfo, wfi, key))
                        {
                            throw new WikiEngineInternalSearchOkException();
                        }
                        throw new WikiEngineInternalSearchEmptyException();
                    }
                    finally
                    {
                        try
                        {
                            this._lockFs.ExitReadLock();
                        }
                        catch (LockRecursionException e)
                        {
                            this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                        }
                        catch (SynchronizationLockException e)
                        {
                            this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                        }
                    }
                }
                throw new WikiEngineInternalSearchEmptyException();
            }
            catch (WikiEngineInternalSearchOkException)
            {
                // Cache write result
                if (this._isCacheEnable)
                {
                    _WikiFileCacheWrite(wfo, wfp.CacheId);
                }
                //
                return(wfo);
            }
            catch (WikiEngineInternalSearchEmptyException)
            {
                return(null);
            }
            catch (WikiEngineInternalSearchErrorException e)
            {
                this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                return(null);
            }
            catch (Exception e)
            {
                this.Fire_ProcessError(new WikiErrorEventArgs(e, MethodBase.GetCurrentMethod().Name));
                return(null);
            }
        }
        /// <summary>
        /// part from _WikiFileGetFileInfo(WikiFileParse) internal method
        /// </summary>
        /// <param name="wfp">internal class <see cref="WikiEngine.WikiFile.WikiFileParse"/>WikiEngine.WikiFile.WikiFileParse</param>
        /// <returns><see cref="WikiEngine.WikiFileInfo"/>WikiEngine.WikiFileInfo</returns>
        internal WikiFileInfo __WikiFileGetFileInfoSelect(WikiFileParse wfp)
        {
            WikiFileInfo   wfo = null;
            WikiFolderInfo wfi = null;

            try
            {
                if (wfp.FolderInfo != null)
                {
                    wfi = wfp.FolderInfo;
                }
                else
                {
                    if ((wfi = this.__WikiFilesFindFiles(wfp)) == null)
                    {
                        throw new WikiEngineInternalSearchEmptyException(
                                  string.Format(
                                      Properties.ResourceWikiEngine.mdErrorSearchEmpty,
                                      wfp.NameSpacePatern,
                                      wfp.SearchPatern,
                                      wfp.FolderType
                                      )
                                  );
                    }
                }
                if (wfi.Dirs.Count != 1)
                {
                    string exceptMessage = string.Format(
                        "Directory count: {0} '{1}{2}'",
                        wfi.Dirs.Count,
                        wfp.NameSpacePatern,
                        wfp.SearchPatern
                        );
                    if (wfi.Dirs.Count == 0)
                    {
                        throw new WikiEngineInternalSearchEmptyException(exceptMessage);
                    }
                    throw new WikiEngineInternalSearchManyResultException(exceptMessage);
                }
                if ((wfo = wfi.Dirs
                           .ElementAtOrDefault <KeyValuePair <string, WikiFolderInfo> >(0)
                           .Value
                           .Files
                           .FirstOrDefault <WikiFileInfo>()) == null)
                {
                    throw new WikiEngineInternalSearchEmptyException(
                              string.Format(
                                  Properties.ResourceWikiEngine.mdErrorSearchEmpty,
                                  wfp.NameSpacePatern,
                                  wfp.SearchPatern,
                                  wfp.FolderType
                                  )
                              );
                }
                return(wfo);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Beispiel #11
0
        /// <summary>
        /// find pages list
        /// </summary>
        private byte[] _WikiFindList(WikiFileType wft, string namesspace)
        {
            WikiFileParse  wfp = null;
            WikiFolderInfo wfi = null;

            try
            {
                if ((wfp = (WikiFileParse)this._WikiFilesParse(
                         wft, namesspace, null, null, false
                         )) == null)
                {
                    throw new WikiEngineInternalNameSpaceErrorException(
                              string.Format(
                                  Properties.ResourceWikiEngine.fmtErrorMapTree,
                                  MethodBase.GetCurrentMethod().Name,
                                  namesspace,
                                  wft.ToString()
                                  )
                              );
                }
                switch (wfp.FolderType)
                {
                case WikiFileType.FileReadMd:
                case WikiFileType.FileWriteMd:
                case WikiFileType.FileReadBinary:
                case WikiFileType.FileWriteBinary:
                case WikiFileType.FileReadAttic:
                case WikiFileType.FileWriteAttic:
                {
                    if ((wfi = (WikiFolderInfo)this._FindFiles(wfp)) == null)
                    {
                        throw new WikiEngineInternalSearchEmptyException(
                                  string.Format(
                                      Properties.ResourceWikiEngine.fmtErrorMapTree,
                                      MethodBase.GetCurrentMethod().Name,
                                      namesspace,
                                      wfp.FolderType.ToString()
                                      )
                                  );
                    }
                    break;
                }

                default:
                {
                    throw new WikiEngineNotImplementPageException(
                              string.Format(
                                  Properties.ResourceWikiEngine.fmtErrorMapTree,
                                  MethodBase.GetCurrentMethod().Name,
                                  namesspace,
                                  wfp.FolderType.ToString()
                                  )
                              );
                }
                }
                return(Encoding.UTF8.GetBytes(
                           this.ResourceListToMdString(wfi, null, wfp.FolderType)
                           ));
            }
            catch (Exception e)
            {
                throw e;
            }
        }