⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xwikistatsserviceimpl.java

📁 xwiki 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * ===================================================================
 *
 * Copyright (c) 2003,2004 Ludovic Dubost, All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details, published at
 * http://www.gnu.org/copyleft/lesser.html or in lesser.txt in the
 * root folder of this distribution.

 * Created by
 * User: Ludovic Dubost
 * Date: 31 juil. 2004
 * Time: 11:58:01
 */
package com.xpn.xwiki.stats.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.buffer.CircularFifoBuffer;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.notify.XWikiActionRule;
import com.xpn.xwiki.notify.XWikiNotificationRule;
import com.xpn.xwiki.stats.api.XWikiStatsService;
import com.xpn.xwiki.store.XWikiHibernateStore;
import com.xpn.xwiki.util.Util;
import com.xpn.xwiki.web.XWikiRequest;
import com.xpn.xwiki.web.Utils;

public class XWikiStatsServiceImpl implements XWikiStatsService {

    private static final Log log = LogFactory.getLog(XWikiStatsServiceImpl.class);

    public static Date expirationDate;
    public static String[] cookieDomains;

    /**
     * Initializes the Stats Service by inserting the notification rule
     * to be notified of all actions on documents
     * @param context
     */
    public void init(XWikiContext context) {
        // Let's init the expirationDate for the cookie
        Calendar cal = Calendar.getInstance();
        cal.set(2030,0,0);
        expirationDate = cal.getTime();

        cookieDomains = StringUtils.split(context.getWiki().Param("xwiki.authentication.cookiedomains"),",");

        // Adding the rule which will allow this module to be called on each page view
        if ("1".equals(context.getWiki().Param("xwiki.stats","1")))
         context.getWiki().getNotificationManager().addGeneralRule(new XWikiActionRule(this));
    }

    /**
     * Gets total statistics on a document for a specific action
     *
     * @param docname fully qualified document named
     * @param action can be "view", "edit", "save", etc..
     * @param context
     * @return DocStats - statistics object
     */
    public DocumentStats getDocTotalStats(String docname, String action, XWikiContext context) {
        return new DocumentStats();
    }

    /**
     * Gets monthly statistics on a document for a specific action
     * @param docname
     * @param action
     * @param month
     * @param context
     * @return
     */
    public DocumentStats getDocMonthStats(String docname, String action, Date month, XWikiContext context) {
        XWikiHibernateStore store = context.getWiki().getHibernateStore();
        DocumentStats object = new DocumentStats(docname, action, month, XWikiStats.PERIOD_MONTH);
        try {
            store.loadXWikiCollection(object, context, true);
            return object;
        } catch (XWikiException e) {
            e.printStackTrace();
            return new DocumentStats();
        }
    }

    /**
     * Gets daily statistics on a document for a specific action
     * @param docname
     * @param action
     * @param day
     * @param context
     * @return
     */
    public DocumentStats getDocDayStats(String docname, String action, Date day, XWikiContext context) {
        return new DocumentStats();
    }

    public List getRefMonthStats(String docname, Date month, XWikiContext context) throws XWikiException {
        XWikiHibernateStore store = context.getWiki().getHibernateStore();
        List solist = store.search("from RefererStats as obj where obj.name='" + Utils.SQLFilter(docname) + "'", 0, 0, context);
        return solist;
    }

    public Collection getRecentActions(String action, int size, XWikiContext context) {
        ArrayList list = new ArrayList();
        if ((action.equals("view")||(action.equals("save")))) {
            HttpSession session = context.getRequest().getSession();
            Collection actions = (Collection) session.getAttribute("recent_" + action);

            if (actions!=null) {
               Object[] actionsarray = actions.toArray();
               CollectionUtils.reverseArray(actionsarray);
               int nb = Math.min(actions.size(), size);
               for (int i=0;i<nb;i++)
                    list.add((String) actionsarray[i]);
               }
        }
        return list;
    }

    /**
     * Notification rule to store usage statistics
     * @param rule
     * @param doc
     * @param action
     * @param context
     */
    public void notify(XWikiNotificationRule rule, XWikiDocument doc, String action, XWikiContext context) {

        if (context.getWiki().isReadOnly()) {
            // the server is in read-only mode, forget about the statistics
            return;
        }
        // Unless this is a "view", "save" or "download" action, we are not interested
        if (!(action.equals("view")||action.equals("save")))
            return;

        // Let's save in the session the last elements view, saved
        if (!action.equals("download")) {
            HttpSession session = context.getRequest().getSession();
            Collection actions = (Collection) session.getAttribute("recent_" + action);
            if (actions==null) {
                actions = new CircularFifoBuffer(context.getWiki().getXWikiPreferenceAsInt("recent_visits_size", 20, context));
                session.setAttribute("recent_" + action, actions);
            }
            String element = context.getDatabase() + ":" + doc.getFullName();
            if (actions.contains(element))
             actions.remove(element);
            actions.add(element);
        }

        // Let's check if this wiki should have statistics disabled
        String statsdefault = context.getWiki().Param("xwiki.stats.default");
        String statsactive = context.getWiki().getXWikiPreference("statistics", "", context);
        if ("0".equals(statsactive))
            return;
        // If nothing is said we use the default parameter
        if (("".equals(statsactive))&&("0".equals(statsdefault)))
            return;


        XWikiHibernateStore store = context.getWiki().getHibernateStore();

        VisitStats vobject = findVisit(context);
            // We count page views in the sessions only for the "view" action
            if (action.equals("view"))
                vobject.incPageViews();
            // We count "save" and "download" actions separately
            else if (action.equals("save"))
                vobject.incPageSaves();
            else if (action.equals("download"))
                vobject.incDownloads();

            vobject.setEndDate(new Date());
            try {
                // In case we have store the old object
                // then we need to remove it
                // before saving the other one
                // because the ID info have changed
                VisitStats oldObject  = vobject.getOldObject();
                if (oldObject!=null) {
                    store.deleteXWikiCollection(oldObject, context, true, true);
                }

                store.saveXWikiCollection(vobject, context, true);
            } catch (XWikiException e) {
                // Statistics should never make xwiki fail !
                e.printStackTrace();
            }

        addPageView(doc.getFullName(), action, XWikiStats.PERIOD_MONTH, store, context, vobject);
        addPageView(doc.getWeb(), action, XWikiStats.PERIOD_MONTH, store, context, vobject);
        addPageView("", action, XWikiStats.PERIOD_MONTH, store, context, vobject);
        addPageView(doc.getFullName(), action, XWikiStats.PERIOD_DAY, store, context, vobject);
        addPageView(doc.getWeb(), action, XWikiStats.PERIOD_DAY, store, context, vobject);
        addPageView("", action, XWikiStats.PERIOD_DAY, store, context, vobject);

        // In case of a "view" action we want to store referer info
        if (action.equals("view")) {
            String referer = getReferer(context);
            if ((referer !=null)&&(!referer.equals(""))) {
                // Visits of the web
                RefererStats robject = new RefererStats(doc.getFullName(), referer, new Date(), XWikiStats.PERIOD_MONTH);
                try {
                    store.loadXWikiCollection(robject, context, true);
                } catch (XWikiException e) {
                }

                robject.incPageViews();
                try {
                    store.saveXWikiCollection(robject, context, true);
                } catch (XWikiException e) {
                    // Statistics should never make xwiki fail !
                    e.printStackTrace();
                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -