Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
160 lines (128 loc) · 5.17 KB
/
Copy pathindex.php
File metadata and controls
160 lines (128 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Brd6\NotionSdkPhp\Client;
use Brd6\NotionSdkPhp\ClientOptions;
use Brd6\NotionSdkPhp\Exception\ApiResponseException;
use Brd6\NotionSdkPhp\Resource\DataSource;
use Brd6\NotionSdkPhp\Resource\Database\DatabaseRequest;
use Brd6\NotionSdkPhp\Resource\Database\PropertyObject\TitlePropertyObject;
use Brd6\NotionSdkPhp\Resource\Page\Parent\DatabaseIdParent;
use Brd6\NotionSdkPhp\Resource\Pagination\PaginationRequest;
use Brd6\NotionSdkPhp\Resource\RichText\Text;
use Brd6\NotionSdkPhp\Resource\Search\SearchRequest;
use Dotenv\Dotenv;
function exitWithError(string $message): void
{
echo "Error: {$message}\n";
exit(1);
}
function loadEnvironmentVariables(): void
{
if (!file_exists(__DIR__ . '/.env')) {
exitWithError('Missing .env file. Copy .env.example to .env and configure it.');
}
Dotenv::createImmutable(__DIR__)->load();
}
function validateRequiredEnvironmentVariables(): void
{
$requiredVars = ['NOTION_TOKEN', 'NOTION_DATABASE_ID'];
foreach ($requiredVars as $requiredVar) {
if (empty($_ENV[$requiredVar])) {
exitWithError("Missing required environment variable: {$requiredVar}");
}
}
}
function createNotionClient(): Client
{
$options = (new ClientOptions())
->setAuth($_ENV['NOTION_TOKEN'])
->setNotionVersion('2025-09-03');
return new Client($options);
}
function isWriteModeEnabled(): bool
{
$val = getenv('NOTION_RUN_WRITES') ?: ($_ENV['NOTION_RUN_WRITES'] ?? '0');
return in_array((string) $val, ['1', 'true', 'yes'], true);
}
function resolveDataSourceId(Client $notion, string $databaseId): string
{
$database = $notion->databases()->retrieve($databaseId);
$dataSources = $database->getDataSources();
echo "Database retrieved: {$database->getId()}\n";
echo "Data sources linked to database: " . count($dataSources) . "\n";
$envDataSourceId = (string) ($_ENV['NOTION_DATA_SOURCE_ID'] ?? '');
if ($envDataSourceId !== '') {
echo "Using data source from NOTION_DATA_SOURCE_ID.\n";
return $envDataSourceId;
}
if (!isset($dataSources[0])) {
exitWithError('No data source found in the database response. Set NOTION_DATA_SOURCE_ID manually.');
}
$resolvedDataSourceId = $dataSources[0]->getId();
echo "Using first discovered data source id: {$resolvedDataSourceId}\n";
return $resolvedDataSourceId;
}
function runReadOnlyChecks(Client $notion, string $dataSourceId): void
{
$dataSource = $notion->dataSources()->retrieve($dataSourceId);
echo "Data source retrieved: {$dataSource->getId()}\n";
$queryRequest = (new DatabaseRequest())->setFilter([
'property' => 'Name',
'title' => ['is_not_empty' => true],
]);
$paginationRequest = (new PaginationRequest())->setPageSize(1);
$queryResults = $notion->dataSources()->query($dataSourceId, $queryRequest, $paginationRequest);
echo "Data source query ok. Returned pages: " . count($queryResults->getResults()) . "\n";
$searchRequest = (new SearchRequest())->setFilter([
'property' => 'object',
'value' => 'data_source',
]);
$searchResults = $notion->search($searchRequest);
echo "Search (object=data_source) ok. Returned items: " . count($searchResults->getResults()) . "\n";
}
function runWriteChecks(Client $notion, string $databaseId): void
{
echo "Write mode is enabled. Running create/update checks...\n";
$baseName = 'SDK Integration Check ' . date('Y-m-d H:i:s');
$created = $notion->dataSources()->create(
(new DataSource())
->setParent((new DatabaseIdParent())->setDatabaseId($databaseId))
->setTitle([Text::fromContent($baseName)])
->setProperties(['Name' => new TitlePropertyObject()]),
);
echo "Data source created: {$created->getId()}\n";
$dataSourceForUpdate = new DataSource();
$dataSourceForUpdate->setId($created->getId());
$updated = $notion->dataSources()->update(
$dataSourceForUpdate
->setTitle([Text::fromContent($baseName . ' Updated')])
->setProperties(['Name' => new TitlePropertyObject()])
->setInTrash(true),
);
echo 'Data source updated. In trash: ' . ($updated->isInTrash() ? 'yes' : 'no') . "\n";
}
function main(): void
{
try {
echo "Notion SDK PHP - Data Sources API Integration\n";
echo "=======================================\n\n";
loadEnvironmentVariables();
validateRequiredEnvironmentVariables();
$notion = createNotionClient();
$databaseId = (string) $_ENV['NOTION_DATABASE_ID'];
$dataSourceId = resolveDataSourceId($notion, $databaseId);
runReadOnlyChecks($notion, $dataSourceId);
if (isWriteModeEnabled()) {
runWriteChecks($notion, $databaseId);
} else {
echo "Write mode disabled. Skipping create/update checks.\n";
}
echo "\nIntegration checks completed.\n";
} catch (ApiResponseException $exception) {
exitWithError('Notion API error: ' . $exception->getMessage());
} catch (Exception $exception) {
exitWithError($exception->getMessage());
}
}
main();