Fixes 27460, Remove references to old umbracoStats code

Fixes 27109, Issue with GetMedia and the new schema
Implements 27473, GetMedia and GetMember caching in Umbraco.Library

[TFS Changeset #67795]
This commit is contained in:
hartvig
2010-06-10 19:47:25 +00:00
parent 9dccbea95d
commit 64f515f25d
7 changed files with 28 additions and 408 deletions

View File

@@ -64,6 +64,9 @@
<!-- The html injected into a (x)html page if Umbraco is running in preview mode -->
<PreviewBadge><![CDATA[<a id="umbracoPreviewBadge" style="position: absolute; top: 0; right: 0; border: 0; width: 149px; height: 149px; background: url('{1}/preview/previewModeBadge.png') no-repeat;" href="{0}/endPreview.aspx?redir={2}"><span style="display:none;">In Preview Mode - click to end</span></a>]]></PreviewBadge>
<!-- Cache cycle of Media and Member data fetched from the umbraco.library methods -->
<!-- In seconds. 0 will disable cache -->
<UmbracoLibraryCacheDuration>1800</UmbracoLibraryCacheDuration>
</content>
<requestHandler>

View File

@@ -1,38 +0,0 @@
using System;
namespace umbraco.cms.businesslogic.stat
{
/// <summary>
/// Summary description for Entry.
/// </summary>
public class Entry
{
private int _currentPage;
private int _lastPage;
private System.DateTime _entryTime;
public Entry(int CurrentPage, int LastPage)
{
_entryTime = System.DateTime.Now;
_currentPage = CurrentPage;
_lastPage = LastPage;
}
public int CurrentPage
{
get {return _currentPage;}
set {_currentPage = value;}
}
public int LastPage
{
get {return _lastPage;}
set {_lastPage = value;}
}
public DateTime EntryTime
{
get {return _entryTime;}
set {_entryTime = value;}
}
}
}

View File

@@ -1,330 +0,0 @@
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using umbraco.cms.businesslogic.member;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
namespace umbraco.cms.businesslogic.stat
{
/// <summary>
/// Summary description for Session.
/// </summary>
///
public class Session
{
#region Declarations
private static Hashtable _sessions = Hashtable.Synchronized(new Hashtable());
private ArrayList _entries = new ArrayList();
private Guid _sessionId = Guid.NewGuid();
private Guid _member;
private int _newsletter = 0;
private bool _returningUser = false;
private bool _cookies = false;
private DateTime _sessionStart;
private string _language = "";
private string _userAgent = "";
private string _browserBrand = "unknown";
private string _browserVersion = "unknown";
private string _browserType = "unknown";
private string _os = "unknown";
private string _ip = "";
private string _referrer = "";
private string _referrerKeyword = "";
private int _sqlSessionId;
private string _visitorId;
private bool _isHuman = true;
#endregion
protected static ISqlHelper SqlHelper
{
get { return Application.SqlHelper; }
}
public static string Request(string key)
{
if(HttpContext.Current.Request[key] != null)
return HttpContext.Current.Request[key];
else
return "";
}
public Session()
{
HttpRequest currentRequest = HttpContext.Current.Request;
// Update member id
if(Member.IsLoggedOn())
_member = new Member(Member.CurrentMemberId()).UniqueId;
// TODO: Newsletter id
if(Request("umbNl") != "")
_newsletter = int.Parse(Request("umbNl"));
// Add session start time
_sessionStart = DateTime.Now;
// Returning user
if(currentRequest.Cookies["umbracoReturningUser"] != null)
if(bool.Parse(currentRequest.Cookies["umbracoReturningUser"].Value))
_returningUser = true;
// User id
if(currentRequest.Cookies["umbracoVisitorId"] != null && !string.IsNullOrEmpty(currentRequest.Cookies["umbracoVisitorId"].Value))
_visitorId = currentRequest.Cookies["umbracoVisitorId"].Value;
else
_visitorId = Guid.NewGuid().ToString();
// Useragent
if(currentRequest.UserAgent == null)
_userAgent = "Unknown";
else
_userAgent = currentRequest.UserAgent.ToLower();
// System OS and Browser (based on useragent)
if(currentRequest.Browser != null)
{
_os = currentRequest.Browser.Platform;
_browserBrand = currentRequest.Browser.Browser;
_browserVersion = currentRequest.Browser.Version;
_browserType = currentRequest.Browser.Type;
// Cookies
if(currentRequest.Browser.Cookies)
_cookies = true;
}
// Detect bots
if(_userAgent.IndexOf("bot") > -1 || _browserBrand.ToLower() == "unknown")
_isHuman = false;
// Language
if(currentRequest.UserLanguages != null)
{
if(currentRequest.UserLanguages.Length > -1)
_language = currentRequest.UserLanguages[0];
}
// IP
_ip = currentRequest.UserHostAddress;
// Referer and keyword
if(currentRequest.UrlReferrer != null)
{
_referrer = currentRequest.UrlReferrer.ToString();
_referrerKeyword = findKeyword(_referrer);
}
this.LogStatSession();
// Add cookies
if(Array.IndexOf(HttpContext.Current.Response.Cookies.AllKeys, "umbracoVisitorId") > -1)
HttpContext.Current.Response.Cookies.Remove("umbracoVisitorId");
if(Array.IndexOf(HttpContext.Current.Response.Cookies.AllKeys, "umbracoReturningUser") > -1)
HttpContext.Current.Response.Cookies.Remove("umbracoReturningUser");
// Add cookie with visitor-id
HttpCookie c = new HttpCookie("umbracoVisitorId", _visitorId);
c.Value = _visitorId;
c.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(c);
// Add cookie with returning user
HttpCookie c2 = new HttpCookie("umbracoReturningUser", "true");
c2.Value = "true";
c2.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(c2);
// Check for old session id
if(HttpContext.Current.Session["umbracoSessionId"] != null)
{
_sessionId = new Guid(HttpContext.Current.Session["umbracoSessionId"].ToString());
// If it exists in the servers session collection, we need to remove it
if(_sessions.ContainsKey(_sessionId))
_sessions.Remove(_sessionId);
}
else
{
// Add guid to visitor
HttpContext.Current.Session.Add("umbracoSessionId", _sessionId.ToString());
/* System.Web.HttpCookie c = new System.Web.HttpCookie("umbracoSessionId");
c.Expires = DateTime.Now.AddDays(1);
c.Value = _sessionId.ToString();
System.Web.HttpContext.Current.Response.Cookies.Add(c);
*/
}
_sessions.Add(_sessionId, this);
}
private void LogStatSession()
{
// Save to db
IParameter[] sqlParams = {
SqlHelper.CreateParameter("@MemberId", this._member),
SqlHelper.CreateParameter("@NewsletterId", this._newsletter),
SqlHelper.CreateParameter("@ReturningUser", this._returningUser),
SqlHelper.CreateParameter("@SessionStart", this._sessionStart),
SqlHelper.CreateParameter("@Language", this._language),
SqlHelper.CreateParameter("@UserAgent", this._userAgent),
SqlHelper.CreateParameter("@Browser", this._browserBrand),
SqlHelper.CreateParameter("@BrowserVersion", this._browserVersion),
SqlHelper.CreateParameter("@OperatingSystem", this._os),
SqlHelper.CreateParameter("@Cookies", this._cookies),
SqlHelper.CreateParameter("@Ip", this._ip),
SqlHelper.CreateParameter("@Referrer", this._referrer),
SqlHelper.CreateParameter("@ReferrerKeyword", this._referrerKeyword),
SqlHelper.CreateParameter("@visitorId", this._visitorId),
SqlHelper.CreateParameter("@browserType", this._browserType),
SqlHelper.CreateParameter("@isHuman", this._isHuman)
};
WaitCallback callback =
delegate
{
try
{
// cannot synchronize inline delegate, so lock on type
lock (GetType())
{
SqlHelper.ExecuteNonQuery("INSERT INTO umbracoStatSession (MemberId, NewsletterId, ReturningUser, SessionStart, [Language], UserAgent, Browser, BrowserVersion, OperatingSystem, Ip, Referrer, ReferrerKeyword, allowCookies, browserType, visitorId, isHuman) VALUES (@MemberId, @NewsletterId, @ReturningUser, @SessionStart, @Language, @UserAgent, @Browser, @BrowserVersion, @OperatingSystem, @Ip, @Referrer, @ReferrerKeyword, @Cookies, @browserType, @visitorId, @isHuman)", sqlParams);
this._sqlSessionId = SqlHelper.ExecuteScalar<int>("SELECT MAX(id) FROM umbracoStatSession");
}
}
catch(Exception e)
{
Debug.WriteLine("Error writing to db - umbracoStatSession: " + e);
}
};
if (GlobalSettings.EnableAsyncStatLogging)
{
ThreadPool.QueueUserWorkItem(callback);
}
else
{
callback(null);
}
}
public void EndSession()
{
updateSessionTime();
// Remove from hashtable
_sessions.Remove(this._sessionId);
}
private void updateSessionTime()
{
WaitCallback callback =
delegate
{
try
{
SqlHelper.ExecuteNonQuery(
"update umbracoStatSession set sessionEnd = @sessionEnd where id = @id",
SqlHelper.CreateParameter("@sessionEnd", DateTime.Now),
SqlHelper.CreateParameter("@id", _sqlSessionId));
}
catch(Exception e)
{
Debug.WriteLine("Error writing to db - umbracoStatSession: " + e);
}
};
// Save end time to db
if(GlobalSettings.EnableAsyncStatLogging)
ThreadPool.QueueUserWorkItem(callback);
else
callback(null);
}
private string findKeyword(string refString)
{
Match keyword = Regex.Match(refString, @"[\?|\&]q=([^\&]*)|[\?|\&]query=([^\&]*)|[\?|\&]p=([^\&]*)",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
if(keyword.Success)
if(keyword.Groups[0].Value != "")
return keyword.Groups[0].Value.Trim().Replace("?q=", "").Replace("&q=", "");
else if(keyword.Groups[1].Value != "")
return keyword.Groups[1].Value.Trim().Replace("?query=", "").Replace("&query=", "");
else if(keyword.Groups[2].Value != "")
return keyword.Groups[2].Value.Trim().Replace("?p=", "").Replace("&p=", "");
else
// will never be called
return "";
else
return "";
}
public void AddEntry(int CurrentPage)
{
int LastPage;
if(_entries.Count > 0)
LastPage = ((Entry)_entries[_entries.Count - 1]).CurrentPage;
else
LastPage = 0;
Entry e = new Entry(CurrentPage, LastPage);
_entries.Add(e);
WaitCallback callback =
delegate
{
try
{
SqlHelper.ExecuteNonQuery(
"insert into umbracoStatEntry (SessionId, EntryTime, RefNodeId, NodeId) values (@SessionId, @EntryTime, @RefNodeId, @NodeId)",
SqlHelper.CreateParameter("@SessionId", _sqlSessionId),
SqlHelper.CreateParameter("@EntryTime", e.EntryTime),
SqlHelper.CreateParameter("@RefNodeId", e.LastPage),
SqlHelper.CreateParameter("@NodeId", e.CurrentPage));
}
catch(Exception e1)
{
Debug.WriteLine("Error writing to db - umbracoStatEntry: " + e1);
}
};
// Save to db
if (GlobalSettings.EnableAsyncStatLogging)
ThreadPool.QueueUserWorkItem(callback);
else
callback(null);
updateSessionTime();
}
public static void AddEntry(Guid SessionId, int CurrentPage)
{
// If session doesn't exists then add it
if(_sessions.ContainsKey(SessionId))
{
Session session = _sessions[SessionId] as Session;
if(session != null)
session.AddEntry(CurrentPage);
}
}
public static void EndSession(Guid SessionId)
{
// If session doesn't exists then add it
if(_sessions.ContainsKey(SessionId))
{
Session session = _sessions[SessionId] as Session;
if(session != null)
session.EndSession();
}
}
}
}

View File

@@ -5,7 +5,6 @@ using System.Threading;
using umbraco.BusinessLogic;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.businesslogic.datatype.controls;
using umbraco.cms.businesslogic.stat;
namespace umbraco
{
@@ -19,21 +18,6 @@ namespace umbraco
}
protected virtual void Session_Start(Object sender, EventArgs e)
{
if (GlobalSettings.EnableStat)
{
try
{
new Session();
}
catch (Exception state)
{
Log.Add(LogTypes.Error, BusinessLogic.User.GetUser(0), -1, "Error initializing stat: " + state);
}
}
}
protected virtual void Application_BeginRequest(object sender, EventArgs e)
{

View File

@@ -146,6 +146,11 @@ namespace umbraco
{
// Clear macro cache
Cache.ClearCacheObjectTypes("umbraco.MacroCacheContent");
// Clear library cache
if (UmbracoSettings.UmbracoLibraryCacheDuration > 0)
{
Cache.ClearCacheObjectTypes("MS.Internal.Xml.XPath.XPathSelectionIterator");
}
requestHandler.ClearProcessedRequests();
_xmlContent = value;
@@ -1014,21 +1019,28 @@ order by umbracoNode.level, umbracoNode.sortOrder";
/* Alex Norcliffe 2010 06 03 - removing all launching of ThreadPool threads, instead we just
* flag on the context that the Xml should be saved and an event in the requestModule
* will check for this and call PersistXmlToFile() if necessary */
HttpContext.Current.Items[PersistenceFlagContextKey] = true;
if (HttpContext.Current != null)
{
if (!HttpContext.Current.Items.Contains(PersistenceFlagContextKey))
HttpContext.Current.Items.Add(PersistenceFlagContextKey, null);
HttpContext.Current.Items[PersistenceFlagContextKey] = true;
}
else
{
//// Save copy of content
if (UmbracoSettings.CloneXmlCacheOnPublish)
{
XmlDocument xmlContentCopy = CloneXmlDoc(_xmlContent);
//// Save copy of content
//if (UmbracoSettings.CloneXmlCacheOnPublish)
//{
// XmlDocument xmlContentCopy = CloneXmlDoc(_xmlContent);
ThreadPool.QueueUserWorkItem(
delegate { PersistXmlToFile(xmlContentCopy); });
// ThreadPool.QueueUserWorkItem(
// delegate { PersistXmlToFile(xmlContentCopy); });
//}
//else
// ThreadPool.QueueUserWorkItem(
// delegate { PersistXmlToFile(); });
}
else
ThreadPool.QueueUserWorkItem(
delegate { PersistXmlToFile(); });
}
}
internal bool IsXmlQueuedForPersistenceToFile

View File

@@ -226,18 +226,6 @@ namespace umbraco
umbPageHolder.Populate(m_umbPage);
}
// Stat
if (GlobalSettings.EnableStat)
{
if (Session["umbracoSessionId"] != null)
{
// If session just has been initialized we should use the cookie from the response object
if (Session["umbracoSessionId"] == null)
cms.businesslogic.stat.Session.AddEntry(new Guid(Session["umbracoSessionId"].ToString()), m_umbPage.PageID);
else
cms.businesslogic.stat.Session.AddEntry(new Guid(Session["umbracoSessionId"].ToString()), m_umbPage.PageID);
}
}
}
else
{

View File

@@ -400,6 +400,7 @@
<Compile Include="umbraco\cache\factory.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="umbraco\cache\libraryRefreshers.cs" />
<Compile Include="umbraco\cache\pageRefresher.cs">
<SubType>Code</SubType>
</Compile>