using System;
using System.Collections.Generic;
using System.Text;
using ScrewTurn.Wiki.PluginFramework;
namespace ScrewTurn.Wiki {
///
/// Manages navigation paths.
///
public static class NavigationPaths {
///
/// Gets the list of the Navigation Paths.
///
/// The navigation paths, sorted by name.
public static List GetAllNavigationPaths() {
List allPaths = new List(30);
// Retrieve paths from every Pages provider
foreach(IPagesStorageProviderV30 provider in Collectors.PagesProviderCollector.AllProviders) {
allPaths.AddRange(provider.GetNavigationPaths(null));
foreach(NamespaceInfo nspace in provider.GetNamespaces()) {
allPaths.AddRange(provider.GetNavigationPaths(nspace));
}
}
allPaths.Sort(new NavigationPathComparer());
return allPaths;
}
///
/// Gets the list of the Navigation Paths in a namespace.
///
/// The namespace.
/// The navigation paths, sorted by name.
public static List GetNavigationPaths(NamespaceInfo nspace) {
List allPaths = new List(30);
// Retrieve paths from every Pages provider
foreach(IPagesStorageProviderV30 provider in Collectors.PagesProviderCollector.AllProviders) {
allPaths.AddRange(provider.GetNavigationPaths(nspace));
}
allPaths.Sort(new NavigationPathComparer());
return allPaths;
}
///
/// Finds a Navigation Path's Name.
///
/// The Name.
/// True if the Navigation Path exists.
public static bool Exists(string name) {
return Find(name) != null;
}
///
/// Finds and returns a Path.
///
/// The full name.
/// The correct object or null if no path is found.
public static NavigationPath Find(string fullName) {
List allPaths = GetAllNavigationPaths();
int idx = allPaths.BinarySearch(new NavigationPath(fullName, null), new NavigationPathComparer());
if(idx >= 0) return allPaths[idx];
else return null;
}
///
/// Adds a new Navigation Path.
///
/// The target namespace (null for the root).
/// The Name.
/// The Pages.
/// The Provider to use for the new Navigation Path, or null for the default provider.
/// True if the Path is added successfully.
public static bool AddNavigationPath(NamespaceInfo nspace, string name, List pages, IPagesStorageProviderV30 provider) {
string namespaceName = nspace != null ? nspace.Name : null;
string fullName = NameTools.GetFullName(namespaceName, name);
if(Exists(fullName)) return false;
if(provider == null) provider = Collectors.PagesProviderCollector.GetProvider(Settings.DefaultPagesProvider);
NavigationPath newPath = provider.AddNavigationPath(namespaceName, name, pages.ToArray());
if(newPath != null) Log.LogEntry("Navigation Path " + fullName + " added", EntryType.General, Log.SystemUsername);
else Log.LogEntry("Creation failed for Navigation Path " + fullName, EntryType.Error, Log.SystemUsername);
return newPath != null;
}
///
/// Removes a Navigation Path.
///
/// The full name of the path to remove.
/// true if the path is removed, false otherwise.
public static bool RemoveNavigationPath(string fullName) {
NavigationPath path = Find(fullName);
if(path == null) return false;
bool done = path.Provider.RemoveNavigationPath(path);
if(done) Log.LogEntry("Navigation Path " + fullName + " removed", EntryType.General, Log.SystemUsername);
else Log.LogEntry("Deletion failed for Navigation Path " + fullName, EntryType.Error, Log.SystemUsername);
return done;
}
///
/// Modifies a Navigation Path.
///
/// The full name of the path to modify.
/// The list of Pages.
/// true if the path is modified, false otherwise.
public static bool ModifyNavigationPath(string fullName, List pages) {
NavigationPath path = Find(fullName);
if(path == null) return false;
NavigationPath newPath = path.Provider.ModifyNavigationPath(path, pages.ToArray());
if(newPath != null) Log.LogEntry("Navigation Path " + fullName + " modified", EntryType.General, Log.SystemUsername);
else Log.LogEntry("Modification failed for Navigation Path " + fullName, EntryType.Error, Log.SystemUsername);
return newPath != null;
}
///
/// Finds all the Navigation Paths that include a Page.
///
/// The Page.
/// The list of Navigation Paths.
public static string[] PathsPerPage(PageInfo page) {
NamespaceInfo pageNamespace = Pages.FindNamespace(NameTools.GetNamespace(page.FullName));
List result = new List(10);
List allPaths = GetNavigationPaths(pageNamespace);
for(int i = 0; i < allPaths.Count; i++) {
List pages = new List(allPaths[i].Pages);
if(pages.Contains(page.FullName)) {
result.Add(allPaths[i].FullName);
}
}
return result.ToArray();
}
}
}