private function run()
 {
     global $wgRequest;
     wfProfileIn(__METHOD__);
     if (preg_match('/^[a-zA-Z]+$/', $this->mAction)) {
         $func = "__" . $this->mAction;
         if (method_exists($this, $func)) {
             $this->mStats = WikiStats::newFromId($this->mCityId);
             $this->mResult = $this->{$func}();
         }
     }
     wfProfileOut(__METHOD__);
 }
 public function execute($subpage)
 {
     global $wgUser, $wgOut, $wgRequest, $wgCityId, $wgDBname, $wgLang;
     if ($wgUser->isBlocked()) {
         throw new UserBlockedError($this->getUser()->mBlock);
     }
     if (wfReadOnly()) {
         $wgOut->readOnlyPage();
         return;
     }
     // Set the current wiki ID, DB name and user from globals
     $this->mCityId = $wgCityId;
     $this->mCityDBName = $wgDBname;
     $this->mUser = $wgUser;
     // Check the current $wgUser against the set of groups WikiStats recognizes
     $this->userIsSpecial = WikiStats::isAllowed();
     $this->mFromDate = $wgRequest->getVal("wsfrom", WIKISTATS_MIN_STATS_YEAR . WIKISTATS_MIN_STATS_MONTH);
     $this->mToDate = $wgRequest->getVal("wsto", date("Ym"));
     $this->mTitle = Title::makeTitle(NS_SPECIAL, "WikiStats");
     $this->mAction = $wgRequest->getVal("action", "");
     $this->mXLS = $wgRequest->getVal("wsxls", false);
     $this->mMonth = $wgRequest->getVal("wsmonth", 0);
     $this->mLimit = $wgRequest->getVal("wslimit", WIKISTATS_WIKIANS_RANK_NBR);
     $this->mAllWikis = 0;
     // Use the first part of the subpage as the action
     if ($subpage) {
         $path = explode("/", $subpage);
         $this->mAction = $path[0];
     }
     // Redirect to the default action if one hasn't been set
     if (empty($this->mAction)) {
         $wgOut->redirect($this->mTitle->getFullURL("action={$this->defaultAction}"));
     }
     // Split out the the from and to month and year for convenience
     if (preg_match("/^([0-9]{4})([0-9]{1,2})/", $this->mFromDate, $m)) {
         list(, $this->fromYear, $this->fromMonth) = $m;
     } else {
         $wgOut->showErrorPage("Bad parameters", "wikistats_error_malformed_date");
         return;
     }
     if (preg_match("/^([0-9]{4})([0-9]{1,2})/", $this->mToDate, $m)) {
         list(, $this->toYear, $this->toMonth) = $m;
     } else {
         $wgOut->showErrorPage("Bad parameters", "wikistats_error_malformed_date");
         return;
     }
     $domain = $all = "";
     if ($this->userIsSpecial) {
         $this->mLang = $wgRequest->getVal("wslang", "");
         $this->mHub = $wgRequest->getVal("wscat", "");
         $this->mNS = $wgRequest->getIntArray("wsns", "");
         $domain = $wgRequest->getVal("wswiki", "");
         $all = $wgRequest->getVal("wsall", 0);
         $this->mNamespaces = $wgLang->getNamespaces();
     }
     // Override some values if we're special and got a domain (or 'all')
     if ($domain == 'all' || $all == 1) {
         $this->mCityId = 0;
         $this->mCityDBName = WIKISTATS_CENTRAL_ID;
         $this->mCityDomain = 'all';
         $this->mAllWikis = 1;
     } elseif (!empty($domain) && $this->userIsSpecial == 1) {
         $this->mCityId = WikiFactory::DomainToId($domain);
         $this->mCityDBName = WikiFactory::IDToDB($this->mCityId);
         $this->mCityDomain = $domain;
     } else {
         $this->mCityDomain = WikiFactory::DBToDomain($this->mCityDBName);
     }
     $this->mStats = WikiStats::newFromId($this->mCityId);
     $this->mPredefinedNamespaces = $this->mStats->getPageNSList();
     $this->mStats->setStatsDate(array('fromMonth' => $this->fromMonth, 'fromYear' => $this->fromYear, 'toMonth' => $this->toMonth, 'toYear' => $this->toYear));
     $this->mStats->setHub($this->mHub);
     $this->mStats->setLang($this->mLang);
     #---
     $this->mSkin = RequestContext::getMain()->getSkin();
     if (is_object($this->mSkin)) {
         $skinname = get_class($this->mSkin);
         $skinname = strtolower(str_replace("Skin", "", $skinname));
         $this->mSkinName = $skinname;
     }
     $this->showForm();
     if ($this->mAction) {
         $func = 'show' . ucfirst(strtolower($this->mAction));
         if (method_exists($this, $func)) {
             $this->{$func}($subpage);
         }
     }
 }
 public function getWikiActivity($params = array(), $xls = 0)
 {
     global $wgLang, $wgStatsDB, $wgUser, $wgMemc, $wgStatsDBEnabled;
     wfProfileIn(__METHOD__);
     if (empty($params)) {
         wfProfileOut(__METHOD__);
         return false;
     }
     if (empty($wgStatsDBEnabled)) {
         wfProfileOut(__METHOD__);
         return false;
     }
     # only for special users
     if (!WikiStats::isAllowed()) {
         Wikia::log(__METHOD__, false, "unauthorized user: "******" tried to retrieve data");
         wfProfileOut(__METHOD__);
         return false;
     }
     $data = array('cnt' => 0, 'res' => null);
     $year = isset($params['year']) ? intval($params['year']) : date('Y');
     $month = isset($params['month']) ? intval($params['month']) : date('m');
     $lang = isset($params['lang']) ? $params['lang'] : '';
     $cat = isset($params['cat']) ? $params['cat'] : '';
     $order = isset($params['order']) ? $params['order'] : '';
     $limit = isset($params['limit']) ? intval($params['limit']) : self::DEF_LIMIT;
     $offset = isset($params['offset']) ? intval($params['offset']) : 0;
     $summary = isset($params['summary']) ? intval($params['summary']) : 0;
     $lang_id = WikiFactory::LangCodeToId($lang);
     if (empty($lang_id)) {
         Wikia::log(__METHOD__, false, "invalid language code: {$lang}");
         wfProfileOut(__METHOD__);
         return false;
     }
     $dbr = wfGetDB(DB_SLAVE, 'stats', $wgStatsDB);
     # order
     $orderOptions = array('id' => 'wiki_id', 'dbname' => 'city_dbname', 'title' => 'city_title', 'url' => 'city_url', 'users' => 'users_all', 'edits' => 'articles_edits', 'articles' => 'articles', 'lastedit' => 'city_last_timestamp');
     $sql_order = 'null';
     if (!empty($order)) {
         $options = array();
         $tmp = explode('|', $order);
         if (!empty($tmp)) {
             foreach ($tmp as $inx) {
                 @(list($val, $desc) = explode(':', $inx));
                 $options[] = $orderOptions[$val] . " " . $desc;
             }
         }
         if (!empty($options)) {
             $sql_order = implode(',', $options);
         }
     }
     # tables
     if ($summary) {
         $tables = array('lang_monthly_stats');
     } else {
         $tables = array('wikia_monthly_stats', 'wikicities.city_list AS cl', 'wikicities.city_cat_mapping AS ccm');
     }
     #conditions
     $conditions = $join = array();
     if ($summary) {
         $conditions = array('stats_date' => sprintf("%04d%02d", $year, $month), 'wiki_lang_id' => $lang_id);
     } else {
         $conditions = array('stats_date' => sprintf("%04d%02d", $year, $month), 'city_public' => 1);
         if (!empty($lang)) {
             $conditions['cl.city_lang'] = $lang;
         }
         if (!empty($cat)) {
             $conditions['ccm.cat_id'] = $cat;
         }
         # join
         $join = array('wikicities.city_list AS cl' => array('JOIN', 'cl.city_id = wiki_id'), 'wikicities.city_cat_mapping AS ccm' => array('JOIN', 'ccm.city_id = wiki_id'));
     }
     $memkey = sprintf("count_%s_%s_%s_%d", __METHOD__, implode('_', array_keys($conditions)), implode('_', array_values($conditions)), $xls);
     $data['cnt'] = $wgMemc->get($memkey);
     /* number of records */
     if ($summary) {
         $data['cnt'] = 1;
     } else {
         if (empty($data['cnt'])) {
             $oRow = $dbr->selectRow($tables, array('count(0) as cnt'), $conditions, __METHOD__, '', $join);
             if (is_object($oRow)) {
                 $data['cnt'] = $oRow->cnt;
             }
             $wgMemc->set($memkey, $data['cnt'], 60 * 60 * 3);
         }
     }
     if ($data['cnt'] > 0) {
         $memkey = sprintf("acdata_%s_%s_%d", __METHOD__, $year . '_' . $month . '_' . $lang . '_' . $cat . '_' . $order . '_' . $limit . '_' . $offset . '_' . $wgLang->getCode() . '_' . $summary, $xls);
         $data['res'] = $wgMemc->get($memkey);
         if (empty($data['res'])) {
             $data['res'] = array();
             # order & limit
             $order = $xls == 1 ? array() : array('ORDER BY' => $sql_order, 'LIMIT' => $limit, 'OFFSET' => $offset);
             if ($summary) {
                 $oRes = $dbr->select($tables, array('0 as city_id', '\'\' as city_dbname', '\'\' as city_title', '\'\' as city_url', 'users_all', 'articles', 'articles_edits', 'ts'), $conditions, __METHOD__);
             } else {
                 $oRes = $dbr->select($tables, array('cl.city_id', 'city_dbname', 'city_title', 'city_url', 'users_all', 'articles', 'articles_edits', 'city_last_timestamp as ts'), $conditions, __METHOD__, $order, $join);
             }
             while ($oRow = $dbr->fetchObject($oRes)) {
                 $data['res'][$summary ? 0 : $oRow->city_id] = array($summary ? 0 : $oRow->city_id, $summary ? wfMsg('wikistats_summary_data') : $oRow->city_dbname, $summary ? wfMsg('wikistats_summary_data') : $oRow->city_title, $summary ? wfMsg('wikistats_summary_data') : $oRow->city_url, $oRow->users_all, $oRow->articles_edits, $oRow->articles, $summary ? '' : $wgLang->timeanddate($oRow->ts), 0, 0, 0);
             }
             $dbr->freeResult($oRes);
             if (!empty($data['res']) && $xls == 0) {
                 $prev_year = $year;
                 $prev_month = $month - 1;
                 if ($prev_month == 0) {
                     $prev_month = 12;
                     $prev_year--;
                 }
                 if ($summary) {
                     $conditions['stats_date'] = sprintf("%04d%02d", $prev_year, $prev_month);
                     $oRes = $dbr->select($tables, array('0 as wiki_id', 'users_all', 'articles', 'articles_edits'), $conditions, __METHOD__);
                 } else {
                     $where = array('stats_date' => sprintf("%04d%02d", $prev_year, $prev_month), 'wiki_id' => array_keys($data['res']));
                     $oRes = $dbr->select(array('wikia_monthly_stats'), array('wiki_id', 'users_all', 'articles', 'articles_edits'), $where, __METHOD__);
                 }
                 while ($oRow = $dbr->fetchObject($oRes)) {
                     $data['res'][$oRow->wiki_id][8] = $oRow->users_all > $data['res'][$oRow->wiki_id][4] ? 1 : ($oRow->users_all < $data['res'][$oRow->wiki_id][4] ? -1 : 0);
                     $data['res'][$oRow->wiki_id][9] = $oRow->articles_edits > $data['res'][$oRow->wiki_id][5] ? 1 : ($oRow->articles_edits < $data['res'][$oRow->wiki_id][5] ? -1 : 0);
                     $data['res'][$oRow->wiki_id][10] = $oRow->articles > $data['res'][$oRow->wiki_id][6] ? 1 : ($oRow->articles < $data['res'][$oRow->wiki_id][6] ? -1 : 0);
                 }
                 $dbr->freeResult($oRes);
             }
             $wgMemc->set($memkey, $data['res'], 60 * 60 * 3);
         }
     }
     wfProfileOut(__METHOD__);
     return $data;
 }