/**
  * Gives out a system gift with the ID of $gift_id, purges memcached and
  * optionally sends out e-mail to the user about their new system gift.
  *
  * @param $gift_id Integer: ID number of the system gift
  * @param $email Boolean: true to send out notification e-mail to users,
  *							otherwise false
  */
 public function sendSystemGift($gift_id, $email = true)
 {
     global $wgMemc;
     $g = new SystemGifts();
     $repeatableGift = $g->getRepeatableGifts();
     $gift_info = SystemGifts::getGift($gift_id);
     if ($gift_info) {
         $gift_category = $gift_info['gift_category'];
     }
     if (!in_array($gift_category, $repeatableGift)) {
         if ($this->doesUserHaveGift($this->user_id, $gift_id)) {
             return '';
         }
     }
     //is bot
     $user = User::newFromId($this->user_id);
     $user_group = $user->getEffectiveGroups();
     if (!in_array('bot', $user_group) && !in_array('bot-global', $user_group)) {
         $dbw = wfGetDB(DB_MASTER);
         $dbw->insert('user_system_gift', array('sg_gift_id' => $gift_id, 'sg_user_id' => $this->user_id, 'sg_user_name' => $this->user_name, 'sg_status' => 1, 'sg_date' => date('Y-m-d H:i:s')), __METHOD__);
         $sg_gift_id = $dbw->insertId();
         self::incGiftGivenCount($gift_id);
         // Add to new gift count cache for receiving user
         $this->incNewSystemGiftCount($this->user_id);
         if ($email && !empty($sg_gift_id)) {
             $this->sendGiftNotificationEmail($this->user_id, $sg_gift_id);
         }
         $wgMemc->delete(wfForeignMemcKey('huiji', '', 'user', 'profile', 'system_gifts', $this->user_id));
         return $sg_gift_id;
     } else {
         return '';
     }
 }
 /**
  * Show the special page
  *
  * @param $par Mixed: parameter passed to the page or null
  */
 public function execute($par)
 {
     $out = $this->getOutput();
     $request = $this->getRequest();
     $user = $this->getUser();
     // Set the page title, robot policies, etc.
     $this->setHeaders();
     // If the user doesn't have the required 'awardsmanage' permission, display an error
     if (!$user->isAllowed('awardsmanage')) {
         $out->permissionRequired('awardsmanage');
         return;
     }
     // Show a message if the database is in read-only mode
     if (wfReadOnly()) {
         $out->readOnlyPage();
         return;
     }
     // If user is blocked, s/he doesn't need to access this page
     if ($user->isBlocked()) {
         $out->blockedPage();
         return;
     }
     // Add CSS
     $out->addModuleStyles('ext.socialprofile.systemgifts.css');
     if ($request->wasPosted()) {
         $g = new SystemGifts();
         $gift_category = $request->getVal('gift_category');
         $un_update = $g->getRepeatableGifts();
         if (!$request->getInt('id')) {
             // Add the new system gift to the database
             $gift_id = $g->addGift($request->getVal('gift_name'), $request->getVal('gift_description'), $request->getVal('gift_category'), $request->getInt('gift_threshold'));
             $out->addHTML('<span class="view-status">' . $this->msg('ga-created')->plain() . '</span><br /><br />');
         } else {
             $gift_id = $request->getInt('id');
             $g->updateGift($gift_id, $request->getVal('gift_name'), $request->getVal('gift_description'), $request->getVal('gift_category'), $request->getInt('gift_threshold'));
             $out->addHTML('<span class="view-status">' . $this->msg('ga-saved')->plain() . '</span><br /><br />');
         }
         if (!in_array($gift_category, $un_update)) {
             $g->update_system_gifts($gift_id);
         }
         $out->addHTML($this->displayForm($gift_id));
     } else {
         $gift_id = $request->getInt('id');
         if ($gift_id || $request->getVal('method') == 'edit') {
             $out->addHTML($this->displayForm($gift_id));
         } else {
             $out->addHTML('<div><b><a href="' . htmlspecialchars($this->getPageTitle()->getFullURL('method=edit')) . '">' . $this->msg('ga-addnew')->plain() . '</a></b></div>');
             $out->addHTML($this->displayGiftList());
         }
     }
 }
 /**
  * Increase a given social statistic field by $val.
  *
  * @param $field String: field name in user_stats database table
  * @param $val Integer: increase $field by this amount, defaults to 1
  */
 function incStatField($field, $val = 1)
 {
     global $wgUser, $wgMemc, $wgSystemGifts, $wgUserStatsTrackWeekly, $wgUserStatsTrackMonthly;
     if (!$wgUser->isAllowed('bot') && !$wgUser->isAnon() && $this->stats_fields[$field]) {
         $dbw = wfGetDB(DB_MASTER);
         $dbw->update('user_stats', array($this->stats_fields[$field] . '=' . $this->stats_fields[$field] . "+{$val}"), array('stats_user_id' => $this->user_id), __METHOD__);
         $this->updateTotalPoints();
         $this->clearCache();
         // update weekly/monthly points
         if (isset($this->point_values[$field]) && !empty($this->point_values[$field])) {
             if ($wgUserStatsTrackWeekly) {
                 $this->updateWeeklyPoints($this->point_values[$field]);
             }
             if ($wgUserStatsTrackMonthly) {
                 $this->updateMonthlyPoints($this->point_values[$field]);
             }
         }
         $s = $dbw->selectRow('user_stats', array($this->stats_fields[$field]), array('stats_user_id' => $this->user_id), __METHOD__);
         $stat_field = $this->stats_fields[$field];
         $field_count = $s->{$stat_field};
         $key = wfForeignMemcKey('huiji', '', 'system_gift', 'id', $field . '-' . $field_count);
         $data = $wgMemc->get($key);
         if ($data != '' && is_int($data)) {
             wfDebug("Got system gift ID from cache\n");
             $systemGiftID = $data;
         } else {
             $g = new SystemGifts();
             $repeatableGift = $g->getRepeatableGifts();
             $categories = array_flip($g->getCategories());
             $systemGiftID = $g->doesGiftExistForThreshold($field, $field_count);
             // echo $field;die;
             if ($systemGiftID) {
                 $wgMemc->set($key, $systemGiftID, 60 * 30);
             }
         }
         if (isset($systemGiftID) && $field != "points_winner_weekly" && $field != "points_winner_monthly") {
             $sg = new UserSystemGifts($this->user_name);
             $sg->sendSystemGift($systemGiftID);
         }
     }
 }