1 | <?php
|
---|
2 |
|
---|
3 | # PHP4 and early PHP5 bug workaround:
|
---|
4 | require_once ( "filter_named_entities.php" ) ;
|
---|
5 |
|
---|
6 | require_once ( "global_functions.php" ) ;
|
---|
7 | require_once ( "wiki2xml.php" ) ;
|
---|
8 | require_once ( "content_provider.php" ) ;
|
---|
9 |
|
---|
10 | # A funtion to remove directories and subdirectories
|
---|
11 | # Modified from php.net
|
---|
12 | function SureRemoveDir($dir) {
|
---|
13 | if(!$dh = @opendir($dir)) return;
|
---|
14 | while (($obj = readdir($dh))) {
|
---|
15 | if($obj=='.' || $obj=='..') continue;
|
---|
16 | if (!@unlink($dir.'/'.$obj)) {
|
---|
17 | SureRemoveDir($dir.'/'.$obj);
|
---|
18 | }
|
---|
19 | }
|
---|
20 | @closedir ( $dh ) ;
|
---|
21 | @rmdir($dir) ;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * The main converter class
|
---|
26 | */
|
---|
27 | class MediaWikiConverter {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Converts a single article in MediaWiki format to XML
|
---|
31 | */
|
---|
32 | function article2xml ( $title , &$text , $params = array () ) {
|
---|
33 | global $content_provider , $wiki2xml_authors ;
|
---|
34 | $ot = $title ;
|
---|
35 | $title = urlencode ( $title ) ;
|
---|
36 | $p = new wiki2xml ;
|
---|
37 | $p->auto_fill_templates = $params['resolvetemplates'] ;
|
---|
38 | $p->template_list = array () ; ;
|
---|
39 | foreach ( $params['templates'] AS $x ) {
|
---|
40 | $x = trim ( ucfirst ( $x ) ) ;
|
---|
41 | if ( $x != "" ) $p->template_list[] = $x ;
|
---|
42 | }
|
---|
43 | $xml = '<article' ;
|
---|
44 | if ( $title != "" ) {
|
---|
45 | $xml .= " title='{$title}'" ;
|
---|
46 | $content_provider->add_article ( urldecode ( $ot ) ) ;
|
---|
47 | }
|
---|
48 | $xml .= '>' ;
|
---|
49 | $xml .= $p->parse ( $text ) ;
|
---|
50 | if ( count ( $wiki2xml_authors ) > 0 ) {
|
---|
51 | $xml .= "<authors>" ;
|
---|
52 | foreach ( $wiki2xml_authors AS $author )
|
---|
53 | $xml .= "<author>{$author}</author>" ;
|
---|
54 | $xml .= "</authors>" ;
|
---|
55 | }
|
---|
56 | $xml .= "</article>" ;
|
---|
57 | return $xml ;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Converts XML to plain text
|
---|
62 | */
|
---|
63 | function articles2text ( &$xml , $params = array () ) {
|
---|
64 | global $wiki2xml_authors ;
|
---|
65 | require_once ( "xml2txt.php" ) ;
|
---|
66 |
|
---|
67 | $wiki2xml_authors = array () ;
|
---|
68 | $x2t = new xml2php ;
|
---|
69 | $tree = $x2t->scanString ( $xml ) ;
|
---|
70 | if ( $params['plaintext_markup'] ) {
|
---|
71 | $tree->bold = '*' ;
|
---|
72 | $tree->italics = '/' ;
|
---|
73 | $tree->underline = '_' ;
|
---|
74 | }
|
---|
75 | if ( $params['plaintext_prelink'] ) {
|
---|
76 | $tree->pre_link = "→" ;
|
---|
77 | }
|
---|
78 |
|
---|
79 | $text = trim ( $tree->parse ( $tree ) ) ;
|
---|
80 |
|
---|
81 | $authors = "" ;
|
---|
82 | if ( count ( $wiki2xml_authors ) > 0 ) {
|
---|
83 | asort ( $wiki2xml_authors ) ;
|
---|
84 | $authors = "\n--------------------\nTHE ABOVE TEXT IS LICENSED UNDER THE GFDL. CONTRIBUTORS INCLUDE:\n\n" .
|
---|
85 | implode ( ", " , $wiki2xml_authors ) ;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return $text . $authors ;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Converts XML to XHTML
|
---|
93 | */
|
---|
94 | function articles2xhtml ( &$xml , $params = array () ) {
|
---|
95 | global $xml2xhtml ;
|
---|
96 | require_once ( "xml2xhtml.php" ) ;
|
---|
97 | $lang = "EN" ; # Dummy
|
---|
98 |
|
---|
99 | $ret = "" ;
|
---|
100 | $ret .= '<?xml version="1.0" encoding="UTF-8" ?>' ;
|
---|
101 | $ret .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//' . $lang . '" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' ;
|
---|
102 | if ( !$params['xhtml_source'] )
|
---|
103 | $ret .= '<html xmlns="http://www.w3.org/1999/xhtml">' ;
|
---|
104 | else $ret .= '<html>' ;
|
---|
105 | $ret .= '<head>' ;
|
---|
106 | #$ret .= '<link rel="stylesheet" type="text/css" media="screen,projection" href="http://de.wikipedia.org/skins-1.5/monobook/main.css" />' ;
|
---|
107 | #$ret .= '<link rel="stylesheet" type="text/css" media="print" href="http://en.wikipedia.org/skins-1.5/common/commonPrint.css" />' ;
|
---|
108 | $ret .= '<link rel="stylesheet" type="text/css" href="href://' ;
|
---|
109 | $ret .= $params["site_base_url"] . 'index.php?title=MediaWiki:Common.css&action=raw" />' ;
|
---|
110 | #$ret .= '<link rel="stylesheet" type="text/css" href="href://' ;
|
---|
111 | #$ret .= $params["site_base_url"] . 'index.php?title=MediaWiki:Monobook.css&action=raw" />' ;
|
---|
112 | $ret .= '<title>' ;
|
---|
113 | if ( isset ( $params['page_title'] ) ) $ret .= $params['page_title'] ;
|
---|
114 | else $ret .= $params["book_title"] ;
|
---|
115 | $ret .= '</title>' ;
|
---|
116 | $ret .= '</head>' ;
|
---|
117 | $ret .= '<body>' ;
|
---|
118 |
|
---|
119 | convert_xml_xhtml ( $xml ) ;
|
---|
120 | $ret .= $xml2xhtml->s ;
|
---|
121 |
|
---|
122 | # $xml2xhtml = new XML2XHTML ;
|
---|
123 | # $ret .= $xml2xhtml->scan_xml ( $xml ) ;
|
---|
124 |
|
---|
125 | $ret .= '</body>' ;
|
---|
126 | $ret .= '</html>' ;
|
---|
127 | return $ret ;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Converts XML to ODT XML
|
---|
132 | */
|
---|
133 | function articles2odt ( &$xml , $params = array () , $use_gfdl = false ) {
|
---|
134 | global $wiki2xml_authors , $xml2odt ;
|
---|
135 | require_once ( "xml2odt.php" ) ;
|
---|
136 |
|
---|
137 | # XML text to tree
|
---|
138 | $xml2odt = new XML2ODT ;
|
---|
139 | $wiki2xml_authors = array () ;
|
---|
140 | $x2t = new xml2php ;
|
---|
141 | $tree = $x2t->scanString ( $xml ) ;
|
---|
142 |
|
---|
143 | # Tree to ODT
|
---|
144 | $out = "<?xml version='1.0' encoding='UTF-8' ?>\n" ;
|
---|
145 | $body = $tree->parse ( $tree ) ;
|
---|
146 | $out .= $xml2odt->get_odt_start () ;
|
---|
147 | $out .= '<office:body><office:text>' ;
|
---|
148 | $out .= $body ;
|
---|
149 | $out .= '</office:text></office:body>' ;
|
---|
150 | $out .= "</office:document-content>" ;
|
---|
151 | return $out ;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Converts XML to DocBook XML
|
---|
156 | */
|
---|
157 | function articles2docbook_xml ( &$xml , $params = array () , $use_gfdl = false ) {
|
---|
158 | global $wiki2xml_authors ;
|
---|
159 | require_once ( "xml2docbook_xml.php" ) ;
|
---|
160 |
|
---|
161 | $wiki2xml_authors = array () ;
|
---|
162 | $x2t = new xml2php ;
|
---|
163 | $tree = $x2t->scanString ( $xml ) ;
|
---|
164 |
|
---|
165 | # Chosing DTD; parameter-given or default
|
---|
166 | $dtd = "" ;
|
---|
167 | if ( isset ( $params['docbook']['dtd'] ) )
|
---|
168 | $dtd = $params['docbook']['dtd'] ;
|
---|
169 | if ( $dtd == "" ) $dtd = 'http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd' ;
|
---|
170 |
|
---|
171 | $out = "<?xml version='1.0' encoding='UTF-8' ?>\n" ;
|
---|
172 | $out .= '<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "' . $dtd . '"' ;
|
---|
173 | if ( $use_gfdl ) {
|
---|
174 | $out .= "\n[<!ENTITY gfdl SYSTEM \"gfdl.xml\">]\n" ;
|
---|
175 | }
|
---|
176 | $out .= ">\n\n<book>\n" ;
|
---|
177 | $out2 = trim ( $tree->parse ( $tree ) ) ;
|
---|
178 |
|
---|
179 | $out .= "<bookinfo>" ;
|
---|
180 | $out .= "<title>" . $params['book_title'] . "</title>" ;
|
---|
181 | if ( count ( $wiki2xml_authors ) > 0 ) {
|
---|
182 | asort ( $wiki2xml_authors ) ;
|
---|
183 | $out .= "<authorgroup>" ;
|
---|
184 | foreach ( $wiki2xml_authors AS $author ) {
|
---|
185 | $out .= "<author><othername>{$author}</othername></author>" ;
|
---|
186 | }
|
---|
187 | $out .= "</authorgroup>" ;
|
---|
188 | }
|
---|
189 | $out .= "<legalnotice><para>" ;
|
---|
190 | $out .= "Permission to use, copy, modify and distribute this document under the GNU Free Documentation License (GFDL)." ;
|
---|
191 | $out .= "</para></legalnotice>" ;
|
---|
192 | $out .= "</bookinfo>" ;
|
---|
193 |
|
---|
194 | $out .= $out2 ;
|
---|
195 | /*
|
---|
196 | if ( count ( $wiki2xml_authors ) > 0 ) {
|
---|
197 | asort ( $wiki2xml_authors ) ;
|
---|
198 | $out .= "<appendix>" ;
|
---|
199 | $out .= "<title>List of contributors</title>" ;
|
---|
200 | $out .= "<para>All text in this document is licensed under the GFDL. The following is a list of contributors (anonymous editors are not listed).</para>" ;
|
---|
201 | $out .= "<para>" ;
|
---|
202 | $out .= implode ( ", " , $wiki2xml_authors ) ;
|
---|
203 | $out .= "</para>" ;
|
---|
204 | $out .= "</appendix>" ;
|
---|
205 | }
|
---|
206 | */
|
---|
207 | if ( $use_gfdl ) {
|
---|
208 | $out .= "\n&gfdl;\n" ;
|
---|
209 | }
|
---|
210 |
|
---|
211 | $out .= "\n</book>\n" ;
|
---|
212 |
|
---|
213 | return $out ;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Converts XML to PDF via DocBook
|
---|
218 | * Requires special parameters in local.php to be set (see sample_local.php)
|
---|
219 | * Uses articles2docbook_xml
|
---|
220 | */
|
---|
221 | function articles2docbook_pdf ( &$xml , $params = array () , $mode = "PDF" ) {
|
---|
222 | global $xmlg ;
|
---|
223 | $docbook_xml = $this->articles2docbook_xml ( $xml , $params , $params['add_gfdl'] ) ;
|
---|
224 |
|
---|
225 | # Create temporary directory
|
---|
226 | $temp_dir = "MWC" ;
|
---|
227 | $temp_dir .= substr ( mt_rand() , 0 , 4 ) ;
|
---|
228 | $temp_dir = tempnam ( $params['docbook']['temp_dir'], $temp_dir ) ;
|
---|
229 | $project = basename ( $temp_dir ) ;
|
---|
230 | unlink ( $temp_dir ) ; # It is currently a file, so...
|
---|
231 | mkdir ( $temp_dir ) ;
|
---|
232 |
|
---|
233 | # Write XML file
|
---|
234 | $xml_file = $temp_dir . "/" . $project . ".xml" ;
|
---|
235 | $handle = fopen ( $xml_file , 'wb' ) ;
|
---|
236 | fwrite ( $handle , utf8_encode ( $docbook_xml ) ) ;
|
---|
237 | fclose ( $handle ) ;
|
---|
238 | if ( $params['add_gfdl'] ) {
|
---|
239 | copy ( $xmlg['sourcedir'] . "/gfdl.xml" , $temp_dir . "/gfdl.xml" ) ;
|
---|
240 | }
|
---|
241 |
|
---|
242 | if ( $params['docbook']['out_dir'] ) {
|
---|
243 | $output_dir = $params['docbook']['out_dir'];
|
---|
244 | } else {
|
---|
245 | $output_dir = $params['docbook']['temp_dir'];
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | # Call converter
|
---|
250 | if ( $mode == "PDF" ) {
|
---|
251 | $command = str_replace ( "%1" , $xml_file , $params['docbook']['command_pdf'] ) ;
|
---|
252 | $out_subdir = 'pdf' ;
|
---|
253 | } else if ( $mode == "HTML" ) {
|
---|
254 | $command = str_replace ( "%1" , $xml_file , $params['docbook']['command_html'] ) ;
|
---|
255 | $out_subdir = 'html' ;
|
---|
256 | }
|
---|
257 |
|
---|
258 | # PHP4 does not have recursive mkdir
|
---|
259 | $output_dir = $output_dir . '/' . $out_subdir ;
|
---|
260 | if ( ! file_exists( $output_dir ) ) {
|
---|
261 | mkdir ( $output_dir ) ;
|
---|
262 | }
|
---|
263 | $output_dir = $output_dir . '/' . $project;
|
---|
264 | if ( ! file_exists( $output_dir ) ) {
|
---|
265 | mkdir ( $output_dir ) ;
|
---|
266 | }
|
---|
267 |
|
---|
268 | $command = $command . ' --nochunks --output ' . $output_dir;
|
---|
269 |
|
---|
270 | exec ( $command ) ;
|
---|
271 |
|
---|
272 | # Cleanup xml file
|
---|
273 | SureRemoveDir ( $temp_dir ) ;
|
---|
274 |
|
---|
275 | # Check if everything is OK
|
---|
276 | $output_filename = $output_dir . '/' . $project . '.' . $out_subdir ;
|
---|
277 | if ( !file_exists ( $output_filename ) ) {
|
---|
278 | header('Content-type: text/html; charset=utf-8');
|
---|
279 | print "ERROR : Document was not created: Docbook creator has failed! Command was: $command. output_filename = $output_filename" ;
|
---|
280 | }
|
---|
281 |
|
---|
282 | # Return pdf filename
|
---|
283 | return $output_filename ;
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | ?>
|
---|