-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSO365Handler.hooks.php
99 lines (82 loc) · 3.51 KB
/
MSO365Handler.hooks.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
<?php
/**
* @author Spas Z. Spasov <spas.z.spasov@gmail.com>
* @copyright 2020 Spas Z. Spasov
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
* @home https://github.com/metalevel-tech/mw-MSO365Handler
*
* This file is a part of the MediaWiki Extension:MSO365Handler
*
*/
if (!defined('MEDIAWIKI')) {
die('This file is an extension to MediaWiki and thus not a valid entry point.');
}
/**
* This is the main Class of the extension
* Ref: https://www.mediawiki.org/wiki/Manual:Hooks
*/
class MSO365HandlerHooks
{
/**
* This is the main function, the one that will process the content.
* Ref: https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
*/
public static function onParserAfterTidy( Parser $parser, &$text )
{
global $wgMSO365Handler;
global $wgUploadDirectory;
// Get the current NameSpace
$currentNS = $parser->getTitle()->getNamespace();
$pageTitle = $parser->getTitle()->getText();
// Test whether the current NameSpace belongs to the Allowed NameSpaces
if (in_array($currentNS, $wgMSO365Handler['allowedNameSpaces']) && strpos( $text, $wgMSO365Handler['container'] ) !== false ) {
if (preg_match('~^([^\/]+\.(docx|xlsx|pptx|ppsx|txt|sh|xlsm))$~', $pageTitle, $capturedArr)) {
$filename = $capturedArr[1]; // $capturedArr contains the groups; //echo $capturedArr[1]; //if (count($capturedArr) == 3) { $page = $capturedArr[2]; }
$fileObject = MediaWiki\MediaWikiServices::getInstance()->getRepoGroup()->findFile($filename);
if ($fileObject) {
$filePath = $fileObject->getPath();
$fullFileName = str_replace("mwstore://local-backend/local-public", $wgUploadDirectory, $filePath);
$output = shell_exec('bash '. __DIR__ .'/'. $wgMSO365Handler['bash-processor'] .' "'. $fullFileName .'" 2>&1');
$text = str_replace("MSO365SearchIndex", $output, $text);
return true;
}
}
}
return true;
}
/**
* Load the extension's Scripts And Styles
* Ref: https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
*/
public static function onBeforePageDisplay(OutputPage $out, Skin $skin)
{
global $wgMSO365Handler;
// Get the current NameSpace and if it is File: add the embed script
$currentNS = $out->getTitle()->getNamespace();
if (in_array($currentNS, $wgMSO365Handler['allowedNameSpaces'])) {
$out->addModules('MSO365HandlerScriptsAndStyles');
}
// Add MultiMedia Viewer redirect script
$out->addModules('MSO365HandlerMMVRedirectScript');
return true;
}
/**
* Exposrt some variables to the JS environment
* Ref: https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
*
* Actually the JS embed the file,
* above we just proccess its content and "push it to the search index"
*/
public static function onResourceLoaderGetConfigVars( array &$vars )
{
global $wgMSO365Handler;
// Forward some PHP variables to the JavaScript environment
$vars['wgMSO365Handler'] = [
'width' => $wgMSO365Handler['width'],
'height' => $wgMSO365Handler['height'],
'style' => $wgMSO365Handler['style'],
'action' => $wgMSO365Handler['action'],
];
return true;
}
}