-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocExport.php
491 lines (442 loc) · 17.4 KB
/
DocExport.php
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
* MediaWiki DocExport extension
* Version 1.5 compatible with MediaWiki 1.19-1.26
*
* Copyright © 2008-2015 Stas Fomin, Vitaliy Filippov
* http://wiki.4intra.net/DocExport
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* INSTALLATION:
* 1) Put require_once("$IP/extensions/DocExport/DocExport.php"); into your LocalSettings.php
* 2) DocExport-templates.xml file contains some useful M$Word voodoo magic templates.
* Import it into your Wiki via Special:Import to use them.
* 3) Install cleanmonobook skin from mediawiki4intranet if you want to use "Clean HTML"
*
* FEATURES:
* 1) Content-action tab "purge" is added - click it to clear article cache
* 2) "->m$word", "->openoffice" links are added to toolbox - click to open article in m$word/OO.o
* 3) "Clean HTML" link is also added - the intent is to allow display of "clean" page version, without
* any toolboxes and navigation, but WITH normal page styles. Link leads to &useskin=cleanmonobook
* by default ("cleanmonobook" skin is required). To change it, set the following variable:
* $egDocexportCleanHtmlParams = "useskin=cleanmonobook";
* To disable:
* $egDocexportCleanHtmlParams = false;
*/
if (!defined('MEDIAWIKI'))
{
?>
<p>This is the DocExport extension. To enable it, put </p>
<pre>require_once("$IP/extensions/DocExport/DocExport.php");</pre>
<p>at the bottom of your LocalSettings.php.</p>
<?php
exit(1);
}
$wgHooks['SkinTemplateContentActions'][] = 'DocExport::onSkinTemplateContentActions';
$wgHooks['UnknownAction'][] = 'DocExport::onUnknownAction';
$wgHooks['SkinTemplateNavigation'][] = 'DocExport::onSkinTemplateNavigation';
$wgHooks['SkinTemplateToolboxEnd'][] = 'DocExport::SkinTemplateToolboxEnd';
$wgHooks['MagicWordwgVariableIDs'][] = 'DocExport::MagicWordwgVariableIDs';
$wgHooks['ParserGetVariableValueSwitch'][] = 'DocExport::ParserGetVariableValueSwitch';
$wgHooks['ParserFirstCallInit'][] = 'DocExport::ParserFirstCallInit';
$wgExtensionMessagesFiles['DocExport'] = dirname(__FILE__).'/DocExport.i18n.php';
$wgExtensionFunctions[] = 'DocExport::Setup';
$wgExtensionCredits['other'][] = array(
'name' => 'DocExport',
'author' => 'Stas Fomin',
'version' => DocExport::$version,
'description' => 'Adds 3 new actions for pages: render as HTML for M$WORD / OpenOffice, purge article',
'url' => 'http://wiki.4intra.net/DocExport',
);
$egDocexportCleanHtmlParams = "useskin=cleanmonobook";
class DocExport
{
static $version = '1.5 (2015-10-20)';
static $required_mw = '1.19';
static $actions = NULL;
static $css = '';
static $expandingUrls = false;
static function Setup()
{
// A current MW-Version is required so check for it...
wfUseMW(self::$required_mw);
}
//// hooks ////
// Hook that creates {{DOCEXPORT}} magic word
static function MagicWordwgVariableIDs(&$mVariablesIDs)
{
$mVariablesIDs[] = 'docexport';
return true;
}
// Hook that evaluates {{DOCEXPORT}} magic word
static function ParserGetVariableValueSwitch($parser, &$varCache, &$index, &$ret)
{
if ($index == 'docexport')
$ret = !empty($parser->extIsDocExport) ? '1' : '';
return true;
}
// Parser function used to add custom css for export
static function docexportcss($parser, $args)
{
self::$css .= trim($args)."\n";
return '';
}
// Sets function hook to parser
static function ParserFirstCallInit($parser)
{
$parser->setFunctionHook('docexportcss', 'DocExport::docexportcss');
return true;
}
// Hook used to display a tab in standard skins
static function onSkinTemplateContentActions(&$content_actions)
{
self::fillActions();
if (!empty(self::$actions['purge']))
$content_actions['purge'] = self::$actions['purge'];
return true;
}
// Hook used to display a tab in Vector (MediaWiki 1.16+) skin
// Also standard skins in MediaWiki 1.18
static function onSkinTemplateNavigation($skin, &$links)
{
self::fillActions();
if (!empty(self::$actions['purge']))
$links[get_class($skin) == 'SkinVector' ? 'views' : 'actions']['purge'] = self::$actions['purge'];
return true;
}
// Hook for handling DocExport actions
static function onUnknownAction($action, $article)
{
$action = strtolower($action);
if ($action == 'export2word' || $action == 'export2oo')
{
self::sendTo($article, substr($action, 7));
return false;
}
return true;
}
// Output our TOOLBOX links
static function SkinTemplateToolboxEnd($tpl)
{
self::fillActions();
foreach (array('cleanmonobook', 'export2word', 'export2oo') as $link)
if (!empty(self::$actions[$link]))
print '<li id="t-'.$link.'" title="'.
htmlspecialchars(self::$actions[$link]['tooltip']).
'"><a href="'.self::$actions[$link]['href'].'">'.
htmlspecialchars(self::$actions[$link]['text']).
'</a></li>';
return true;
}
//// non-hooks ////
// fills self::$actions for current title
static function fillActions()
{
// Actions already filled?
if (self::$actions !== NULL)
return true;
self::$actions = array();
global $wgTitle, $wgRequest, $egDocexportCleanHtmlParams;
if (!$wgTitle)
return false;
$disallow_actions = array('edit', 'submit'); // disallowed actions
$action = $wgRequest->getVal('action');
$current_ns = $wgTitle->getNamespace();
// Disable for special pages
if ($current_ns < 0)
return false;
// Disable for edit/preview
if (in_array($action, $disallow_actions))
return false;
self::$actions['export2word'] = array(
'text' => wfMessage('docexport-msword-export-link')->text(),
'tooltip' => wfMessage('tooltip-ca-export2word')->text(),
'href' => $wgTitle->getFullURL('action=export2word'),
'class' => '',
);
self::$actions['export2oo'] = array(
'text' => wfMessage('docexport-oo-export-link')->text(),
'tooltip' => wfMessage('tooltip-ca-export2oo')->text(),
'href' => $wgTitle->getFullURL('action=export2oo'),
'class' => '',
);
self::$actions['purge'] = array(
'text' => wfMessage('docexport-purge-tab')->text(),
'tooltip' => wfMessage('tooltip-ca-purge')->text(),
'href' => $wgTitle->getFullURL('action=purge'),
'class' => '',
);
if ($egDocexportCleanHtmlParams)
{
self::$actions['cleanmonobook'] = array(
'text' => wfMessage('link-cleanmonobook')->text(),
'tooltip' => wfMessage('tooltip-link-cleanmonobook')->text(),
'href' => $wgTitle->getLocalURL($egDocexportCleanHtmlParams),
'class' => '',
);
}
return true;
}
// Output HTML code with correct content-type for M$WORD / OO
static function sendTo($article, $to)
{
global $wgParser, $wgOut;
$html = self::getPureHTML($article);
$title = $article->getTitle();
// Fetch styles from MediaWiki:docexport-$to.css, expand templates
$st = wfMessageNoTrans("docexport-$to.css")->text();
$st = $wgParser->preprocess($st, Title::makeTitleSafe(NS_MEDIAWIKI, "docexport-$to.css"), new ParserOptions());
if ($to == 'word')
{
// Add styles for HTML list numbering
$html = self::multinumLists($html, $st);
// Enable page numbering
$html = "<div class=\"SectionNumbered\">$html</div>";
}
if (!empty(self::$css))
{
if (preg_match('/mso-(even|first|)-?(header|footer)/is', self::$css))
{
// Remove headers/footers when page is using custom ones
$st = preg_replace('/mso-(even|first|)-?(header|footer)\s*:[^;]*;\s*/is', '', $st);
}
$st = trim($st)."\n".self::$css;
}
$html =
'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><html><head>' .
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' .
($to == 'word' ? '<meta name=ProgId content=Word.Document>' : '') .
'<style type="text/css"><!--' . "\n" .
$st .
'/*-->*/</style></head><body>' .
$html .
'</body></html>';
$wgOut->setPrintable();
$wgOut->disable();
wfResetOutputBuffers();
header('Content-Type: '.($to == 'word' ? 'application/msword' : 'vnd.oasis.opendocument.text'));
header('Content-Length: '.strlen($html));
$filename = $title.($to == 'word' ? '.doc' : '.odp');
header('Content-Disposition: attachment; filename="'.$filename.'"');
echo $html;
}
/* Load HTML content into a DOMDocument */
static function loadDOM($html)
{
$dom = new DOMDocument();
$oe = error_reporting();
error_reporting($oe & ~E_WARNING);
$dom->loadHTML("<?xml version='1.0' encoding='UTF-8'?>".mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
error_reporting($oe);
return $dom;
}
/* Export children of $element to an HTML string */
static function saveChildren($element, $trim = false)
{
$xml = $element->ownerDocument->saveXML($element, LIBXML_NOEMPTYTAG);
$xml = preg_replace('/^\s*<[^>]*>(.*?)<\/[^\>]*>\s*$/uis', '\1', $xml);
$xml = preg_replace('#(<(br|input)(\s+[^<>]*[^/])?)></\2>#', '\1 />', $xml);
return $xml;
}
/* Make HTML ordered lists with class=multinum or inside an element with class=multinum
numbered hierarchically */
static function multinumLists($html, &$css)
{
if (!preg_match('/<([a-z0-9-:]+)[^<>]*class="[^<>\"\'\s]*multinum[^<>]*>/is', $html))
return $html;
$maxlevel = array();
$dom = self::loadDOM($html);
$stack = array(array($dom->documentElement, 0, false, 0));
$maxlist = 0;
while ($stack)
{
list($p, $i, $multi, $listindex) = $stack[0];
if ($i >= $p->childNodes->length)
{
array_shift($stack);
continue;
}
$stack[0][1]++;
$e = $p->childNodes->item($i);
if ($e->nodeType == XML_ELEMENT_NODE)
{
if (!$multi && preg_match('/\bmultinum\b/s', $e->getAttribute('class')))
{
// Begin multinumbered list
$stack[0][2] = $multi = 1;
}
if ($multi && $e->nodeName == 'li')
{
// Add M$Word pseudo-style
$level = $multi-1;
$style = "mso-list: l$listindex level$level lfo$level";
if (!($a = $e->getAttribute('style')))
$e->setAttribute('style', $style);
else
$a->value = rtrim($a->value, "; \t\r\n") . '; ' . $style;
}
elseif ($multi && $e->nodeName == 'ol')
{
if ($multi < 2)
$listindex = ++$maxlist;
$maxlevel[$multi][$listindex] = true;
$multi++;
}
if ($e->childNodes->length)
array_unshift($stack, array($e, 0, $multi, $listindex));
}
}
// Append CSS classes to $st
$st = '';
for ($i = 1; $maxlevel[$i]; $i++)
{
$st .= '%'.$i.'\.';
$k = array_keys($maxlevel[$i]);
foreach ($k as &$list)
$list = "@list l$list:level$i";
$css .= implode(", ", $k) . " { mso-level-text:\"$st\"; }\n";
}
return self::saveChildren($dom->documentElement->childNodes->item(0));
}
static function getPureHTML($article)
{
global $wgUser, $wgParser;
$title = $article->getTitle();
while ($title->isRedirect())
{
if (!$title->userCan('read'))
break;
$title = $article->getRedirectTarget();
$article = new Article($title);
}
// Check read permission
if (!$title->userCan('read'))
{
print '<html><body>DocExport: Permission Denied</body></html>';
exit;
}
self::expandLocalUrls(true);
$parserOptions = ParserOptions::newFromUser($wgUser);
$parserOptions->setEditSection(false);
$parserOptions->setTidy(true);
$wgParser->mShowToc = false;
$wgParser->extIsDocExport = true;
$parserOutput = $wgParser->parse($article->preSaveTransform($article->getContent())."\n", $title, $parserOptions);
$wgParser->extIsDocExport = false;
$html = self::html2print($parserOutput->getText(), $title);
self::expandLocalUrls(false);
return $html;
}
static function html2print($html, $title = NULL)
{
$html = self::clearScreenOnly($html);
// Remove [svg] graphviz links
$html = str_replace('[svg]</a>', '</a>', $html);
// Remove hyperlinks to images on the server
$html = self::clearHrefs($html);
// Remove enclosing <object type="image/svg+xml"> for SVG+PNG images
$html = preg_replace('#<object[^<>]*type=[\"\']?image/svg\+xml[^<>]*>(.*?)</object\s*>#is', '\1', $html);
// Replace links to anchors within self to just anchors
if ($title)
$html = str_replace('href="'.$title->getLocalUrl().'#', 'href="#', $html);
return $html;
}
static function expandLocalUrls($enable = true)
{
global $wgArticlePath, $wgScriptPath, $wgUploadPath, $wgStylePath, $wgMathPath, $wgLocalFileRepo;
static $originalPaths = null;
$prev = self::$expandingUrls;
if ($enable)
{
if (!self::$expandingUrls)
{
self::$expandingUrls = true;
// Save original values.
$originalPaths = array(
$wgArticlePath, $wgScriptPath, $wgUploadPath, $wgStylePath, $wgMathPath, $wgLocalFileRepo['url']
);
// Expand paths.
$wgArticlePath = wfExpandUrl($wgArticlePath);
$wgScriptPath = wfExpandUrl($wgScriptPath);
$wgUploadPath = wfExpandUrl($wgUploadPath);
$wgStylePath = wfExpandUrl($wgStylePath);
$wgMathPath = wfExpandUrl($wgMathPath);
$wgLocalFileRepo['url'] = wfExpandUrl($wgLocalFileRepo['url']);
// Destroy existing RepoGroup, if any.
RepoGroup::destroySingleton();
}
}
else
{
if (self::$expandingUrls)
{
self::$expandingUrls = false;
// Restore original values.
list($wgArticlePath, $wgScriptPath, $wgUploadPath, $wgStylePath, $wgMathPath, $wgLocalFileRepo['url']) = $originalPaths;
// Destroy existing RepoGroup, if any.
RepoGroup::destroySingleton();
}
}
return $prev;
}
static function clearScreenOnly($text)
{
return self::cutBlock($text, "/<\\s*div\\s*class=\"(screenonly|printfooter)\"/i","/<\\/\\s*div\\s*>/i");
}
static function clearHrefs($text)
{
global $wgScriptPath;
$regexp = "/<a[^<>]*href=[\"\']?" . str_replace("/", "\/", $wgScriptPath) . "\/images[^<>]*>/i";
return self::stripTags($text, $regexp, '#</\s*a\s*>#i');
}
static function stripTags($text, $startRegexp, $endRegexp)
{
$stripped = '';
while ('' != $text)
{
$p = preg_split($startRegexp, $text, 2);
$stripped .= $p[0];
if ((count($p) < 2) || ('' == $p[1]))
$text = '';
else
{
$q = preg_split($endRegexp, $p[1], 2);
$stripped .= $q[0];
$text = $q[1];
}
}
return $stripped;
}
static function cutBlock($text, $startRegexp, $endRegexp)
{
$stripped = '';
while ('' != $text)
{
$p = preg_split($startRegexp, $text, 2);
$stripped .= $p[0];
if ((count($p) < 2) || ('' == $p[1]))
$text = '';
else
{
$q = preg_split($endRegexp, $p[1], 2);
$text = $q[1];
}
}
return $stripped;
}
}