-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOPML.xxm
302 lines (277 loc) · 8.45 KB
/
OPML.xxm
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
[[@Classes,ActiveX,Variants,MSXML2_TLB,DataLank,xxmSession,fCommon]][[!var
m:TStreamAdapter;
d:DOMDocument60;
db:TDataConnection;
qr:TQueryResult;
c,c1,pn:string;
cn,i,j,l:integer;
p:IXxmParameter;
const
DefaultReadWidth=32;
procedure DoOutlines(xl:IXMLDOMNodeList;const Group:string);
var
x:IXMLDOMElement;
s,t,feedcolor,autounread:string;
qr:TQueryResult;
id,id1,rw,ur0:integer;
ur:Variant;
begin
x:=xl.nextNode as IXMLDOMElement;
if x<>nil then
begin
Context.SendHTML('<div style="margin-left:0.5em;">'#13#10);
while x<>nil do
begin
s:=VarToStr(x.getAttribute('xmlUrl'));
if s<>'' then
begin
//feed exists?
t:=VarToStr(x.getAttribute('f:label'));
qr:=TQueryResult.Create(db,'select id, name from "Feed" where url=$1',[s]);
try
if qr.EOF then id:=0 else
begin
id:=qr.GetInt(0);
if t='' then t:=qr.GetStr(1);
end;
finally
qr.Free;
end;
if id=0 then
begin
if t='' then t:=NameFromFeedURL(s);
id1:=0;
end
else
begin
//already subscribed?
qr:=TQueryResult.Create(db,'select id from "Subscription" where user_id=$1 and feed_id=$2',[Session.UserID,id]);
try
if qr.EOF then id1:=0 else id1:=qr.GetInt(0);
finally
qr.Free;
end;
end;
feedcolor:=VarToStr(x.getAttribute('f:color'));
if not TryStrToInt(VarToStr(x.getAttribute('f:readwidth')),rw) then
rw:=DefaultReadWidth;
autounread:=VarToStr(x.getAttribute('f:autounread'));
if autounread='' then ur:=Null else
if TryStrToInt(autounread,ur0) then ur:=ur0 else ur:=Null;
if id1=0 then
Context.SendHTML('<label><input type="checkbox" name="f" value="'+URLEncode(
['l',s
,'f',id
,'t',t
,'c',feedcolor
,'x',Group
,'w',rw
,'u',ur
])+'" checked="1" />')
else
Context.SendHTML(' ');
Context.SendHTML(' '+ShowLabel(t,feedcolor,'')+' '+HTMLEncode(t)+' <span style="color:silver;">'+HTMLEncode(s)+'</span>');
if Group<>'' then Context.SendHTML(' ('+HTMLEncode(Group)+')');
if id1=0 then
Context.SendHTML('</label>');
Context.SendHTML('<br />'#13#10);
end;
s:=VarToStr(x.getAttribute('title'));
if s='' then s:=Group else if Group<>'' then s:=Group+'\'+s;
DoOutlines(x.selectNodes('outline'),s);
DoOutlines(x.selectNodes('opml:outline'),s);
x:=xl.nextNode as IXMLDOMElement;
end;
Context.SendHTML('</div>'#13#10);
end;
end;
var
s,t,u,g,feedcolor:string;
id,id1,rw:integer;
ur:Variant;
]][[
Context.Include('dHead.xxmi');
<<div style="position:fixed;top:0;left:0em;background-color:gold;padding:4pt;user-select:none;">
<a href="."><img src="img_l.png" width="16" height="16" border="0" alt="Back" /></a>
<a href="Feeds.xxm"><img src="img_l.png" width="16" height="16" border="0" alt="Back" /></a>
<b>OPML</b>
</div>
<div style="height:1.5em;padding-bottom:2pt;"> </div>>
case Context['x'].AsInteger of
0:
begin
<ul>
<li><a href="?x=1">import...</a></li>
<li><a href="?x=2">export...</a></li>
</ul>
<p><a href=".">back</a></p>
end;
1://import
begin
<form method="post" action="?" enctype="multipart/form-data">
<input type="hidden" name="x" value="3" />
<dl>
<dt>OPML file:</dt>
<dd><input type="file" name="f" /></dd>
<dt>Prefix categories with:</dt>
<dd><input type="text" name="c" /></dd>
</dl>
<p><input type="submit" value=" Upload " /></p>
</form>
<p><a href="?">back</a></p>
end;
2://export
begin
<<p><a href="OPML1.xxm">Click here do download your personal "subscriptions.opml"...</a></p>
<form method="post" action="OPML1.xxm">
<input type="hidden" name="x" value="1" />
<dl>
<dt>subscriptions</dt>
<dd>>
qr:=TQueryResult.Create(Session.Connection,
'select S.*, F.name, F.url'+
' from "Subscription" S'+
' inner join "Feed" F on F.id=S.feed_id'+
' where S.user_id=$1'+
' order by lower(S.category), lower(S.label), lower(F.name), S.id',[Session.UserID]);
try
c:='';
cn:=0;
while qr.Read do
begin
c1:=qr.GetStr('category');
if c<>c1 then
begin
inc(cn);
<<br /><div class="catlink"><a href="#t-[[=c1]]" onclick="return ct([[=cn]]);">>=c1<</a></div>>
c:=c1;
end;
]]
<label style="white-space:nowrap;"><input type="checkbox" name="s[[.id]]" value="1" xc="[[=cn]]" /> [[#StringReplace(
ShowLabel(qr.GetStr('label'),qr.GetStr('color'),''),
'<div ','<div title="'+HTMLEncode(qr.GetStr('name'))+'" ',[])<</label>>
end;
finally
qr.Free;
end;
<</dd>
</dl>
<p><input type="submit" value="Export OPML of selected subscriptions..." /></p>
</form>
<script>
function ct(n){
var e=document.forms[0].elements;
for(var i=0;i<e.length;i++){
var f=e[i];
if(f.attributes["xc"]&&f.attributes["xc"].value==n)f.checked=!f.checked;
}
return false;
}
</script>
<p><a href="Feeds.xxm">back</a></p>>
end;
3://import upload
begin
<form method="post" action="?">
<input type="hidden" name="x" value="4" />
db:=Session.Connection;
m:=TStreamAdapter.Create(TMemoryStream.Create,soOwned);
try
(m as IUnknown)._AddRef;
(Context['f'] as IxxmParameterPostFile).SaveToStream(m);
m.Stream.Position:=0;
d:=CoDOMDocument60.Create;
d.async:=false;
d.validateOnParse:=false;
d.resolveExternals:=false;
if not d.load(m as IStream) then
raise Exception.Create(d.parseError.reason);
d.setProperty('SelectionNamespaces','xmlns:opml=''http://opml.org/spec2'' xmlns:f=''http://yoy.be/schema/2018/feeder_subs''');
DoOutlines(d.documentElement.selectNodes('body/outline'),Context['c'].Value);
DoOutlines(d.documentElement.selectNodes('opml:body/opml:outline'),Context['c'].Value);
finally
d:=nil;
db:=nil;
//m.Free;
(m as IUnknown)._Release;
end;
<p><input type="submit" value=" Add " /></p>
</form>
<p><a href="?">back</a></p>
end;
4://import selected
begin
//TODO
db:=Session.Connection;
db.BeginTrans;
try
p:=Context['f'];
while p<>nil do
begin
id:=0;//default
id1:=0;//default
rw:=0;//default
pn:=p.Value;
l:=Length(pn);
i:=2;//assert pn[1]='?'
while i<=l do
begin
//assert pn[i+1]='='
j:=i+2;
while (j<=l) and (pn[j]<>'&') do inc(j);
u:=URLDecode(AnsiString(Copy(pn,i+2,j-i-2)));
case pn[i] of
'l':s:=u;
'f':id:=StrToInt(u);
't':t:=u;
'c':feedcolor:=u;
'x':g:=u;
'w':rw:=StrToInt(u);
'u':if u='' then ur:=Null else ur:=StrToInt(u);
else raise Exception.Create('Unexpected parameter "'+pn[i]+'"');
end;
i:=j+1;
end;
<<p>>=s<</p>>
Context.Flush;
if id=0 then
id:=db.Insert('Feed',['name',t,'url',s,'created',double(UtcNow)],'id');
if id1=0 then
begin
//add subscription
db.Insert('Subscription',
['user_id',Session.UserID
,'feed_id',id
,'label',t
,'color',feedcolor
,'category',g
,'readwidth',rw
,'autounread',ur
,'created',double(UtcNow)
],'id');
//mark recent posts as unread
db.Execute('insert into "UserPost" (user_id,post_id,subscription_id,pubdate) select $1,P.id,S.id,P.pubdate '
+' from "Post" P'
+' inner join "Subscription" S on S.feed_id=P.feed_id and S.user_id=$1'
+' where P.feed_id=$2 and P.pubDate>$3',
[Session.UserID,id,double(UtcNow-366.0)]);
end
else
begin
//update category? (allow duplicates?)
end;
p:=p.NextBySameName;
end;
db.CommitTrans;
except
db.RollbackTrans;
raise;
end;
<p><a href="Feeds.xxm">continue...</a></p>
end
//5:
else
raise Exception.Create('Unknown action');
end;
Context.Include('dFoot.xxmi');