public static function badWordsTest($title) { global $wgLanguageCode; wfProfileIn(__METHOD__); $text = preg_replace('/[^\\PP]+/', '', $title->getText()); $text = preg_replace('/\\s+/', ' ', $text); $blocksData = Phalanx::getFromFilter(Phalanx::TYPE_ANSWERS_QUESTION_TITLE, $wgLanguageCode); $blockData = null; $result = Phalanx::findBlocked($text, $blocksData, true, $blockData); if ($result['blocked']) { Wikia::log(__METHOD__, __LINE__, "Block '{$result['msg']}' blocked '{$text}'."); wfProfileOut(__METHOD__); return false; } wfProfileOut(__METHOD__); return true; }
public static function isAllowedText($text, $where, $split) { wfProfileIn(__METHOD__); $text = trim($text); $blocksData = Phalanx::getFromFilter(Phalanx::TYPE_WIKI_CREATION); if (!empty($blocksData) && !empty($text)) { $blockData = null; $result = Phalanx::findBlocked($text, $blocksData, true, $blockData); if ($result['blocked']) { wfProfileOut(__METHOD__); Wikia::log(__METHOD__, __LINE__, "Block '{$result['msg']}' blocked '{$text}'."); return false; } } wfProfileOut(__METHOD__); return true; }
/** * @static * @param User $user * @return array|bool|null */ public static function blockCheck(User $user) { global $wgUser, $wgMemc; wfProfileIn(__METHOD__); // dependancy -- if this doesn't exist, quit early if (!class_exists('AccountCreationTracker')) { wfProfileOut(__METHOD__); return true; } // we don't block anons with this filter if ($user->isAnon()) { wfProfileOut(__METHOD__); return true; } $ret = true; // RT#42011: RegexBlock records strange results // don't write stats for other user than visiting user $isCurrentUser = $user->getName() == $wgUser->getName(); // check cache first before proceeding $cachedState = self::getBlockFromCache($user, $isCurrentUser); if (!is_null($cachedState)) { wfProfileOut(__METHOD__); return $cachedState; } $tracker = F::build('AccountCreationTracker'); /** @var $tracker AccountCreationTracker */ $hashes = $tracker->getHashesByUser($user); $blocksData = Phalanx::getFromFilter(self::TYPE); if (!empty($blocksData) && !empty($hashes)) { foreach ($hashes as $hash) { $ret = self::blockCheckInternal($user, $blocksData, $hash, false, $isCurrentUser); if (!$ret) { // only check until we get first blocking match break; } } } // populate cache if not done before if ($ret) { $cacheKey = self::getCacheKey($user); $cachedState = array('timestamp' => wfTimestampNow(), 'block' => false, 'return' => $ret); $wgMemc->set($cacheKey, $cachedState); } wfProfileOut(__METHOD__); return $ret; }
public static function filterWordsTest($question) { wfProfileIn(__METHOD__); $text = preg_replace('/\\pP+/', '', $question); $text = preg_replace('/\\s+/', ' ', $text); $blocksData = Phalanx::getFromFilter(Phalanx::TYPE_ANSWERS_RECENT_QUESTIONS); if (!empty($blocksData) && !empty($text)) { $blockData = null; $result = Phalanx::findBlocked($text, $blocksData, true, $blockData); if ($result['blocked']) { Wikia::log(__METHOD__, __LINE__, "Block '{$result['msg']}' blocked '{$text}'."); wfProfileOut(__METHOD__); return false; } } wfProfileOut(__METHOD__); return true; }
public static function checkTitle($title) { wfProfileIn(__METHOD__); if (is_null(self::$blocksData)) { self::$blocksData = Phalanx::getFromFilter(Phalanx::TYPE_TITLE); } $fullText = $title->getFullText(); $result = array('blocked' => false); if (!empty(self::$blocksData)) { $blockData = null; $result = Phalanx::findBlocked($fullText, self::$blocksData, true, $blockData); if ($result['blocked']) { self::spamPage($result['msg'], $title); Wikia::log(__METHOD__, __LINE__, "Block '{$result['msg']}' blocked '{$fullText}'."); } } wfProfileOut(__METHOD__); return !$result['blocked']; }
<?php /** * A maintenance script to rebuild Phalanx's cache. Rebuilding the cache * during regular HTTP requests has become too resource-consuming. * * @file extensions/wikia/Phalanx/maintenance/rebuildPhalanxCache.php * @author Michał Roszka (Mix) <*****@*****.**> */ // MediaWiki include __DIR__ . '/../../../../maintenance/commandLine.inc'; // Phalanx caches its blocks by the type and by the language. Let's // get supported types and languages. $aTypes = array_keys(Phalanx::$typeNames); $aLanguages = array_keys($wgPhalanxSupportedLanguages); // Walk through all types... foreach ($aTypes as $iType) { // ... and languages. foreach ($aLanguages as $sLanguage) { // Fill the cache with the current data from DB_MASTER. Phalanx::getFromFilter($iType, $sLanguage, true, true); if ($iType == Phalanx::TYPE_USER) { Phalanx::getFromFilterShort($iType, $sLanguage, true, true); } echo "iType = {$iType}, {$sLanguage}, {$time} sec \n"; } // Touch. Phalanx::setLastUpdate(); } exit(0);
/** * Hook handler * Checks if user just being created is not blocked * * @author wladek * @param $user User * @param $name string * @param $validate string */ public static function onAbortNewAccount($user, &$abortError) { $text = $user->getName(); $blocksData = Phalanx::getFromFilter(self::TYPE); $state = self::blockCheckInternal($user, $blocksData, $text, false, true); if (!$state) { $abortError = wfMsg('phalanx-user-block-new-account'); return false; } // Check if email is blocked $emailBlocksData = Phalanx::getFromFilter(Phalanx::TYPE_EMAIL); $userEmail = $user->getEmail(); if ($userEmail !== '') { foreach ($emailBlocksData as $emailBlockData) { $result = Phalanx::isBlocked($userEmail, $emailBlockData, true); if ($result['blocked']) { $abortError = wfMsg('phalanx-user-block-new-account'); return false; } } } return true; }
public static function onSpamFilterCheck($text, $type, &$blockData) { wfProfileIn(__METHOD__); if (!empty($text)) { $filters = Phalanx::getFromFilter($type); foreach ($filters as $filter) { $result = Phalanx::isBlocked($text, $filter); if ($result['blocked']) { wfProfileOut(__METHOD__); return false; } } } wfProfileOut(__METHOD__); return true; }
public static function genericContentCheck($content) { wfProfileIn(__METHOD__); $blocksData = Phalanx::getFromFilter(Phalanx::TYPE_CONTENT); if (!empty($blocksData) && $content != '') { $content = self::applyWhitelist($content); foreach ($blocksData as $blockData) { $result = Phalanx::isBlocked($content, $blockData); if ($result['blocked']) { Wikia::log(__METHOD__, __LINE__, "Block '{$result['msg']}' blocked '{$content}'."); wfProfileOut(__METHOD__); return false; } } } wfProfileOut(__METHOD__); return true; }
/** * @brief Uses Phalanx to filter spam texts * * @param string $text the text to be filtered * @param string $type one of Phalanx static names consts: TYPE_CONTENT, TYPE_SUMMARY, TYPE_TITLE, TYPE_USER, TYPE_ANSWERS_QUESTION_TITLE, TYPE_ANSWERS_RECENT_QUESTIONS, TYPE_WIKI_CREATION, TYPE_COOKIE, TYPE_EMAIL; if $type is null it'll set to Phalanx::TYPE_CONTENT * * @return string empty string if text was blocked; given text otherwise * @FIXME this needs to be MOVED to Phalanx and called using hooks */ private function doPhalanxFilter($text, $type = null) { $this->app->wf->ProfileIn(__METHOD__); if (!empty($this->app->wg->EnablePhalanxExt) && !empty($text)) { if (is_null($type)) { $type = Phalanx::TYPE_CONTENT; } else { //fb#23473 $type = constant("Phalanx::{$type}"); } $filters = Phalanx::getFromFilter($type); foreach ($filters as $filter) { $result = Phalanx::isBlocked($text, $filter); if ($result['blocked']) { $this->app->wf->ProfileOut(__METHOD__); return ''; } } } $this->app->wf->ProfileOut(__METHOD__); return $text; }
/** * testBlock * * performs a test of all available filters and returns matching filters * * @param $text String to match * @return String with HTML to display via AJAX * * @author tor <*****@*****.**> */ public static function testBlock($text) { $data = array(); $output = ''; $aModules = Phalanx::$typeNames; $link_unblock = wfMsg('phalanx-link-unblock'); $link_modify = wfMsg('phalanx-link-modify'); $link_stats = wfMsg('phalanx-link-stats'); foreach ($aModules as $module => $name) { $filters = Phalanx::getFromFilter($module); $data[$module] = array(); if (empty($filters)) { continue; } foreach ($filters as $filter) { $result = Phalanx::isBlocked($text, $filter); if ($result['blocked'] == true) { $data[$module][] = $filter; } } if (!empty($data[$module])) { $output .= Xml::element('h2', null, $name); $output .= Xml::openElement('ul'); foreach ($data[$module] as $match) { $phalanxUrl = Title::newFromText('Phalanx', NS_SPECIAL)->getFullUrl(array('id' => $match['id'])); $statsUrl = Title::newFromText('PhalanxStats', NS_SPECIAL)->getFullUrl() . '/' . $match['id']; $line = htmlspecialchars($match['text']) . ' • ' . Xml::element('a', array('href' => $phalanxUrl, 'class' => 'unblock'), $link_unblock) . ' • ' . Xml::element('a', array('href' => $phalanxUrl, 'class' => 'modify'), $link_modify) . ' • ' . Xml::element('a', array('href' => $statsUrl, 'class' => 'stats'), $link_stats) . ' • ' . '#' . $match['id']; $output .= Xml::tags('li', null, $line); } $output .= Xml::closeElement('ul'); } } if (empty($output)) { $output = 'No matches found.'; } return $output; }
public function Phalanx() { global $wgRequest; wfProfileIn(__METHOD__); $text = $wgRequest->getVal('text', ''); $blockedKeywords = array(); $filters = Phalanx::getFromFilter(Phalanx::TYPE_CONTENT); foreach ($filters as $filter) { $result = Phalanx::isBlocked($text, $filter); if ($result['blocked']) { $blockedKeywords[] = $result['msg']; } } $this->msgHeader = ''; $this->msgBody = ''; if (count($blockedKeywords) > 0) { $keywords = ''; for ($i = 0; $i < count($blockedKeywords); $i++) { if ($i != 0) { $keywords .= ', '; } $keywords .= $blockedKeywords[$i]; } $this->msgHeader = wfMsg('cnw-badword-header'); $this->msgBody = wfMsg('cnw-badword-msg', $keywords); } wfProfileOut(__METHOD__); }