1 | <?php
|
---|
2 |
|
---|
3 | class TextStyle {
|
---|
4 | var $name = "" ;
|
---|
5 | var $bold = false ;
|
---|
6 | var $italics = false ;
|
---|
7 | var $underline = false ;
|
---|
8 | var $count = 0 ;
|
---|
9 | }
|
---|
10 |
|
---|
11 | class XML2ODT {
|
---|
12 | var $tags ;
|
---|
13 | var $textstyle_current ;
|
---|
14 | var $textstyles = array () ;
|
---|
15 | var $listcode = "" ;
|
---|
16 | var $list_is_open = false ;
|
---|
17 | var $list_list = array () ;
|
---|
18 | var $list_item_name = array () ;
|
---|
19 | var $image_counter = 0 ;
|
---|
20 | var $image_frames = array () ;
|
---|
21 | var $table_counter = 0 ;
|
---|
22 | var $open_tables = array () ;
|
---|
23 | var $table_styles = array () ;
|
---|
24 | var $col_styles = array () ;
|
---|
25 | var $cell_styles = array () ;
|
---|
26 | var $col_counter = array () ;
|
---|
27 | var $row_counter = array () ;
|
---|
28 | var $footnote_counter = 0 ;
|
---|
29 | var $article_counter = 0 ;
|
---|
30 | var $footnote_index = array () ;
|
---|
31 | var $footnote_text = array () ;
|
---|
32 |
|
---|
33 | function XML2ODT () {
|
---|
34 | $this->textstyle_current = new TextStyle ;
|
---|
35 | $this->textstyle_current->name = "T0" ;
|
---|
36 | $this->textstyles['T0'] = $this->textstyle_current ;
|
---|
37 | $this->tags = array () ;
|
---|
38 | }
|
---|
39 |
|
---|
40 | function get_url ( $title ) {
|
---|
41 | global $xmlg ;
|
---|
42 | $url = "http://" . $xmlg["site_base_url"] . "/index.php?title=" . urlencode ( $title ) ;
|
---|
43 | return $url ;
|
---|
44 | }
|
---|
45 |
|
---|
46 | function get_footnote_id ( $name , &$text ) {
|
---|
47 | $name = trim ( strtolower ( $name ) ) ;
|
---|
48 | if ( $name != "" && isset ( $this->footnote_index[$name] ) ) {
|
---|
49 | $this->footnote_counter++ ;
|
---|
50 | if ( trim ( $text ) == "" ) $text = $this->footnote_text[$name] ;
|
---|
51 | return $this->footnote_counter ;
|
---|
52 | } else {
|
---|
53 | $this->footnote_counter++ ;
|
---|
54 | if ( $name != "" ) {
|
---|
55 | $this->footnote_index[$name] = $this->footnote_counter ;
|
---|
56 | $this->footnote_text[$name] = $text ;
|
---|
57 | }
|
---|
58 | return $this->footnote_counter ;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | function get_image_frames () {
|
---|
63 | $ret = "" ;
|
---|
64 | foreach ( $this->image_frames AS $f ) {
|
---|
65 | $name = $f->name ;
|
---|
66 | $align = $f->align ;
|
---|
67 | $ret .= '<style:style style:name="' . $name . '" style:family="graphic" style:parent-style-name="Graphics">' .
|
---|
68 | '<style:graphic-properties ' .
|
---|
69 | ' fo:margin-left="' . $f->left .
|
---|
70 | '" fo:margin-right="' . $f->right .
|
---|
71 | '" fo:margin-top="' . $f->top .
|
---|
72 | '" fo:margin-bottom="' . $f->bottom .
|
---|
73 | '" style:run-through="foreground" style:wrap="parallel" style:number-wrapped-paragraphs="no-limit" ' .
|
---|
74 | 'style:wrap-contour="false" style:vertical-pos="top" style:vertical-rel="paragraph" style:horizontal-pos="' .
|
---|
75 | $align . '" style:horizontal-rel="paragraph" ' .
|
---|
76 | 'style:mirror="none" fo:clip="rect(0cm 0cm 0cm 0cm)" draw:luminance="0%" draw:contrast="0%" draw:red="0%" draw:green="0%" draw:blue="0%" ' .
|
---|
77 | 'draw:gamma="100%" draw:color-inversion="false" draw:image-opacity="100%" draw:color-mode="standard"/></style:style>' ;
|
---|
78 | }
|
---|
79 | return $ret ;
|
---|
80 | }
|
---|
81 |
|
---|
82 | function get_image_frame ( $align , $margin = false ) {
|
---|
83 | $i = "fr" . $this->image_counter ;
|
---|
84 | $o->name = $i ;
|
---|
85 | $o->align = $align ;
|
---|
86 | $o->left = $margin && $align == 'right' ? '0.1cm' : '0cm' ;
|
---|
87 | $o->right = $margin && $align == 'left' ? '0.1cm' : '0cm' ;
|
---|
88 | $o->top = '0cm' ;
|
---|
89 | $o->bottom = $margin ? '0.1cm' : '0cm' ;
|
---|
90 | $this->image_frames[$i] = $o ;
|
---|
91 | return $i ;
|
---|
92 | }
|
---|
93 |
|
---|
94 | function get_table_style ( &$tag ) {
|
---|
95 | $this->table_counter++ ;
|
---|
96 | $ret = "Table" . $this->table_counter ;
|
---|
97 | $this->open_tables[] = $ret ;
|
---|
98 | $o->name = $ret ;
|
---|
99 | $o->cols = 0 ;
|
---|
100 | $this->table_styles[$ret] = $o ;
|
---|
101 | $this->col_counter[$ret] = 0 ;
|
---|
102 | $this->row_counter[$ret] = 0 ;
|
---|
103 | return $ret ;
|
---|
104 | }
|
---|
105 |
|
---|
106 | function get_top_table_name () {
|
---|
107 | $x = array_pop ( $this->open_tables ) ;
|
---|
108 | $this->open_tables[] = $x ;
|
---|
109 | return $x ;
|
---|
110 | }
|
---|
111 |
|
---|
112 | function get_column_style () {
|
---|
113 | $t = $this->get_top_table_name () ;
|
---|
114 | $cn = $t . "." . chr ( 65 + $this->col_counter[$t] ) ;
|
---|
115 | $cc = $cn . $this->row_counter[$t] ;
|
---|
116 | $this->col_counter[$t]++ ;
|
---|
117 | if ( !isset ( $this->col_styles[$cn] ) ) {
|
---|
118 | $this->table_styles[$t]->cols = $this->col_counter[$t] ;
|
---|
119 | $o->name = $cn ;
|
---|
120 | $this->col_styles[$cn] = $o ;
|
---|
121 | }
|
---|
122 | return $cc ;
|
---|
123 | }
|
---|
124 |
|
---|
125 | function reset_column () {
|
---|
126 | $t = $this->get_top_table_name () ;
|
---|
127 | $this->col_counter[$t] = 0 ;
|
---|
128 | $this->row_counter[$t]++ ;
|
---|
129 | }
|
---|
130 |
|
---|
131 | function get_table_styles () {
|
---|
132 | $ret = "" ;
|
---|
133 |
|
---|
134 | # Tables
|
---|
135 | foreach ( $this->table_styles AS $ts ) {
|
---|
136 | $ret .= '<style:style style:name="' . $ts->name . '" style:family="table">' .
|
---|
137 | '<style:table-properties style:width="auto" table:align="margins"/>' .
|
---|
138 | '</style:style>' ;
|
---|
139 | }
|
---|
140 |
|
---|
141 | # Columns
|
---|
142 | foreach ( $this->col_styles AS $cs ) {
|
---|
143 | $ret .= '<style:style style:name="' . $cs->name . '" style:family="table-column">' .
|
---|
144 | '<style:table-column-properties style:column-width="auto" style:rel-column-width="1*"/>' .
|
---|
145 | '</style:style>' ;
|
---|
146 | }
|
---|
147 |
|
---|
148 | return $ret ;
|
---|
149 | }
|
---|
150 |
|
---|
151 | function ensure_list_open () {
|
---|
152 | if ( $this->list_is_open ) return "" ;
|
---|
153 | $this->list_is_open = true ;
|
---|
154 | if ( substr ( $this->listcode , -1 ) == '#' ) $o->type = 'numbered' ;
|
---|
155 | else $o->type = 'bullet' ;
|
---|
156 | $o->depth = strlen ( $this->listcode ) ;
|
---|
157 | $o->number = count ( $this->list_list ) + 1 ;
|
---|
158 | $this->list_list[] = $o ;
|
---|
159 | while ( count ( $this->list_item_name ) <= $o->depth ) $this->list_item_name[] = "" ;
|
---|
160 | $this->list_item_name[$o->depth] = 'PL' . $o->number ;
|
---|
161 | return '<text:list text:style-name="List_20_' . $o->number . '">' ;
|
---|
162 | }
|
---|
163 |
|
---|
164 | function ensure_list_closed () {
|
---|
165 | if ( !$this->list_is_open ) return "" ;
|
---|
166 | $this->list_is_open = false ;
|
---|
167 | $ret = "" ;
|
---|
168 | $ot = $this->tags ;
|
---|
169 | do {
|
---|
170 | $x = array_pop ( $this->tags ) ;
|
---|
171 | $ret .= "</{$x}>" ;
|
---|
172 | } while ( $x != "text:list-item" && count ( $this->tags ) > 0 ) ;
|
---|
173 | if ( $x != "text:list-item" ) {
|
---|
174 | $ret = "" ;
|
---|
175 | $this->tags = $ot ;
|
---|
176 | }
|
---|
177 | $ret .= "</text:list>" ;
|
---|
178 | return $ret ;
|
---|
179 | }
|
---|
180 |
|
---|
181 | function get_text_style ( $find ) {
|
---|
182 | $found = "" ;
|
---|
183 | foreach ( $this->textstyles AS $k => $ts ) {
|
---|
184 | if ( $ts->bold != $find->bold ) continue ;
|
---|
185 | if ( $ts->italics != $find->italics ) continue ;
|
---|
186 | if ( $ts->underline != $find->underline ) continue ;
|
---|
187 | $this->textstyles[$k]->count++ ;
|
---|
188 | return $ts ;
|
---|
189 | }
|
---|
190 |
|
---|
191 | # Create new style
|
---|
192 | $found = "T" . count ( $this->textstyles ) ;
|
---|
193 | $find->name = $found ;
|
---|
194 | $find->count = 1 ;
|
---|
195 | $this->textstyles[$found] = $find ;
|
---|
196 | return $find ;
|
---|
197 | }
|
---|
198 |
|
---|
199 | function get_styles_xml () {
|
---|
200 | $ret = '<office:automatic-styles>' ;
|
---|
201 |
|
---|
202 | # Default styles
|
---|
203 | $ret .= '<style:style style:name="PHR" style:family="paragraph" style:parent-style-name="Standard">' .
|
---|
204 | '<style:paragraph-properties fo:padding="0.074cm" fo:border-left="none" fo:border-right="none" fo:border-top="none" fo:border-bottom="0.002cm solid #000000" style:join-border="false"/>' .
|
---|
205 | '</style:style>' .
|
---|
206 | '<style:style style:name="PAGEBREAK" style:family="paragraph" style:parent-style-name="Standard">' .
|
---|
207 | '<style:paragraph-properties fo:break-before="page"/>' .
|
---|
208 | '</style:style>' ;
|
---|
209 |
|
---|
210 | # Text styles
|
---|
211 | foreach ( $this->textstyles AS $ts ) {
|
---|
212 | if ( $ts->count == 0 ) {
|
---|
213 | $ret .= '<style:style style:name="' . $ts->name . '" style:family="paragraph">' ;
|
---|
214 | $ret .= '<style:paragraph-properties fo:text-align="justify" style:justify-single-word="false"/>' ;
|
---|
215 | } else {
|
---|
216 | $ret .= '<style:style style:name="' . $ts->name . '" style:family="text">' ;
|
---|
217 | $ret .= '<style:text-properties' ;
|
---|
218 | if ( $ts->italics ) $ret .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"' ;
|
---|
219 | if ( $ts->bold ) $ret .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"' ;
|
---|
220 | if ( $ts->underline ) {
|
---|
221 | $ret .= ' style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"' ;
|
---|
222 | }
|
---|
223 | $ret .= '/>' ;
|
---|
224 | }
|
---|
225 | $ret .= '</style:style>' ;
|
---|
226 | }
|
---|
227 |
|
---|
228 | # List styles
|
---|
229 | $cm = 0.3 ;
|
---|
230 | foreach ( $this->list_list AS $list ) {
|
---|
231 | $l = "List_20_" . $list->number ;
|
---|
232 | $p = "PL" . $list->number ;
|
---|
233 | $ret .= '<style:style style:name="'.$p.'" style:family="paragraph" style:parent-style-name="Standard" style:list-style-name="'.$l.'">' ;
|
---|
234 | if ( $list->depth > 1 ) {
|
---|
235 | $off = $cm * $list->depth ;
|
---|
236 | $ret .= '<style:paragraph-properties fo:margin-left="' .
|
---|
237 | $off .
|
---|
238 | 'cm" fo:margin-right="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/>' ;
|
---|
239 | }
|
---|
240 | $ret .= '</style:style>' ;
|
---|
241 | $ret .= '<text:list-style style:name="' . $l . '">' ;
|
---|
242 | $off = 0 ;
|
---|
243 | for ( $depth = 1 ; $depth <= 10 ; $depth++ ) {
|
---|
244 | $off += $cm ;
|
---|
245 | if ( $list->type == 'numbered' ) {
|
---|
246 | $ret .= '<text:list-level-style-number text:level="' .
|
---|
247 | $depth .
|
---|
248 | '" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1">' .
|
---|
249 | '<style:list-level-properties text:space-before="' .
|
---|
250 | $off . 'cm" text:min-label-width="' . $cm . 'cm"/>' .
|
---|
251 | '</text:list-level-style-number>' ;
|
---|
252 | } else {
|
---|
253 | $ret .= '<text:list-level-style-bullet text:level="' .
|
---|
254 | $depth .
|
---|
255 | '" text:style-name="Bullet_20_Symbols" style:num-suffix="." text:bullet-char="î">' .
|
---|
256 | '<style:list-level-properties text:space-before="' .
|
---|
257 | $off . 'cm" text:min-label-width="' . $cm . 'cm"/>' .
|
---|
258 | '<style:text-properties style:font-name="StarSymbol"/>' .
|
---|
259 | '</text:list-level-style-bullet>' ;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | $ret .= '</text:list-style>' ;
|
---|
263 | }
|
---|
264 |
|
---|
265 | $ret .= $this->get_image_frames () ;
|
---|
266 | $ret .= $this->get_table_styles () ;
|
---|
267 |
|
---|
268 | $ret .= '</office:automatic-styles>' ;
|
---|
269 |
|
---|
270 | return $ret ;
|
---|
271 | }
|
---|
272 |
|
---|
273 | function get_odt_start () {
|
---|
274 | $ret = "" ;
|
---|
275 |
|
---|
276 | $ret .= '<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">' ;
|
---|
277 |
|
---|
278 |
|
---|
279 | $ret .= '<office:scripts/>
|
---|
280 | <office:font-face-decls>
|
---|
281 | <style:font-face style:name="Tahoma1" svg:font-family="Tahoma"/>
|
---|
282 | <style:font-face style:name="Lucida Sans Unicode" svg:font-family="'Lucida Sans Unicode'" style:font-pitch="variable"/>
|
---|
283 | <style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-pitch="variable"/>
|
---|
284 | <style:font-face style:name="Times New Roman" svg:font-family="'Times New Roman'" style:font-family-generic="roman" style:font-pitch="variable"/>
|
---|
285 | <style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss" style:font-pitch="variable"/>
|
---|
286 | </office:font-face-decls>' ;
|
---|
287 |
|
---|
288 | $ret .= $this->get_styles_xml () ;
|
---|
289 | return $ret ;
|
---|
290 | }
|
---|
291 |
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 |
|
---|
296 | class element {
|
---|
297 | var $name = '';
|
---|
298 | var $attrs = array ();
|
---|
299 | var $children = array ();
|
---|
300 |
|
---|
301 | # Temporary variables for link tags
|
---|
302 | var $link_target = "" ;
|
---|
303 | var $link_trail = "" ;
|
---|
304 | var $link_parts = array () ;
|
---|
305 |
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Parse the children ... why won't anybody think of the children?
|
---|
309 | */
|
---|
310 | function sub_parse(& $tree) {
|
---|
311 | $ret = '' ;
|
---|
312 | $temp = "" ;
|
---|
313 | foreach ($this->children as $key => $child) {
|
---|
314 | if (is_string($child)) {
|
---|
315 | $temp .= $child ;
|
---|
316 | } elseif ($child->name != 'ATTRS') {
|
---|
317 | $ret .= $this->add_temp_text ( $temp ) ;
|
---|
318 | $sub = $child->parse ( $tree , "" , $this ) ;
|
---|
319 | if ( $this->name == 'LINK' ) {
|
---|
320 | if ( $child->name == 'TARGET' ) $this->link_target = $sub ;
|
---|
321 | else if ( $child->name == 'PART' ) $this->link_parts[] = $sub ;
|
---|
322 | else if ( $child->name == 'TRAIL' ) $this->link_trail = $sub ;
|
---|
323 | }
|
---|
324 | $ret .= $sub ;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | return $ret . $this->add_temp_text ( $temp ) ;
|
---|
328 | }
|
---|
329 |
|
---|
330 | function fix_text ( $s ) {
|
---|
331 | /* $s = html_entity_decode ( $s ) ;
|
---|
332 | filter_named_entities ( $s ) ;
|
---|
333 | $s = str_replace ( "&" , "&" , $s ) ;
|
---|
334 | $s = str_replace ( "<" , "<" , $s ) ;
|
---|
335 | $s = str_replace ( ">" , ">" , $s ) ;
|
---|
336 | return utf8_decode ( $s ) ;*/
|
---|
337 | filter_named_entities ( $s ) ;
|
---|
338 | $s = str_replace ( "&" , "&" , $s ) ;
|
---|
339 | $s = str_replace ( "<" , "<" , $s ) ;
|
---|
340 | $s = str_replace ( ">" , ">" , $s ) ;
|
---|
341 | return $s ;
|
---|
342 | }
|
---|
343 |
|
---|
344 | function add_temp_text ( &$temp ) {
|
---|
345 | $s = $temp ;
|
---|
346 | $temp = "" ;
|
---|
347 | return $this->fix_text ( $s ) ;
|
---|
348 | }
|
---|
349 |
|
---|
350 | function push_tag ( $tag , $params = "" ) {
|
---|
351 | global $xml2odt ;
|
---|
352 | $n = "<" . $tag ;
|
---|
353 | if ( $params != "" ) $n .= " " . $params ;
|
---|
354 | $n .= ">" ;
|
---|
355 | $xml2odt->tags[] = $tag ;
|
---|
356 | return $n ;
|
---|
357 | }
|
---|
358 |
|
---|
359 | function pop_tag () {
|
---|
360 | global $xml2odt ;
|
---|
361 | if ( count ( $xml2odt->tags ) == 0 ) return "" ;
|
---|
362 | $x = array_pop ( $xml2odt->tags ) ;
|
---|
363 | return "</{$x}>" ;
|
---|
364 | }
|
---|
365 |
|
---|
366 | function top_tag () {
|
---|
367 | global $xml2odt ;
|
---|
368 | if ( count ( $xml2odt->tags ) == 0 ) return "" ;
|
---|
369 | $x = array_pop ( $xml2odt->tags ) ;
|
---|
370 | $xml2odt->tags[] = $x ;
|
---|
371 | return $x ;
|
---|
372 | }
|
---|
373 |
|
---|
374 | function handle_link ( &$tree ) {
|
---|
375 | # <text:a xlink:type="simple" xlink:href="http://www.google.de/">http://www.google.de</text:a>
|
---|
376 | global $content_provider , $xml2odt , $xmlg ;
|
---|
377 | # $ot = $tree->opentags ;
|
---|
378 | $sub = $this->sub_parse ( $tree ) ;
|
---|
379 | # $tree->opentags = $ot ;
|
---|
380 | $link = "" ;
|
---|
381 | if ( isset ( $this->attrs['TYPE'] ) AND strtolower ( $this->attrs['TYPE'] ) == 'external' ) { # External link
|
---|
382 | $href = htmlentities ( $this->attrs['HREF'] ) ;
|
---|
383 | if ( trim ( $sub ) == "" ) {
|
---|
384 | $sub = $href ;
|
---|
385 | $sub = explode ( '://' , $sub , 2 ) ;
|
---|
386 | $sub = explode ( '/' , array_pop ( $sub ) , 2 ) ;
|
---|
387 | $sub = array_shift ( $sub ) ;
|
---|
388 | }
|
---|
389 | $sub = $this->fix_text ( $sub ) ;
|
---|
390 | $link = '<text:a xlink:type="simple" xlink:href="' . $href . '/">' . $sub . '</text:a>' ;
|
---|
391 | } else { # Internal link
|
---|
392 | $link = "LINK" ;
|
---|
393 | if ( count ( $this->link_parts ) > 0 ) {
|
---|
394 | $link = array_pop ( $this->link_parts ) ;
|
---|
395 | array_push ( $this->link_parts , $link ) ; # Compensating array_pop
|
---|
396 | }
|
---|
397 | $link_text = $link ;
|
---|
398 | if ( $link == "" ) $link = $this->link_target ;
|
---|
399 | $link .= $this->link_trail ;
|
---|
400 |
|
---|
401 | $ns = $content_provider->get_namespace_id ( $this->link_target ) ;
|
---|
402 |
|
---|
403 |
|
---|
404 | if ( $ns == 6 ) { # Image
|
---|
405 | $nstext = explode ( ":" , $this->link_target , 2 ) ;
|
---|
406 | $target = array_pop ( $nstext ) ;
|
---|
407 | $nstext = array_shift ( $nstext ) ;
|
---|
408 |
|
---|
409 | $text = array_pop ( $this->link_parts ) . $this->link_trail ;
|
---|
410 |
|
---|
411 | $href = $content_provider->get_image_url ( $target ) ;
|
---|
412 | $xml2odt->image_counter++ ;
|
---|
413 | $image_file = $content_provider->copyimagefromwiki ( $target , $href ) ;
|
---|
414 | $image_file_full = $xmlg['image_destination'] . "/" . $image_file ;
|
---|
415 | $image_file = "Pictures/" . $image_file ;
|
---|
416 |
|
---|
417 | # Dimensions
|
---|
418 | list($i_width, $i_height, $i_type, $i_attr) = @getimagesize($image_file_full);
|
---|
419 | if ( $i_width <= 0 ) { # Paranoia
|
---|
420 | $i_width = 100 ;
|
---|
421 | $i_height = 100 ;
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | $is_thumb = false ;
|
---|
426 | $align = '' ;
|
---|
427 | $width = '' ;
|
---|
428 | foreach ( $this->link_parts AS $s ) {
|
---|
429 | $s = trim ( $s ) ;
|
---|
430 | if ( $s == 'thumb' ) {
|
---|
431 | $is_thumb = true ;
|
---|
432 | if ( $align == '' ) $align = 'right' ;
|
---|
433 | if ( $width == '' ) $width = '400' ;
|
---|
434 | } else if ( substr ( trim ( strtolower ( $s ) ) , -2 ) == 'px' ) {
|
---|
435 | $s = trim ( strtolower ( $s ) ) ;
|
---|
436 | $s = trim ( substr ( $s , 0 , strlen ( $s ) - 2 ) ) ;
|
---|
437 | $width = $s * 2 ;
|
---|
438 | }
|
---|
439 | }
|
---|
440 | if ( $width == '' ) $width = $i_width ;
|
---|
441 | if ( $align == '' ) $align = 'left' ;
|
---|
442 |
|
---|
443 | $page_width = 1000 ; # Arbitary: page width = 1000 px
|
---|
444 | if ( $width > $page_width ) $width = $page_width ;
|
---|
445 | $width = $width / 100 ;
|
---|
446 | $height = ( $i_height * $width ) / $i_width ;
|
---|
447 | $width .= "cm" ;
|
---|
448 | $height .= "cm" ;
|
---|
449 |
|
---|
450 | $link = "" ;
|
---|
451 | $fr = $xml2odt->get_image_frame ( $align ) ;
|
---|
452 | $image_counter = $xml2odt->image_counter ;
|
---|
453 | if ( $is_thumb && $text != "" ) {
|
---|
454 | $ofr = $xml2odt->get_image_frame ( $align , true ) ;
|
---|
455 | $link .= '<draw:frame draw:style-name="' .
|
---|
456 | $ofr .
|
---|
457 | '" draw:name="Frame' .
|
---|
458 | $xml2odt->image_counter .
|
---|
459 | '" text:anchor-type="paragraph" svg:width="'.
|
---|
460 | $width .
|
---|
461 | '" draw:z-index="0">' ;
|
---|
462 | $link .= '<draw:text-box fo:min-height="' . $height . '">' ;
|
---|
463 | $link .= '<text:p text:style-name="Illustration">' ;
|
---|
464 | }
|
---|
465 | $link .= '<draw:frame draw:style-name="' . $fr . '" draw:name="Figure'.
|
---|
466 | $image_counter .
|
---|
467 | '" text:anchor-type="paragraph" svg:width="' . $width . '" svg:height="' . $height . '" draw:z-index="0">' .
|
---|
468 | '<draw:image xlink:href="' . $image_file .
|
---|
469 | '" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>' .
|
---|
470 | '</draw:frame>' ;
|
---|
471 | if ( $is_thumb && $text != "" ) {
|
---|
472 | $link .= $text ;
|
---|
473 | $link .= '</text:p></draw:text-box></draw:frame>' ;
|
---|
474 | }
|
---|
475 |
|
---|
476 | } else if ( $ns == -9 ) { # Interlanguage link
|
---|
477 | $sub = $this->link_target ;
|
---|
478 | $nstext = explode ( ":" , $sub , 2 ) ;
|
---|
479 | $name = array_pop ( $nstext ) ;
|
---|
480 | $nstext = array_shift ( $nstext ) ;
|
---|
481 | $sub = utf8_encode ( $sub ) ;
|
---|
482 | $href = "http://{$nstext}.wikipedia.org/wiki/" . urlencode ( $name ) ;
|
---|
483 | $link = '<text:a xlink:type="simple" xlink:href="' . $href . '/">' . $sub . '</text:a>' ;
|
---|
484 | if ( !$xmlg['keep_interlanguage'] ) $link = "" ; # No interlanguage links?
|
---|
485 | } else if ( $ns == -8 ) { # Category link
|
---|
486 | if ( $link_text == "!" || $link_text == '*' ) $link = "" ;
|
---|
487 | else if ( $link_text != $this->link_target ) $link = " ({$link_text})" ;
|
---|
488 | else $link = "" ;
|
---|
489 | $link = "" . $this->link_target . $link . "" ;
|
---|
490 | if ( !$xmlg['keep_categories'] ) $link = "" ; # No category links?
|
---|
491 | } else {
|
---|
492 | if ( $content_provider->is_an_article ( $this->link_target ) ) {
|
---|
493 | $link = "SEITEN-INTERNER LINK" ;
|
---|
494 | # dub sez... working internal links
|
---|
495 | $lt = ( trim ( $this->link_target ) ) ;
|
---|
496 | $lt = str_replace ( "+" , " " , $lt ) ;
|
---|
497 | $text = array_pop ( $this->link_parts ) ;
|
---|
498 | if (!$text)
|
---|
499 | $text = $lt;
|
---|
500 | $link = '<text:a xlink:type="simple" xlink:href="#' . $lt . '|outline">' . $text . '</text:a>' ;
|
---|
501 | #$link = "<link linkend='{$lt}'>#{$lt}|outline</link>" ;
|
---|
502 | } else {
|
---|
503 | $href = $xml2odt->get_url ( $this->link_target ) ;
|
---|
504 | if ( count ( $this->link_parts ) == 0 ) $text = $this->link_target ;
|
---|
505 | else $text = array_pop ( $this->link_parts ) ;
|
---|
506 | $text .= $this->link_trail ;
|
---|
507 | $link = '<text:a xlink:type="simple" xlink:href="' . $href . '">' . $text . '</text:a>' ;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 | return $link ;
|
---|
512 | }
|
---|
513 |
|
---|
514 | function handle_extensions ( &$tree ) {
|
---|
515 | global $content_provider , $xml2odt , $xmlg ;
|
---|
516 | $ret = "" ;
|
---|
517 | $name = strtolower ( $this->attrs['EXTENSION_NAME'] ) ;
|
---|
518 | $sub = $this->sub_parse ( $tree ) ;
|
---|
519 |
|
---|
520 | if ( $name == "ref" ) {
|
---|
521 | if ( isset ( $this->attrs['NAME'] ) ) $fname = $this->attrs['NAME'] ;
|
---|
522 | else $fname = "" ;
|
---|
523 | $note_class = strtolower ( trim ( $xmlg["odt_footnote"] ) ) ;
|
---|
524 | $note_style = ucfirst ( $note_class ) ;
|
---|
525 | $id = $xml2odt->get_footnote_id ( $fname , $sub ) ;
|
---|
526 | $ret .= '<text:note text:id="ftn' . $id .
|
---|
527 | '" text:note-class="' . $note_class . '"><text:note-citation>' .
|
---|
528 | $id .
|
---|
529 | '</text:note-citation>' ;
|
---|
530 | $ret .= '<text:note-body><text:p text:style-name="' . $note_style . '">' .
|
---|
531 | $sub .
|
---|
532 | '</text:p></text:note-body>' ;
|
---|
533 | $ret .= '</text:note>' ;
|
---|
534 | } else { # Unhandeled extension
|
---|
535 | $ret = $sub ;
|
---|
536 | }
|
---|
537 |
|
---|
538 | return $ret ;
|
---|
539 | }
|
---|
540 |
|
---|
541 | function parse ( &$tree ) {
|
---|
542 | global $xml2odt ;
|
---|
543 | $ret = '';
|
---|
544 | $tag = $this->name; # Shortcut
|
---|
545 |
|
---|
546 | $old_text_style = $xml2odt->textstyle_current ;
|
---|
547 | $tag_count = count ( $xml2odt->tags ) ;
|
---|
548 |
|
---|
549 | # Open tag
|
---|
550 | if ( $tag == "SPACE" ) {
|
---|
551 | return '<text:s/>' ;
|
---|
552 | } else if ( $tag == "ARTICLE" ) {
|
---|
553 | if ( $xml2odt->article_counter > 0 ) {
|
---|
554 | $ret .= '<text:p text:style-name="PAGEBREAK"/>' ;
|
---|
555 | }
|
---|
556 | $xml2odt->article_counter++ ;
|
---|
557 | if ( isset ( $this->attrs['TITLE'] ) ) {
|
---|
558 | $title = $this->attrs['TITLE'] ;
|
---|
559 | $ret .= '<text:h text:style-name="Heading_20_1" text:outline-level="1">' ;
|
---|
560 | $ret .= urldecode ( $title ) ;
|
---|
561 | $ret .= '</text:h>' ;
|
---|
562 | }
|
---|
563 |
|
---|
564 | } else if ( $tag == "TEMPLATE" ) {
|
---|
565 | return "" ;
|
---|
566 | } else if ( $tag == "TEMPLATEVAR" ) {
|
---|
567 | return "" ;
|
---|
568 | } else if ( $tag == "MAGIC_VARIABLE" ) {
|
---|
569 | return "" ;
|
---|
570 | } else if ( $tag == "HR" ) {
|
---|
571 | return '<text:p text:style-name="PHR"/>' ;
|
---|
572 | } else if ( $tag == "EXTENSION" ) {
|
---|
573 | return $this->handle_extensions ( $tree ) ;
|
---|
574 | } else if ( $tag == "HEADING" || substr ( $tag , 0 , 7 ) == "XHTML:H" ) {
|
---|
575 | if ( $tag == "HEADING" ) $level = $this->attrs['LEVEL'] ;
|
---|
576 | else $level = substr ( $tag , 7 , 1 ) ;
|
---|
577 | $ret .= $this->push_tag ( "text:h" , 'text:style-name="Heading_20_' . $level . '" text:outline-level="' . $level . '"' ) ;
|
---|
578 | } else if ( $tag == "BOLD" || $tag == "XHTML:B" || $tag == "XHTML:STRONG" ) {
|
---|
579 | $xml2odt->textstyle_current->bold = true ;
|
---|
580 | $xml2odt->textstyle_current = $xml2odt->get_text_style ( $xml2odt->textstyle_current ) ;
|
---|
581 | $ret .= $this->push_tag ( "text:span" , "text:style-name=\"" . $xml2odt->textstyle_current->name . "\"" ) ;
|
---|
582 | } else if ( $tag == "XHTML:U" ) {
|
---|
583 | $xml2odt->textstyle_current->underline = true ;
|
---|
584 | $xml2odt->textstyle_current = $xml2odt->get_text_style ( $xml2odt->textstyle_current ) ;
|
---|
585 | $ret .= $this->push_tag ( "text:span" , "text:style-name=\"" . $xml2odt->textstyle_current->name . "\"" ) ;
|
---|
586 | } else if ( $tag == "ITALICS" || $tag == "XHTML:I" || $tag == "XHTML:EM" ) {
|
---|
587 | $xml2odt->textstyle_current->italics = true ;
|
---|
588 | $xml2odt->textstyle_current = $xml2odt->get_text_style ( $xml2odt->textstyle_current ) ;
|
---|
589 | $ret .= $this->push_tag ( "text:span" , "text:style-name=\"" . $xml2odt->textstyle_current->name . "\"" ) ;
|
---|
590 | } else if ( $tag == "PARAGRAPH" || $tag == "XHTML:P" ) {
|
---|
591 | if ( $this->top_tag() != "text:p" )
|
---|
592 | $ret .= $this->push_tag ( "text:p" , 'text:style-name="T0"' ) ;
|
---|
593 | } else if ( $tag == "LIST" || $tag == "XHTML:OL" || $tag == "XHTML:UL" ) {
|
---|
594 | $is_list = true ;
|
---|
595 | $ret .= $xml2odt->ensure_list_closed () ;
|
---|
596 | if ( $this->top_tag() == "text:p" ) {
|
---|
597 | $reopen_p = true ;
|
---|
598 | $ret .= $this->pop_tag () ;
|
---|
599 | }
|
---|
600 | if ( $tag == "LIST" ) $type = strtolower ( $this->attrs['TYPE'] ) ;
|
---|
601 | else $type = "" ;
|
---|
602 | if ( $type == 'numbered' || $tag == 'XHTML:OL' ) $xml2odt->listcode .= "#" ;
|
---|
603 | if ( $type == 'ident' ) $xml2odt->listcode .= " " ;
|
---|
604 | else $xml2odt->listcode .= "*" ;
|
---|
605 | } else if ( $tag == "LINK" ) {
|
---|
606 | return $this->handle_link ( $tree ) ;
|
---|
607 | } else if ( $tag == "LISTITEM" || $tag == "XHTML:LI" ) {
|
---|
608 | $ret .= $xml2odt->ensure_list_open () ;
|
---|
609 | $tag_count = count ( $xml2odt->tags ) ;
|
---|
610 | $p = $xml2odt->list_item_name[strlen($xml2odt->listcode)] ;
|
---|
611 | $ret .= $this->push_tag ( "text:list-item" ) ;
|
---|
612 | $ret .= $this->push_tag ( "text:p" , 'text:style-name="' . $p . '"' ) ;
|
---|
613 |
|
---|
614 | } else if ( $tag == "TABLE" ) {
|
---|
615 | if ( $this->top_tag() == "text:p" ) {
|
---|
616 | $reopen_p = true ;
|
---|
617 | $ret .= $this->pop_tag () ;
|
---|
618 | }
|
---|
619 | $name = $xml2odt->get_table_style ( $this ) ;
|
---|
620 | $ret .= $this->push_tag ( "table:table" , 'table:style-name="' . $name . '"' ) ;
|
---|
621 | $other_ret = $ret ;
|
---|
622 | $ret = "" ;
|
---|
623 | } else if ( $tag == "TABLEROW" ) {
|
---|
624 | $xml2odt->reset_column () ;
|
---|
625 | $ret .= $this->push_tag ( "table:table-row" ) ;
|
---|
626 | } else if ( $tag == "TABLECELL" || $tag == "TABLEHEAD" ) {
|
---|
627 | $name = $xml2odt->get_column_style () ;
|
---|
628 | $ret .= $this->push_tag ( "table:table-cell" , 'table:style_name="' . $name . '" office:value-type="string"' ) ;
|
---|
629 | if ( $tag == "TABLEHEAD" ) $name = "Table_20_Heading" ;
|
---|
630 | else $name = "Table_20_Contents" ;
|
---|
631 | $ret .= $this->push_tag ( "text:p" , 'text:style-name="' . $name . '"' ) ;
|
---|
632 | } else if ( $tag == "TABLECAPTION" ) {
|
---|
633 | return "" ; # Skipping caption
|
---|
634 | }
|
---|
635 |
|
---|
636 | # Children
|
---|
637 | $ret .= $this->sub_parse ( $tree ) ;
|
---|
638 |
|
---|
639 | # Close tag
|
---|
640 | $xml2odt->textstyle_current = $old_text_style ;
|
---|
641 |
|
---|
642 | while ( $tag_count < count ( $xml2odt->tags ) ) {
|
---|
643 | $x = array_pop ( $xml2odt->tags ) ;
|
---|
644 | $ret .= "</{$x}>" ;
|
---|
645 | }
|
---|
646 |
|
---|
647 | if ( isset ( $is_list ) ) {
|
---|
648 | $ret .= $xml2odt->ensure_list_closed () ;
|
---|
649 | $xml2odt->listcode = substr ( $xml2odt->listcode , 0 , strlen ( $xml2odt->listcode ) - 1 ) ;
|
---|
650 | }
|
---|
651 |
|
---|
652 | if ( $tag == "TABLE" ) {
|
---|
653 | $t = $xml2odt->get_top_table_name () ;
|
---|
654 | for ( $a = 0 ; $a < $xml2odt->table_styles[$t]->cols ; $a++ ) {
|
---|
655 | $name = $t . "." . chr ( 65 + $a ) ;
|
---|
656 | $other_ret .= '<table:table-column table:style-name="' . $name . '" table:number-columns-repeated="1"/>' ;
|
---|
657 | }
|
---|
658 | $ret = $other_ret . $ret ;
|
---|
659 | array_pop ( $xml2odt->open_tables ) ;
|
---|
660 | }
|
---|
661 |
|
---|
662 | if ( isset ( $reopen_p ) ) {
|
---|
663 | $ret .= $this->push_tag ( "text:p" , 'text:style-name="T0"' ) ;
|
---|
664 | }
|
---|
665 |
|
---|
666 | return $ret ;
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | require_once ( "xml2tree.php" ) ; # Uses the "element" class defined above
|
---|
671 |
|
---|
672 | ?>
|
---|