Ejemplo n.º 1
0
 /**
  * @param $actions_taken
  * @param $log_template
  * @param $action
  * @param $vars AbuseFilterVariableHolder
  * @param string $group
  * @return mixed
  */
 public static function addLogEntries($actions_taken, $log_template, $action, $vars, $group = 'default')
 {
     wfProfileIn(__METHOD__);
     $dbw = wfGetDB(DB_MASTER);
     $central_log_template = array('afl_wiki' => wfWikiID());
     $log_rows = array();
     $central_log_rows = array();
     $logged_local_filters = array();
     $logged_global_filters = array();
     foreach ($actions_taken as $filter => $actions) {
         $globalIndex = self::decodeGlobalName($filter);
         $thisLog = $log_template;
         $thisLog['afl_filter'] = $filter;
         $thisLog['afl_action'] = $action;
         $thisLog['afl_actions'] = implode(',', $actions);
         // Don't log if we were only throttling.
         if ($thisLog['afl_actions'] != 'throttle') {
             $log_rows[] = $thisLog;
             if (!$globalIndex) {
                 $logged_local_filters[] = $filter;
             }
             // Global logging
             if ($globalIndex) {
                 $title = Title::makeTitle($thisLog['afl_namespace'], $thisLog['afl_title']);
                 $centralLog = $thisLog + $central_log_template;
                 $centralLog['afl_filter'] = $globalIndex;
                 $centralLog['afl_title'] = $title->getPrefixedText();
                 $centralLog['afl_namespace'] = 0;
                 $central_log_rows[] = $centralLog;
                 $logged_global_filters[] = $globalIndex;
             }
         }
     }
     if (!count($log_rows)) {
         wfProfileOut(__METHOD__);
         return;
     }
     // Only store the var dump if we're actually going to add log rows.
     $var_dump = self::storeVarDump($vars);
     $var_dump = "stored-text:{$var_dump}";
     // To distinguish from stuff stored directly
     wfProfileIn(__METHOD__ . '-hitstats');
     global $wgMemc;
     // Increment trigger counter
     $wgMemc->incr(self::filterMatchesKey());
     $local_log_ids = array();
     global $wgAbuseFilterNotifications, $wgAbuseFilterNotificationsPrivate;
     foreach ($log_rows as $data) {
         $data['afl_var_dump'] = $var_dump;
         $data['afl_id'] = $dbw->nextSequenceValue('abuse_filter_log_afl_id_seq');
         $dbw->insert('abuse_filter_log', $data, __METHOD__);
         $local_log_ids[] = $dbw->insertId();
         if ($data['afl_id'] === null) {
             $data['afl_id'] = $dbw->insertId();
         }
         $entry = new ManualLogEntry('abusefilter', 'hit');
         // Construct a user object
         $user = User::newFromId($data['afl_user']);
         $user->setName($data['afl_user_text']);
         $entry->setPerformer($user);
         // Set action target
         $entry->setTarget(Title::makeTitle($data['afl_namespace'], $data['afl_title']));
         // Additional info
         $entry->setParameters(array('action' => $data['afl_action'], 'filter' => $data['afl_filter'], 'actions' => $data['afl_actions'], 'log' => $data['afl_id']));
         // Send data to CheckUser if installed and we
         // aren't already sending a notification to recentchanges
         // Requires MW 1.23+
         if (is_callable('CheckUserHooks::updateCheckUserData') && is_callable('ManualLogEntry::getRecentChange') && strpos($wgAbuseFilterNotifications, 'rc') === false) {
             $rc = $entry->getRecentChange();
             CheckUserHooks::updateCheckUserData($rc);
         }
         if ($wgAbuseFilterNotifications !== false) {
             if (self::filterHidden($data['afl_filter']) && !$wgAbuseFilterNotificationsPrivate) {
                 continue;
             }
             $entry->publish(0, $wgAbuseFilterNotifications);
         }
     }
     $method = __METHOD__;
     if (count($logged_local_filters)) {
         // Update hit-counter.
         $dbw->onTransactionPreCommitOrIdle(function () use($dbw, $logged_local_filters, $method) {
             $dbw->update('abuse_filter', array('af_hit_count=af_hit_count+1'), array('af_id' => $logged_local_filters), $method);
         });
     }
     $global_log_ids = array();
     // Global stuff
     if (count($logged_global_filters)) {
         $vars->computeDBVars();
         $global_var_dump = self::storeVarDump($vars, true);
         $global_var_dump = "stored-text:{$global_var_dump}";
         foreach ($central_log_rows as $index => $data) {
             $central_log_rows[$index]['afl_var_dump'] = $global_var_dump;
         }
         global $wgAbuseFilterCentralDB;
         $fdb = wfGetDB(DB_MASTER, array(), $wgAbuseFilterCentralDB);
         foreach ($central_log_rows as $row) {
             $fdb->insert('abuse_filter_log', $row, __METHOD__);
             $global_log_ids[] = $dbw->insertId();
         }
         $fdb->onTransactionPreCommitOrIdle(function () use($fdb, $logged_global_filters, $method) {
             $fdb->update('abuse_filter', array('af_hit_count=af_hit_count+1'), array('af_id' => $logged_global_filters), $method);
         });
     }
     $vars->setVar('global_log_ids', $global_log_ids);
     $vars->setVar('local_log_ids', $local_log_ids);
     // Check for emergency disabling.
     $total = $wgMemc->get(AbuseFilter::filterUsedKey($group));
     self::checkEmergencyDisable($group, $logged_local_filters, $total);
     wfProfileOut(__METHOD__ . '-hitstats');
     wfProfileOut(__METHOD__);
 }