37
37
38
38
# Section1: Create, read, and update wiki pages
39
39
# Create a new wiki page for the project with plain text markdown
40
- wiki_page_1 = WikiPage (
40
+ root_wiki_page = WikiPage (
41
41
owner_id = my_test_project .id ,
42
42
title = "My Root Wiki Page" ,
43
43
markdown = "# Welcome to My Root Wiki\n \n This is a sample root wiki page created with the Synapse client." ,
44
44
).store ()
45
45
46
- # OR you can create a wiki page with an existing markdown file
46
+ # OR you can create a wiki page with an existing markdown file. More instructions can be found in section 2.
47
47
markdown_file_path = "path/to/your_markdown_file.md"
48
- wiki_page_1 = WikiPage (
48
+ root_wiki_page = WikiPage (
49
49
owner_id = my_test_project .id ,
50
50
title = "My First Root Wiki Page Version with existing markdown file" ,
51
51
markdown = markdown_file_path ,
52
52
).store ()
53
53
54
54
# Create a new wiki page with updated content
55
- wiki_page_2 = WikiPage (
55
+ root_wiki_page_new = WikiPage (
56
56
owner_id = my_test_project .id ,
57
- title = "My First Root Wiki Page Version 1 " ,
58
- markdown = "# Welcome to My Root Wiki Version 1 \n \n This is a sample root wiki page created with the Synapse client." ,
59
- id = wiki_page_1 .id ,
57
+ title = "My First Root Wiki Page NEW " ,
58
+ markdown = "# Welcome to My Root Wiki NEW \n \n This is a sample root wiki page created with the Synapse client." ,
59
+ id = root_wiki_page .id ,
60
60
).store ()
61
61
62
62
# Restore the wiki page to the original version
63
63
wiki_page_restored = WikiPage (
64
- owner_id = my_test_project .id , id = wiki_page_1 .id , wiki_version = "0"
64
+ owner_id = my_test_project .id , id = root_wiki_page .id , wiki_version = "0"
65
65
).restore ()
66
66
67
67
# check if the content is restored
68
68
comparisons = [
69
- wiki_page_1 .markdown_file_handle_id == wiki_page_restored .markdown_file_handle_id ,
70
- wiki_page_1 .id == wiki_page_restored .id ,
71
- wiki_page_1 .title == wiki_page_restored .title ,
69
+ root_wiki_page .markdown_file_handle_id
70
+ == wiki_page_restored .markdown_file_handle_id ,
71
+ root_wiki_page .id == wiki_page_restored .id ,
72
+ root_wiki_page .title == wiki_page_restored .title ,
72
73
]
73
74
print (f"All fields match: { all (comparisons )} " )
74
75
75
76
# Create a sub-wiki page
76
- sub_wiki = WikiPage (
77
+ sub_wiki_1 = WikiPage (
77
78
owner_id = my_test_project .id ,
78
79
title = "Sub Wiki Page 1" ,
79
- parent_id = wiki_page_1 .id , # Use the ID of the parent wiki page we created '633033'
80
+ parent_id = root_wiki_page .id , # Use the ID of the parent wiki page we created '633033'
80
81
markdown = "# Sub Page 1\n \n This is a sub-page of another wiki." ,
81
82
).store ()
82
83
85
86
print (wiki_header_tree )
86
87
87
88
# Once you know the wiki page id, you can retrieve the wiki page with the id
88
- retrieved_wiki = WikiPage (owner_id = my_test_project .id , id = sub_wiki .id ).get ()
89
+ retrieved_wiki = WikiPage (owner_id = my_test_project .id , id = sub_wiki_1 .id ).get ()
89
90
print (f"Retrieved wiki page with title: { retrieved_wiki .title } " )
90
91
91
92
# Or you can retrieve the wiki page with the title
92
- retrieved_wiki = WikiPage (owner_id = my_test_project .id , title = wiki_page_1 .title ).get ()
93
+ retrieved_wiki = WikiPage (owner_id = my_test_project .id , title = sub_wiki_1 .title ).get ()
93
94
print (f"Retrieved wiki page with title: { retrieved_wiki .title } " )
94
95
95
96
# Check if the retrieved wiki page is the same as the original wiki page
96
97
comparisons = [
97
- wiki_page_1 .markdown_file_handle_id == retrieved_wiki .markdown_file_handle_id ,
98
- wiki_page_1 .id == retrieved_wiki .id ,
99
- wiki_page_1 .title == retrieved_wiki .title ,
98
+ sub_wiki_1 .markdown_file_handle_id == retrieved_wiki .markdown_file_handle_id ,
99
+ sub_wiki_1 .id == retrieved_wiki .id ,
100
+ sub_wiki_1 .title == retrieved_wiki .title ,
100
101
]
101
102
print (f"All fields match: { all (comparisons )} " )
102
103
@@ -120,9 +121,9 @@ def hello_world():
120
121
"""
121
122
122
123
# Create wiki page from markdown text
123
- markdown_wiki_1 = WikiPage (
124
+ sub_wiki_2 = WikiPage (
124
125
owner_id = my_test_project .id ,
125
- parent_id = wiki_page_1 .id ,
126
+ parent_id = root_wiki_page .id ,
126
127
title = "Sub Page 2 created from markdown text" ,
127
128
markdown = markdown_content ,
128
129
).store ()
@@ -134,111 +135,150 @@ def hello_world():
134
135
gz .write ("This is a markdown file" )
135
136
136
137
# Create wiki page from markdown file
137
- markdown_wiki_2 = WikiPage (
138
+ sub_wiki_3 = WikiPage (
138
139
owner_id = my_test_project .id ,
139
- parent_id = wiki_page_1 .id ,
140
+ parent_id = root_wiki_page .id ,
140
141
title = "Sub Page 3 created from markdown file" ,
141
142
markdown = markdown_file_path ,
142
143
).store ()
143
144
144
145
# Download the markdown file
145
- # delete the markdown file after downloading
146
+ # delete the markdown file after uploading to test the download function
147
+ os .remove (markdown_file_path )
148
+ # Note: If the markdown is generated from plain text using the client, the downloaded file will be named wiki_markdown_<wiki_page_title>.md.gz. If it is generated from an existing markdown file, the downloaded file will retain the original filename with the .gz suffix appended.
149
+ # Download the markdown file for sub_wiki_2 that is created from markdown text
150
+ wiki_page_markdown_2 = WikiPage (
151
+ owner_id = my_test_project .id , id = sub_wiki_2 .id
152
+ ).get_markdown (
153
+ download_file = True ,
154
+ download_location = "." ,
155
+ download_file_name = f"wiki_markdown_{ sub_wiki_2 .title } .md.gz" ,
156
+ )
157
+ print (
158
+ f"Wiki page markdown for sub_wiki_2 successfully downloaded: { os .path .exists (f'wiki_markdown_{ sub_wiki_2 .title } .md.gz' )} "
159
+ )
160
+ # clean up the downloaded markdown file
161
+ os .remove (f"wiki_markdown_{ sub_wiki_2 .title } .md.gz" )
162
+
163
+ # Download the markdown file for sub_wiki_3 that is created from a markdown file
164
+ wiki_page_markdown_3 = WikiPage (
165
+ owner_id = my_test_project .id , id = sub_wiki_3 .id
166
+ ).get_markdown (
167
+ download_file = True , download_location = "." , download_file_name = markdown_file_path
168
+ )
169
+ print (
170
+ f"Wiki page markdown for sub_wiki_3 successfully downloaded: { os .path .exists (markdown_file_path )} "
171
+ )
172
+ # clean up the downloaded markdown file
146
173
os .remove (markdown_file_path )
147
- markdown_file_2 = WikiPage (
148
- owner_id = my_test_project .id , id = markdown_wiki_2 .id
149
- ).get_markdown (download_file = True , download_location = "." )
150
-
151
- print (f"Markdown file downloaded to: { markdown_file_2 } " )
152
174
153
175
# Section 3: WikiPage with Attachments
154
176
# Create a temporary file for the attachment
155
177
attachment_file_name = "temp_attachment.txt.gz"
156
178
with gzip .open (attachment_file_name , "wt" , encoding = "utf-8" ) as gz :
157
179
gz .write ("This is a sample attachment." )
158
180
159
- # reformat the attachment file name to be a valid attachment path
181
+ # reformat '.' and '_' in the attachment file name to be a valid attachment path
160
182
attachment_file_name_reformatted = attachment_file_name .replace ("." , "%2E" )
161
183
attachment_file_name_reformatted = attachment_file_name_reformatted .replace ("_" , "%5F" )
162
184
163
- wiki_with_attachments = WikiPage (
185
+ sub_wiki_4 = WikiPage (
164
186
owner_id = my_test_project .id ,
165
- parent_id = wiki_page_1 .id ,
187
+ parent_id = root_wiki_page .id ,
166
188
title = "Sub Page 4 with Attachments" ,
167
189
markdown = f"# Sub Page 4 with Attachments\n \n This is a attachment: ${{previewattachment?fileName={ attachment_file_name_reformatted } }}" ,
168
190
attachments = [attachment_file_name ],
169
191
).store ()
170
192
171
193
# Get attachment handles
172
194
attachment_handles = WikiPage (
173
- owner_id = my_test_project .id , id = wiki_with_attachments .id
195
+ owner_id = my_test_project .id , id = sub_wiki_4 .id
174
196
).get_attachment_handles ()
175
- print (f"Found { len (attachment_handles ['list' ])} attachments" )
197
+ print (f"Attachment handles: { attachment_handles ['list' ]} " )
198
+
199
+ # Get attachment URL without downloading
200
+ wiki_page_attachment_url = WikiPage (
201
+ owner_id = my_test_project .id , id = sub_wiki_4 .id
202
+ ).get_attachment (
203
+ file_name = "temp_attachment.txt.gz" ,
204
+ download_file = False ,
205
+ )
206
+ print (f"Attachment URL: { wiki_page_attachment_url } " )
176
207
177
- # Delete the attachment file after uploading --> check if the file is deleted
178
- os .remove (attachment_file_name )
179
208
# Download an attachment
180
- wiki_page = WikiPage (
181
- owner_id = my_test_project .id , id = wiki_with_attachments .id
209
+ # Delete the attachment file after uploading to test the download function
210
+ os .remove (attachment_file_name )
211
+ wiki_page_attachment = WikiPage (
212
+ owner_id = my_test_project .id , id = sub_wiki_4 .id
182
213
).get_attachment (
183
214
file_name = attachment_file_name ,
184
215
download_file = True ,
185
216
download_location = "." ,
186
217
)
187
218
print (f"Attachment downloaded: { os .path .exists (attachment_file_name )} " )
219
+ os .remove (attachment_file_name )
188
220
189
- # Get attachment URL without downloading
190
- wiki_page_url = WikiPage (
191
- owner_id = my_test_project .id , id = wiki_with_attachments .id
192
- ).get_attachment (
221
+ # Download an attachment preview. Instead of using the file_name from the attachmenthandle response when isPreview=True, you should use the original file name in the get_attachment_preview request. The downloaded file will still be named according to the file_name provided in the response when isPreview=True.
222
+ # Get attachment preview URL without downloading
223
+ attachment_preview_url = WikiPage (
224
+ owner_id = my_test_project .id , id = sub_wiki_4 .id
225
+ ).get_attachment_preview (
193
226
file_name = "temp_attachment.txt.gz" ,
194
227
download_file = False ,
195
228
)
196
- print (f"Attachment URL: { wiki_page_url } " )
229
+ print (f"Attachment preview URL: { attachment_preview_url } " )
197
230
198
- # Download an attachment preview--? Failed to download the attachment preview, synapseclient.core.exceptions.SynapseHTTPError: 404 Client Error: Cannot find a wiki attachment for OwnerID: syn68493645, ObjectType: ENTITY, WikiPageId: 633100, fileName: preview.txt
199
- attachment_handles = WikiPage (
200
- owner_id = my_test_project .id , id = wiki_with_attachments .id
201
- ).get_attachment_handles ()
202
- print (f"Attachment handles: { attachment_handles } " )
203
- wiki_page = WikiPage (
204
- owner_id = my_test_project .id , id = wiki_with_attachments .id
231
+ # Download an attachment preview
232
+ attachment_preview = WikiPage (
233
+ owner_id = my_test_project .id , id = sub_wiki_4 .id
205
234
).get_attachment_preview (
206
- file_name = "preview .txt" ,
235
+ file_name = "temp_attachment .txt.gz " ,
207
236
download_file = True ,
208
237
download_location = "." ,
209
238
)
239
+ # From the attachment preview URL or attachment handle response, the downloaded preview file is preview.txt
240
+ os .remove ("preview.txt" )
210
241
211
242
# Section 4: WikiHeader - Working with Wiki Hierarchy
212
-
213
243
# Get wiki header tree (hierarchy)
214
- # Note: Uncomment to actually get the header tree
215
244
headers = WikiHeader .get (owner_id = my_test_project .id )
216
245
print (f"Found { len (headers )} wiki pages in hierarchy" )
246
+ print (f"Wiki header tree: { headers } " )
217
247
218
248
# Section 5. WikiHistorySnapshot - Version History
219
- # Get wiki history
220
- history = WikiHistorySnapshot .get (owner_id = my_test_project .id , id = wiki_page_1 .id )
221
-
222
- print (f"Found { len (history )} versions in history for { wiki_page_1 .title } " )
249
+ # Get wiki history for root_wiki_page
250
+ history = WikiHistorySnapshot .get (owner_id = my_test_project .id , id = root_wiki_page .id )
251
+ print (f"History: { history } " )
223
252
224
253
# Section 6. WikiOrderHint - Ordering Wiki Pages
225
- # Get wiki order hint --> failed to get the order hint
254
+ # Set the wiki order hint
226
255
order_hint = WikiOrderHint (owner_id = my_test_project .id ).get ()
256
+ print (f"Order hint for { my_test_project .id } : { order_hint .id_list } " )
257
+ # As you can see from the printed message, the order hint is not set by default, so you need to set it explicitly at the beginning.
258
+ order_hint .id_list = [
259
+ root_wiki_page .id ,
260
+ sub_wiki_3 .id ,
261
+ sub_wiki_4 .id ,
262
+ sub_wiki_1 .id ,
263
+ sub_wiki_2 .id ,
264
+ ]
265
+ order_hint .store ()
227
266
print (f"Order hint for { my_test_project .id } : { order_hint } " )
228
267
229
268
# Update wiki order hint
230
- order_hint .id_list = [wiki_page_1 .id ]
231
-
232
- print (f"Created order hint for { len (order_hint .id_list )} wiki pages" )
233
-
234
- # Update order hint
235
- order_hint .id_list = ["633084" , "633085" , "633086" , "633087" , "633088" ] # Reorder
269
+ order_hint = WikiOrderHint (owner_id = my_test_project .id ).get ()
270
+ order_hint .id_list = [
271
+ root_wiki_page .id ,
272
+ sub_wiki_1 .id ,
273
+ sub_wiki_2 .id ,
274
+ sub_wiki_3 .id ,
275
+ sub_wiki_4 .id ,
276
+ ]
236
277
order_hint .store ()
278
+ print (f"Order hint for { my_test_project .id } : { order_hint } " )
237
279
238
280
# Delete a wiki page
239
- wiki_page_to_delete = WikiPage (
240
- owner_id = my_test_project .id , id = wiki_with_attachments .id
241
- ).delete ()
281
+ wiki_page_to_delete = WikiPage (owner_id = my_test_project .id , id = sub_wiki_3 .id ).delete ()
242
282
243
283
# clean up
244
284
my_test_project .delete ()
0 commit comments