Example #1
0
/**
 * Writes an event to a file descriptor or socket.
 * Takes an event ID and an event, encodes it as query string,
 * and writes it to the UDP / TCP address or file specified by
 * $wgEventLoggingFile. If $wgEventLoggingFile is not set, returns
 * false without logging anything.
 *
 * @see wfErrorLog
 *
 * @param string $schema: Schema name.
 * @param integer $revId: revision ID of schema
 * @param array $event: Map of event keys/vals.
 * @return bool: Whether the event was logged.
 */
function efLogServerSideEvent($schemaName, $revId, $event)
{
    global $wgDBname, $wgEventLoggingFile;
    if (!$wgEventLoggingFile) {
        return false;
    }
    wfProfileIn(__FUNCTION__);
    $remoteSchema = new RemoteSchema($schemaName, $revId);
    $schema = $remoteSchema->get();
    try {
        $isValid = is_array($schema) && efSchemaValidate($event, $schema);
    } catch (JsonSchemaException $e) {
        $isValid = false;
    }
    $encapsulated = array('event' => $event, 'schema' => $schemaName, 'revision' => $revId, 'clientValidated' => $isValid, 'wiki' => $wgDBname, 'recvFrom' => gethostname(), 'timestamp' => $_SERVER['REQUEST_TIME']);
    if (isset($_SERVER['HTTP_HOST'])) {
        $encapsulated['webHost'] = $_SERVER['HTTP_HOST'];
    }
    // To make the resultant JSON easily extracted from a row of
    // space-separated values, we replace literal spaces with unicode
    // escapes. This is permitted by the JSON specs.
    $json = str_replace(' ', '\\u0020', FormatJson::encode($encapsulated));
    wfErrorLog($json . "\n", $wgEventLoggingFile);
    wfProfileOut(__FUNCTION__);
    return true;
}
 /**
  * @throws JsonSchemaException: If invalid.
  * @return bool: True if valid.
  */
 function validate()
 {
     $schema = FormatJson::decode($this->getNativeData(), true);
     if (!is_array($schema)) {
         throw new JsonSchemaException(wfMessage('eventlogging-invalid-json')->parse());
     }
     return efSchemaValidate($schema);
 }
 /**
  * Checks user input JSON to make sure that it produces a valid campaign object
  *
  * @throws JsonSchemaException: If invalid.
  * @return bool: True if valid.
  */
 function validate()
 {
     $campaign = $this->getJsonData();
     if (!is_array($campaign)) {
         throw new JsonSchemaException(wfMessage('eventlogging-invalid-json')->parse());
     }
     $schema = (include __DIR__ . '/CampaignSchema.php');
     // Only validate fields we care about
     $campaignFields = array_keys($schema['properties']);
     $fullConfig = UploadWizardConfig::getConfig();
     $defaultCampaignConfig = array();
     foreach ($fullConfig as $key => $value) {
         if (in_array($key, $campaignFields)) {
             $defaultCampaignConfig[$key] = $value;
         }
     }
     $mergedConfig = UploadWizardConfig::array_replace_sanely($defaultCampaignConfig, $campaign);
     return efSchemaValidate($mergedConfig, $schema);
 }
 /**
  * Tests invalidation of objects that deviate from schema.
  * @covers efSchemaValidate
  */
 function testSchemaInvalidate()
 {
     $this->setExpectedException('JsonSchemaException');
     efSchemaValidate(self::$invalidObject, self::$validSchema);
 }