improved some edit docstrings fix #12
[cl-mediawiki.git] / src / edit.lisp
blobf549168c127bb9bcc38966f66ee7fae9d72f9e39
1 (in-package :cl-mediawiki)
3 (defun check-edit-response (title xml)
4 "Checks for the expected 'success' message
5 signals match-errors assertion-errors and media-wiki-errors
7 (match-response-with-error-reporting
8 (`("api"
9 NIL
10 ("edit" ?alist))
11 xml)
12 (assert (string-equal "success" (sxml-attribute-value "result" alist))
13 (alist) "Failed to create page: '~a' ~a ~a ~%~%~A" title (sxml-attribute-value "result" alist) (string-equal "success "(sxml-attribute-value "result" alist)) alist)
18 (defun create-page
19 (title text &key
20 (summary "cl-mediawiki:create-page")
21 (override nil))
22 "Creates a new wiki page
23 If override is true, replace the existing page with the text passed in (if the page already exists)
25 (let* ((tokens (get-action-tokens title))
26 (parameters
27 (make-parameters
28 `((action edit)
29 (token ,(edit-token tokens))
30 (title ,title)
31 (basetimestamp ,(timestamp tokens))
32 ,(when (not override)
33 '(createonly true))
34 (summary ,summary)
35 (text ,text)))
37 (check-edit-response
38 title
39 (parse-api-response-to-sxml (make-api-request parameters :method :post))))
43 (defun add-new-page-section (title section-title section-text &key no-create)
44 "Creates a new == section-title == at the bottom of the page. followed by the specified text"
45 (let* ((tokens (get-action-tokens title))
46 (parameters
47 (make-parameters
48 `((action edit)
49 (token ,(edit-token tokens))
50 (title ,title)
51 (section new)
52 (summary ,section-title)
53 (basetimestamp ,(timestamp tokens))
54 ,(when no-create
55 '(nocreate T))
56 (text ,section-text)))
58 (check-edit-response
59 title
60 (parse-api-response-to-sxml
61 (make-api-request parameters :method :post)))
64 (defun append-text-to-page
65 (title text &key
66 no-create
67 (summary "cl-mediawiki:append-text-to-page"))
68 "appends the text the the end of the page (will create the page if neccessary, unless no-create is passed)"
69 (let* ((tokens (get-action-tokens title))
70 (parameters
71 (make-parameters
72 `((action edit)
73 (token ,(edit-token tokens))
74 (title ,title)
75 (summary ,summary)
76 (basetimestamp ,(timestamp tokens))
77 ,(when no-create
78 '(nocreate T))
79 (appendtext ,text)))
81 (check-edit-response
82 title
83 (parse-api-response-to-sxml
84 (make-api-request parameters :method :post)))
87 (defun prepend-text-to-page
88 (title text &key
89 (summary "cl-mediawiki:prepend-text-to-page")
90 no-create)
91 "Adds the text to the beginning of the page named title
92 (will create the page if neccessary unless no-create is true)"
93 (let* ((tokens (get-action-tokens title))
94 (parameters
95 (make-parameters
96 `((action edit)
97 (token ,(edit-token tokens))
98 (title ,title)
99 (summary ,summary)
100 (basetimestamp ,(timestamp tokens))
101 ,(when no-create
102 '(nocreate T))
103 (prependtext ,text)))
105 (check-edit-response
106 title
107 (parse-api-response-to-sxml
108 (make-api-request parameters :method :post)))
111 (defun set-page-content
112 (title text &key no-create
113 (summary "cl-mediawiki:set-page-content"))
114 "sets the text of a wiki page 'title' to the specified 'text',
116 title: The wiki page to set the content of
117 text: The new content that the wiki page should have
118 no-create:, do not create the wiki page if it does not exist
119 summary: The comment associated with changing the page content
121 (let* ((tokens (get-action-tokens title))
122 (parameters
123 (make-parameters
124 `((action edit)
125 (token ,(edit-token tokens))
126 (title ,title)
127 (summary ,summary)
128 (basetimestamp ,(timestamp tokens))
129 ,(when no-create
130 '(nocreate T))
131 (text ,text)))
133 (check-edit-response
134 title
135 (parse-api-response-to-sxml
136 (make-api-request parameters :method :post)))
139 (defun regex-replace-all (regex target-page replacement &key default-content (summary "cl-mediawiki:regex-replace-all"))
140 "Does a regex find/replace on the target page. If the page is empty, will set to default content if provided
141 Works by calling get-content then regex-replacing on the content, then calling set-content "
142 (let ((content (get-page-content target-page)))
143 (set-page-content target-page
144 (if content
145 (cl-ppcre:regex-replace-all regex content replacement)
146 default-content)
147 :no-create (null default-content)
148 :summary summary)))
153 ;; Copyright (c) 2008 Accelerated Data Works, Russ Tyndall
155 ;; Permission is hereby granted, free of charge, to any person
156 ;; obtaining a copy of this software and associated documentation files
157 ;; (the "Software"), to deal in the Software without restriction,
158 ;; including without limitation the rights to use, copy, modify, merge,
159 ;; publish, distribute, sublicense, and/or sell copies of the Software,
160 ;; and to permit persons to whom the Software is furnished to do so,
161 ;; subject to the following conditions:
163 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
164 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
165 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
166 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
167 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
168 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
169 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.