using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ScrewTurn.Wiki.PluginFramework;
using System.Globalization;
namespace ScrewTurn.Wiki {
public partial class AdminHome : BasePage {
protected void Page_Load(object sender, EventArgs e) {
AdminMaster.RedirectToLoginIfNeeded();
if(!AdminMaster.CanManageConfiguration(SessionFacade.GetCurrentUsername(), SessionFacade.GetCurrentGroupNames())) UrlTools.Redirect("AccessDenied.aspx");
PrintSystemStatus();
if(!Page.IsPostBack) {
rptPages.DataBind();
rptIndex.DataBind();
DisplayOrphansCount();
}
}
///
/// Displays the orphan pages count.
///
private void DisplayOrphansCount() {
int orphans = Pages.GetOrphanedPages(null).Length;
foreach(NamespaceInfo nspace in Pages.GetNamespaces()) {
orphans += Pages.GetOrphanedPages(nspace).Length;
}
lblOrphanPagesCount.Text = orphans.ToString();
}
protected void btnClearCache_Click(object sender, EventArgs e) {
Redirections.Clear();
Content.ClearPseudoCache();
Content.InvalidateAllPages();
PrintSystemStatus();
}
protected void rptPages_DataBinding(object sender, EventArgs e) {
List result = new List(50);
Dictionary> links = Pages.GetWantedPages(null);
foreach(KeyValuePair> pair in links) {
result.Add(new WantedPageRow("<root>", "", pair.Key, pair.Value));
}
foreach(NamespaceInfo nspace in Pages.GetNamespaces()) {
links = Pages.GetWantedPages(nspace.Name);
foreach(KeyValuePair> pair in links) {
result.Add(new WantedPageRow(nspace.Name, nspace.Name + ".", pair.Key, pair.Value));
}
}
rptPages.DataSource = result;
}
protected void btnRebuildPageLinks_Click(object sender, EventArgs e) {
RebuildPageLinks(Pages.GetPages(null));
foreach(NamespaceInfo nspace in Pages.GetNamespaces()) {
RebuildPageLinks(Pages.GetPages(nspace));
}
DisplayOrphansCount();
}
///
/// Rebuilds the page links for the specified pages.
///
/// The pages.
private void RebuildPageLinks(IList pages) {
foreach(PageInfo page in pages) {
PageContent content = Content.GetPageContent(page, false);
Pages.StorePageOutgoingLinks(page, content.Content);
}
}
protected void rptIndex_DataBinding(object sender, EventArgs e) {
List result = new List(5);
foreach(IPagesStorageProviderV30 prov in Collectors.PagesProviderCollector.AllProviders) {
result.Add(new IndexRow(prov));
}
rptIndex.DataSource = result;
}
protected void rptIndex_ItemCommand(object sender, CommandEventArgs e) {
Log.LogEntry("Index rebuild requested for " + e.CommandArgument as string, EntryType.General, SessionFacade.GetCurrentUsername());
IPagesStorageProviderV30 provider = Collectors.PagesProviderCollector.GetProvider(e.CommandArgument as string);
provider.RebuildIndex();
Log.LogEntry("Index rebuild completed for " + e.CommandArgument as string, EntryType.General, Log.SystemUsername);
rptIndex.DataBind();
}
protected void btnShutdownConfirm_Click(object sender, EventArgs e) {
Log.LogEntry("WebApp shutdown requested", EntryType.General, SessionFacade.CurrentUsername);
Response.Clear();
Response.Write(@"Web Application has been shut down, please go to the home page." + "\n\n");
Response.Flush();
Response.Close();
Log.LogEntry("Executing WebApp shutdown", EntryType.General, Log.SystemUsername);
HttpRuntime.UnloadAppDomain();
}
public void PrintSystemStatus() {
StringBuilder sb = new StringBuilder(500);
sb.Append(Properties.Messages.OnlineUsers + ": " +
ScrewTurn.Wiki.Cache.OnlineUsers.ToString() + "
" + "\n");
int inactive = 0;
List users = Users.GetUsers();
for(int i = 0; i < users.Count; i++) {
if(!users[i].Active) inactive++;
}
sb.Append(Properties.Messages.UserCount + ": " + users.Count.ToString() + " (" + inactive.ToString() + " " + Properties.Messages.InactiveUsers + ")
" + "\n");
sb.Append(Properties.Messages.CachedPages + ": " + ScrewTurn.Wiki.Cache.PageCacheUsage.ToString() + "/" + Pages.GetGlobalPageCount().ToString() + " (" + ScrewTurn.Wiki.Cache.FormattedPageCacheUsage.ToString() + " " + Properties.Messages.Formatted + ")
" + "\n");
sb.Append(Properties.Messages.WikiVersion + ": " + Settings.WikiVersion + "" + "\n");
if(!Page.IsPostBack) {
sb.Append(CheckVersion());
}
sb.Append("
");
sb.Append(Properties.Messages.ServerUptime + ": " + Tools.TimeSpanToString(Tools.SystemUptime) + " (" +
Properties.Messages.MayBeInaccurate + ")");
lblSystemStatusContent.Text = sb.ToString();
}
private string CheckVersion() {
if(Settings.DisableAutomaticVersionCheck) return "";
StringBuilder sb = new StringBuilder(100);
sb.Append("(");
string newVersion = null;
string ignored = null;
UpdateStatus status = Tools.GetUpdateStatus("http://www.screwturn.eu/Version/Wiki/3.htm",
Settings.WikiVersion, out newVersion, out ignored);
if(status == UpdateStatus.Error) {
sb.Append(@"" + Properties.Messages.VersionCheckError + "");
}
else if(status == UpdateStatus.NewVersionFound) {
sb.Append(@"" + Properties.Messages.NewVersionFound + ": " + newVersion + "");
}
else if(status == UpdateStatus.UpToDate) {
sb.Append(@"" + Properties.Messages.WikiUpToDate + "");
}
else throw new NotSupportedException();
sb.Append(")");
return sb.ToString();
}
}
///
/// Represents a missing or orphaned page.
///
public class WantedPageRow {
private string nspace, nspacePrefix, name, linkingPages;
///
/// Initializes a new instance of the class.
///
/// The namespace.
/// The namespace prefix.
/// The full name.
/// The pages that link the wanted page.
public WantedPageRow(string nspace, string nspacePrefix, string name, List linkingPages) {
this.nspace = nspace;
this.nspacePrefix = nspacePrefix;
this.name = name;
StringBuilder sb = new StringBuilder(100);
for(int i = 0; i < linkingPages.Count; i++) {
PageInfo page = Pages.FindPage(linkingPages[i]);
if(page != null) {
PageContent content = Content.GetPageContent(page, false);
sb.AppendFormat(@"{2}, ", page.FullName, Settings.PageExtension,
FormattingPipeline.PrepareTitle(content.Title, false, FormattingContext.Other, page));
}
}
this.linkingPages = sb.ToString().TrimEnd(' ', ',');
}
///
/// Gets the namespace.
///
public string Nspace {
get { return nspace; }
}
///
/// Gets the namespace prefix.
///
public string NspacePrefix {
get { return nspacePrefix; }
}
///
/// Gets the full name.
///
public string Name {
get { return name; }
}
///
/// Gets the linker pages.
///
public string LinkingPages {
get { return linkingPages; }
}
}
///
/// Represents the status of a search engine index.
///
public class IndexRow {
private string provider, providerType, documents, words, occurrences, size;
private bool isOk;
///
/// Initializes a new instance of the class.
///
/// The original provider.
public IndexRow(IPagesStorageProviderV30 provider) {
this.provider = provider.Information.Name;
providerType = provider.GetType().FullName;
int docCount, wordCount, matchCount;
long size;
provider.GetIndexStats(out docCount, out wordCount, out matchCount, out size);
this.documents = docCount.ToString();
this.words = wordCount.ToString();
this.occurrences = matchCount.ToString();
this.size = Tools.BytesToString(size);
this.isOk = !provider.IsIndexCorrupted;
}
///
/// Gets the provider.
///
public string Provider {
get { return provider; }
}
///
/// Gets the provider type.
///
public string ProviderType {
get { return providerType; }
}
///
/// Gets the number of documents.
///
public string Documents {
get { return documents; }
}
///
/// Gets the number of words.
///
public string Words {
get { return words; }
}
///
/// Gets the number of occurrences.
///
public string Occurrences {
get { return occurrences; }
}
///
/// Gets the size of the index.
///
public string Size {
get { return size; }
}
///
/// Gets a value indicating whether the index is OK.
///
public bool IsOK {
get { return isOk; }
}
}
}