Debian Codegraph @

plugins/mediawiki/JSXGraph.php

raw
1 <?php
2 /*
3 Copyright 2008-2025
4 Alfred Wassermann,
5 Peter Wilfahrt
6
7 This file is part of JSXGraph.
8
9 JSXGraph is free software dual licensed under the GNU LGPL or MIT License.
10
11 You can redistribute it and/or modify it under the terms of the
12
13 * GNU Lesser General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version
16 OR
17 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT
18
19 JSXGraph is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public License and
25 the MIT License along with JSXGraph. If not, see <https://www.gnu.org/licenses/>
26 and <https://opensource.org/licenses/MIT/>.
27 */
28
29
30 /**
31 * JSXGraph extension
32 *
33 * @author Alfred Wassermann
34 * @author Peter Wilfahrt
35 * @version 1.00.0
36 */
37 // use MediaWiki\MediaWikiServices;
38
39 /** Requirements:
40 * Allowing upload of .gxt-Files:
41 * - Add the following two lines to LocalSettings.php:
42 * $wgFileExtensions[] = 'gxt';
43 * $wgVerifyMimeType = false;
44 * - Place this file into:
45 * Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.
46 * $IP/extensions/JSXGraph.php
47 * - To install following line has to be added to LocalSettings.php:
48 * require_once("$IP/extensions/JSXGraph.php");
49 * - Now you can include JSXGraph drawings with a .gxt-file:
50 * <jsxgraph height="300" width="600" filename="myFile.gxt" />
51 * or providing a gxt-string:
52 * <jsxgraph height="300" width="600" filestring="$$GXT-binarystring should be here$$" />
53 * or providing a javascript-input:
54 * <jsxgraph height="300" width="600">
55 * var brd = JXG.JSXGraph....();
56 * </jsxgraph>
57 *
58 * jsxgraph-params:
59 * width: default: 500
60 * height: default: 400
61 * modules: default: "", additional javascript files that should be loaded, comma separated and without the file
62 * extension. Those have to be in the same directory as the jsxgraphcore.js, e.g. "Square,JessieScript"
63 * filename, filestring or $input (between <jsxgraph>-tags) --> required
64 * box: default: jxgbox
65 * board: default: brd
66 */
67 $jsxgraph_version = '1.2.2';
68
69 // CHANGE this to load local files:
70 //$outputURI = '//jsxgraph.org/distrib';
71 $outputURI = '//jsxgraph.uni-bayreuth.de/~alfred/jsxgraph/distrib';
72 // $outputURICDN = '//cdnjs.cloudflare.com/ajax/libs/jsxgraph/' . $jsxgraph_version;
73 $outputURICDN = 'https://cdn.jsdelivr.net/npm/jsxgraph@' . $jsxgraph_version . '/distrib';
74 // $outputURI = $outputURICDN;
75
76 if (!defined('MEDIAWIKI')) {
77 echo ("This is an extension to the MediaWiki package and cannot be run standalone.\n");
78 die(-1);
79 }
80
81 //Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980
82 if (defined('MW_SUPPORTS_PARSERFIRSTCALLINIT')) {
83 // Newer
84 echo "VERSION 1";
85 //$wgHooks['ParserFirstCallInit'][] = 'jsxgraphSetup';
86 $wgHooks['ParserAfterTidy'][] = 'jsxgraphParserAfterTidy';
87 } else {
88 // Very old
89 // $wgExtensionFunctions[] = 'jsxgraphSetup';
90 $wgHooks['ParserAfterTidy'][] = 'jsxgraphParserAfterTidy';
91 }
92
93 $wgExtensionCredits['parserhook'][] = array(
94 'name' => 'JSXGraph MediaWiki Plugin',
95 'author' => 'Alfred Wassermann, Peter Wilfahrt',
96 'url' => 'https://www.jsxgraph.org/',
97 'description' => 'Add [https://www.jsxgraph.org JSXGraph] to MediaWiki pages.',
98 'version' => $jsxgraph_version
99 );
100
101 function jsxgraphSetup()
102 {
103 // global $wgParser;
104 // $wgParser->setHook('jsxgraph', 'jsxgraphOutput');
105
106 // $parser = \MediaWiki\MediaWikiServices::getInstance()->getParser();
107 // $parser->setHook('jsxgraph', 'jsxgraphOutput');
108 // MediaWikiServices::getInstance()->getParser()->setHook('jsxgraph', 'jsxgraphOutput');
109 return true;
110 }
111
112 $markerList = array();
113 // function jsxgraphOutput($input, $args, $parser)
114 function jsxgraphOutput($input, $args)
115 {
116 global $wgServer; // URL of the WIKI's server
117 global $jsxgraph_version; // see line 9 of this file
118 global $markerList;
119 global $outputURI;
120 global $outputURICDN;
121
122 $error_message = "no error"; //will be overwritten, if error occurs
123 $CRLF = "\r\n";
124
125 // Look for required parameters
126 if (!(isset($args['filename']) || isset($args['filestring']) || isset($input))) {
127 $error_message = "Missing parameter (width or height, filename, string or input).";
128 }
129 $output = "<!-- JSXGraph MediaWiki extension " . $jsxgraph_version . " -->";
130
131 $markercount = count($markerList);
132 if ($markercount > 0) {
133 $defaultBoard = "brd" . $markercount;
134 $defaultBox = "jxgbox" . $markercount;
135 } else {
136 $defaultBoard = "brd";
137 $defaultBox = "jxgbox";
138 }
139 $outputDivId = (isset($args['box'])) ? htmlspecialchars(strip_tags($args['box'])) : $defaultBox;
140 $outputBoardId = (isset($args['board'])) ? htmlspecialchars(strip_tags($args['board'])) : $defaultBoard;
141 $width = (isset($args['width'])) ? htmlspecialchars(strip_tags($args['width'])) : 500;
142 $height = (isset($args['height'])) ? htmlspecialchars(strip_tags($args['height'])) : 400;
143
144 // Load necessary stylesheet und scripts
145 if ($markercount == 0) {
146 $output .= "<link rel='stylesheet' type='text/css' href='" . $outputURICDN . "/jsxgraph.css' />";
147 if (preg_match("/^132\.180/", getenv("REMOTE_ADDR"))) {
148 // Use the local version when developing
149 $output .= "<script src='" . $outputURI . "/jsxgraphcore.js' type='text/javascript'></script>";
150 $output .= "<script src='" . $outputURI . "/geonext.min.js' type='text/javascript'></script>";
151 } else {
152 $output .= "<script src='" . $outputURICDN . "/jsxgraphcore.js' type='text/javascript'></script>";
153 $output .= "<script src='" . $outputURICDN . "/geonext.min.js' type='text/javascript'></script>";
154 }
155 }
156
157 if (isset($args['modules']) && trim($args['modules']) != "") {
158 $modules = explode(',', $args['modules']);
159 for ($i = 0; $i < count($modules); $i++) {
160 $output .= "<script src='" . $outputURI . "/" . $modules[$i] . ".js' type='text/javascript'></script>";
161 }
162 }
163
164 // Output div
165 $output .= "<div id='" . $outputDivId . "' class='jxgbox' style='width:" . $width . "px; height:" . $height . "px;'></div>";
166 $output .= "<script type='text/javascript'>";
167
168 // construction input method
169 if (isset($args['filename'])) { // string of url to gxt-file
170 // retrieve URL of .gxt file
171 $gxtBinary = htmlspecialchars(strip_tags($args['filename']));
172 $gxtFile = wfFindFile($gxtBinary);
173 if (!($gxtFile->exists())) {
174 $error_message = "File " . $gxtFile . " not found.";
175 } else {
176 $gxtURL = $wgServer . $gxtFile->getURL();
177 }
178 $output .= " var " . $outputBoardId . " = JXG.JSXGraph.loadBoardFromFile('" . $outputDivId . "', '" . $gxtURL . "', 'Geonext');";
179 }
180 if (isset($args['filestring'])) { // binary content of gxt-file
181 $output .= " var " . $outputBoardId . " = JXG.JSXGraph.loadBoardFromString('" . $outputDivId . "', '" . htmlspecialchars(strip_tags($args['filestring'])) . "', 'Geonext');";
182 }
183 if (isset($input)) { // content between <jsxgraph>-tags
184 $output .= $input;
185 }
186 $output .= "</script>";
187
188 // if error occurred, discard and output error message
189 if ($error_message != "no error") {
190 $output = "<p>Error in MediaWiki extension (JSXGraph.php): <em>" . $error_message . "</em></p>" . $CRLF;
191 }
192
193 // Send the output to the browser
194 $marker = "jsxgraph-marker" . $markercount . "-jsxgraph";
195 $markerList[$markercount] = $output;
196 return $marker;
197 }
198
199 function jsxgraphParserAfterTidy($parser, &$text)
200 {
201 // find markers in $text
202 // replace markers with actual output
203 global $markerList;
204 $keys = array();
205 $marker_count = count($markerList);
206
207 for ($i = 0; $i < $marker_count; $i++) {
208 $keys[] = 'jsxgraph-marker' . $i . '-jsxgraph';
209 }
210 $text = str_replace($keys, $markerList, $text);
211 return true;
212 }
213
214 ?>