source: archiver/pipelines/actions/archive-resource.xpl @ 3d18e9d

Revision 3d18e9d, 11.5 KB checked in by Eric van der Vlist <vdv@dyomedea.com>, 14 months ago (diff)

Adding a mechanism to avoid to archive multiple times the same resource for a single archive set.

  • Property mode set to 100644
Line 
1<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/">
2
3  <p:param name="data" type="input"/>
4
5  <!-- Look if the resource has already been archived for that set -->
6  <p:processor name="oxf:pipeline">
7    <p:input name="config" href="/data-access.xpl"/>
8    <p:input name="data" transform="oxf:xslt" href="#data">
9      <config xsl:version="2.0">
10        <relpath>
11          <xsl:value-of select="/action/@directory"/>
12          <xsl:text>index.xml</xsl:text>
13        </relpath>
14        <operation>read</operation>
15        <type>xquery</type>
16        <parameter name="url" type="string">
17          <xsl:value-of select="/action/@url"/>
18        </parameter>
19      </config>
20    </p:input>
21    <p:input name="param">
22      <xquery><![CDATA[
23       
24boolean(//archive[@url = $(url)])
25               
26                ]]></xquery>
27    </p:input>
28    <p:output name="data" id="duplicate" debug="duplicate"/>
29  </p:processor>
30
31  <p:choose href="#duplicate">
32
33    <p:when test="/*/* = 'true'">
34      <!-- Already archived, nothing to do -->
35      <!-- Update the queue -->
36      <p:processor name="oxf:pipeline">
37        <p:input name="config" href="/data-access.xpl"/>
38        <p:input name="data" transform="oxf:xslt" href="#data">
39          <config xsl:version="2.0">
40            <relpath>queue.xml</relpath>
41            <operation>write</operation>
42            <type>xquery</type>
43            <parameter name="uuid" type="string">
44              <xsl:value-of select="/action/@uuid"/>
45            </parameter>
46          </config>
47        </p:input>
48        <p:input name="param">
49          <xquery><![CDATA[
50           
51for $a in /queue/action where $a/@uuid = $(uuid) return
52    update
53        delete $a
54       
55                ]]></xquery>
56        </p:input>
57        <p:output name="data" id="response4" debug="response"/>
58      </p:processor>
59      <p:processor name="oxf:null-serializer">
60        <p:input name="data" href="#response4"/>
61      </p:processor>
62    </p:when>
63
64    <p:otherwise>
65      <!-- Otherwise, archive the resource... -->
66      <!-- Fetch the resource -->
67      <p:processor name="oxf:url-generator">
68        <p:input name="config" transform="oxf:xslt" href="#data">
69          <config xsl:version="2.0">
70            <url>
71              <xsl:value-of select="/action/@url"/>
72            </url>
73            <header>
74              <name>User-Agent</name>
75              <value>
76                <xsl:value-of select="doc('oxf:/config.xml')/config/user-agent"/>
77              </value>
78            </header>
79            <mode>archive</mode>
80          </config>
81        </p:input>
82        <p:output name="data" id="archive" debug="archive"/>
83      </p:processor>
84
85
86      <!-- Store the archive in the database -->
87      <p:processor name="oxf:pipeline">
88        <p:input name="config" href="/data-access.xpl"/>
89        <p:input name="data" transform="oxf:xslt" href="#data">
90          <config xsl:version="2.0">
91            <relpath>
92              <xsl:value-of select="/action/@directory"/>
93              <xsl:value-of select="/action/@filename"/>
94            </relpath>
95            <operation>write</operation>
96            <type>document</type>
97          </config>
98        </p:input>
99        <p:input name="param" href="#archive"/>
100        <p:output name="data" id="response2"/>
101      </p:processor>
102
103      <p:processor name="oxf:null-serializer">
104        <p:input name="data" href="#response2"/>
105      </p:processor>
106
107
108      <!-- Test the type of document to see if it needs to be rewritten -->
109      <p:choose href="#archive">
110
111        <!-- HTML document : need to update the links... -->
112        <p:when test="/archive/response/document/@content-type=('text/html', 'text/css')">
113
114          <!-- Call the corresponding pipeline to extract the links and rewrite them -->
115          <p:processor name="oxf:url-generator">
116            <p:input name="config" transform="oxf:xslt" href="#archive">
117              <config xsl:version="2.0">
118                <url>
119                  <xsl:text>oxf:/actions/mediatypes/</xsl:text>
120                  <xsl:value-of select="substring-after(/archive/response/document/@content-type, '/')"/>
121                  <xsl:text>.xpl</xsl:text>
122                </url>
123              </config>
124            </p:input>
125            <p:output name="data" id="pipeline"/>
126          </p:processor>
127
128          <p:processor name="oxf:pipeline">
129            <p:input name="config" href="#pipeline"/>
130            <p:input name="archive" href="#archive"/>
131            <p:output name="rewritten" id="rewritten"/>
132            <p:output name="links" id="links"/>
133          </p:processor>
134
135
136          <!-- Store the rewritten document in the database -->
137          <p:processor name="oxf:pipeline">
138            <p:input name="config" href="/data-access.xpl"/>
139            <p:input name="data" transform="oxf:xslt" href="#data">
140              <config xsl:version="2.0">
141                <relpath>
142                  <xsl:value-of select="/action/@directory"/>
143                  <xsl:text>rewritten-</xsl:text>
144                  <xsl:value-of select="/action/@filename"/>
145                </relpath>
146                <operation>write</operation>
147                <type>document</type>
148              </config>
149            </p:input>
150            <p:input name="param" href="#rewritten"/>
151            <p:output name="data" id="response3"/>
152          </p:processor>
153          <p:processor name="oxf:null-serializer">
154            <p:input name="data" href="#response3"/>
155          </p:processor>
156
157
158
159          <!-- Update the archive index -->
160          <p:processor name="oxf:pipeline">
161            <p:input name="config" href="/data-access.xpl"/>
162            <p:input name="data" transform="oxf:xslt" href="#data">
163              <config xsl:version="2.0">
164                <relpath>
165                  <xsl:value-of select="/action/@directory"/>
166                  <xsl:text>index.xml</xsl:text>
167                </relpath>
168                <operation>write</operation>
169                <type>xquery</type>
170                <parameter name="url" type="string">
171                  <xsl:value-of select="/action/@url"/>
172                </parameter>
173                <parameter name="filename" type="string">
174                  <xsl:value-of select="/action/@filename"/>
175                </parameter>
176                <parameter name="filename-rewritten" type="string">
177                  <xsl:text>rewritten-</xsl:text>
178                  <xsl:value-of select="/action/@filename"/>
179                </parameter>
180              </config>
181            </p:input>
182            <p:input name="param">
183              <xquery><![CDATA[
184for $as in /archive-set
185    return
186      update
187        insert <archive url=$(url) href=$(filename) href-rewritten=$(filename-rewritten) dateTime="{current-dateTime()}"/>
188        into $as               
189                ]]></xquery>
190            </p:input>
191            <p:output name="data" id="response1"/>
192          </p:processor>
193          <p:processor name="oxf:null-serializer">
194            <p:input name="data" href="#response1"/>
195          </p:processor>
196
197          <!-- Update the queue -->
198          <p:processor name="oxf:pipeline">
199            <p:input name="config" href="/data-access.xpl"/>
200            <p:input name="data" transform="oxf:xslt" href="aggregate('root', #data, #links)">
201              <config xsl:version="2.0">
202                <relpath>queue.xml</relpath>
203                <operation>write</operation>
204                <type>xquery</type>
205                <parameter name="directory" type="string">
206                  <xsl:value-of select="/root/action/@directory"/>
207                </parameter>
208                <parameter name="uuid" type="string">
209                  <xsl:value-of select="/root/action/@uuid"/>
210                </parameter>
211                <parameter name="priority" type="string">
212                  <xsl:value-of select="/root/action/@priority"/>
213                </parameter>
214                <parameter name="links" type="node-set">
215                  <xsl:copy-of select="/root/links"/>
216                </parameter>
217              </config>
218            </p:input>
219            <p:input name="param">
220              <xquery><![CDATA[
221declare namespace util = "http://exist-db.org/xquery/util";
222declare variable $links := $(links);
223
224for $q in /queue[$links/link/@abs-href]
225  return
226    update
227        insert
228          for $href in distinct-values($links/link/@abs-href)
229            let $link := $links/link[@abs-href = $href][1]
230            return <action priority=$(priority) uuid="{util:uuid()}" type="archive-resource" url="{$link/@abs-href}" directory=$(directory) filename="{$link/@filename}"/>
231        into $q,
232       
233for $a in /queue/action where $a/@uuid = $(uuid) return
234    update
235        delete $a
236       
237                ]]></xquery>
238            </p:input>
239            <p:output name="data" id="response4" debug="response"/>
240          </p:processor>
241          <p:processor name="oxf:null-serializer">
242            <p:input name="data" href="#response4"/>
243          </p:processor>
244
245
246        </p:when>
247
248        <!-- Otherwise: no need to rewrite -->
249        <p:otherwise>
250          <!-- Update the archive index -->
251          <p:processor name="oxf:pipeline">
252            <p:input name="config" href="/data-access.xpl"/>
253            <p:input name="data" transform="oxf:xslt" href="#data">
254              <config xsl:version="2.0">
255                <relpath>
256                  <xsl:value-of select="/action/@directory"/>
257                  <xsl:text>index.xml</xsl:text>
258                </relpath>
259                <operation>write</operation>
260                <type>xquery</type>
261                <parameter name="url" type="string">
262                  <xsl:value-of select="/action/@url"/>
263                </parameter>
264                <parameter name="filename" type="string">
265                  <xsl:value-of select="/action/@filename"/>
266                </parameter>
267              </config>
268            </p:input>
269            <p:input name="param">
270              <xquery><![CDATA[
271for $as in /archive-set
272    return
273      update
274        insert <archive url=$(url) href=$(filename) dateTime="{current-dateTime()}"/>
275        into $as               
276                ]]></xquery>
277            </p:input>
278            <p:output name="data" id="response1"/>
279          </p:processor>
280          <p:processor name="oxf:null-serializer">
281            <p:input name="data" href="#response1"/>
282          </p:processor>
283
284          <!-- Update the queue -->
285          <p:processor name="oxf:pipeline">
286            <p:input name="config" href="/data-access.xpl"/>
287            <p:input name="data" transform="oxf:xslt" href="#data">
288              <config xsl:version="2.0">
289                <relpath>queue.xml</relpath>
290                <operation>write</operation>
291                <type>xquery</type>
292                <parameter name="uuid" type="string">
293                  <xsl:value-of select="/action/@uuid"/>
294                </parameter>
295              </config>
296            </p:input>
297            <p:input name="param">
298              <xquery><![CDATA[
299               
300for $a in /queue/action where $a/@uuid = $(uuid) return
301    update
302        delete $a
303       
304                ]]></xquery>
305            </p:input>
306            <p:output name="data" id="response4" debug="response"/>
307          </p:processor>
308          <p:processor name="oxf:null-serializer">
309            <p:input name="data" href="#response4"/>
310          </p:processor>
311
312        </p:otherwise>
313
314      </p:choose>
315    </p:otherwise>
316  </p:choose>
317
318
319
320</p:config>
Note: See TracBrowser for help on using the repository browser.