internal static bool MakeCategoryUrl(NewsArticlesFriendlyUrlProvider provider, Match categoryUrlMatch, Regex categoryUrlRegex, string friendlyUrlPath, DotNetNuke.Entities.Tabs.TabInfo tab, FriendlyUrlOptions options, TabUrlOptions urlOptions, string cultureCode, ref string endingPageName, ref bool useDnnPagePath, ref List <string> messages, out string categoryUrl) { bool result = false; categoryUrl = null; //this is a url that looks like an category url. We want to modify it and create the new one. string rawId = categoryUrlMatch.Groups["catid"].Value; int categoryId = 0; if (int.TryParse(rawId, out categoryId)) { Hashtable friendlyUrlIndex = null; //the friendly url index is the lookup we use //we have obtained the item Id out of the Url //get the friendlyUrlIndex (it comes from the database via the cache) friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tab.TabID, tab.PortalID, provider, options, urlOptions); if (friendlyUrlIndex != null) { //item urls are indexed with a + category id ("c5") - this is so authors/articles/categories can be mixed and matched string furlkey = "c" + categoryId.ToString(); //create the lookup key for the friendly url index string path = (string)friendlyUrlIndex[furlkey]; //check if in the index if (path == null) { //don't normally expect to have a no-match with a friendly url path when an categoryId was in the Url. //could be a new item that has been created and isn't in the index //do a direct call and find out if it's there //path = UrlController.CheckForMissingNewscategoryItem(categoryId, "category", tab.TabID, tab.PortalID, provider, options, urlOptions, ref messages); } if (path != null) //got a valid path { //url found in the index for this entry. So replace the matched part of the path with the friendly url if (categoryUrlMatch.Groups["l"].Success) //if the path had a leading /, then make sure to add that onto the replacement { path = provider.EnsureLeadingChar("/", path); } /* finish it all off */ messages.Add("Item Friendly Url Replacing : " + friendlyUrlPath + " in Path : " + path); //this is the point where the Url is modified! //replace the path in the path - which leaves any other parts of a path intact. categoryUrl = categoryUrlRegex.Replace(friendlyUrlPath, path);//replace the part in the friendly Url path with it's replacement. //check if this tab is the one specified to not use a path if (provider.NoDnnPagePathTabId == tab.TabID) { useDnnPagePath = false;//make this Url relative from the site root } //set back to default.aspx so that Url Master removes it - just in case it wasn't standard endingPageName = DotNetNuke.Common.Globals.glbDefaultPage; result = true; } } } return(result); }
/// <summary> /// Determines when to do a redirect. This is separate to the rewriting process. The module developer can create any type of Url redirect here, because the entire Url of the original request is passed in. /// </summary> /// <param name="tabId">Identified TabId, if known. -1 if no valid tabid identified.</param> /// <param name="portalid">Identified portalId.</param> /// <param name="httpAlias">Identified httpAlias of the request.</param> /// <param name="requestUri">The original requested Url</param> /// <param name="queryStringCol">The querystring collection of the original request</param> /// <param name="options">The friendly url options that currently apply.</param> /// <param name="redirectLocation">Out parameter that shows where to redirect to.</param> /// <param name="messages">List of messages for debug purposes. Add to this list to help debug your module.</param> /// <returns>true if 301 redirect is required, false if not. If true, the redirectLocation value must be a valid fully qualified Url.</returns> public override bool CheckForRedirect(int tabId, int portalid, string httpAlias, Uri requestUri, System.Collections.Specialized.NameValueCollection queryStringCol, FriendlyUrlOptions options, out string redirectLocation, ref List <string> messages) { bool doRedirect = false; if (messages == null) { messages = new List <string>(); } redirectLocation = "";//set blank location //compare to known pattern of old Urls if (_redirectUrls) { Regex oldNewsRegex = new Regex(@"(&articleType=(?<type>[^&]+))?((&(?<idname>[a-z]*Id)=(?<id>\d+))|((&month=(?<mm>[\d]{1,2}))?&year=(?<yyyy>[\d]{4})))(&(?<pgname>PageId|CurrentPage)=(?<pg>[\d]+))?", RegexOptions.IgnoreCase); Match oldNewsMatch = oldNewsRegex.Match(queryStringCol.ToString()); if (oldNewsMatch.Success) { Group typeGroup = oldNewsMatch.Groups["type"]; Group idNameGroup = oldNewsMatch.Groups["idname"]; Group idGroup = oldNewsMatch.Groups["id"]; Group pageGroup = oldNewsMatch.Groups["pg"]; Group pgNameGrp = oldNewsMatch.Groups["pgname"]; string msg = ""; string id = null; string furlKey = null; string friendlyUrl = null; if (idGroup != null && idGroup.Success) { id = idGroup.Value; } string idType = null; if (typeGroup != null && typeGroup.Success) { idType = typeGroup.Value.ToLower(); } else { if (idNameGroup != null && idNameGroup.Success) { //check if it's the 'ID' value if (idNameGroup.Value.ToLower() == "id") { idType = "id"; } } } //now look at the idType string pagePath = null; if (pgNameGrp != null && pgNameGrp.Success == true && pageGroup != null && pageGroup.Success) { pagePath = pgNameGrp.Value + "/" + pageGroup.Value; } switch (idType) { case "articleview": case "id": msg = "Identified as old-style news article"; //article if (pageGroup != null && pageGroup.Success) { furlKey = "p" + pageGroup.Value; pagePath = null; //taking care of page separately } else { int articleId = -1; //only for items that are in the range of allowed article ids if (int.TryParse(id, out articleId)) { if (articleId >= this.StartingArticleId) { furlKey = "a" + id; } } } break; case "categoryview": msg = "Identified as old-style news category"; furlKey = "c" + id; break; case "archiveview": //get the mm and yyyy msg = "Identified as old-style news archive"; Group yyyyGrp = oldNewsMatch.Groups["yyyy"]; if (yyyyGrp != null && yyyyGrp.Success) { string yyyy = yyyyGrp.Value; string mm = null; Group mmGrp = oldNewsMatch.Groups["mm"]; if (mmGrp != null && mmGrp.Success) { mm = mmGrp.Value; } friendlyUrl = yyyy; if (mm != null) { friendlyUrl += "/" + mm; } } break; case "authorview": msg = "Identified as old-style news author"; furlKey = "u" + id; break; } if (furlKey != null) { //now lookup the friendly url index TabUrlOptions urlOptions = GetTabUrlOptions(tabId); Hashtable friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tabId, portalid, this, options, urlOptions); if (friendlyUrlIndex != null && friendlyUrlIndex.ContainsKey(furlKey)) { //look up the index for the item if we don't already have a friendly Url friendlyUrl = (string)friendlyUrlIndex[furlKey]; } } if (friendlyUrl != null) { //now merge with the friendly url for the selected page DotNetNuke.Entities.Portals.PortalAliasInfo alias = DotNetNuke.Entities.Portals.PortalAliasController.Instance.GetPortalAlias(httpAlias); DotNetNuke.Entities.Portals.PortalSettings ps = new DotNetNuke.Entities.Portals.PortalSettings(tabId, alias); if (pagePath != null) { friendlyUrl += this.EnsureLeadingChar("/", pagePath); } string baseUrl = ""; if (_noDnnPagePathTabId == tabId) { baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + httpAlias + this.EnsureLeadingChar("/", friendlyUrl) + options.PageExtension;//put onto http Alias with no page path } else { baseUrl = DotNetNuke.Common.Globals.NavigateURL(tabId, ps, "", friendlyUrl); //add on with page path } if (baseUrl != null) { redirectLocation = baseUrl; doRedirect = true; msg += ", found friendly url " + friendlyUrl + ", redirecting"; messages.Add(msg); } } } } return(doRedirect); }
/// <summary> /// Creates a friendly article url, depending on the options /// </summary> /// <param name="provider"></param> /// <param name="articleUrlMatch"></param> /// <param name="articleUrlRegex"></param> /// <param name="friendlyUrlPath"></param> /// <param name="tab"></param> /// <param name="options"></param> /// <param name="urlOptions"></param> /// <param name="cultureCode"></param> /// <param name="endingPageName"></param> /// <param name="useDnnPagePath"></param> /// <param name="messages"></param> /// <param name="articleUrl"></param> /// <returns></returns> internal static bool MakeArticleUrl(NewsArticlesFriendlyUrlProvider provider, Match articleUrlMatch, Regex articleUrlRegex, string friendlyUrlPath, DotNetNuke.Entities.Tabs.TabInfo tab, FriendlyUrlOptions options, TabUrlOptions urlOptions, string cultureCode, ref string endingPageName, ref bool useDnnPagePath, ref List <string> messages, out string articleUrl) { bool result = false; articleUrl = null; //this is a url that looks like an article url. We want to modify it and create the new one. string rawId = articleUrlMatch.Groups["artid"].Value; int articleId = 0; if (int.TryParse(rawId, out articleId) && (provider.StartingArticleId <= articleId)) { Hashtable friendlyUrlIndex = null; //the friendly url index is the lookup we use //we have obtained the item Id out of the Url //get the friendlyUrlIndex (it comes from the database via the cache) friendlyUrlIndex = UrlController.GetFriendlyUrlIndex(tab.TabID, tab.PortalID, provider, options, urlOptions); if (friendlyUrlIndex != null) { string furlkey = null; int pageId = -1; //first check if we are seeking page or article if (articleUrlMatch.Groups["pageid"].Success) { //page item urls are index by p + page id. But we only use this if it is present //ie pages override articles when both are present string rawPageId = articleUrlMatch.Groups["pageid"].Value; if (int.TryParse(rawPageId, out pageId)) { furlkey = "p" + rawPageId; } } else { //item urls are indexed with a + articleId ("a5") - this is so we could mix and match entities if necessary furlkey = "a" + articleId.ToString(); //create the lookup key for the friendly url index } string path = (string)friendlyUrlIndex[furlkey]; //check if in the index if (path == null) { //don't normally expect to have a no-match with a friendly url path when an articleId was in the Url. //could be that the page id is bunk - in that case, just use the article Id if (furlkey.Contains("p")) { furlkey = "a" + articleId.ToString(); //create the lookup key for the friendly url index path = (string)friendlyUrlIndex[furlkey]; //check if in the index } if (path == null) { //could be a new item that has been created and isn't in the index //do a direct call and find out if it's there path = UrlController.CheckForMissingNewsArticleItem(articleId, "article", tab.TabID, tab.PortalID, provider, options, urlOptions, ref messages); } } if (path != null) //got a valid path { //url found in the index for this entry. So replace the matched part of the path with the friendly url if (articleUrlMatch.Groups["l"].Success) //if the path had a leading /, then make sure to add that onto the replacement { path = provider.EnsureLeadingChar("/", path); } /* finish it all off */ messages.Add("Item Friendly Url Replacing : " + friendlyUrlPath + " in Path : " + path); //this is the point where the Url is modified! //replace the path in the path - which leaves any other parts of a path intact. articleUrl = articleUrlRegex.Replace(friendlyUrlPath, path);//replace the part in the friendly Url path with it's replacement. //check if this tab is the one specified to not use a path if (provider.NoDnnPagePathTabId == tab.TabID) { useDnnPagePath = false;//make this Url relative from the site root } //set back to default.aspx so that Url Master removes it - just in case it wasn't standard endingPageName = DotNetNuke.Common.Globals.glbDefaultPage; result = true; } } } return(result); }