fixed a bug in check-edit-response where it would never find success
[cl-mediawiki.git] / src / edit.lisp
blob09bde351b1fdfa88a4d462cfa1c414af239be8cc
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 (check-sxml-for-error xml)
8 (let* ((kid (first
9 (find-nodes-by-name "edit" xml)))
10 (alist (second kid)))
11 (unless alist
12 (error 'media-wiki-error
13 :obj xml
14 :message "Couldnt find edit results"))
15 (unless (string-equal "success" (sxml-attribute-value "result" alist))
16 (error 'media-wiki-error
17 :message (format nil "Failed to Edit ~A : ~A "
18 title alist)
19 :code nil
20 :obj xml))))
22 (defun create-page
23 (title text &key
24 (summary "cl-mediawiki:create-page")
25 (override nil))
26 "Creates a new wiki page
27 If override is true, replace the existing page with the text passed in (if the page already exists)
29 (let* ((tokens (get-action-tokens title))
30 (parameters
31 (make-parameters
32 `((action edit)
33 (token ,(edit-token tokens))
34 (title ,title)
35 (basetimestamp ,(timestamp tokens))
36 ,(when (not override)
37 '(createonly true))
38 (summary ,summary)
39 (text ,text)))
41 (check-edit-response
42 title
43 (parse-api-response-to-sxml (make-api-request parameters :method :post)))))
45 (defun add-new-page-section (title section-title section-text &key no-create)
46 "Creates a new == section-title == at the bottom of the page. followed by the specified text"
47 (let* ((tokens (get-action-tokens title))
48 (parameters
49 (make-parameters
50 `((action edit)
51 (token ,(edit-token tokens))
52 (title ,title)
53 (section new)
54 (summary ,section-title)
55 (basetimestamp ,(timestamp tokens))
56 ,(when no-create
57 '(nocreate T))
58 (text ,section-text)))
60 (check-edit-response
61 title
62 (parse-api-response-to-sxml
63 (make-api-request parameters :method :post)))
66 (defun append-text-to-page
67 (title text &key
68 no-create
69 (summary "cl-mediawiki:append-text-to-page"))
70 "appends the text the the end of the page (will create the page if neccessary, unless no-create is passed)"
71 (let* ((tokens (get-action-tokens title))
72 (parameters
73 (make-parameters
74 `((action edit)
75 (token ,(edit-token tokens))
76 (title ,title)
77 (summary ,summary)
78 (basetimestamp ,(timestamp tokens))
79 ,(when no-create
80 '(nocreate T))
81 (appendtext ,text)))
83 (check-edit-response
84 title
85 (parse-api-response-to-sxml
86 (make-api-request parameters :method :post)))
89 (defun prepend-text-to-page
90 (title text &key
91 (summary "cl-mediawiki:prepend-text-to-page")
92 no-create)
93 "Adds the text to the beginning of the page named title
94 (will create the page if neccessary unless no-create is true)"
95 (let* ((tokens (get-action-tokens title))
96 (parameters
97 (make-parameters
98 `((action edit)
99 (token ,(edit-token tokens))
100 (title ,title)
101 (summary ,summary)
102 (basetimestamp ,(timestamp tokens))
103 ,(when no-create
104 '(nocreate T))
105 (prependtext ,text)))
107 (check-edit-response
108 title
109 (parse-api-response-to-sxml
110 (make-api-request parameters :method :post)))
113 (defun set-page-content
114 (title text &key no-create
115 (summary "cl-mediawiki:set-page-content"))
116 "sets the text of a wiki page 'title' to the specified 'text',
118 title: The wiki page to set the content of
119 text: The new content that the wiki page should have
120 no-create:, do not create the wiki page if it does not exist
121 summary: The comment associated with changing the page content
123 (let* ((tokens (get-action-tokens title))
124 (parameters
125 (make-parameters
126 `((action edit)
127 (token ,(edit-token tokens))
128 (title ,title)
129 (summary ,summary)
130 (basetimestamp ,(timestamp tokens))
131 ,(when no-create
132 '(nocreate T))
133 (text ,text)))
135 (check-edit-response
136 title
137 (parse-api-response-to-sxml
138 (make-api-request parameters :method :post)))
141 #+cl-ppcre
142 (defun regex-replace-all (regex target-page replacement &key default-content (summary "cl-mediawiki:regex-replace-all"))
143 "Does a regex find/replace on the target page. If the page is empty, will set to default content if provided
144 Works by calling get-content then regex-replacing on the content, then calling set-content "
145 (let ((content (get-page-content target-page)))
146 (set-page-content target-page
147 (if content
148 (cl-ppcre:regex-replace-all regex content replacement)
149 default-content)
150 :no-create (null default-content)
151 :summary summary)))
156 ;; Copyright (c) 2008 Accelerated Data Works, Russ Tyndall
158 ;; Permission is hereby granted, free of charge, to any person
159 ;; obtaining a copy of this software and associated documentation files
160 ;; (the "Software"), to deal in the Software without restriction,
161 ;; including without limitation the rights to use, copy, modify, merge,
162 ;; publish, distribute, sublicense, and/or sell copies of the Software,
163 ;; and to permit persons to whom the Software is furnished to do so,
164 ;; subject to the following conditions:
166 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
167 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
168 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
169 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
170 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
171 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
172 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.