-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwikidot.js
159 lines (129 loc) · 3.8 KB
/
wikidot.js
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
/* jshint node:true */
// Wikidot API library for node.js
// by Jay Bienvenu
var XmlRpc = require('xmlrpc'); // https://www.npmjs.org/package/xmlrpc
exports.ContentTypes = {
'General' : 0,
'DataForm' : 1,
'LiveTemplate' : 2
};
exports.username = ''; // name of the user holding the API key
exports.apiKey = '';
exports.site = ''; // optional
// next: function(error,value)
exports.call = function(method,parameters,next) {
clientOptions = {
'host': 'www.wikidot.com',
'port': 443,
'path': '/xml-rpc-api.php',
'basic_auth': {
'user': exports.username,
'pass': exports.apiKey
}
};
var client = XmlRpc.createSecureClient(clientOptions);
client.methodCall(method,[parameters],next);
};
// The following code creates a .<namespace>.<method> method for each namespace and method
// defined on http://www.wikidot.com/doc:api
var namespaces = ['categories', 'files', 'tags', 'pages', 'posts', 'users'];
var methods = [
'categories.select',
'files.select','files.get_meta','files.get_one','files.save_one',
'tags.select',
'pages.select','pages.get_meta','pages.get_one','pages.save_one',
'posts.select','posts.get',
'users.get_me'
];
for (var i = 0; i < namespaces.length; i++) {
namespace = namespaces[i];
exports[namespace] = {};
}
for (var j = 0; j < methods.length; j++) {
methodArray = methods[j].split('.');
namespace = methodArray[0];
method = methodArray[1];
exports[namespace][method] = function(params,callback) {
exports.call(methods[j],params,callback);
};
}
// Some convenience functions.
// For each function, callback: function(error,value)
exports.listCategory = function(category,callback) {
exports.call('pages.select',{
'site': exports.site,
'categories': [category]
}, callback);
};
exports.getPage = function(fullname,callback) {
exports.call('pages.get_one',{
'site': exports.site,
'page': fullname
}, callback);
};
// page: WikidotPage object below.
exports.putPage = function(page,comment,callback) {
exports.call('pages.save_one',{
'site': exports.site,
'page': page.fullname,
'content': page.content,
'revision_comment': comment ? comment : "Change via Wikidot library."
}, callback);
};
exports.WikidotPage = function() {
this.injectContent = function(oData,contentType) {
if (!oData) return;
this.fullname = oData.fullname;
this.title = oData.title;
this.tags = oData.tags;
this.parent_fullname = oData.parent_fullname;
this.content = oData.content;
this.contentType = contentType;
if (contentType == exports.ContentTypes.DataForm) {
// Split the content into separate fields.
content = oData.content.split("\n");
for (index in content) {
parts = content[index].split(": ");
text = parts[1];
if (text) {
if (text.substr(0,1) == '\'') {
text = text.substr(1,text.length-2);
}
if (text.substr(0,1) == '\"') {
text = text.substr(1,text.length-2);
}
this[parts[0]] = text;
}
}
} else if (contentType == exports.ContentTypes.LiveTemplate) {
// Split content into an array of sections.
this.contentArray = oData.content.split("\n====\n");
}
}
// [ slug: { selected, title, fn: change function } ]
this.changeProposals = [];
// Outbound object with parameters that will be given to the Wikidot API.
this.addProposal = function(oProposal) {
this.changeProposals.push({
'selected': true,
'title': oProposal.title,
'fn': oProposal.apply
});
}
this.compileChangeProposals = function() {
//request = angular.copy(this);
angular.forEach(this.changeProposals, function(oProposal, index) {
oProposal.fn(this);
}, request);
return request;
}
this.hasTag = function(tag) {
return (this.tags.indexOf(tag) > -1);
}
}
exports.addTagProposal = function(tag) {
this.title = "Add tag "+tag+".";
this.apply = function(request) {
request['tags'].push(tag);
}
}