PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/WebApplication/AdminHome.aspx.cs

https://bitbucket.org/screwturn/screwturn-wiki/
C# | 347 lines | 229 code | 65 blank | 53 comment | 27 complexity | 48991baab0f55be03e8c3c859bbb2164 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using ScrewTurn.Wiki.PluginFramework;
  11. using System.Globalization;
  12. namespace ScrewTurn.Wiki {
  13. public partial class AdminHome : BasePage {
  14. protected void Page_Load(object sender, EventArgs e) {
  15. AdminMaster.RedirectToLoginIfNeeded();
  16. if(!AdminMaster.CanManageConfiguration(SessionFacade.GetCurrentUsername(), SessionFacade.GetCurrentGroupNames())) UrlTools.Redirect("AccessDenied.aspx");
  17. PrintSystemStatus();
  18. if(!Page.IsPostBack) {
  19. rptPages.DataBind();
  20. rptIndex.DataBind();
  21. DisplayOrphansCount();
  22. string anon = Settings.AnonymousGroup;
  23. foreach(UserGroup group in Users.GetUserGroups()) {
  24. if(group.Name != anon) {
  25. ListItem item = new ListItem(group.Name, group.Name);
  26. item.Selected = true;
  27. lstGroups.Items.Add(item);
  28. }
  29. }
  30. }
  31. }
  32. /// <summary>
  33. /// Displays the orphan pages count.
  34. /// </summary>
  35. private void DisplayOrphansCount() {
  36. int orphans = Pages.GetOrphanedPages(null as NamespaceInfo).Length;
  37. foreach(NamespaceInfo nspace in Pages.GetNamespaces()) {
  38. orphans += Pages.GetOrphanedPages(nspace).Length;
  39. }
  40. lblOrphanPagesCount.Text = orphans.ToString();
  41. }
  42. protected void cvGroups_ServerValidate(object sender, ServerValidateEventArgs e) {
  43. e.IsValid = false;
  44. foreach(ListItem item in lstGroups.Items) {
  45. if(item.Selected) {
  46. e.IsValid = true;
  47. break;
  48. }
  49. }
  50. }
  51. protected void btnSendBulkEmail_Click(object sender, EventArgs e) {
  52. lblEmailResult.CssClass = "";
  53. lblEmailResult.Text = "";
  54. Page.Validate("email");
  55. if(!Page.IsValid) return;
  56. List<string> emails = new List<string>();
  57. foreach(ListItem item in lstGroups.Items) {
  58. if(item.Selected) {
  59. UserGroup group = Users.FindUserGroup(item.Value);
  60. if(group != null) {
  61. foreach(string user in group.Users) {
  62. UserInfo u = Users.FindUser(user);
  63. if(u != null) emails.Add(u.Email);
  64. }
  65. }
  66. }
  67. }
  68. EmailTools.AsyncSendMassEmail(emails.ToArray(), Settings.SenderEmail,
  69. txtSubject.Text, txtBody.Text, false);
  70. lblEmailResult.CssClass = "resultok";
  71. lblEmailResult.Text = Properties.Messages.MassEmailSent;
  72. }
  73. protected void btnClearCache_Click(object sender, EventArgs e) {
  74. Redirections.Clear();
  75. Content.ClearPseudoCache();
  76. Content.InvalidateAllPages();
  77. PrintSystemStatus();
  78. }
  79. protected void rptPages_DataBinding(object sender, EventArgs e) {
  80. List<WantedPageRow> result = new List<WantedPageRow>(50);
  81. Dictionary<string, List<string>> links = Pages.GetWantedPages(null);
  82. foreach(KeyValuePair<string, List<string>> pair in links) {
  83. result.Add(new WantedPageRow("&lt;root&gt;", "", pair.Key, pair.Value));
  84. }
  85. foreach(NamespaceInfo nspace in Pages.GetNamespaces()) {
  86. links = Pages.GetWantedPages(nspace.Name);
  87. foreach(KeyValuePair<string, List<string>> pair in links) {
  88. result.Add(new WantedPageRow(nspace.Name, nspace.Name + ".", pair.Key, pair.Value));
  89. }
  90. }
  91. rptPages.DataSource = result;
  92. }
  93. protected void btnRebuildPageLinks_Click(object sender, EventArgs e) {
  94. Pages.RebuildPageLinks(Pages.GetPages(null));
  95. foreach(NamespaceInfo nspace in Pages.GetNamespaces()) {
  96. Pages.RebuildPageLinks(Pages.GetPages(nspace));
  97. }
  98. DisplayOrphansCount();
  99. }
  100. protected void rptIndex_DataBinding(object sender, EventArgs e) {
  101. List<IndexRow> result = new List<IndexRow>(5);
  102. foreach(IPagesStorageProviderV30 prov in Collectors.PagesProviderCollector.AllProviders) {
  103. result.Add(new IndexRow(prov));
  104. }
  105. rptIndex.DataSource = result;
  106. }
  107. protected void rptIndex_ItemCommand(object sender, CommandEventArgs e) {
  108. Log.LogEntry("Index rebuild requested for " + e.CommandArgument as string, EntryType.General, SessionFacade.GetCurrentUsername());
  109. IPagesStorageProviderV30 provider = Collectors.PagesProviderCollector.GetProvider(e.CommandArgument as string);
  110. provider.RebuildIndex();
  111. Log.LogEntry("Index rebuild completed for " + e.CommandArgument as string, EntryType.General, Log.SystemUsername);
  112. rptIndex.DataBind();
  113. }
  114. protected void btnShutdownConfirm_Click(object sender, EventArgs e) {
  115. Log.LogEntry("WebApp shutdown requested", EntryType.General, SessionFacade.CurrentUsername);
  116. Response.Clear();
  117. Response.Write(@"Web Application has been shut down, please go to the <a href=""Default.aspx"">home page</a>." + "\n\n");
  118. Response.Flush();
  119. Response.Close();
  120. Log.LogEntry("Executing WebApp shutdown", EntryType.General, Log.SystemUsername);
  121. HttpRuntime.UnloadAppDomain();
  122. }
  123. public void PrintSystemStatus() {
  124. StringBuilder sb = new StringBuilder(500);
  125. sb.Append(Properties.Messages.OnlineUsers + ": <b>" +
  126. ScrewTurn.Wiki.Cache.OnlineUsers.ToString() + "</b><br />" + "\n");
  127. int inactive = 0;
  128. List<UserInfo> users = Users.GetUsers();
  129. for(int i = 0; i < users.Count; i++) {
  130. if(!users[i].Active) inactive++;
  131. }
  132. sb.Append(Properties.Messages.UserCount + ": <b>" + users.Count.ToString() + "</b> (" + inactive.ToString() + " " + Properties.Messages.InactiveUsers + ")<br />" + "\n");
  133. sb.Append(Properties.Messages.CachedPages + ": <b>" + ScrewTurn.Wiki.Cache.PageCacheUsage.ToString() + "/" + Pages.GetGlobalPageCount().ToString() + "</b> (" + ScrewTurn.Wiki.Cache.FormattedPageCacheUsage.ToString() + " " + Properties.Messages.Formatted + ")<br />" + "\n");
  134. sb.Append(Properties.Messages.WikiVersion + ": <b>" + Settings.WikiVersion + "</b>" + "\n");
  135. if(!Page.IsPostBack) {
  136. sb.Append(CheckVersion());
  137. }
  138. sb.Append("<br />");
  139. sb.Append(Properties.Messages.ServerUptime + ": <b>" + Tools.TimeSpanToString(Tools.SystemUptime) + "</b> (" +
  140. Properties.Messages.MayBeInaccurate + ")");
  141. lblSystemStatusContent.Text = sb.ToString();
  142. }
  143. private string CheckVersion() {
  144. if(Settings.DisableAutomaticVersionCheck) return "";
  145. StringBuilder sb = new StringBuilder(100);
  146. sb.Append("(");
  147. string newVersion = null;
  148. string ignored = null;
  149. UpdateStatus status = Tools.GetUpdateStatus("http://www.screwturn.eu/Version/Wiki/3.htm",
  150. Settings.WikiVersion, out newVersion, out ignored);
  151. if(status == UpdateStatus.Error) {
  152. sb.Append(@"<span class=""resulterror"">" + Properties.Messages.VersionCheckError + "</span>");
  153. }
  154. else if(status == UpdateStatus.NewVersionFound) {
  155. sb.Append(@"<span class=""resulterror"">" + Properties.Messages.NewVersionFound + ": <b>" + newVersion + "</b></span>");
  156. }
  157. else if(status == UpdateStatus.UpToDate) {
  158. sb.Append(@"<span class=""resultok"">" + Properties.Messages.WikiUpToDate + "</span>");
  159. }
  160. else throw new NotSupportedException();
  161. sb.Append(")");
  162. return sb.ToString();
  163. }
  164. }
  165. /// <summary>
  166. /// Represents a missing or orphaned page.
  167. /// </summary>
  168. public class WantedPageRow {
  169. private string nspace, nspacePrefix, name, linkingPages;
  170. /// <summary>
  171. /// Initializes a new instance of the <see cref="T:PageRow" /> class.
  172. /// </summary>
  173. /// <param name="nspace">The namespace.</param>
  174. /// <param name="nspacePrefix">The namespace prefix.</param>
  175. /// <param name="name">The full name.</param>
  176. /// <param name="linkingPages">The pages that link the wanted page.</param>
  177. public WantedPageRow(string nspace, string nspacePrefix, string name, List<string> linkingPages) {
  178. this.nspace = nspace;
  179. this.nspacePrefix = nspacePrefix;
  180. this.name = name;
  181. StringBuilder sb = new StringBuilder(100);
  182. for(int i = 0; i < linkingPages.Count; i++) {
  183. PageInfo page = Pages.FindPage(linkingPages[i]);
  184. if(page != null) {
  185. PageContent content = Content.GetPageContent(page, false);
  186. sb.AppendFormat(@"<a href=""{0}{1}"" title=""{2}"" target=""_blank"">{2}</a>, ", page.FullName, Settings.PageExtension,
  187. FormattingPipeline.PrepareTitle(content.Title, false, FormattingContext.Other, page));
  188. }
  189. }
  190. this.linkingPages = sb.ToString().TrimEnd(' ', ',');
  191. }
  192. /// <summary>
  193. /// Gets the namespace.
  194. /// </summary>
  195. public string Nspace {
  196. get { return nspace; }
  197. }
  198. /// <summary>
  199. /// Gets the namespace prefix.
  200. /// </summary>
  201. public string NspacePrefix {
  202. get { return nspacePrefix; }
  203. }
  204. /// <summary>
  205. /// Gets the full name.
  206. /// </summary>
  207. public string Name {
  208. get { return name; }
  209. }
  210. /// <summary>
  211. /// Gets the linker pages.
  212. /// </summary>
  213. public string LinkingPages {
  214. get { return linkingPages; }
  215. }
  216. }
  217. /// <summary>
  218. /// Represents the status of a search engine index.
  219. /// </summary>
  220. public class IndexRow {
  221. private string provider, providerType, documents, words, occurrences, size;
  222. private bool isOk;
  223. /// <summary>
  224. /// Initializes a new instance of the <see cref="T:IndexRow" /> class.
  225. /// </summary>
  226. /// <param name="provider">The original provider.</param>
  227. public IndexRow(IPagesStorageProviderV30 provider) {
  228. this.provider = provider.Information.Name;
  229. providerType = provider.GetType().FullName;
  230. int docCount, wordCount, matchCount;
  231. long size;
  232. provider.GetIndexStats(out docCount, out wordCount, out matchCount, out size);
  233. this.documents = docCount.ToString();
  234. this.words = wordCount.ToString();
  235. this.occurrences = matchCount.ToString();
  236. this.size = Tools.BytesToString(size);
  237. this.isOk = !provider.IsIndexCorrupted;
  238. }
  239. /// <summary>
  240. /// Gets the provider.
  241. /// </summary>
  242. public string Provider {
  243. get { return provider; }
  244. }
  245. /// <summary>
  246. /// Gets the provider type.
  247. /// </summary>
  248. public string ProviderType {
  249. get { return providerType; }
  250. }
  251. /// <summary>
  252. /// Gets the number of documents.
  253. /// </summary>
  254. public string Documents {
  255. get { return documents; }
  256. }
  257. /// <summary>
  258. /// Gets the number of words.
  259. /// </summary>
  260. public string Words {
  261. get { return words; }
  262. }
  263. /// <summary>
  264. /// Gets the number of occurrences.
  265. /// </summary>
  266. public string Occurrences {
  267. get { return occurrences; }
  268. }
  269. /// <summary>
  270. /// Gets the size of the index.
  271. /// </summary>
  272. public string Size {
  273. get { return size; }
  274. }
  275. /// <summary>
  276. /// Gets a value indicating whether the index is OK.
  277. /// </summary>
  278. public bool IsOK {
  279. get { return isOk; }
  280. }
  281. }
  282. }