context: 
  using System;
  using System.Data;
  using System.Configuration;
  using System.Collections;
  using System.Collections.Generic;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using ScrewTurn.Wiki.PluginFramework;
  using System.Text;
  using System.Globalization;
  
  namespace ScrewTurn.Wiki {
  
  	public partial class AllPages : BasePage {
  
  		/// <summary>
  		/// The number of items in a page.
  		/// </summary>
  		public int PageSize = 50;
  
  		private int selectedPage = 0;
  		private int rangeBegin = 0;
  		private int rangeEnd = 49;
  
  		private IList<PageContent> currentPages = null;
  
  		protected void Page_Load(object sender, EventArgs e) {
  			string currentWiki = DetectWiki();
  			PageSize = Settings.GetListSize(currentWiki);
  			rangeEnd = PageSize - 1;
  
  			LoginTools.VerifyReadPermissionsForCurrentNamespace(currentWiki);
  
  			if(Request["Cat"] != null) {
  				if(Request["Cat"].Equals("-")) {
  					lblPages.Text = Properties.Messages.UncategorizedPages;
  				}
  				else {
  					lblPages.Text = Properties.Messages.PagesOfCategory + " <i>" + Request["Cat"] + "</i>";
  				}
  			}
  
  			if(!Page.IsPostBack) {
  				lnkCategories.NavigateUrl = UrlTools.BuildUrl(currentWiki, "Category.aspx");
  				lnkSearch.NavigateUrl = UrlTools.BuildUrl(currentWiki, "Search.aspx");
  
  				currentPages = GetAllPages();
  				pageSelector.ItemCount = currentPages.Count;
  				pageSelector.PageSize = PageSize;
  
  				string p = Request["Page"];
  				if(!int.TryParse(p, out selectedPage)) selectedPage = 0;
  				pageSelector.SelectPage(selectedPage);
  			}
  
  			Page.Title = Properties.Messages.AllPagesTitle + " (" + (rangeBegin + 1).ToString() + "-" + (rangeEnd + 1).ToString() + ") - " + Settings.GetWikiTitle(currentWiki);
  
  			// Important note
  			// This page cannot use a repeater because the page list has particular elements used for grouping pages
  
  			PrintPages();
  		}
  
  		protected void pageSelector_SelectedPageChanged(object sender, SelectedPageChangedEventArgs e) {
  			rangeBegin = e.SelectedPage * PageSize;
  			rangeEnd = rangeBegin + e.ItemCount - 1;
  			selectedPage = e.SelectedPage;
  
  			PrintPages();
  		}
  
  		/// <summary>
  		/// Gets the creator of a page.
  		/// </summary>
  		/// <param name="page">The page.</param>
  		/// <returns>The creator.</returns>
  		private string GetCreator(PageContent page) {
  			List<int> baks = Pages.GetBackups(page);
  
  			PageContent temp = null;
  			if(baks.Count > 0) {
  				temp = Pages.GetBackupContent(page, baks[0]);
  			}
  			else {
  				temp = page;
  			}
  
  			return temp.User;
  		}
  
  		/// <summary>
  		/// Gets all the pages in the namespace.
  		/// </summary>
  		/// <returns>The pages.</returns>
  		private IList<PageContent> GetAllPages() {
  			IList<PageContent> pages = null;
  
  			string currentWiki = DetectWiki();
  
  			// Categories Management
  			if(Request["Cat"] != null) {
  				if(Request["Cat"].Equals("-")) {
  					pages = Pages.GetUncategorizedPages(currentWiki, DetectNamespaceInfo());
  				}
  				else {
  					CategoryInfo cat = Pages.FindCategory(currentWiki, Request["Cat"]);
  					if(cat != null) {
  						pages = new PageContent[cat.Pages.Length];
  						for(int i = 0; i < cat.Pages.Length; i++) {
  							pages[i] = Pages.FindPage(currentWiki, cat.Pages[i]);
  						}
  						Array.Sort(pages as PageContent[], new PageNameComparer());
  					}
  					else {
  						pages = new PageContent[0];
  					}
  				}
  			}
  			else {
  				pages = Pages.GetPages(currentWiki, DetectNamespaceInfo());
  			}
  
  			return pages;
  		}
  
  		/// <summary>
  		/// Prints the pages.
  		/// </summary>
  		public void PrintPages() {
  			StringBuilder sb = new StringBuilder(65536);
  
  			if(currentPages == null) currentPages = GetAllPages();
  
  			string currentWiki = DetectWiki();
  
  			// Prepare ExtendedPageInfo array
  			ExtendedPageInfo[] tempPageList = new ExtendedPageInfo[rangeEnd - rangeBegin + 1];
  			for(int i = 0; i < tempPageList.Length; i++) {
  				tempPageList[i] = new ExtendedPageInfo(currentPages[rangeBegin + i], GetCreator(currentPages[rangeBegin + i]), currentPages[rangeBegin + i].User);
  			}
  
  			// Prepare for sorting
  			bool reverse = false;
  			SortingMethod sortBy = SortingMethod.Title;
  			if(Request["SortBy"] != null) {
  				try {
  					sortBy = (SortingMethod)Enum.Parse(typeof(SortingMethod), Request["SortBy"], true);
  				}
  				catch {
  					// Backwards compatibility
  					if(Request["SortBy"].ToLowerInvariant() == "date") sortBy = SortingMethod.DateTime;
  				}
  				if(Request["Reverse"] != null) reverse = true;
  			}
  
  			SortedDictionary<SortingGroup, List<ExtendedPageInfo>> sortedPages = PageSortingTools.Sort(tempPageList, sortBy, reverse);
  
  			sb.Append(@"<table id=""PageListTable"" class=""generic"" cellpadding=""0"" cellspacing=""0"">");
  			sb.Append("<thead>");
  			sb.Append(@"<tr class=""tableheader"">");
  			
  			// Page title
  			sb.Append(@"<th><a rel=""nofollow"" href=""");
  			UrlTools.BuildUrl(currentWiki, sb, "AllPages.aspx?SortBy=Title",
  				(!reverse && sortBy == SortingMethod.Title ? "&amp;Reverse=1" : ""),
  				(Request["Cat"] != null ? "&amp;Cat=" + Tools.UrlEncode(Request["Cat"]) : ""),
  				"&amp;Page=", selectedPage.ToString());
  
  			sb.Append(@""" title=""");
  			sb.Append(Properties.Messages.SortByTitle);
  			sb.Append(@""">");
  			sb.Append(Properties.Messages.PageTitle);
  			sb.Append((reverse && sortBy.Equals("title") ? " &uarr;" : ""));
  			sb.Append((!reverse && sortBy.Equals("title") ? " &darr;" : ""));
  			sb.Append("</a></th>");
  
  			// Message count
  			sb.Append(@"<th><img src=""Images/Comment.png"" alt=""Comments"" /></th>");
  
  			// Creation date/time
  			sb.Append(@"<th><a rel=""nofollow"" href=""");
  			UrlTools.BuildUrl(currentWiki, sb, "AllPages.aspx?SortBy=Creation",
  				(!reverse && sortBy == SortingMethod.Creation ? "&amp;Reverse=1" : ""),
  				(Request["Cat"] != null ? "&amp;Cat=" + Tools.UrlEncode(Request["Cat"]) : ""),
  				"&amp;Page=", selectedPage.ToString());
  
  			sb.Append(@""" title=""");
  			sb.Append(Properties.Messages.SortByDate);
  			sb.Append(@""">");
  			sb.Append(Properties.Messages.CreatedOn);
  			sb.Append((reverse && sortBy.Equals("creation") ? " &uarr;" : ""));
  			sb.Append((!reverse && sortBy.Equals("creation") ? " &darr;" : ""));
  			sb.Append("</a></th>");
  
  			// Mod. date/time
  			sb.Append(@"<th><a rel=""nofollow"" href=""");
  			UrlTools.BuildUrl(currentWiki, sb, "AllPages.aspx?SortBy=DateTime",
  				(!reverse && sortBy == SortingMethod.DateTime ? "&amp;Reverse=1" : ""),
  				(Request["Cat"] != null ? "&amp;Cat=" + Tools.UrlEncode(Request["Cat"]) : ""),
  				"&amp;Page=", selectedPage.ToString());
  
  			sb.Append(@""" title=""");
  			sb.Append(Properties.Messages.SortByDate);
  			sb.Append(@""">");
  			sb.Append(Properties.Messages.ModifiedOn);
  			sb.Append((reverse && sortBy.Equals("date") ? " &uarr;" : ""));
  			sb.Append((!reverse && sortBy.Equals("date") ? " &darr;" : ""));
  			sb.Append("</a></th>");
  
  			// Creator
  			sb.Append(@"<th><a rel=""nofollow"" href=""");
  			UrlTools.BuildUrl(currentWiki, sb, "AllPages.aspx?SortBy=Creator",
  				(!reverse && sortBy == SortingMethod.Creator ? "&amp;Reverse=1" : ""),
  				(Request["Cat"] != null ? "&amp;Cat=" + Tools.UrlEncode(Request["Cat"]) : ""),
  				"&amp;Page=", selectedPage.ToString());
  
  			sb.Append(@""" title=""");
  			sb.Append(Properties.Messages.SortByUser);
  			sb.Append(@""">");
  			sb.Append(Properties.Messages.CreatedBy);
  			sb.Append((reverse && sortBy.Equals("creator") ? " &uarr;" : ""));
  			sb.Append((!reverse && sortBy.Equals("creator") ? " &darr;" : ""));
  			sb.Append("</a></th>");
  
  			// Last author
  			sb.Append(@"<th><a rel=""nofollow"" href=""");
  			UrlTools.BuildUrl(currentWiki, sb, "AllPages.aspx?SortBy=User",
  				(!reverse && sortBy == SortingMethod.User ? "&amp;Reverse=1" : ""),
  				(Request["Cat"] != null ? "&amp;Cat=" + Tools.UrlEncode(Request["Cat"]) : ""),
  				"&amp;Page=", selectedPage.ToString());
  
  			sb.Append(@""" title=""");
  			sb.Append(Properties.Messages.SortByUser);
  			sb.Append(@""">");
  			sb.Append(Properties.Messages.ModifiedBy);
  			sb.Append((reverse && sortBy.Equals("user") ? " &uarr;" : ""));
  			sb.Append((!reverse && sortBy.Equals("user") ? " &darr;" : ""));
  			sb.Append("</a></th>");
  
  			// Categories
  			sb.Append("<th>");
  			sb.Append(Properties.Messages.Categories);
  			sb.Append("</th>");
  
  			sb.Append("</tr>");
  			sb.Append("</thead><tbody>");
  
  			foreach(SortingGroup key in sortedPages.Keys) {
  				List<ExtendedPageInfo> pageList = sortedPages[key];
  				for(int i = 0; i < pageList.Count; i++) {
  					if(i == 0) {
  						// Add group header
  						sb.Append(@"<tr class=""tablerow"">");
  						if(sortBy == SortingMethod.Title) {
  							sb.AppendFormat("<td colspan=\"7\"><b>{0}</b></td>", key.Label);
  						}
  						else if(sortBy == SortingMethod.Creation) {
  							sb.AppendFormat("<td colspan=\"2\"></td><td colspan=\"5\"><b>{0}</b></td>", key.Label);
  						}
  						else if(sortBy == SortingMethod.DateTime) {
  							sb.AppendFormat("<td colspan=\"3\"></td><td colspan=\"4\"><b>{0}</b></td>", key.Label);
  						}
  
gt: 						else if(sortBy == SortingMethod.Creator) {