Example #1
0
 /**
  * Directly check for IP block which is quicker than looping through all filters as we don't support
  * anything other than exact for IP blocks, and this significantly improves performance
  *
  * @author grunny
  */
 protected static function blockCheckIP(User $user, $text, $writeStats = true)
 {
     global $wgMemc, $wgExternalSharedDB;
     wfProfileIn(__METHOD__);
     PhalanxShadowing::setType(Phalanx::TYPE_USER);
     $dbr = wfGetDB(DB_SLAVE, array(), $wgExternalSharedDB);
     $moduleId = Phalanx::TYPE_USER;
     $timestampNow = wfTimestampNow();
     $ipAddr = IP::toHex($text);
     $row = $dbr->selectRow('phalanx', '*', array("p_type & {$moduleId} = {$moduleId}", "p_lang IS NULL", 'p_ip_hex' => $ipAddr, "p_expire IS NULL OR p_expire > {$dbr->addQuotes($timestampNow)}"), __METHOD__);
     if ($row !== false) {
         $blockData = array('id' => $row->p_id, 'author_id' => $row->p_author_id, 'text' => $row->p_text, 'type' => $row->p_type, 'timestamp' => $row->p_timestamp, 'expire' => $row->p_expire, 'exact' => $row->p_exact, 'regex' => $row->p_regex, 'case' => $row->p_case, 'reason' => $row->p_reason, 'lang' => $row->p_lang);
         Wikia::log(__METHOD__, __LINE__, "Block '{$blockData['text']}' blocked '{$text}'.");
         if ($writeStats) {
             Phalanx::addStats($blockData['id'], $blockData['type']);
         }
         self::setUserData($user, $blockData, $text, true);
         $cachedState = array('timestamp' => wfTimestampNow(), 'block' => $blockData, 'return' => false);
         $wgMemc->set(self::getCacheKey($user), $cachedState);
         PhalanxShadowing::check($user->getName(), $blockData['id']);
         wfProfileOut(__METHOD__);
         return false;
     }
     PhalanxShadowing::check($user->getName(), 0);
     wfProfileOut(__METHOD__);
     return true;
 }
Example #2
0
 public static function getFromFilterShort($moduleId, $lang = null, $master = false, $skipCache = false)
 {
     global $wgExternalSharedDB, $wgMemc;
     wfProfileIn(__METHOD__);
     $timestampNow = wfTimestampNow();
     $key = 'phalanx:' . $moduleId . ':' . ($lang ? $lang : 'all') . ':short';
     $sLang = $lang ? $lang : 'all';
     if ($skipCache) {
         $blocksData = null;
     } else {
         if (isset(self::$moduleDataShort[$moduleId][$sLang])) {
             $blocksData = self::$moduleDataShort[$moduleId][$sLang];
         } else {
             $blocksData = $wgMemc->get($key);
         }
     }
     //cache miss (or we have expired blocks in cache), get from DB
     if (empty($blocksData) || !is_null($blocksData['closestExpire']) && $blocksData['closestExpire'] < $timestampNow && $blocksData['closestExpire']) {
         $blocks = $cond = array();
         $closestTimestamp = 0;
         $dbr = wfGetDB($master ? DB_MASTER : DB_SLAVE, array(), $wgExternalSharedDB);
         if (!empty($moduleId) && is_numeric($moduleId)) {
             $cond[] = "p_type & {$moduleId} = {$moduleId}";
         }
         if (!empty($lang) && Language::isValidCode($lang)) {
             $cond[] = "(p_lang = '{$lang}' OR p_lang IS NULL)";
         } else {
             $cond[] = "p_lang IS NULL";
         }
         $cond[] = "p_expire is null or p_expire > '{$timestampNow}'";
         $res = $dbr->select('phalanx', '*', $cond, __METHOD__);
         foreach ($res as $row) {
             $blocks[$row->p_id] = array($row->p_id, $row->p_text, ($row->p_exact ? self::FLAG_EXACT : 0) + ($row->p_regex ? self::FLAG_REGEX : 0) + ($row->p_case ? self::FLAG_CASE : 0));
             if (!is_null($row->p_expire) && $closestTimestamp > $row->p_expire || !$closestTimestamp) {
                 $closestTimestamp = $row->p_expire;
             }
         }
         $blocksData['blocks'] = $blocks;
         $blocksData['closestExpire'] = $closestTimestamp;
         $wgMemc->set($key, $blocksData);
     }
     self::$moduleDataShort[$moduleId][$sLang] = $blocksData;
     PhalanxShadowing::setType($moduleId);
     wfProfileOut(__METHOD__);
     return $blocksData['blocks'];
 }