Example #1
0
function wfObjectToArray($object, $recursive = true)
{
    $array = array();
    foreach (get_object_vars($object) as $key => $value) {
        if (is_object($value) && $recursive) {
            $value = wfObjectToArray($value);
        }
        $array[$key] = $value;
    }
    return $array;
}
Example #2
0
 public static function decode($value, $assoc = false)
 {
     if (!function_exists('json_decode')) {
         $json = new Services_JSON();
         $jsonDec = $json->decode($value);
         if ($assoc) {
             $jsonDec = wfObjectToArray($jsonDec);
         }
         return $jsonDec;
     } else {
         return json_decode($value, $assoc);
     }
 }
Example #3
0
/**
 * Recursively converts the parameter (an object) to an array with the same data
 *
 * @param object|array $objOrArray
 * @param bool $recursive
 * @return array
 */
function wfObjectToArray($objOrArray, $recursive = true)
{
    $array = array();
    if (is_object($objOrArray)) {
        $objOrArray = get_object_vars($objOrArray);
    }
    foreach ($objOrArray as $key => $value) {
        if ($recursive && (is_object($value) || is_array($value))) {
            $value = wfObjectToArray($value);
        }
        $array[$key] = $value;
    }
    return $array;
}
 function loadRequest($filter, $history_id = null)
 {
     static $row = null;
     static $actions = null;
     $request = $this->getRequest();
     if (!is_null($actions) && !is_null($row)) {
         return array($row, $actions);
     } elseif ($request->wasPosted()) {
         # Nothing, we do it all later
     } elseif ($history_id) {
         return $this->loadHistoryItem($history_id);
     } else {
         return $this->loadFilterData($filter);
     }
     // We need some details like last editor
     list($row, $origActions) = $this->loadFilterData($filter);
     $row->mOriginalRow = clone $row;
     $row->mOriginalActions = $origActions;
     // Check for importing
     $import = $request->getVal('wpImportText');
     if ($import) {
         $data = json_decode($import);
         $importRow = $data->row;
         $actions = wfObjectToArray($data->actions);
         $copy = array('af_public_comments', 'af_pattern', 'af_comments', 'af_deleted', 'af_enabled', 'af_hidden');
         foreach ($copy as $name) {
             $row->{$name} = $importRow->{$name};
         }
     } else {
         $textLoads = array('af_public_comments' => 'wpFilterDescription', 'af_pattern' => 'wpFilterRules', 'af_comments' => 'wpFilterNotes');
         foreach ($textLoads as $col => $field) {
             $row->{$col} = trim($request->getVal($field));
         }
         $row->af_deleted = $request->getBool('wpFilterDeleted');
         $row->af_enabled = $request->getBool('wpFilterEnabled') && !$row->af_deleted;
         $row->af_hidden = $request->getBool('wpFilterHidden');
         global $wgAbuseFilterIsCentral;
         $row->af_global = $request->getBool('wpFilterGlobal') && $wgAbuseFilterIsCentral;
         // Actions
         global $wgAbuseFilterAvailableActions;
         $actions = array();
         foreach ($wgAbuseFilterAvailableActions as $action) {
             // Check if it's set
             $enabled = $request->getBool('wpFilterAction' . ucfirst($action));
             if ($enabled) {
                 $parameters = array();
                 if ($action == 'throttle') {
                     // We need to load the parameters
                     $throttleCount = $request->getIntOrNull('wpFilterThrottleCount');
                     $throttlePeriod = $request->getIntOrNull('wpFilterThrottlePeriod');
                     $throttleGroups = explode("\n", trim($request->getText('wpFilterThrottleGroups')));
                     $parameters[0] = $this->mFilter;
                     // For now, anyway
                     $parameters[1] = "{$throttleCount},{$throttlePeriod}";
                     $parameters = array_merge($parameters, $throttleGroups);
                 } elseif ($action == 'warn') {
                     $specMsg = $request->getVal('wpFilterWarnMessage');
                     if ($specMsg == 'other') {
                         $specMsg = $request->getVal('wpFilterWarnMessageOther');
                     }
                     $parameters[0] = $specMsg;
                 } elseif ($action == 'tag') {
                     $parameters = explode("\n", $request->getText('wpFilterTags'));
                 }
                 $thisAction = array('action' => $action, 'parameters' => $parameters);
                 $actions[$action] = $thisAction;
             }
         }
     }
     $row->af_actions = implode(',', array_keys(array_filter($actions)));
     return array($row, $actions);
 }
 public static function naiveResolvePath($data, $path, $split = true)
 {
     if (is_object($data)) {
         if ($dom instanceof DOMNode) {
             throw new MWException("naiveResolvePath does not like DOMNode objects");
         }
         $data = wfObjectToArray($data);
     }
     if (!is_array($data) || $path === '.') {
         return $data;
     }
     if ($split && is_string($path)) {
         $path = DataTransclusionSource::splitList($path, '/');
     }
     if (is_string($path) || is_int($path)) {
         return @$data[$path];
     }
     if (!$path) {
         return $data;
     }
     $p = array_shift($path);
     if (is_string($p) && preg_match('/^(@)?(\\d+)$/', $p, $m)) {
         //numberic index
         $i = (int) $m[2];
         if ($m[1]) {
             //meta-index
             $k = array_keys($data);
             $p = $k[$i];
         }
     }
     if (!isset($data[$p])) {
         return false;
     }
     $next = $data[$p];
     if ($next && $path) {
         return FlattenRecord::naiveResolvePath($next, $path);
     } else {
         return $next;
     }
     //TODO: named components. separator??
 }