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
|
<?php
namespace SMW;
/**
* Encapsulate Semantic MediaWiki settings
*
* @note Initial idea has been borrowed from EducationProgram Extension/Jeroen De Dauw
*
* @file
*
* @license GNU GPL v2+
* @since 1.9
*
* @author mwjames
*/
/**
* Encapsulate Semantic MediaWiki settings to access values through a
* specified interface
*
* @ingroup SMW
*/
class Settings extends SimpleDictionary {
/** @var Settings */
private static $instance = null;
/**
* Assemble individual SMW related settings into one accessible array for
* easy instantiation since we don't have unique way of accessing only
* SMW related settings ( e.g. $smwgSettings['...']) we need this method
* as short cut to invoke only smwg* related settings
*
* @par Example:
* @code
* $settings = Settings::newFromGlobals();
* $settings->get( 'smwgDefaultStore' );
* @endcode
*
* @since 1.9
*
* @return Settings
*/
public static function newFromGlobals() {
$settings = array(
'smwgScriptPath' => $GLOBALS['smwgScriptPath'],
'smwgIP' => $GLOBALS['smwgIP'],
'smwgDefaultStore' => $GLOBALS['smwgDefaultStore'],
'smwgSparqlDatabaseConnector' => $GLOBALS['smwgSparqlDatabaseConnector'],
'smwgSparqlDatabase' => $GLOBALS['smwgSparqlDatabase'],
'smwgSparqlQueryEndpoint' => $GLOBALS['smwgSparqlQueryEndpoint'],
'smwgSparqlUpdateEndpoint' => $GLOBALS['smwgSparqlUpdateEndpoint'],
'smwgSparqlDataEndpoint' => $GLOBALS['smwgSparqlDataEndpoint'],
'smwgSparqlDefaultGraph' => $GLOBALS['smwgSparqlDefaultGraph'],
'smwgHistoricTypeNamespace' => $GLOBALS['smwgHistoricTypeNamespace'],
'smwgNamespaceIndex' => $GLOBALS['smwgNamespaceIndex'],
'smwgShowFactbox' => $GLOBALS['smwgShowFactbox'],
'smwgShowFactboxEdit' => $GLOBALS['smwgShowFactboxEdit'],
'smwgToolboxBrowseLink' => $GLOBALS['smwgToolboxBrowseLink'],
'smwgInlineErrors' => $GLOBALS['smwgInlineErrors'],
'smwgUseCategoryHierarchy' => $GLOBALS['smwgUseCategoryHierarchy'],
'smwgCategoriesAsInstances' => $GLOBALS['smwgCategoriesAsInstances'],
'smwgLinksInValues' => $GLOBALS['smwgLinksInValues'],
'smwgDefaultNumRecurringEvents' => $GLOBALS['smwgDefaultNumRecurringEvents'],
'smwgMaxNumRecurringEvents' => $GLOBALS['smwgMaxNumRecurringEvents'],
'smwgBrowseShowInverse' => $GLOBALS['smwgBrowseShowInverse'],
'smwgBrowseShowAll' => $GLOBALS['smwgBrowseShowAll'],
'smwgSearchByPropertyFuzzy' => $GLOBALS['smwgSearchByPropertyFuzzy'],
'smwgTypePagingLimit' => $GLOBALS['smwgTypePagingLimit'],
'smwgConceptPagingLimit' => $GLOBALS['smwgConceptPagingLimit'],
'smwgPropertyPagingLimit' => $GLOBALS['smwgPropertyPagingLimit'],
'smwgQEnabled' => $GLOBALS['smwgQEnabled'],
'smwgQMaxLimit' => $GLOBALS['smwgQMaxLimit'],
'smwgIgnoreQueryErrors' => $GLOBALS['smwgIgnoreQueryErrors'],
'smwgQSubcategoryDepth' => $GLOBALS['smwgQSubcategoryDepth'],
'smwgQEqualitySupport' => $GLOBALS['smwgQEqualitySupport'],
'smwgQSortingSupport' => $GLOBALS['smwgQSortingSupport'],
'smwgQRandSortingSupport' => $GLOBALS['smwgQRandSortingSupport'],
'smwgQDefaultNamespaces' => $GLOBALS['smwgQDefaultNamespaces'],
'smwgQComparators' => $GLOBALS['smwgQComparators'],
'smwStrictComparators' => $GLOBALS['smwStrictComparators'],
'smwgQMaxSize' => $GLOBALS['smwgQMaxSize'],
'smwgQMaxDepth' => $GLOBALS['smwgQMaxDepth'],
'smwgQFeatures' => $GLOBALS['smwgQFeatures'],
'smwgQDefaultLimit' => $GLOBALS['smwgQDefaultLimit'],
'smwgQMaxInlineLimit' => $GLOBALS['smwgQMaxInlineLimit'],
'smwgQPrintoutLimit' => $GLOBALS['smwgQPrintoutLimit'],
'smwgQDefaultLinking' => $GLOBALS['smwgQDefaultLinking'],
'smwgQConceptCaching' => $GLOBALS['smwgQConceptCaching'],
'smwgQConceptMaxSize' => $GLOBALS['smwgQConceptMaxSize'],
'smwgQConceptMaxDepth' => $GLOBALS['smwgQConceptMaxDepth'],
'smwgQConceptFeatures' => $GLOBALS['smwgQConceptFeatures'],
'smwgQConceptCacheLifetime' => $GLOBALS['smwgQConceptCacheLifetime'],
'smwgResultFormats' => $GLOBALS['smwgResultFormats'],
'smwgResultAliases' => $GLOBALS['smwgResultAliases'],
'smwgQuerySources' => $GLOBALS['smwgQuerySources'],
'smwgPDefaultType' => $GLOBALS['smwgPDefaultType'],
'smwgAllowRecursiveExport' => $GLOBALS['smwgAllowRecursiveExport'],
'smwgExportBacklinks' => $GLOBALS['smwgExportBacklinks'],
'smwgMaxNonExpNumber' => $GLOBALS['smwgMaxNonExpNumber'],
'smwgEnableUpdateJobs' => $GLOBALS['smwgEnableUpdateJobs'],
'smwgNamespacesWithSemanticLinks' => $GLOBALS['smwgNamespacesWithSemanticLinks'],
'smwgPageSpecialProperties' => $GLOBALS['smwgPageSpecialProperties'],
'smwgDeclarationProperties' => $GLOBALS['smwgDeclarationProperties'],
'smwgTranslate' => $GLOBALS['smwgTranslate'],
'smwgAdminRefreshStore' => $GLOBALS['smwgAdminRefreshStore'],
'smwgAutocompleteInSpecialAsk' => $GLOBALS['smwgAutocompleteInSpecialAsk'],
'smwgAutoRefreshSubject' => $GLOBALS['smwgAutoRefreshSubject'],
'smwgAutoRefreshOnPurge' => $GLOBALS['smwgAutoRefreshOnPurge'],
'smwgAutoRefreshOnPageMove' => $GLOBALS['smwgAutoRefreshOnPageMove'],
'smwgContLang' => $GLOBALS['smwgContLang'],
'smwgMaxPropertyValues' => $GLOBALS['smwgMaxPropertyValues'],
'smwgQSubpropertyDepth' => $GLOBALS['smwgQSubpropertyDepth'],
'smwgNamespace' => $GLOBALS['smwgNamespace'],
'smwgMasterStore' => $GLOBALS['smwgMasterStore'],
'smwgIQRunningNumber' => $GLOBALS['smwgIQRunningNumber'],
'smwgCacheType' => $GLOBALS['smwgCacheType'],
'smwgCacheUsage' => $GLOBALS['smwgCacheUsage'],
'smwgFixedProperties' => $GLOBALS['smwgFixedProperties'],
'smwgPropertyLowUsageThreshold' => $GLOBALS['smwgPropertyLowUsageThreshold'],
'smwgPropertyZeroCountDisplay' => $GLOBALS['smwgPropertyZeroCountDisplay'],
'smwgShowHiddenCategories' => $GLOBALS['smwgShowHiddenCategories'],
'smwgFactboxUseCache' => $GLOBALS['smwgFactboxUseCache'],
'smwgFactboxCacheRefreshOnPurge' => $GLOBALS['smwgFactboxCacheRefreshOnPurge'],
'smwgQueryProfiler' => $GLOBALS['smwgQueryProfiler'],
'smwgEnabledSpecialPage' => $GLOBALS['smwgEnabledSpecialPage'],
'smwgOnDeleteAction' => $GLOBALS['smwgOnDeleteAction']
);
$settings = $settings + array(
'smwgCanonicalNames' => NamespaceManager::getCanonicalNames()
);
if ( self::$instance === null ) {
self::$instance = self::newFromArray( $settings ) ;
}
return self::$instance;
}
/**
* Factory method for immediate instantiation of a settings object for a
* given array
*
* @par Example:
* @code
* $settings = Settings::newFromArray( array( 'Foo' => 'Bar' ) );
* $settings->get( 'Foo' );
* @endcode
*
* @since 1.9
*
* @return Settings
*/
public static function newFromArray( array $settings ) {
return new self( $settings );
}
/**
* Returns settings for a given key (nested settings are supported)
*
* @par Example:
* @code
* $settings = Settings::newFromArray( array(
* 'Foo' => 'Bar'
* 'Parent' => array(
* 'Child' => array( 'Lisa', 'Lula', array( 'Lila' ) )
* )
* );
*
* $settings->get( 'Child' ) will return array( 'Lisa', 'Lula', array( 'Lila' ) )
* @endcode
*
* @since 1.9
*
* @param string $key
*
* @return mixed
* @throws InvalidSettingsArgumentException
*/
public function get( $key ) {
if ( !$this->has( $key ) ) {
// If the key wasn't found it could be because of a nested array
// therefore iterate and verify otherwise throw an exception
$value = $this->doIterate( $key );
if ( $value !== null ) {
return $value;
}
throw new InvalidSettingsArgumentException( "'{$key}' is not a valid settings key" );
}
return $this->lookup( $key );
}
/**
* Resets the instance
*
* @since 1.9
*/
public static function clear() {
self::$instance = null;
}
/**
* Iterates over a nested array to find a element
*
* @since 1.9
*
* @param string $key
*
* @return mixed|null
*/
private function doIterate( $key ) {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveArrayIterator( $this->toArray() ),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach( $iterator as $it => $value ) {
if ( $key === $it ) {
return $value;
}
}
return null;
}
}
|