add new routes

This commit is contained in:
Nicolas Le Goff
2012-04-30 21:01:07 +02:00
parent c1dc074172
commit c37ae56b2e
6 changed files with 468 additions and 45 deletions

View File

@@ -156,38 +156,116 @@ return call_user_func(function() {
throw new \API_V1_exception_badrequest(); throw new \API_V1_exception_badrequest();
}; };
$route = '/scheduler/state/'; /**
* Get sheduler state
*/
$route = '/monitor/scheduler/';
$app->get( $app->get(
$route, function(\Silex\Application $app, Request $request) { $route, function(\Silex\Application $app, Request $request) {
return $app['api']->get_scheduler_state($request)->get_response(); return $app['api']->get_scheduler_state($request)->get_response();
} }
); );
$route = '/scheduler/start/'; /**
* task list
*/
$route = '/monitor/tasks/';
$app->get( $app->get(
$route, function(\Silex\Application $app, Request $request) { $route, function(\Silex\Application $app, Request $request) {
/* @var $user \User_Adapter */ /* @var $user \User_Adapter */
$user = $app['token']->get_account()->get_user(); $user = $app['token']->get_account()->get_user();
if(!$user->is_admin()){ if ( ! $user->is_admin()) {
throw new \API_V1_exception_unauthorized(); throw new \API_V1_exception_unauthorized();
} }
return $app['api']->start_scheduler($request)->get_response(); return $app['api']->get_task_list($request)->get_response();
} }
); );
$route = '/scheduler/stop/'; /**
* Get task informations
*/
$route = '/monitor/task/{idTask}/';
$app->get(
$route, function(\Silex\Application $app, Request $request, $idTask) {
/* @var $user \User_Adapter */
$user = $app['token']->get_account()->get_user();
if ( ! $user->is_admin()) {
throw new \API_V1_exception_unauthorized();
}
return $app['api']->get_task($request, $idTask)->get_response();
}
);
/**
* Modify Task
* @param name
* @param autostart
*/
$route = '/monitor/task/{idTask}/';
$app->post(
$route, function(\Silex\Application $app, Request $request, $idTask) {
/* @var $user \User_Adapter */
$user = $app['token']->get_account()->get_user();
if ( ! $user->is_admin()) {
throw new \API_V1_exception_unauthorized();
}
return $app['api']->set_task_property($app, $idTask)->get_response();
}
);
/**
* Start task
*/
$route = '/monitor/task/{idTask}/start/';
$app->post(
$route, function(\Silex\Application $app, Request $request, $idTask) {
/* @var $user \User_Adapter */
$user = $app['token']->get_account()->get_user();
if ( ! $user->is_admin()) {
throw new \API_V1_exception_unauthorized();
}
return $app['api']->start_task($app, $idTask)->get_response();
}
);
/**
* Stop task
*/
$route = '/monitor/task/{idTask}/stop/';
$app->post(
$route, function(\Silex\Application $app, Request $request, $idTask) {
/* @var $user \User_Adapter */
$user = $app['token']->get_account()->get_user();
if ( ! $user->is_admin()) {
throw new \API_V1_exception_unauthorized();
}
return $app['api']->stop_task($app,$idTask)->get_response();
}
);
/**
* Get some information about phraseanet
*/
$route = '/monitor/phraseanet/';
$app->get( $app->get(
$route, function(\Silex\Application $app, Request $request) { $route, function(\Silex\Application $app, Request $request) {
/* @var $user \User_Adapter */ /* @var $user \User_Adapter */
$user = $app['token']->get_account()->get_user(); $user = $app['token']->get_account()->get_user();
if(!$user->is_admin()){ if ( ! $user->is_admin()) {
throw new \API_V1_exception_unauthorized(); throw new \API_V1_exception_unauthorized();
} }
return $app['api']->get_phraseanet_monitor($app)->get_response();
return $app['api']->stop_scheduler($request)->get_response();
} }
); );
@@ -607,7 +685,6 @@ return call_user_func(function() {
* Parameters : * Parameters :
* *
*/ */
$route = '/feeds/list/'; $route = '/feeds/list/';
$app->get( $app->get(
$route, function() use ($app) { $route, function() use ($app) {
@@ -626,7 +703,6 @@ return call_user_func(function() {
* PUBLICATION_ID : required INT * PUBLICATION_ID : required INT
* *
*/ */
$route = '/feeds/{feed_id}/content/'; $route = '/feeds/{feed_id}/content/';
$app->get( $app->get(
$route, function($feed_id) use ($app) { $route, function($feed_id) use ($app) {

View File

@@ -127,7 +127,7 @@ class Configuration
return $this; return $this;
} }
public function get() public function get($name)
{ {
return $this->configuration->get($name); return $this->configuration->get($name);
} }

View File

@@ -93,13 +93,17 @@ class API_V1_adapter extends API_V1_Abstract
return $this->version; return $this->version;
} }
/**
*
* @return API_V1_result
*/
public function get_scheduler_state(Request $request) public function get_scheduler_state(Request $request)
{ {
$result = new API_V1_result($request, $this); $result = new \API_V1_result($request, $this);
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = \appbox::get_instance(\bootstrap::getCore());
$task_manager = new task_manager($appbox); $task_manager = new \task_manager($appbox);
$state = $task_manager->get_scheduler_state(); $state = $task_manager->get_scheduler_state();
@@ -108,20 +112,27 @@ class API_V1_adapter extends API_V1_Abstract
return $result; return $result;
} }
public function start_scheduler(Request $request) /**
*
* @return API_V1_result
*/
public function get_task_list(Request $request)
{ {
$result = new API_V1_result($request, $this); $result = new \API_V1_result($request, $this);
$scheduler = new task_Scheduler(); $appbox = \appbox::get_instance(\bootstrap::getCore());
$task_manager = new \task_manager($appbox);
$tasks = $task_manager->get_tasks();
$ret = array('success' => true); $ret = array();
try foreach ($tasks as $task) {
{ $ret[] = array(
$scheduler->run(); 'id' => $task->get_task_id(),
} 'status' => $task->get_status(),
catch(\Exception $e) 'pid' => $task->get_pid(),
{ 'title' => $task->get_title(),
$ret = array('success' => false); 'last_exec_time' => $task->get_last_exec_time()
);
} }
$result->set_datas($ret); $result->set_datas($ret);
@@ -129,23 +140,351 @@ class API_V1_adapter extends API_V1_Abstract
return $result; return $result;
} }
public function stop_scheduler(Request $request) /**
*
* @return API_V1_result
*/
public function get_task(Request $request, $task_id)
{ {
$result = new API_V1_result($request, $this); $result = new \API_V1_result($request, $this);
$ret = array('success' => true);
try
{
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox = \appbox::get_instance(\bootstrap::getCore());
$task_manager = new task_manager($appbox); $task_manager = new task_manager($appbox);
$task_manager->set_sched_status(task_manager::STATUS_SCHED_TOSTOP); $ret = array();
} try {
catch(\Exception $e) $task = $task_manager->get_task($task_id);
{ $ret['id'] = $task->get_task_id();
$ret['status'] = $task->get_status();
$ret['pid'] = $task->get_pid();
$ret['title'] = $task->get_title();
$ret['last_exec_time'] = $task->get_last_exec_time();
} catch (\Exception_NotFound $e) {
$result->set_error_code(404);
$ret = array('success' => false);
} catch (\Exception_InvalidArgument $e) {
$result->set_error_code(400);
$ret = array('success' => false);
} catch (\Exception $e) {
$result->set_error_code(500);
$ret = array('success' => false); $ret = array('success' => false);
} }
$result->set_datas($ret);
return $result;
}
public function start_task(\Silex\Application $app, $task_id)
{
$result = new \API_V1_result($app['request'], $this);
$appbox = \appbox::get_instance(\bootstrap::getCore());
$task_manager = new \task_manager($appbox);
$ret = array('success' => true);
try {
$task = $task_manager->get_task($task_id);
$task->set_status(\task_abstract::STATUS_TOSTART);
} catch (\Exception_NotFound $e) {
$result->set_error_code(404);
$ret = array('success' => false);
} catch (\Exception_InvalidArgument $e) {
$result->set_error_code(400);
$ret = array('success' => false);
} catch (\Exception $e) {
$result->set_error_code(500);
$ret = array('success' => false);
}
$result->set_datas($ret);
return $result;
}
public function stop_task(\Silex\Application $app, $task_id)
{
$result = new API_V1_result($app['request'], $this);
$appbox = \appbox::get_instance(\bootstrap::getCore());
$task_manager = new \task_manager($appbox);
$ret = array();
try {
$task = $task_manager->get_task($task_id);
$task->set_status(\task_abstract::STATUS_TOSTOP);
} catch (\Exception_NotFound $e) {
$result->set_error_code(404);
$ret = array('success' => false);
} catch (\Exception_InvalidArgument $e) {
$result->set_error_code(400);
$ret = array('success' => false);
} catch (\Exception $e) {
$result->set_error_code(500);
$ret = array('success' => false);
}
$result->set_datas($ret);
return $result;
}
public function set_task_property(\Silex\Application $app, $task_id)
{
$result = new API_V1_result($app['request'], $this);
$name = $app['request']->get('name');
$autostart = $app['request']->get('autostart');
$ret = array('success' => false);
try {
if (null === $name && null === $autostart) {
throw new \Exception_InvalidArgument();
}
$task = $task_manager->get_task($task_id);
if ($name) {
$task->set_title($name);
}
if ($autostart) {
$task->set_active( ! ! $autostart);
}
$ret = array('success' => true);
} catch (\Exception_NotFound $e) {
$result->set_error_code(404);
$ret = array('success' => false);
} catch (\Exception_InvalidArgument $e) {
$result->set_error_code(400);
$ret = array('success' => false);
} catch (\Exception $e) {
$result->set_error_code(500);
$ret = array('success' => false);
}
$result->set_datas($ret);
return $result;
}
/**
*
* @return array
*/
protected function get_cache_info(\Silex\Application $app)
{
$mainCache = $app['Core']['Cache'];
$opCodeCache = $app['Core']['OpcodeCache'];
$ret = array();
if ($mainCache instanceof \Alchemy\Phrasea\Cache\Cache) {
$ret['cache']['main'] = array(
'type' => strtolower(substr(array_pop(explode('\\', get_class($mainCache))), 0, -5)),
'stats' => $mainCache->getStats()
);
} else {
$ret['cache']['main'] = null;
}
if ($opCodeCache instanceof \Alchemy\Phrasea\Cache\Cache) {
$ret['cache']['op_code'] = array(
'type' => strtolower(substr(array_pop(explode('\\', get_class($opCodeCache))), 0, -5)),
'stats' => $opCodeCache->getStats()
);
} else {
$ret['cache']['op_code'] = null;
}
return $ret;
}
protected function get_config_info(\Silex\Application $app)
{
$ret = array();
$ret['phraseanet']['version'] = array(
'name' => $app['Core']['Version']::getName(),
'number' => $app['Core']['Version']::getNumber(),
);
$config = $app['Core']->getConfiguration();
$ret['phraseanet']['environment'] = $app['Core']->getEnv();
$ret['phraseanet']['debug'] = $config->isDebug();
$ret['phraseanet']['maintenance'] = $config->isMaintained();
$ret['phraseanet']['errorsLog'] = $config->isDisplayingErrors();
$ret['phraseanet']['serverName'] = $config->getPhraseanet()->get('servername');
return $ret;
}
protected function get_gv_info(\Silex\Application $app)
{
$registry = $app['Core']['Registry'];
return array(
'global_values' => array(
'serverName' => $registry->get('GV_ServerName'),
'title' => $registry->get('GV_homeTitle'),
'keywords' => $registry->get('GV_metaKeywords'),
'description' => $registry->get('GV_metaDescription'),
'httpServer' => array(
'logErrors' => $registry->get('GV_log_errors'),
'phpTimezone' => $registry->get('GV_timezone'),
'siteId' => $registry->get('GV_sit'),
'staticUrl' => $registry->get('GV_STATIC_URL'),
'defaultLanguage' => $registry->get('id_GV_default_lng'),
'allowIndexing' => $registry->get('GV_allow_search_engine'),
'modes' => array(
'XsendFile' => $registry->get('GV_modxsendfile'),
'nginxXAccelRedirect' => $registry->get('GV_X_Accel_Redirect'),
'nginxXAccelRedirectMountPoint' => $registry->get('GV_X_Accel_Redirect_mount_point'),
'h264Streaming' => $registry->get('GV_h264_streaming'),
'authTokenDirectory' => $registry->get('GV_mod_auth_token_directory'),
'authTokenDirectoryPath' => $registry->get('GV_mod_auth_token_directory_path'),
'authTokenPassphrase' => $registry->get('GV_mod_auth_token_passphrase'),
),
'files' => array(
'owner' => $registry->get('GV_filesOwner'),
'group' => $registry->get('GV_filesOwner'),
)
),
'maintenance' => array(
'alertMessage' => $registry->get('GV_message'),
'displayMessage' => $registry->get('GV_message_on'),
),
'webServices' => array(
'googleApi' => $registry->get('GV_google_api'),
'googleAnalyticsId' => $registry->get('GV_googleAnalytics'),
'googleChromeFrameDisclaimer' => $registry->get('GV_display_gcf'),
'i18nWebService' => $registry->get('GV_i18n_service'),
'recaptacha' => array(
'active' => $registry->get('GV_captchas'),
'publicKey' => $registry->get('GV_captcha_public_key'),
'privateKey' => $registry->get('GV_captcha_private_key'),
),
'youtube' => array(
'active' => $registry->get('GV_youtube_api'),
'clientId' => $registry->get('GV_youtube_client_id'),
'clientSecret' => $registry->get('GV_youtube_client_secret'),
'devKey' => $registry->get('GV_youtube_dev_key'),
),
'flickr' => array(
'active' => $registry->get('GV_flickr_api'),
'clientId' => $registry->get('GV_flickr_client_id'),
'clientSecret' => $registry->get('GV_flickr_client_secret'),
),
'dailymtotion' => array(
'active' => $registry->get('GV_dailymotion_api'),
'clientId' => $registry->get('GV_dailymotion_client_id'),
'clientSecret' => $registry->get('GV_dailymotion_client_secret'),
)
),
'navigator' => array(
'active' => $registry->get('GV_client_navigator'),
),
'homepage' => array(
'viewType' => $registry->get('GV_home_publi'),
),
'report' => array(
'anonymous' => $registry->get('GV_anonymousReport'),
),
'events' => array(
'events' => $registry->get('GV_events'),
'notifications' => $registry->get('GV_notifications'),
),
'upload' => array(
'allowedFileExtension' => $registry->get('GV_appletAllowedFileEx'),
),
'filesystem' => array(
'web' => $registry->get('GV_base_datapath_web'),
'noWeb' => $registry->get('GV_base_datapath_noweb'),
'thumbnail' => $registry->get('GV_base_dataurl'),
),
'searchEngine' => array(
'configuration' => array(
'defaultQuery' => $registry->get('GV_defaultQuery'),
'defaultQueryType' => $registry->get('GV_defaultQuery_type'),
),
'sphinx' => array(
'active' => $registry->get('GV_sphinx'),
'host' => $registry->get('GV_sphinx_host'),
'port' => $registry->get('GV_sphinx_port'),
'realtimeHost' => $registry->get('GV_sphinx_rt_host'),
'realtimePort' => $registry->get('GV_sphinx_rt_port'),
),
'phrasea' => array(
'minChar' => $registry->get('GV_min_letters_truncation'),
'sort' => $registry->get('GV_phrasea_sort'),
),
),
'binary' => array(
'phpCli' => $registry->get('GV_cli'),
'phpIni' => $registry->get('GV_PHP_INI'),
'imagick' => $registry->get('GV_imagick'),
'exiftool' => $registry->get('GV_exiftool'),
'swfExtract' => $registry->get('GV_swf_extract'),
'pdf2swf' => $registry->get('GV_pdf2swf'),
'swfRender' => $registry->get('GV_swf_render'),
'unoconv' => $registry->get('GV_unoconv'),
'ffmpeg' => $registry->get('GV_ffmpeg'),
'mp4box' => $registry->get('GV_mp4box'),
'mplayer' => $registry->get('GV_mplayer'),
'pdftotext' => $registry->get('GV_pdftotext'),
'pdfmaxpages' => $registry->get('GV_pdfmaxpages'),),
'mainConfiguration' => array(
'adminMail' => $registry->get('GV_adminMail'),
'viewBasAndCollName' => $registry->get('GV_view_bas_and_coll'),
'chooseExportTitle' => $registry->get('GV_choose_export_title'),
'defaultExportTitle' => $registry->get('GV_default_export_title'),
'socialTools' => $registry->get('GV_social_tools'),),
'modules' => array(
'thesaurus' => $registry->get('GV_thesaurus'),
'storyMode' => $registry->get('GV_multiAndReport'),
'docSubsitution' => $registry->get('GV_seeOngChgDoc'),
'subdefSubstitution' => $registry->get('GV_seeNewThumb'),),
'email' => array(
'defaultMailAddress' => $registry->get('GV_defaulmailsenderaddr'),
'smtp' => array(
'active' => $registry->get('GV_smtp'),
'auth' => $registry->get('GV_smtp_auth'),
'host' => $registry->get('GV_smtp_host'),
'port' => $registry->get('GV_smtp_port'),
'secure' => $registry->get('GV_smtp_secure'),
'user' => $registry->get('GV_smtp_user'),
'password' => $registry->get('GV_smtp_password'),
),
),
'ftp' => array(
'active' => $registry->get('GV_activeFTP'),
'activeForUser' => $registry->get('GV_ftp_for_user'),),
'client' => array(
'maxSizeDownload' => $registry->get('GV_download_max'),
'tabSearchMode' => $registry->get('GV_ong_search'),
'tabAdvSearchPosition' => $registry->get('GV_ong_advsearch'),
'tabTopicsPosition' => $registry->get('GV_ong_topics'),
'tabOngActifPosition' => $registry->get('GV_ong_actif'),
'renderTopicsMode' => $registry->get('GV_client_render_topics'),
'displayRolloverPreview' => $registry->get('GV_rollover_reg_preview'),
'displayRolloverBasket' => $registry->get('GV_rollover_chu'),
'collRenderMode' => $registry->get('GV_client_coll_ckbox'),
'viewSizeBaket' => $registry->get('GV_viewSizeBaket'),
'clientAutoShowProposals' => $registry->get('GV_clientAutoShowProposals'),
'needAuth2DL' => $registry->get('GV_needAuth2DL'),),
'inscription' => array(
'autoSelectDB' => $registry->get('GV_autoselectDB'),
'autoRegister' => $registry->get('GV_autoregister'),
),
'push' => array(
'validationReminder' => $registry->get('GV_validation_reminder'),
'expirationValue' => $registry->get('GV_val_expiration'),
),
)
);
}
public function get_phraseanet_monitor(\Silex\Application $app)
{
$result = new API_V1_result($app['request'], $this);
$ret = array_merge(
$this->get_config_info($app), $this->get_cache_info($app), $this->get_gv_info($app)
);
$result->set_datas($ret); $result->set_datas($ret);

View File

@@ -40,10 +40,6 @@ class task_Scheduler
$date_obj = new DateTime(); $date_obj = new DateTime();
$message = sprintf("%s\t%s", $date_obj->format(DATE_ATOM), $message); $message = sprintf("%s\t%s", $date_obj->format(DATE_ATOM), $message);
if ($this->output instanceof OutputInterface) {
// $this->output->writeln($message);
}
// $this->output->writeln($this->input->getOption('nolog'));
if ($this->input && ! ($this->input->getOption('nolog'))) { if ($this->input && ! ($this->input->getOption('nolog'))) {
file_put_contents($logdir . "scheduler_l.log", $message . "\n", FILE_APPEND); file_put_contents($logdir . "scheduler_l.log", $message . "\n", FILE_APPEND);
} }

View File

@@ -138,7 +138,7 @@ abstract class task_abstract
, self::STATUS_TOSTART , self::STATUS_TOSTART
); );
if ( ! in_array($status, $av_status)) if ( ! in_array($status, $av_status))
throw new Exception(sprintf('unknown status `%s`', $status)); throw new Exception_InvalidArgument(sprintf('unknown status `%s`', $status));
$conn = connection::getPDOConnection(); $conn = connection::getPDOConnection();
@@ -400,6 +400,18 @@ abstract class task_abstract
return $this; return $this;
} }
public function get_last_exec_time()
{
$conn = connection::getPDOConnection();
$sql = 'SELECT last_exec_time FROM task2 WHERE task_id = :taskid';
$stmt = $conn->prepare($sql);
$stmt->execute(array(':taskid' => $this->get_task_id()));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
return isset($row['last_exec_time']) ? $row['last_exec_time'] : '';
}
public function get_pid() public function get_pid()
{ {
$pid = NULL; $pid = NULL;

View File

@@ -120,7 +120,7 @@ class task_manager
$tasks = $this->get_tasks(); $tasks = $this->get_tasks();
if ( ! isset($tasks[$task_id])) if ( ! isset($tasks[$task_id]))
throw new Exception('Unknown task_id'); throw new Exception_NotFound('Unknown task_id');
return $tasks[$task_id]; return $tasks[$task_id];
} }