private bool ValidateTitle() { if (textPageTitle.Text == "") { MsgBox.Show(this, "Page title cannot be empty."); return(false); } if (textPageTitle.Text == PageTitle) { //"rename" to the same thing. DialogResult = DialogResult.Cancel; return(false); } string errorMsg = ""; if (!WikiPages.IsWikiPageTitleValid(textPageTitle.Text, out errorMsg)) { MessageBox.Show(errorMsg); //errorMsg was already translated. return(false); } if (PageTitle != null && textPageTitle.Text.ToLower() == PageTitle.ToLower()) { //the user is just trying to change the capitalization, which is allowed return(true); } WikiPage wp = WikiPages.GetByTitle(textPageTitle.Text); //this is case insensitive, so it won't let you name it the same as another page even if capitalization is different. if (wp != null) { MsgBox.Show(this, "Page title already exists."); return(false); } return(true); }
///<summary>Adds a new wikipage.</summary> private void butAdd_Click(object sender, EventArgs e) { FormWikiRename FormWR = new FormWikiRename(); FormWR.ShowDialog(); if (FormWR.DialogResult != DialogResult.OK) { return; } Action <string> onWikiSaved = new Action <string>((pageTitleNew) => { //return the new wikipage added to FormWikiEdit WikiPage wp = WikiPages.GetByTitle(pageTitleNew); if (wp != null && OwnerForm != null && !OwnerForm.IsDisposed) { OwnerForm.RefreshPage(wp); } }); FormWikiEdit FormWE = new FormWikiEdit(onWikiSaved); FormWE.WikiPageCur = new WikiPage(); FormWE.WikiPageCur.IsNew = true; FormWE.WikiPageCur.PageTitle = FormWR.PageTitle; FormWE.WikiPageCur.PageContent = "[[" + OwnerForm.WikiPageCur.PageTitle + "]]\r\n" //link back + "<h1>" + FormWR.PageTitle + "</h1>\r\n"; //page title FormWE.Show(); Close(); }
///<summary>Loads page from history based on historyCurIndex.</summary> private void NavToHistory() { if (historyNavBack < 0 || historyNavBack > historyNav.Count - 1) { //This should never happen. MsgBox.Show(this, "Invalid history index."); return; } string pageName = historyNav[historyNav.Count - (1 + historyNavBack)]; //-1 is the last/current page. if (pageName.StartsWith("wiki:")) { pageName = pageName.Substring(5); WikiPage wpage = WikiPages.GetByTitle(pageName); if (wpage == null) { MessageBox.Show("'" + historyNav[historyNav.Count - (1 + historyNavBack)] + "' page does not exist."); //very rare return; } //historyNavBack--;//no need to decrement since this is only called from Back_Click and Forward_Click and the appropriate adjustment to this index happens there LoadWikiPage(pageName); //because it's a duplicate, it won't add it again to the list. } else if (pageName.StartsWith("http://")) //www //no need to set the text because the Navigating event will fire and take care of that. { webBrowserWiki.Navigate(pageName); } else { //? } }
private void Save_Click() { if (!ValidateWikiPage(true)) { return; } WikiPage wikiPageDB = WikiPages.GetByTitle(WikiPageCur.PageTitle); if (wikiPageDB != null && WikiPageCur.DateTimeSaved < wikiPageDB.DateTimeSaved) { if (!MsgBox.Show(this, MsgBoxButtons.OKCancel, "This page has been modified and saved since it was opened on this computer. Save anyway?")) { return; } } WikiPageCur.PageContent = textContent.Text; //Fix case on all internal links MatchCollection matches = Regex.Matches(WikiPageCur.PageContent, @"\[\[.+?\]\]"); foreach (Match match in matches) { if (match.Value.StartsWith("[[img:") || match.Value.StartsWith("[[keywords:") || match.Value.StartsWith("[[file:") || match.Value.StartsWith("[[folder:") || match.Value.StartsWith("[[list:") || match.Value.StartsWith("[[color:")) { continue; //we don't care about these. We are only checking internal links } //Get the pagename of the link string oldTitle = match.Value.Substring(2, match.Value.Length - 4); string newTitle = WikiPages.GetTitle(oldTitle); if (oldTitle == newTitle) //casing matches { continue; } if (newTitle == "") //broken link, leave alone { continue; } WikiPageCur.PageContent = WikiPageCur.PageContent.Replace("[[" + oldTitle + "]]", "[[" + newTitle + "]]"); } WikiPageCur.UserNum = Security.CurUser.UserNum; Regex regex = new Regex(@"\[\[(keywords:).+?\]\]"); //only grab first match Match m = regex.Match(textContent.Text); WikiPageCur.KeyWords = m.Value.Replace("[[keywords:", "").TrimEnd(']'); //will be empty string if no match WikiPages.InsertAndArchive(WikiPageCur); FormWiki formWiki = (FormWiki)this.OwnerForm; if (formWiki != null && !formWiki.IsDisposed) { formWiki.RefreshPage(WikiPageCur.PageTitle); } closingIsSave = true; Close(); //should be dialog result?? }
private void LoadWikiPage(string WikiPageTitleCur) { webBrowserWiki.AllowNavigation = true; butRestore.Enabled = false; if (checkDeletedOnly.Checked) { webBrowserWiki.DocumentText = WikiPages.TranslateToXhtml(WikiPageHists.GetDeletedByTitle(WikiPageTitleCur).PageContent, true); butRestore.Enabled = true; } else { webBrowserWiki.DocumentText = WikiPages.TranslateToXhtml(WikiPages.GetByTitle(WikiPageTitleCur).PageContent, true); } }
private bool HasExistingWikiPage(WikiPage wikiPage, out WikiPage wpExisting) { wpExisting = null; if (wikiPage == null) { return(false); //This shouldn't happen. } wpExisting = WikiPages.GetByTitle(wikiPage.PageTitle); if (wpExisting == null) { return(false); } return(true); }
///<summary>Before calling this, make sure to increment/decrement the historyNavBack index to keep track of the position in history. If loading a new page, decrement historyNavBack before calling this function. </summary> private void LoadWikiPage(string pageTitle) { //This is called from 11 different places, any time the program needs to refresh a page from the db. //It's also called from the browser_Navigating event when a "wiki:" link is clicked. WikiPage wpage = WikiPages.GetByTitle(pageTitle); if (wpage == null) { if (!MsgBox.Show(this, MsgBoxButtons.YesNo, "That page does not exist. Would you like to create it?")) { return; } FormWikiEdit FormWE = new FormWikiEdit(); FormWE.WikiPageCur = new WikiPage(); FormWE.WikiPageCur.IsNew = true; FormWE.WikiPageCur.PageTitle = pageTitle; FormWE.WikiPageCur.PageContent = "[[" + WikiPageCur.PageTitle + "]]\r\n" //link back + "<h1>" + pageTitle + "</h1>\r\n"; //page title FormWE.OwnerForm = this; FormWE.Show(); return; } WikiPageCur = wpage; webBrowserWiki.DocumentText = WikiPages.TranslateToXhtml(WikiPageCur.PageContent, false); Text = "Wiki - " + WikiPageCur.PageTitle; #region historyMaint //This region is duplicated in webBrowserWiki_Navigating() for external links. Modifications here will need to be reflected there. int indexInHistory = historyNav.Count - (1 + historyNavBack); //historyNavBack number of pages before the last page in history. This is the index of the page we are loading. if (historyNav.Count == 0) //empty history { historyNavBack = 0; historyNav.Add("wiki:" + pageTitle); } else if (historyNavBack < 0) //historyNavBack could be negative here. This means before the action that caused this load, we were not navigating through history, simply set back to 0 and add to historyNav[] if necessary. { historyNavBack = 0; if (historyNav[historyNav.Count - 1] != "wiki:" + pageTitle) { historyNav.Add("wiki:" + pageTitle); } } else if (historyNavBack >= 0 && historyNav[indexInHistory] != "wiki:" + pageTitle) //branching from page in history { historyNav.RemoveRange(indexInHistory, historyNavBack + 1); //remove "forward" history. branching off in a new direction historyNavBack = 0; historyNav.Add("wiki:" + pageTitle); } #endregion }
private void butRestore_Click(object sender, EventArgs e) { if (gridMain.GetSelectedIndex() == -1) { return; //should never happen. } wikiPageTitleSelected = listWikiPageTitles[gridMain.SelectedIndices[0]]; if (WikiPages.GetByTitle(wikiPageTitleSelected) != null) { MsgBox.Show(this, "Selected page has already been restored."); //should never happen. return; } WikiPage wikiPageRestored = WikiPageHists.RevertFrom(WikiPageHists.GetDeletedByTitle(listWikiPageTitles[gridMain.SelectedIndices[0]])); wikiPageRestored.UserNum = Security.CurUser.UserNum; WikiPages.InsertAndArchive(wikiPageRestored); DialogResult = DialogResult.OK; }
private bool ValidateTitle() { if (textPageTitle.Text == "") { MsgBox.Show(this, "Page title cannot be empty."); return(false); } if (textPageTitle.Text == PageTitle) { //"rename" to the same thing. DialogResult = DialogResult.Cancel; return(false); } if (textPageTitle.Text.StartsWith("_")) { MsgBox.Show(this, "Page title cannot start with the underscore character."); return(false); } if (textPageTitle.Text.Contains("\"")) { MsgBox.Show(this, "Page title cannot contain double quotes."); return(false); } if (textPageTitle.Text.Contains("\r") || textPageTitle.Text.Contains("\n")) { MsgBox.Show(this, "Page title cannot contain carriage return."); //there is also no way to enter one. return(false); } if (PageTitle != null && textPageTitle.Text.ToLower() == PageTitle.ToLower()) { //the user is just trying to change the capitalization, which is allowed return(true); } WikiPage wp = WikiPages.GetByTitle(textPageTitle.Text); //this is case insensitive, so it won't let you name it the same as another page even if capitalization is different. if (wp != null) { MsgBox.Show(this, "Page title already exists."); return(false); } return(true); }
private void LoadWikiPage(string WikiPageTitleCur) { webBrowserWiki.AllowNavigation = true; butRestore.Enabled = false; try { if (checkArchivedOnly.Checked) { webBrowserWiki.DocumentText = WikiPages.TranslateToXhtml(WikiPages.GetByTitle(WikiPageTitleCur, isDeleted: true).PageContent, true); butRestore.Enabled = true; } else { webBrowserWiki.DocumentText = WikiPages.TranslateToXhtml(WikiPages.GetByTitle(WikiPageTitleCur).PageContent, true); } } catch (Exception ex) { webBrowserWiki.DocumentText = ""; MessageBox.Show(this, Lan.g(this, "This page is broken and cannot be viewed. Error message:") + " " + ex.Message); } }
/// <summary></summary> private void FillGrid() { gridMain.BeginUpdate(); gridMain.Columns.Clear(); ODGridColumn col = new ODGridColumn(Lan.g(this, "User"), 70); gridMain.Columns.Add(col); col = new ODGridColumn(Lan.g(this, "Del"), 25); gridMain.Columns.Add(col); col = new ODGridColumn(Lan.g(this, "Saved"), 80); gridMain.Columns.Add(col); gridMain.Rows.Clear(); List <WikiPageHist> listWikiPageHists = WikiPageHists.GetByTitleNoPageContent(PageTitleCur); WikiPage wp = WikiPages.GetByTitle(PageTitleCur); if (wp != null) { listWikiPageHists.Add(WikiPages.PageToHist(wp)); } Dictionary <long, string> dictUsers = Userods.GetUsers(listWikiPageHists.Select(x => x.UserNum).Distinct().ToList()) //gets from cache, very fast .ToDictionary(x => x.UserNum, x => x.UserName); //create dictionary of key=UserNum, value=UserName for fast lookup foreach (WikiPageHist wPage in listWikiPageHists) { ODGridRow row = new ODGridRow(); string userName; if (!dictUsers.TryGetValue(wPage.UserNum, out userName)) { userName = ""; } row.Cells.Add(userName); row.Cells.Add((wPage.IsDeleted?"X":"")); row.Cells.Add(wPage.DateTimeSaved.ToString()); row.Tag = wPage; gridMain.Rows.Add(row); } gridMain.EndUpdate(); gridMain.SetSelected(gridMain.Rows.Count - 1, true); //There will always be at least one page in the history (the current revision of the page) gridMain.ScrollToEnd(); //in case there are LOTS of revisions }
private void butRestore_Click(object sender, EventArgs e) { if (gridMain.GetSelectedIndex() == -1) { return; //should never happen. } wikiPageTitleSelected = listWikiPageTitles[gridMain.SelectedIndices[0]]; if (WikiPages.GetByTitle(wikiPageTitleSelected) != null) { MsgBox.Show(this, "Selected page has already been restored."); //should never happen. return; } WikiPage wikiPageRestored = WikiPages.GetByTitle(listWikiPageTitles[gridMain.SelectedIndices[0]], isDeleted: true); if (wikiPageRestored == null) { MsgBox.Show(this, "Selected page has already been restored."); //should never happen. return; } WikiPages.WikiPageRestore(wikiPageRestored, Security.CurUser.UserNum); Close(); }
/// <summary></summary> private void FillGrid() { gridMain.BeginUpdate(); gridMain.Columns.Clear(); ODGridColumn col = new ODGridColumn(Lan.g(this, "User"), 70); gridMain.Columns.Add(col); col = new ODGridColumn(Lan.g(this, "Del"), 25); gridMain.Columns.Add(col); col = new ODGridColumn(Lan.g(this, "Saved"), 80); gridMain.Columns.Add(col); gridMain.Rows.Clear(); ListWikiPageHists = WikiPageHists.GetByTitle(PageTitleCur); ListWikiPageHists.Add(WikiPages.PageToHist(WikiPages.GetByTitle(PageTitleCur))); for (int i = 0; i < ListWikiPageHists.Count; i++) { ODGridRow row = new ODGridRow(); row.Cells.Add(Userods.GetName(ListWikiPageHists[i].UserNum)); row.Cells.Add((ListWikiPageHists[i].IsDeleted?"X":"")); row.Cells.Add(ListWikiPageHists[i].DateTimeSaved.ToString()); gridMain.Rows.Add(row); } gridMain.EndUpdate(); }
private void webBrowserWiki_Navigating(object sender, WebBrowserNavigatingEventArgs e) { //For debugging, we need to remember that the following happens when you click on an internal link: //1. This method fires. url includes 'wiki:' //2. This causes LoadWikiPage method to fire. It loads the document text. //3. Which causes this method to fire again. url is "about:blank". //This doesn't seem to be a problem. We wrote it so that it won't go into an infinite loop, but it's something to be aware of. if (e.Url.ToString() == "about:blank" || e.Url.ToString().StartsWith("about:blank#")) { //This is a typical wiki page OR this is an internal bookmark. //We want to let the browser control handle the "navigation" so that it correctly loads the page OR simply auto scrolls to the div. } else if (e.Url.ToString().StartsWith("about:")) { //All other URLs that start with about: and do not have the required "blank#" are treated as malformed URLs. e.Cancel = true; return; } else if (e.Url.ToString().StartsWith("wiki:")) //user clicked on an internal link //It is invalid to have more than one space in a row in URLs. //When there is more than one space in a row, WebBrowserNavigatingEventArgs will convert the spaces into ' ' //In order for our internal wiki page links to work, we need to always replace the ' ' chars with spaces again. { string wikiPageTitle = Regex.Replace(e.Url.ToString(), @"\u00A0", " ").Substring(5); WikiPage wikiPageDeleted = WikiPages.GetByTitle(wikiPageTitle, isDeleted: true); //Should most likely be null. if (wikiPageDeleted != null) { if (MessageBox.Show(Lan.g(this, "WikiPage '") + wikiPageTitle + Lan.g(this, "' is currently archived. Would you like to restore it?"), "", MessageBoxButtons.OKCancel) != DialogResult.OK) { e.Cancel = true; return; } else { //User wants to restore the WikiPage. WikiPages.WikiPageRestore(wikiPageDeleted, Security.CurUser.UserNum); } } historyNavBack--; //We have to decrement historyNavBack to tell whether or not we need to branch our page history or add to page history LoadWikiPage(wikiPageTitle); e.Cancel = true; return; } else if (e.Url.ToString().Contains("wikifile:")) { string fileName = e.Url.ToString().Substring(e.Url.ToString().LastIndexOf("wikifile:") + 9).Replace("/", "\\"); if (!File.Exists(fileName)) { MessageBox.Show(Lan.g(this, "File does not exist: ") + fileName); e.Cancel = true; return; } try { System.Diagnostics.Process.Start(fileName); } catch (Exception ex) { ex.DoNothing(); } e.Cancel = true; return; } else if (e.Url.ToString().Contains("folder:")) { string folderName = e.Url.ToString().Substring(e.Url.ToString().LastIndexOf("folder:") + 7).Replace("/", "\\"); if (!Directory.Exists(folderName)) { MessageBox.Show(Lan.g(this, "Folder does not exist: ") + folderName); e.Cancel = true; return; } try { System.Diagnostics.Process.Start(folderName); } catch (Exception ex) { ex.DoNothing(); } e.Cancel = true; return; } else if (e.Url.ToString().Contains("wikifilecloud:")) { string fileName = e.Url.ToString().Substring(e.Url.ToString().LastIndexOf("wikifilecloud:") + 14); if (!FileAtoZ.Exists(fileName)) { MessageBox.Show(Lan.g(this, "File does not exist: ") + fileName); e.Cancel = true; return; } try { FileAtoZ.StartProcess(fileName); } catch (Exception ex) { ex.DoNothing(); } e.Cancel = true; return; } else if (e.Url.ToString().Contains("foldercloud:")) { string folderName = e.Url.ToString().Substring(e.Url.ToString().LastIndexOf("foldercloud:") + 12); if (!FileAtoZ.DirectoryExists(folderName)) { MessageBox.Show(Lan.g(this, "Folder does not exist: ") + folderName); e.Cancel = true; return; } try { FileAtoZ.OpenDirectory(folderName); } catch (Exception ex) { ex.DoNothing(); } e.Cancel = true; return; } else if (e.Url.ToString().StartsWith("http")) //navigating outside of wiki by clicking a link { try { System.Diagnostics.Process.Start(e.Url.ToString()); } catch (Exception ex) { ex.DoNothing(); } e.Cancel = true; //Stops the page from loading in FormWiki. return; } }
///<summary>Before calling this, make sure to increment/decrement the historyNavBack index to keep track of the position in history. If loading a new page, decrement historyNavBack before calling this function. </summary> private void LoadWikiPage(string pageTitle) { //This is called from 11 different places, any time the program needs to refresh a page from the db. //It's also called from the browser_Navigating event when a "wiki:" link is clicked. WikiPage wpage = WikiPages.GetByTitle(pageTitle); if (wpage == null) { string errorMsg = ""; if (!WikiPages.IsWikiPageTitleValid(pageTitle, out errorMsg)) { MsgBox.Show(this, "That page does not exist and cannot be made because the page title contains invalid characters."); return; } if (!MsgBox.Show(this, MsgBoxButtons.YesNo, "That page does not exist. Would you like to create it?")) { return; } Action <string> onWikiSaved = new Action <string>((pageTitleNew) => { //Insert the pageTitleNew where the pageTitle exists in the existing WikiPageCur //(guaranteed to be unique because all INVALID WIKIPAGE LINK's have an ID at the end) string pageContent = WikiPages.GetWikiPageContentWithWikiPageTitles(WikiPageCur.PageContent); WikiPageCur.PageContent = pageContent.Replace(pageTitle, pageTitleNew); WikiPageCur.PageContent = WikiPages.ConvertTitlesToPageNums(WikiPageCur.PageContent); WikiPages.Update(WikiPageCur); }); AddWikiPage(onWikiSaved); return; } WikiPageCur = wpage; try { webBrowserWiki.DocumentText = WikiPages.TranslateToXhtml(WikiPageCur.PageContent, false); } catch (Exception ex) { webBrowserWiki.DocumentText = ""; MessageBox.Show(this, Lan.g(this, "This page is broken and cannot be viewed. Error message:") + " " + ex.Message); } Text = "Wiki - " + WikiPageCur.PageTitle; #region historyMaint //This region is duplicated in webBrowserWiki_Navigating() for external links. Modifications here will need to be reflected there. int indexInHistory = historyNav.Count - (1 + historyNavBack); //historyNavBack number of pages before the last page in history. This is the index of the page we are loading. if (historyNav.Count == 0) //empty history { historyNavBack = 0; historyNav.Add("wiki:" + pageTitle); } else if (historyNavBack < 0) //historyNavBack could be negative here. This means before the action that caused this load, we were not navigating through history, simply set back to 0 and add to historyNav[] if necessary. { historyNavBack = 0; if (historyNav[historyNav.Count - 1] != "wiki:" + pageTitle) { historyNav.Add("wiki:" + pageTitle); } } else if (historyNavBack >= 0 && historyNav[indexInHistory] != "wiki:" + pageTitle) //branching from page in history { historyNav.RemoveRange(indexInHistory, historyNavBack + 1); //remove "forward" history. branching off in a new direction historyNavBack = 0; historyNav.Add("wiki:" + pageTitle); } #endregion }
private void Save_Click() { if (!MarkupL.ValidateMarkup(textContent, true)) { return; } if (_isInvalidPreview) { MsgBox.Show(this, "This page is in an invalid state and cannot be saved."); return; } WikiPage wikiPageDB = WikiPages.GetByTitle(WikiPageCur.PageTitle); if (wikiPageDB != null && WikiPageCur.DateTimeSaved < wikiPageDB.DateTimeSaved) { if (WikiPageCur.IsDraft) { if (!MsgBox.Show(this, MsgBoxButtons.OKCancel, "The wiki page has been edited since this draft was last saved. Overwrite and continue?")) { return; } } else if (!MsgBox.Show(this, MsgBoxButtons.OKCancel, "This page has been modified and saved since it was opened on this computer. Save anyway?")) { return; } } List <string> listInvalidWikiPages = WikiPages.GetMissingPageTitles(textContent.Text); string message = Lan.g(this, "This page has the following invalid wiki page link(s):") + "\r\n" + string.Join("\r\n", listInvalidWikiPages.Take(10)) + "\r\n" + (listInvalidWikiPages.Count > 10 ? "...\r\n\r\n" : "\r\n") + Lan.g(this, "Create the wikipage(s) automatically?"); if (listInvalidWikiPages.Count != 0 && MsgBox.Show(this, MsgBoxButtons.YesNo, message)) { foreach (string title in listInvalidWikiPages) { WikiPage wp = new WikiPage(); wp.PageTitle = title; wp.UserNum = Security.CurUser.UserNum; wp.PageContent = "[[" + WikiPageCur.WikiPageNum + "]]\r\n" //link back + "<h1>" + title + "</h1>\r\n"; //page title WikiPages.InsertAndArchive(wp); } } WikiPageCur.PageContent = WikiPages.ConvertTitlesToPageNums(textContent.Text); WikiPageCur.UserNum = Security.CurUser.UserNum; Regex regex = new Regex(@"\[\[(keywords:).+?\]\]"); //only grab first match Match m = regex.Match(textContent.Text); WikiPageCur.KeyWords = m.Value.Replace("[[keywords:", "").TrimEnd(']'); //will be empty string if no match if (WikiPageCur.IsDraft) { WikiPages.DeleteDraft(WikiPageCur); //remove the draft from the database. WikiPageCur.IsDraft = false; //no longer a draft if (wikiPageDB != null) //wikiPageDb should never be null, otherwise there was no way to get to this draft. //We need to set the draft copy of the wiki page to the WikiPageNum of the real page. //This is because the draft is the most up to date copy of the wiki page, and is about to be saved, however, we want to maintain any //links there may be to the real wiki page, since we link by WikiPageNum instead of PageTitle (see JobNum 4429 for more info) { WikiPageCur.WikiPageNum = wikiPageDB.WikiPageNum; } } WikiPages.InsertAndArchive(WikiPageCur); //If the form was given an action, fire it. _onWikiSaved?.Invoke(WikiPageCur.PageTitle); FormWiki formWiki = (FormWiki)this.OwnerForm; if (formWiki != null && !formWiki.IsDisposed) { formWiki.RefreshPage(WikiPageCur.PageTitle); } HasSaved = true; Close(); //should be dialog result?? }