Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * This class proivdes various helper functions for locating extensions | |
14 | * data. It's designed for compatibility with pre-existing functions from | |
15 | * CRM_Core_Extensions. | |
16 | * | |
17 | * Most of these helper functions originate with the first major iteration | |
18 | * of extensions -- a time when every extension had one eponymous PHP class, | |
19 | * when there was no PHP class-loader, and when there was special-case logic | |
20 | * sprinkled around to handle loading of "extension classes". | |
21 | * | |
22 | * With module-extensions (Civi 4.2+), there are no eponymous classes -- | |
23 | * instead, module-extensions follow the same class-naming and class-loading | |
24 | * practices as core (and don't require special-case logic for class | |
25 | * loading). Consequently, the helpers in here aren't much used with | |
26 | * module-extensions. | |
27 | * | |
28 | * @package CRM | |
ca5cec67 | 29 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
30 | */ |
31 | class CRM_Extension_Mapper { | |
32 | ||
33 | /** | |
fe482240 | 34 | * An URL for public extensions repository. |
6a488035 | 35 | */ |
6a488035 TO |
36 | |
37 | /** | |
fe482240 | 38 | * Extension info file name. |
6a488035 TO |
39 | */ |
40 | const EXT_TEMPLATES_DIRNAME = 'templates'; | |
41 | ||
42 | /** | |
43 | * @var CRM_Extension_Container_Interface | |
44 | */ | |
45 | protected $container; | |
46 | ||
47 | /** | |
51dda21e SL |
48 | * @var \CRM_Extension_Info[] |
49 | * (key => CRM_Extension_Info) | |
6a488035 | 50 | */ |
be2fb01f | 51 | protected $infos = []; |
6a488035 TO |
52 | |
53 | /** | |
54 | * @var array | |
55 | */ | |
56 | protected $moduleExtensions = NULL; | |
57 | ||
58 | /** | |
59 | * @var CRM_Utils_Cache_Interface | |
60 | */ | |
61 | protected $cache; | |
62 | ||
63 | protected $cacheKey; | |
64 | ||
65 | protected $civicrmPath; | |
66 | ||
67 | protected $civicrmUrl; | |
68 | ||
e0ef6999 EM |
69 | /** |
70 | * @param CRM_Extension_Container_Interface $container | |
71 | * @param CRM_Utils_Cache_Interface $cache | |
72 | * @param null $cacheKey | |
73 | * @param null $civicrmPath | |
74 | * @param null $civicrmUrl | |
75 | */ | |
6a488035 TO |
76 | public function __construct(CRM_Extension_Container_Interface $container, CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $civicrmPath = NULL, $civicrmUrl = NULL) { |
77 | $this->container = $container; | |
78 | $this->cache = $cache; | |
79 | $this->cacheKey = $cacheKey; | |
80 | if ($civicrmUrl) { | |
81 | $this->civicrmUrl = rtrim($civicrmUrl, '/'); | |
0db6c3e1 TO |
82 | } |
83 | else { | |
6a488035 TO |
84 | $config = CRM_Core_Config::singleton(); |
85 | $this->civicrmUrl = rtrim($config->resourceBase, '/'); | |
86 | } | |
87 | if ($civicrmPath) { | |
b3a4b879 | 88 | $this->civicrmPath = rtrim($civicrmPath, '/'); |
0db6c3e1 TO |
89 | } |
90 | else { | |
6a488035 | 91 | global $civicrm_root; |
b3a4b879 | 92 | $this->civicrmPath = rtrim($civicrm_root, '/'); |
6a488035 TO |
93 | } |
94 | } | |
95 | ||
96 | /** | |
97 | * Given the class, provides extension's key. | |
98 | * | |
6a488035 | 99 | * |
f41911fd TO |
100 | * @param string $clazz |
101 | * Extension class name. | |
6a488035 | 102 | * |
a6c01b45 CW |
103 | * @return string |
104 | * name of extension key | |
6a488035 TO |
105 | */ |
106 | public function classToKey($clazz) { | |
107 | return str_replace('_', '.', $clazz); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Given the class, provides extension path. | |
112 | * | |
6a488035 | 113 | * |
6c8f6e67 EM |
114 | * @param $clazz |
115 | * | |
a6c01b45 CW |
116 | * @return string |
117 | * full path the extension .php file | |
6a488035 TO |
118 | */ |
119 | public function classToPath($clazz) { | |
120 | $elements = explode('_', $clazz); | |
121 | $key = implode('.', $elements); | |
122 | return $this->keyToPath($key); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Given the string, returns true or false if it's an extension key. | |
127 | * | |
6a488035 | 128 | * |
f41911fd TO |
129 | * @param string $key |
130 | * A string which might be an extension key. | |
6a488035 | 131 | * |
e7483cbe | 132 | * @return bool |
a6c01b45 | 133 | * true if given string is an extension name |
6a488035 TO |
134 | */ |
135 | public function isExtensionKey($key) { | |
136 | // check if the string is an extension name or the class | |
137 | return (strpos($key, '.') !== FALSE) ? TRUE : FALSE; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Given the string, returns true or false if it's an extension class name. | |
142 | * | |
6a488035 | 143 | * |
f41911fd TO |
144 | * @param string $clazz |
145 | * A string which might be an extension class name. | |
6a488035 | 146 | * |
e7483cbe | 147 | * @return bool |
a6c01b45 | 148 | * true if given string is an extension class name |
6a488035 TO |
149 | */ |
150 | public function isExtensionClass($clazz) { | |
151 | ||
152 | if (substr($clazz, 0, 4) != 'CRM_') { | |
153 | return (bool) preg_match('/^[a-z0-9]+(_[a-z0-9]+)+$/', $clazz); | |
154 | } | |
155 | return FALSE; | |
156 | } | |
157 | ||
158 | /** | |
f41911fd TO |
159 | * @param string $key |
160 | * Extension fully-qualified-name. | |
77b97be7 EM |
161 | * @param bool $fresh |
162 | * | |
163 | * @throws CRM_Extension_Exception | |
38c87aa2 | 164 | * |
c490a46a | 165 | * @return CRM_Extension_Info |
6a488035 TO |
166 | */ |
167 | public function keyToInfo($key, $fresh = FALSE) { | |
168 | if ($fresh || !array_key_exists($key, $this->infos)) { | |
169 | try { | |
170 | $this->infos[$key] = CRM_Extension_Info::loadFromFile($this->container->getPath($key) . DIRECTORY_SEPARATOR . CRM_Extension_Info::FILENAME); | |
0db6c3e1 TO |
171 | } |
172 | catch (CRM_Extension_Exception $e) { | |
6a488035 | 173 | // file has more detailed info, but we'll fallback to DB if it's missing -- DB has enough info to uninstall |
f8a21281 | 174 | $dbInfo = CRM_Extension_System::singleton()->getManager()->createInfoFromDB($key); |
175 | if (!$dbInfo) { | |
6a488035 TO |
176 | throw $e; |
177 | } | |
f8a21281 | 178 | $this->infos[$key] = $dbInfo; |
6a488035 TO |
179 | } |
180 | } | |
181 | return $this->infos[$key]; | |
182 | } | |
183 | ||
184 | /** | |
185 | * Given the key, provides extension's class name. | |
186 | * | |
6a488035 | 187 | * |
f41911fd TO |
188 | * @param string $key |
189 | * Extension key. | |
6a488035 | 190 | * |
a6c01b45 CW |
191 | * @return string |
192 | * name of extension's main class | |
6a488035 TO |
193 | */ |
194 | public function keyToClass($key) { | |
195 | return str_replace('.', '_', $key); | |
196 | } | |
197 | ||
198 | /** | |
199 | * Given the key, provides the path to file containing | |
200 | * extension's main class. | |
201 | * | |
6a488035 | 202 | * |
f41911fd TO |
203 | * @param string $key |
204 | * Extension key. | |
6a488035 | 205 | * |
a6c01b45 CW |
206 | * @return string |
207 | * path to file containing extension's main class | |
6a488035 TO |
208 | */ |
209 | public function keyToPath($key) { | |
210 | $info = $this->keyToInfo($key); | |
211 | return $this->container->getPath($key) . DIRECTORY_SEPARATOR . $info->file . '.php'; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Given the key, provides the path to file containing | |
216 | * extension's main class. | |
217 | * | |
f41911fd TO |
218 | * @param string $key |
219 | * Extension key. | |
a6c01b45 CW |
220 | * @return string |
221 | * local path of the extension source tree | |
6a488035 TO |
222 | */ |
223 | public function keyToBasePath($key) { | |
224 | if ($key == 'civicrm') { | |
225 | return $this->civicrmPath; | |
226 | } | |
227 | return $this->container->getPath($key); | |
228 | } | |
229 | ||
230 | /** | |
231 | * Given the key, provides the path to file containing | |
232 | * extension's main class. | |
233 | * | |
6a488035 | 234 | * |
f41911fd TO |
235 | * @param string $key |
236 | * Extension key. | |
6a488035 | 237 | * |
a6c01b45 CW |
238 | * @return string |
239 | * url for resources in this extension | |
78280a63 | 240 | * |
241 | * @throws \CRM_Extension_Exception_MissingException | |
6a488035 TO |
242 | */ |
243 | public function keyToUrl($key) { | |
78280a63 | 244 | if ($key === 'civicrm') { |
dee7a2b1 PJ |
245 | // CRM-12130 Workaround: If the domain's config_backend is NULL at the start of the request, |
246 | // then the Mapper is wrongly constructed with an empty value for $this->civicrmUrl. | |
3d4a4ccf PJ |
247 | if (empty($this->civicrmUrl)) { |
248 | $config = CRM_Core_Config::singleton(); | |
249 | return rtrim($config->resourceBase, '/'); | |
250 | } | |
6a488035 TO |
251 | return $this->civicrmUrl; |
252 | } | |
253 | ||
254 | return $this->container->getResUrl($key); | |
255 | } | |
256 | ||
257 | /** | |
258 | * Fetch the list of active extensions of type 'module' | |
259 | * | |
5a4f6742 CW |
260 | * @param bool $fresh |
261 | * whether to forcibly reload extensions list from canonical store. | |
a6c01b45 | 262 | * @return array |
0e092fe0 | 263 | * array(array('prefix' => $, 'fullName' => $, 'filePath' => $)) |
6a488035 TO |
264 | */ |
265 | public function getActiveModuleFiles($fresh = FALSE) { | |
62f662b0 | 266 | if (!defined('CIVICRM_DSN')) { |
267 | // hmm, ok | |
268 | return []; | |
6a488035 TO |
269 | } |
270 | ||
0e092fe0 HD |
271 | // The list of module files is cached in two tiers. The tiers are slightly |
272 | // different: | |
273 | // | |
274 | // 1. The persistent tier (cache) stores | |
275 | // names WITHOUT absolute paths. | |
276 | // 2. The ephemeral/thread-local tier (statics) stores names | |
277 | // WITH absolute paths. | |
278 | // Return static value instead of re-running query | |
24ef6b49 | 279 | if (isset(Civi::$statics[__CLASS__]['moduleExtensions']) && !$fresh) { |
0e092fe0 HD |
280 | return Civi::$statics[__CLASS__]['moduleExtensions']; |
281 | } | |
282 | ||
6a488035 | 283 | $moduleExtensions = NULL; |
0e092fe0 HD |
284 | |
285 | // Checked if it's stored in the persistent cache. | |
6a488035 | 286 | if ($this->cache && !$fresh) { |
ec2dd0bd | 287 | $moduleExtensions = $this->cache->get($this->cacheKey . '_moduleFiles'); |
6a488035 TO |
288 | } |
289 | ||
0e092fe0 | 290 | // If cache is empty we build it from database. |
6a488035 | 291 | if (!is_array($moduleExtensions)) { |
6542d699 TO |
292 | $compat = CRM_Extension_System::getCompatibilityInfo(); |
293 | ||
6a488035 | 294 | // Check canonical module list |
be2fb01f | 295 | $moduleExtensions = []; |
6a488035 TO |
296 | $sql = ' |
297 | SELECT full_name, file | |
298 | FROM civicrm_extension | |
299 | WHERE is_active = 1 | |
300 | AND type = "module" | |
301 | '; | |
302 | $dao = CRM_Core_DAO::executeQuery($sql); | |
303 | while ($dao->fetch()) { | |
6542d699 TO |
304 | if (!empty($compat[$dao->full_name]['force-uninstall'])) { |
305 | continue; | |
306 | } | |
0851bac5 HD |
307 | $moduleExtensions[] = [ |
308 | 'prefix' => $dao->file, | |
309 | 'fullName' => $dao->full_name, | |
310 | 'filePath' => NULL, | |
311 | ]; | |
6a488035 TO |
312 | } |
313 | ||
314 | if ($this->cache) { | |
ec2dd0bd | 315 | $this->cache->set($this->cacheKey . '_moduleFiles', $moduleExtensions); |
6a488035 TO |
316 | } |
317 | } | |
9194bdb3 HD |
318 | |
319 | // Since we're not caching the full path we add it now. | |
320 | array_walk($moduleExtensions, function(&$value, $key) { | |
0851bac5 | 321 | try { |
456e9097 HD |
322 | if (!$value['filePath']) { |
323 | $value['filePath'] = $this->keyToPath($value['fullName']); | |
324 | } | |
0851bac5 HD |
325 | } |
326 | catch (CRM_Extension_Exception $e) { | |
327 | // Putting a stub here provides more consistency | |
328 | // in how getActiveModuleFiles when racing between | |
329 | // dirty file-removals and cache-clears. | |
330 | CRM_Core_Session::setStatus($e->getMessage(), '', 'error'); | |
331 | $value['filePath'] = NULL; | |
332 | } | |
9194bdb3 HD |
333 | }); |
334 | ||
0e092fe0 HD |
335 | Civi::$statics[__CLASS__]['moduleExtensions'] = $moduleExtensions; |
336 | ||
6a488035 TO |
337 | return $moduleExtensions; |
338 | } | |
339 | ||
e7ff7042 | 340 | /** |
fe482240 | 341 | * Get a list of base URLs for all active modules. |
e7ff7042 | 342 | * |
a6c01b45 CW |
343 | * @return array |
344 | * (string $extKey => string $baseUrl) | |
78280a63 | 345 | * |
346 | * @throws \CRM_Extension_Exception_MissingException | |
e7ff7042 TO |
347 | */ |
348 | public function getActiveModuleUrls() { | |
349 | // TODO optimization/caching | |
be2fb01f | 350 | $urls = []; |
e7ff7042 TO |
351 | $urls['civicrm'] = $this->keyToUrl('civicrm'); |
352 | foreach ($this->getModules() as $module) { | |
353 | /** @var $module CRM_Core_Module */ | |
354 | if ($module->is_active) { | |
78280a63 | 355 | try { |
356 | $urls[$module->name] = $this->keyToUrl($module->name); | |
357 | } | |
358 | catch (CRM_Extension_Exception_MissingException $e) { | |
359 | CRM_Core_Session::setStatus(ts('An enabled extension is missing from the extensions directory') . ':' . $module->name); | |
360 | } | |
e7ff7042 TO |
361 | } |
362 | } | |
363 | return $urls; | |
364 | } | |
365 | ||
3b3f6d23 TO |
366 | /** |
367 | * Get a list of extension keys, filtered by the corresponding file path. | |
368 | * | |
369 | * @param string $pattern | |
370 | * A file path. To search subdirectories, append "*". | |
371 | * Ex: "/var/www/extensions/*" | |
372 | * Ex: "/var/www/extensions/org.foo.bar" | |
373 | * @return array | |
374 | * Array(string $key). | |
375 | * Ex: array("org.foo.bar"). | |
376 | */ | |
377 | public function getKeysByPath($pattern) { | |
be2fb01f | 378 | $keys = []; |
3b3f6d23 TO |
379 | |
380 | if (CRM_Utils_String::endsWith($pattern, '*')) { | |
381 | $prefix = rtrim($pattern, '*'); | |
382 | foreach ($this->container->getKeys() as $key) { | |
383 | $path = CRM_Utils_File::addTrailingSlash($this->container->getPath($key)); | |
384 | if (realpath($prefix) == realpath($path) || CRM_Utils_File::isChildPath($prefix, $path)) { | |
385 | $keys[] = $key; | |
386 | } | |
387 | } | |
388 | } | |
389 | else { | |
390 | foreach ($this->container->getKeys() as $key) { | |
391 | $path = CRM_Utils_File::addTrailingSlash($this->container->getPath($key)); | |
392 | if (realpath($pattern) == realpath($path)) { | |
393 | $keys[] = $key; | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
398 | return $keys; | |
399 | } | |
400 | ||
9bb46670 TO |
401 | /** |
402 | * Get a list of extensions which match a given tag. | |
403 | * | |
404 | * @param string $tag | |
405 | * Ex: 'foo' | |
406 | * @return array | |
407 | * Array(string $key). | |
408 | * Ex: array("org.foo.bar"). | |
409 | */ | |
410 | public function getKeysByTag($tag) { | |
411 | $allTags = $this->getAllTags(); | |
412 | return $allTags[$tag] ?? []; | |
413 | } | |
414 | ||
415 | /** | |
416 | * Get a list of extension tags. | |
417 | * | |
418 | * @return array | |
419 | * Ex: ['form-building' => ['org.civicrm.afform-gui', 'org.civicrm.afform-html']] | |
420 | */ | |
421 | public function getAllTags() { | |
422 | $tags = Civi::cache('short')->get('extension_tags', NULL); | |
423 | if ($tags !== NULL) { | |
424 | return $tags; | |
425 | } | |
426 | ||
427 | $tags = []; | |
428 | $allInfos = $this->getAllInfos(); | |
429 | foreach ($allInfos as $key => $info) { | |
430 | foreach ($info->tags as $tag) { | |
431 | $tags[$tag][] = $key; | |
432 | } | |
433 | } | |
434 | return $tags; | |
435 | } | |
436 | ||
f8a7cfff TO |
437 | /** |
438 | * @return array | |
439 | * Ex: $result['org.civicrm.foobar'] = new CRM_Extension_Info(...). | |
440 | * @throws \CRM_Extension_Exception | |
441 | * @throws \Exception | |
442 | */ | |
443 | public function getAllInfos() { | |
444 | foreach ($this->container->getKeys() as $key) { | |
f8a21281 | 445 | try { |
446 | $this->keyToInfo($key); | |
447 | } | |
448 | catch (CRM_Extension_Exception_ParseException $e) { | |
449 | CRM_Core_Session::setStatus(ts('Parse error in extension: %1', [ | |
450 | 1 => $e->getMessage(), | |
451 | ]), '', 'error'); | |
452 | CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage()); | |
453 | continue; | |
454 | } | |
f8a7cfff TO |
455 | } |
456 | return $this->infos; | |
457 | } | |
458 | ||
e0ef6999 | 459 | /** |
100fef9d | 460 | * @param string $name |
e0ef6999 EM |
461 | * |
462 | * @return bool | |
463 | */ | |
6a488035 TO |
464 | public function isActiveModule($name) { |
465 | $activeModules = $this->getActiveModuleFiles(); | |
466 | foreach ($activeModules as $activeModule) { | |
467 | if ($activeModule['prefix'] == $name) { | |
468 | return TRUE; | |
469 | } | |
470 | } | |
471 | return FALSE; | |
472 | } | |
473 | ||
474 | /** | |
475 | * Get a list of all installed modules, including enabled and disabled ones | |
476 | * | |
a6c01b45 CW |
477 | * @return array |
478 | * CRM_Core_Module | |
6a488035 TO |
479 | */ |
480 | public function getModules() { | |
be2fb01f | 481 | $result = []; |
6a488035 TO |
482 | $dao = new CRM_Core_DAO_Extension(); |
483 | $dao->type = 'module'; | |
484 | $dao->find(); | |
485 | while ($dao->fetch()) { | |
486 | $result[] = new CRM_Core_Module($dao->full_name, $dao->is_active); | |
487 | } | |
488 | return $result; | |
489 | } | |
490 | ||
491 | /** | |
492 | * Given the class, provides the template path. | |
493 | * | |
6a488035 | 494 | * |
f41911fd TO |
495 | * @param string $clazz |
496 | * Extension class name. | |
6a488035 | 497 | * |
a6c01b45 CW |
498 | * @return string |
499 | * path to extension's templates directory | |
6a488035 TO |
500 | */ |
501 | public function getTemplatePath($clazz) { | |
502 | $path = $this->container->getPath($this->classToKey($clazz)); | |
503 | return $path . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME; | |
504 | /* | |
505 | $path = $this->classToPath($clazz); | |
506 | $pathElm = explode(DIRECTORY_SEPARATOR, $path); | |
507 | array_pop($pathElm); | |
508 | return implode(DIRECTORY_SEPARATOR, $pathElm) . DIRECTORY_SEPARATOR . self::EXT_TEMPLATES_DIRNAME; | |
e70a7fc0 | 509 | */ |
6a488035 TO |
510 | } |
511 | ||
512 | /** | |
513 | * Given te class, provides the template name. | |
514 | * @todo consider multiple templates, support for one template for now | |
515 | * | |
6a488035 | 516 | * |
f41911fd TO |
517 | * @param string $clazz |
518 | * Extension class name. | |
6a488035 | 519 | * |
a6c01b45 CW |
520 | * @return string |
521 | * extension's template name | |
6a488035 TO |
522 | */ |
523 | public function getTemplateName($clazz) { | |
524 | $info = $this->keyToInfo($this->classToKey($clazz)); | |
525 | return (string) $info->file . '.tpl'; | |
526 | } | |
527 | ||
528 | public function refresh() { | |
be2fb01f | 529 | $this->infos = []; |
6a488035 TO |
530 | $this->moduleExtensions = NULL; |
531 | if ($this->cache) { | |
ec2dd0bd | 532 | $this->cache->delete($this->cacheKey . '_moduleFiles'); |
6a488035 | 533 | } |
85c7eb67 TO |
534 | // FIXME: How can code so code wrong be so right? |
535 | CRM_Extension_System::singleton()->getClassLoader()->refresh(); | |
6a488035 | 536 | } |
96025800 | 537 | |
6a488035 | 538 | } |