summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/SemanticResultFormats.php
blob: 36a66555bde625f59eb5a6d89ed4d5a951479d82 (plain)
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
<?php

/**
 * @see https://github.com/SemanticMediaWiki/SemanticResultFormats/
 *
 * @defgroup SRF Semantic Result Formats
 */

SemanticResultFormats::load();

/**
 * @codeCoverageIgnore
 */
class SemanticResultFormats {

	/**
	 * @since 2.5
	 *
	 * @note It is expected that this function is loaded before LocalSettings.php
	 * to ensure that settings and global functions are available by the time
	 * the extension is activated.
	 */
	public static function load() {

		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
			include_once __DIR__ . '/vendor/autoload.php';
		}

		// Load DefaultSettings
		require_once __DIR__ . '/DefaultSettings.php';
	}

	/**
	 * @since 2.5
	 */
	public static function initExtension( $credits = [] ) {

		// See https://phabricator.wikimedia.org/T151136
		define( 'SRF_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );

		// Register message files
		$GLOBALS['wgMessagesDirs']['SemanticResultFormats'] = __DIR__ . '/i18n';
		$GLOBALS['wgExtensionMessagesFiles']['SemanticResultFormats'] = __DIR__ . '/SemanticResultFormats.i18n.php';
		$GLOBALS['wgExtensionMessagesFiles']['SemanticResultFormatsMagic'] = __DIR__ . '/SemanticResultFormats.i18n.magic.php';

		$GLOBALS['srfgIP'] = __DIR__;
		$GLOBALS['wgResourceModules'] = array_merge( $GLOBALS['wgResourceModules'], include( __DIR__ . "/Resources.php" ) );

		self::registerHooks();
	}

	/**
	 * @since 2.5
	 */
	public static function registerHooks() {
		$formatDir = __DIR__ . '/formats/';

		unset( $formatDir );

		$GLOBALS['wgHooks']['ParserFirstCallInit'][] = 'SRFParserFunctions::registerFunctions';
		$GLOBALS['wgHooks']['UnitTestsList'][] = 'SRFHooks::registerUnitTests';

		$GLOBALS['wgHooks']['ResourceLoaderTestModules'][] = 'SRFHooks::registerQUnitTests';
		$GLOBALS['wgHooks']['ResourceLoaderGetConfigVars'][] = 'SRFHooks::onResourceLoaderGetConfigVars';

		// Format hooks
		$GLOBALS['wgHooks']['OutputPageParserOutput'][] = 'SRF\Filtered\Hooks::onOutputPageParserOutput';
		$GLOBALS['wgHooks']['MakeGlobalVariablesScript'][] = 'SRF\Filtered\Hooks::onMakeGlobalVariablesScript';

		// register API modules
		$GLOBALS['wgAPIModules']['ext.srf.slideshow.show'] = 'SRFSlideShowApi';

		// User preference
		$GLOBALS['wgHooks']['SMW::GetPreferences'][] = 'SRFHooks::onGetPreferences';

		// Allows last minute changes to the output page, e.g. adding of CSS or JavaScript by extensions
		$GLOBALS['wgHooks']['BeforePageDisplay'][] = 'SRFHooks::onBeforePageDisplay';
	}

	/**
	 * @since 2.5
	 */
	public static function onExtensionFunction() {

		if ( !defined( 'SMW_VERSION' ) ) {

			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
				die( "\nThe 'Semantic Result Formats' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" );
			} else {
				die(
					'<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticResultFormats/">Semantic Result Formats</a> ' .
					'extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be ' .
					'installed and enabled.<br />'
				);
			}
		}

		// Admin Links hook needs to be called in a delayed way so that it
		// will always be called after SMW's Admin Links addition; as of
		// SMW 1.9, SMW delays calling all its hook functions.
		$GLOBALS['wgHooks']['AdminLinks'][] = 'SRFHooks::addToAdminLinks';

		$GLOBALS['srfgScriptPath'] = ( $GLOBALS['wgExtensionAssetsPath'] === false ? $GLOBALS['wgScriptPath'] . '/extensions' : $GLOBALS['wgExtensionAssetsPath'] ) . '/SemanticResultFormats';

		$formatClasses = [
			// Assign the Boilerplate class to a format identifier
			// 'boilerplate' => 'SRFBoilerplate',
			'timeline' => 'SRFTimeline',
			'eventline' => 'SRFTimeline',
			'vcard' => 'SRF\vCard\vCardFileExportPrinter',
			'icalendar' => 'SRF\iCalendar\iCalendarFileExportPrinter',
			'bibtex' => 'SRF\BibTex\BibTexFileExportPrinter',
			'calendar' => 'SRFCalendar',
			'eventcalendar' => 'SRF\EventCalendar',
			'outline' => 'SRF\Outline\OutlineResultPrinter',
			'sum' => 'SRFMath',
			'product' => 'SRFMath',
			'average' => 'SRFMath',
			'min' => 'SRFMath',
			'max' => 'SRFMath',
			'median' => 'SRFMath',
			'exhibit' => 'SRFExhibit',
			'googlebar' => 'SRFGoogleBar',
			'googlepie' => 'SRFGooglePie',
			'jitgraph' => 'SRFJitGraph',
			'jqplotchart' => 'SRFjqPlotChart',
			'jqplotseries' => 'SRFjqPlotSeries',
			'graph' => 'SRF\Graph\GraphPrinter',
			'process' => 'SRFProcess',
			'gallery' => 'SRF\Gallery',
			'tagcloud' => 'SRF\TagCloud',
			'valuerank' => 'SRFValueRank',
			'array' => 'SRFArray',
			'hash' => 'SRFHash',
			'd3chart' => 'SRFD3Chart',
			'tree' => 'SRF\Formats\Tree\TreeResultPrinter',
			'ultree' => 'SRF\Formats\Tree\TreeResultPrinter',
			'oltree' => 'SRF\Formats\Tree\TreeResultPrinter',
			'filtered' => 'SRF\Filtered\Filtered',
			'latest' => 'SRFTime',
			'earliest' => 'SRFTime',
			'slideshow' => 'SRFSlideShow',
			'timeseries' => 'SRFTimeseries',
			'sparkline' => 'SRFSparkline',
			'listwidget' => 'SRFListWidget',
			'pagewidget' => 'SRFPageWidget',
			'dygraphs' => 'SRFDygraphs',
			'incoming' => 'SRFIncoming',
			'media' => 'SRF\MediaPlayer',
			'datatables' => 'SRF\DataTables',
			'gantt' => 'SRF\Gantt\GanttPrinter'
		];

		$formatAliases = [
			'tagcloud'   => [ 'tag cloud' ],
			'datatables'   => [ 'datatable' ],
			'valuerank'  => [ 'value rank' ],
			'd3chart'    => [ 'd3 chart' ],
			'timeseries' =>  [ 'time series' ],
			'jqplotchart' => [ 'jqplot chart', 'jqplotpie', 'jqplotbar' ],
			'jqplotseries' => [ 'jqplot series' ],
		];

		if ( class_exists( '\PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) {
			$formatClasses['spreadsheet'] = 'SRF\SpreadsheetPrinter';
			$formatAliases['spreadsheet'] = [ 'excel' ];
		}

		foreach ( $GLOBALS['srfgFormats'] as $format ) {
			if ( array_key_exists( $format, $formatClasses ) ) {
				$GLOBALS['smwgResultFormats'][$format] = $formatClasses[$format];

				if ( isset( $GLOBALS['smwgResultAliases'] ) && array_key_exists( $format, $formatAliases ) ) {
					$GLOBALS['smwgResultAliases'][$format] = $formatAliases[$format];
				}
			}
		}
	}

	/**
	 * @since 2.5
	 *
	 * @return string|null
	 */
	public static function getVersion() {
		return SRF_VERSION;
	}

}