Exemple #1
0
 static function submit_translations()
 {
     // Request format (HTTP POST):
     //   client_token = <client_token>
     //   uid = <l10n server user id>
     //   signature = md5(user_api_key($uid, $client_token) . $data . $client_token))
     //   data = // JSON payload
     //
     //     {<key_1>: {message: <JSON encoded message>
     //                translations: {<locale_1>: <JSON encoded translation>,
     //                               <locale_2>: ...}},
     //      <key_2>: {...}
     //     }
     // @todo Batch requests (max request size)
     // @todo include base_revision in submission / how to handle resubmissions / edit fights?
     $request = new stdClass();
     foreach (db::build()->select("key", "message", "locale", "base_revision", "translation")->from("outgoing_translations")->execute() as $row) {
         $key = $row->key;
         if (!isset($request->{$key})) {
             $request->{$key} = new stdClass();
             $request->{$key}->translations = new stdClass();
             $request->{$key}->message = json_encode(unserialize($row->message));
         }
         $request->{$key}->translations->{$row->locale} = json_encode(unserialize($row->translation));
     }
     // @todo reduce memory consumption, e.g. free $request
     $request_data = json_encode($request);
     $url = self::_server_url("submit");
     $signature = self::_sign($request_data);
     list($response_data, $response_status) = remote::post($url, array("data" => $request_data, "client_token" => l10n_client::client_token(), "signature" => $signature, "uid" => l10n_client::server_uid()));
     if (!remote::success($response_status)) {
         throw new Exception("@todo TRANSLATIONS_SUBMISSION_FAILED " . $response_status);
     }
     if (empty($response_data)) {
         return;
     }
     $response = json_decode($response_data);
     // Response format (JSON payload):
     //   [{key:<key_1>, locale:<locale_1>, rev:<rev_1>, status:<rejected|accepted|pending>},
     //    {key:<key_2>, ...}
     //   ]
     // @todo Move messages out of outgoing into incoming, using new rev?
     // @todo show which messages have been rejected / are pending?
 }