| 1 |
<?php |
| 2 |
// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName |
| 3 |
|
| 4 |
/** |
| 5 |
* Hooks for jsxgraph extension. |
| 6 |
* |
| 7 |
* @file |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MediaWiki\Extension\JSXGraph; |
| 11 |
|
| 12 |
use MediaWiki\MediaWikiServices; |
| 13 |
use Parser; |
| 14 |
use PPFrame; |
| 15 |
|
| 16 |
/** Hooks */ |
| 17 |
class Hooks implements |
| 18 |
\MediaWiki\Hook\ParserFirstCallInitHook, |
| 19 |
\MediaWiki\Hook\ParserAfterTidyHook |
| 20 |
{ |
| 21 |
|
| 22 |
private $markerList = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Register parser hook. |
| 26 |
* |
| 27 |
* The content of tags of the form <jsxgraph>...</jsxgraph> |
| 28 |
* is stored in the array $this->markerList |
| 29 |
* |
| 30 |
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit |
| 31 |
* @see https://www.mediawiki.org/wiki/Manual:Parser_functions |
| 32 |
* @param Parser $parser |
| 33 |
* @throws \MWException |
| 34 |
*/ |
| 35 |
public function onParserFirstCallInit($parser) |
| 36 |
{ |
| 37 |
$parser->setHook('jsxgraph', [$this, 'parserTagJSXGraph']); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* After call of tidy, the required JSXGraph divs and script |
| 42 |
* tags are injected. jsxgraphcore.js is loaded only once. |
| 43 |
*/ |
| 44 |
public function onParserAfterTidy($parser, &$text) |
| 45 |
{ |
| 46 |
$keys = array(); |
| 47 |
$marker_count = count($this->markerList); |
| 48 |
|
| 49 |
for ($i = 0; $i < $marker_count; $i++) { |
| 50 |
$keys[] = 'jsxgraph-marker' . $i . '-jsxgraph'; |
| 51 |
} |
| 52 |
$text = str_replace($keys, $this->markerList, $text); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Parser hook handler for <jsxgraph>. |
| 58 |
* Called by onParserFirstCallInit |
| 59 |
* |
| 60 |
* @param string $data The content of the tag. |
| 61 |
* @param array $attribs The attributes of the tag. |
| 62 |
* @param Parser $parser Parser instance available to render |
| 63 |
* wikitext into html, or parser methods. |
| 64 |
* @param PPFrame $frame Can be used to see what template |
| 65 |
* arguments ({{{1}}}) this hook was used with. |
| 66 |
* @return string HTML to insert in the page. |
| 67 |
*/ |
| 68 |
public function parserTagJSXGraph($data, $attribs, $parser, $frame) |
| 69 |
{ |
| 70 |
$jsxgraph = [ |
| 71 |
'content' => $data, |
| 72 |
'attributes' => (object) $attribs, |
| 73 |
]; |
| 74 |
return $this->markJSXGraphTags($jsxgraph); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Here, the real work is done. The attributes of the jsxgraph tag are analyzed, |
| 79 |
* the JSXGraph divs are created, source code is stored. |
| 80 |
*/ |
| 81 |
private function markJSXGraphTags($jsxgraph) |
| 82 |
{ |
| 83 |
$output = ""; |
| 84 |
$attr = json_decode(json_encode($jsxgraph["attributes"]), true); |
| 85 |
$error_message = "no error"; |
| 86 |
|
| 87 |
$markercount = count($this->markerList); |
| 88 |
if ($markercount > 0) { |
| 89 |
$default_board = "brd" . $markercount; |
| 90 |
$default_box = "jxgbox" . $markercount; |
| 91 |
} else { |
| 92 |
$default_board = "brd"; |
| 93 |
$default_box = "jxgbox"; |
| 94 |
} |
| 95 |
|
| 96 |
if (!(isset($args['filename']) || isset($args['filestring']) || isset($jsxgraph["content"]))) { |
| 97 |
$error_message = "Missing parameter (width or height, filename, string or input)."; |
| 98 |
} |
| 99 |
|
| 100 |
$outputURI = '//jsxgraph.uni-bayreuth.de/~alfred/jsxgraph/distrib'; |
| 101 |
// $jsxgraph_version = ""; |
| 102 |
// $outputURICDN = 'https://cdn.jsdelivr.net/npm/jsxgraph@' . $jsxgraph_version . '/distrib'; |
| 103 |
$outputURICDN = 'https://cdn.jsdelivr.net/npm/jsxgraph/distrib'; |
| 104 |
|
| 105 |
$outputDivId = (isset($attr['box'])) ? htmlspecialchars(strip_tags($attr['box'])) : $default_box; |
| 106 |
$outputBoardId = (isset($attr['board'])) ? htmlspecialchars(strip_tags($attr['board'])) : $default_board; |
| 107 |
$width = (isset($attr['width'])) ? htmlspecialchars(strip_tags($attr['width'])) : 500; |
| 108 |
$height = (isset($attr['height'])) ? htmlspecialchars(strip_tags($attr['height'])) : 400; |
| 109 |
|
| 110 |
if ($markercount == 0) { |
| 111 |
if (FALSE && preg_match("/^132\.180/", getenv("REMOTE_ADDR"))) { |
| 112 |
// Use the local version when developing |
| 113 |
$output .= "<pre>IP:". getenv("REMOTE_ADDR") ." - use developer version</pre>"; |
| 114 |
$output .= "<link rel='stylesheet' type='text/css' href='" . $outputURI . "/jsxgraph.css' />"; |
| 115 |
$output .= "<script src='" . $outputURI . "/jsxgraphcore.js' type='text/javascript'></script>"; |
| 116 |
$output .= "<script src='" . $outputURI . "/geonext.min.js' type='text/javascript'></script>"; |
| 117 |
} else { |
| 118 |
$output .= "<link rel='stylesheet' type='text/css' href='" . $outputURICDN . "/jsxgraph.css' />"; |
| 119 |
$output .= "<script src='" . $outputURICDN . "/jsxgraphcore.js' type='text/javascript'></script>"; |
| 120 |
$output .= "<script src='" . $outputURICDN . "/geonext.min.js' type='text/javascript'></script>"; |
| 121 |
} |
| 122 |
} |
| 123 |
if (isset($attr['modules']) && trim($attr['modules']) != "") { |
| 124 |
$modules = explode(',', $attr['modules']); |
| 125 |
for ($i = 0; $i < count($modules); $i++) { |
| 126 |
$output .= "<script src='" . $outputURI . "/" . $modules[$i] . ".js'></script>"; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$output .= "<div id='" . $outputDivId . "' class='jxgbox' style='width:" . $width . "px; height:" . $height . "px;'></div>"; |
| 131 |
$output .= "<script>"; |
| 132 |
|
| 133 |
|
| 134 |
if (isset($attr['filename'])) { // string of url to gxt-file |
| 135 |
// retrieve URL of .gxt file |
| 136 |
$gxtBinary = htmlspecialchars(strip_tags($attr['filename'])); |
| 137 |
$gxtFile = MediaWikiServices::getInstance()->getRepoGroup()->findFile()($gxtBinary); |
| 138 |
if (!($gxtFile->exists())) { |
| 139 |
$error_message = "File " . $gxtFile . " not found."; |
| 140 |
} else { |
| 141 |
$gxtURL = $wgCanonicalServer . $gxtFile->getURL(); |
| 142 |
} |
| 143 |
$output .= " var " . $outputBoardId . " = JXG.JSXGraph.loadBoardFromFile('" . $outputDivId . "', '" . $gxtURL . "', 'Geonext');"; |
| 144 |
} |
| 145 |
if (isset($attr['filestring'])) { // binary content of gxt-file |
| 146 |
$output .= " var " . $outputBoardId . |
| 147 |
" = JXG.JSXGraph.loadBoardFromString('" |
| 148 |
. $outputDivId . "', '" . htmlspecialchars(strip_tags($attr['filestring'])) . "', 'Geonext');"; |
| 149 |
} |
| 150 |
if (isset($jsxgraph["content"])) { // content between <jsxgraph>-tags |
| 151 |
$output .= $jsxgraph["content"]; |
| 152 |
} |
| 153 |
$output .= "</script>"; |
| 154 |
|
| 155 |
if ($error_message != "no error") { |
| 156 |
$output = "<p>Error in MediaWiki extension (JSXGraph.php): <em>" . $error_message . "</em></p>\n"; |
| 157 |
} |
| 158 |
|
| 159 |
$marker = "jsxgraph-marker" . $markercount . "-jsxgraph"; |
| 160 |
$this->markerList[$markercount] = $output; |
| 161 |
return $marker; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|