dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
run_job.php
Go to the documentation of this file.
1<?php
13$base = dirname(__DIR__, 4);
14chdir($base);
15$_SERVER['REQUEST_URI'] = '/dbxapp/';
16$_SERVER['HTTP_HOST'] = 'localhost';
17$_SERVER['HTTPS'] = 'on';
18$_SERVER['SCRIPT_NAME'] = '/dbxapp/index.php';
19
20define('dbxSystem', 'dbxWebApp');
21define('dbxRunAsAdmin', 1);
22
23require $base . '/dbx/vendor/autoload.php';
24require_once $base . '/dbx/include/dbxKernel.php';
25require_once $base . '/dbx/modules/dbxKi/include/dbxKiCmsService.class.php';
26
27function dbxki_usage(): void {
28 fwrite(STDERR, "Usage: php dbx/modules/dbxKi/tools/run_job.php job.json|-\n");
29}
30
31function dbxki_read_job(array $argv): array {
32 $source = (string)($argv[1] ?? '');
33 if ($source === '') {
35 exit(2);
36 }
37
38 if ($source === '-') {
39 $json = stream_get_contents(STDIN);
40 } else {
41 $file = dbx()->os_path($source);
42 if (!is_file($file)) {
43 throw new RuntimeException('Job-Datei nicht gefunden: ' . $source);
44 }
45 $json = file_get_contents($file);
46 }
47
48 $job = json_decode((string)$json, true);
49 if (!is_array($job)) {
50 throw new RuntimeException('Job-JSON ist ungueltig.');
51 }
52 if (!is_array($job['steps'] ?? null)) {
53 throw new RuntimeException('Job benoetigt steps[].');
54 }
55 return $job;
56}
57
58function dbxki_result_ref(string $ref, array $results) {
59 $path = substr($ref, 5);
60 $parts = explode('.', $path);
61 $step = array_shift($parts);
62 if ($step === '' || !array_key_exists($step, $results)) {
63 throw new RuntimeException('Unbekannte Step-Referenz: ' . $ref);
64 }
65 $value = $results[$step];
66 foreach ($parts as $part) {
67 if (is_array($value) && array_key_exists($part, $value)) {
68 $value = $value[$part];
69 continue;
70 }
71 throw new RuntimeException('Unbekanntes Referenzfeld: ' . $ref);
72 }
73 return $value;
74}
75
76function dbxki_prepare_asset_file(array $params): array {
77 $assetFile = trim((string)($params['asset_file'] ?? ''));
78 if ($assetFile === '') {
79 return $params;
80 }
81
82 $file = dbx()->os_path($assetFile);
83 if (!is_file($file) || !is_readable($file)) {
84 throw new RuntimeException('asset_file nicht lesbar: ' . $assetFile);
85 }
86 if (empty($params['file_name'])) {
87 $params['file_name'] = basename($file);
88 }
89 $params['data_base64'] = base64_encode(file_get_contents($file));
90 unset($params['asset_file']);
91 return $params;
92}
93
95 if (is_array($value)) {
96 $out = array();
97 foreach ($value as $key => $item) {
98 $out[$key] = dbxki_resolve_params($item, $results);
99 }
101 }
102
103 if (!is_string($value)) {
104 return $value;
105 }
106
107 if (strpos($value, '$ref:') === 0) {
109 }
110
111 if (strpos($value, '$ref:') !== false) {
112 return preg_replace_callback('/\$ref:([A-Za-z0-9_.-]+)/', function($match) use ($results) {
113 return (string)dbxki_result_ref('$ref:' . $match[1], $results);
114 }, $value);
115 }
116
117 return $value;
118}
119
120function dbxki_normalize_result(string $action, array $result): array {
121 $out = $result;
122 if (!empty($result['id'])) {
123 $out['id'] = (int)$result['id'];
124 }
125 if (strpos($action, 'page.') === 0 && !empty($result['id'])) {
126 $out['page_id'] = (int)$result['id'];
127 }
128 if (strpos($action, 'folder.') === 0 && !empty($result['id'])) {
129 $out['folder_id'] = (int)$result['id'];
130 }
131 if (strpos($action, 'media.create') === 0 && !empty($result['id'])) {
132 $out['media_id'] = (int)$result['id'];
133 }
134 if ($action === 'media.assign' && !empty($result['usage_id'])) {
135 $out['usage_id'] = (int)$result['usage_id'];
136 }
137 return $out;
138}
139
140try {
141 $job = dbxki_read_job($argv);
142 $cms = new \dbx\dbxKi\dbxKiCmsService();
143 $results = array();
144
145 foreach ($job['steps'] as $pos => $step) {
146 if (!is_array($step)) {
147 throw new RuntimeException('Step #' . ($pos + 1) . ' ist ungueltig.');
148 }
149 $stepId = trim((string)($step['id'] ?? ('step_' . ($pos + 1))));
150 $action = trim((string)($step['action'] ?? ''));
151 if ($stepId === '' || $action === '') {
152 throw new RuntimeException('Step #' . ($pos + 1) . ' benoetigt id und action.');
153 }
154 if (!$cms->bundleIsAllowedInPackage($action)) {
155 throw new RuntimeException('Aktion ist fuer direkte Jobs nicht freigegeben: ' . $action);
156 }
157
158 $params = dbxki_resolve_params(is_array($step['params'] ?? null) ? $step['params'] : array(), $results);
159 $plan = $cms->bundleBuildPlan($action, $params);
160 $result = $cms->bundleExecutePlan($action, $params, $plan);
161 $results[$stepId] = dbxki_normalize_result($action, $result);
162 }
163
164 echo json_encode(array('ok' => 1, 'results' => $results), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
165 exit(0);
166} catch (Throwable $e) {
167 echo json_encode(array('ok' => 0, 'error' => $e->getMessage()), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
168 exit(1);
169}
$_SERVER['REQUEST_METHOD']
exit
Definition index.php:146
DBX schema administration.
$results
Definition run_job.php:143
dbxki_read_job(array $argv)
Definition run_job.php:31
dbxki_resolve_params($value, array $results)
Definition run_job.php:94
dbxki_result_ref(string $ref, array $results)
Definition run_job.php:58
dbxki_normalize_result(string $action, array $result)
Definition run_job.php:120
dbxki_prepare_asset_file(array $params)
Definition run_job.php:76
dbxki_usage()
Definition run_job.php:27