Elgg  Version 2.3
ElggPluginManifest.php
Go to the documentation of this file.
1 <?php
19 
25  protected $parser;
26 
31  protected $namespace_root = 'http://www.elgg.org/plugin_manifest/';
32 
36  private $depsStructPlugin = array(
37  'type' => '',
38  'name' => '',
39  'version' => '',
40  'comparison' => 'ge'
41  );
42 
46  private $depsStructPriority = array(
47  'type' => '',
48  'priority' => '',
49  'plugin' => ''
50  );
51 
52  /*
53  * The expected structure of elgg_release requires element
54  */
55  private $depsStructElgg = array(
56  'type' => '',
57  'version' => '',
58  'comparison' => 'ge'
59  );
60 
64  private $depsStructPhpVersion = array(
65  'type' => '',
66  'version' => '',
67  'comparison' => 'ge'
68  );
69 
73  private $depsStructPhpIni = array(
74  'type' => '',
75  'name' => '',
76  'value' => '',
77  'comparison' => '='
78  );
79 
83  private $depsStructPhpExtension = array(
84  'type' => '',
85  'name' => '',
86  'version' => '',
87  'comparison' => '='
88  );
89 
93  private $depsConflictsStruct = array(
94  'type' => '',
95  'name' => '',
96  'version' => '',
97  'comparison' => '='
98  );
99 
103  private $depsProvidesStruct = array(
104  'type' => '',
105  'name' => '',
106  'version' => ''
107  );
108 
112  private $screenshotStruct = array(
113  'description' => '',
114  'path' => ''
115  );
116 
120  private $contributorStruct = array(
121  'name' => '',
122  'email' => '',
123  'website' => '',
124  'username' => '',
125  'description' => '',
126  );
127 
133  protected $apiVersion;
134 
140  protected $pluginID;
141 
151  public function __construct($manifest, $plugin_id = null) {
152  if ($plugin_id) {
153  $this->pluginID = $plugin_id;
154  }
155 
156  // see if we need to construct the xml object.
157  if ($manifest instanceof \ElggXMLElement) {
158  $manifest_obj = $manifest;
159  } else {
160  $raw_xml = '';
161  if (substr(trim($manifest), 0, 1) == '<') {
162  // this is a string
163  $raw_xml = $manifest;
164  } elseif (is_file($manifest)) {
165  // this is a file
166  $raw_xml = file_get_contents($manifest);
167  }
168  if ($raw_xml) {
169  $manifest_obj = new \ElggXMLElement($raw_xml);
170  } else {
171  $manifest_obj = null;
172  }
173  }
174 
175  if (!$manifest_obj) {
176  throw new \PluginException(_elgg_services()->translator->translate('PluginException:InvalidManifest',
177  array($this->getPluginID())));
178  }
179 
180  // set manifest api version
181  if (isset($manifest_obj->attributes['xmlns'])) {
182  $namespace = $manifest_obj->attributes['xmlns'];
183  $version = str_replace($this->namespace_root, '', $namespace);
184  } else {
185  $version = 1.7;
186  }
187 
188  $this->apiVersion = $version;
189 
190  $parser_class_name = '\ElggPluginManifestParser' . str_replace('.', '', $this->apiVersion);
191 
192  // @todo currently the autoloader freaks out if a class doesn't exist.
193  try {
194  $class_exists = class_exists($parser_class_name);
195  } catch (Exception $e) {
196  $class_exists = false;
197  }
198 
199  if ($class_exists) {
200  $this->parser = new $parser_class_name($manifest_obj, $this);
201  } else {
202  throw new \PluginException(_elgg_services()->translator->translate('PluginException:NoAvailableParser',
203  array($this->apiVersion, $this->getPluginID())));
204  }
205 
206  if (!$this->parser->parse()) {
207  throw new \PluginException(_elgg_services()->translator->translate('PluginException:ParserError',
208  array($this->apiVersion, $this->getPluginID())));
209  }
210  }
211 
217  public function getApiVersion() {
218  return $this->apiVersion;
219  }
220 
226  public function getPluginID() {
227  if ($this->pluginID) {
228  return $this->pluginID;
229  } else {
230  return _elgg_services()->translator->translate('unknown');
231  }
232  }
233 
242  public function getManifest() {
243  return $this->parser->getManifest();
244  }
245 
246  /***************************************
247  * Parsed and Normalized Manifest Data *
248  ***************************************/
249 
255  public function getName() {
256  $name = $this->parser->getAttribute('name');
257 
258  if (!$name && $this->pluginID) {
259  $name = ucwords(str_replace('_', ' ', $this->pluginID));
260  }
261 
262  return $name;
263  }
264 
271  public function getID() {
272  return trim((string) $this->parser->getAttribute('id'));
273  }
274 
275 
281  public function getDescription() {
282  return $this->parser->getAttribute('description');
283  }
284 
290  public function getBlurb() {
291  $blurb = $this->parser->getAttribute('blurb');
292 
293  if (!$blurb) {
294  $blurb = elgg_get_excerpt($this->getDescription());
295  }
296 
297  return $blurb;
298  }
299 
305  public function getLicense() {
306  // license vs licence. Use license.
307  $en_us = $this->parser->getAttribute('license');
308  if ($en_us) {
309  return $en_us;
310  } else {
311  return $this->parser->getAttribute('licence');
312  }
313  }
314 
320  public function getRepositoryURL() {
321  return $this->parser->getAttribute('repository');
322  }
323 
329  public function getBugTrackerURL() {
330  return $this->parser->getAttribute('bugtracker');
331  }
332 
338  public function getDonationsPageURL() {
339  return $this->parser->getAttribute('donations');
340  }
341 
347  public function getVersion() {
348  return $this->parser->getAttribute('version');
349  }
350 
356  public function getAuthor() {
357  return $this->parser->getAttribute('author');
358  }
359 
365  public function getCopyright() {
366  return $this->parser->getAttribute('copyright');
367  }
368 
374  public function getWebsite() {
375  return $this->parser->getAttribute('website');
376  }
377 
383  public function getCategories() {
384  $bundled_plugins = array(
385  'aalborg_theme',
386  'blog',
387  'bookmarks',
388  'ckeditor',
389  'custom_index',
390  'dashboard',
391  'developers',
392  'diagnostics',
393  'discussions',
394  'embed',
395  'externalpages',
396  'file',
397  'garbagecollector',
398  'groups',
399  'htmlawed',
400  'invitefriends',
401  'legacy_urls',
402  'likes',
403  'logbrowser',
404  'login_as',
405  'logrotate',
406  'members',
407  'messageboard',
408  'messages',
409  'notifications',
410  'pages',
411  'profile',
412  'reportedcontent',
413  'search',
414  'site_notifications',
415  'tagcloud',
416  'thewire',
417  'twitter_api',
418  'uservalidationbyemail',
419  'web_services',
420  );
421 
422  $cats = $this->parser->getAttribute('category');
423 
424  if (!$cats) {
425  $cats = array();
426  }
427 
428  if (in_array('bundled', $cats) && !in_array($this->getPluginID(), $bundled_plugins)) {
429  unset($cats[array_search('bundled', $cats)]);
430  }
431 
432  return $cats;
433  }
434 
440  public function getScreenshots() {
441  $ss = $this->parser->getAttribute('screenshot');
442 
443  if (!$ss) {
444  $ss = array();
445  }
446 
447  $normalized = array();
448  foreach ($ss as $s) {
449  $normalized[] = $this->buildStruct($this->screenshotStruct, $s);
450  }
451 
452  return $normalized;
453  }
454 
460  public function getContributors() {
461  $ss = $this->parser->getAttribute('contributor');
462 
463  if (!$ss) {
464  $ss = array();
465  }
466 
467  $normalized = array();
468  foreach ($ss as $s) {
469  $normalized[] = $this->buildStruct($this->contributorStruct, $s);
470  }
471 
472  return $normalized;
473  }
474 
480  public function getProvides() {
481  // normalize for 1.7
482  if ($this->getApiVersion() < 1.8) {
483  $provides = array();
484  } else {
485  $provides = $this->parser->getAttribute('provides');
486  }
487 
488  if (!$provides) {
489  $provides = array();
490  }
491 
492  // always provide ourself if we can
493  if ($this->pluginID) {
494  $provides[] = array(
495  'type' => 'plugin',
496  'name' => $this->getPluginID(),
497  'version' => $this->getVersion()
498  );
499  }
500 
501  $normalized = array();
502  foreach ($provides as $provide) {
503  $normalized[] = $this->buildStruct($this->depsProvidesStruct, $provide);
504  }
505 
506  return $normalized;
507  }
508 
514  public function getRequires() {
515  $reqs = $this->parser->getAttribute('requires');
516 
517  if (!$reqs) {
518  $reqs = array();
519  }
520 
521  $normalized = array();
522  foreach ($reqs as $req) {
523  $normalized[] = $this->normalizeDep($req);
524  }
525 
526  return $normalized;
527  }
528 
534  public function getSuggests() {
535  $suggests = $this->parser->getAttribute('suggests');
536 
537  if (!$suggests) {
538  $suggests = array();
539  }
540 
541  $normalized = array();
542  foreach ($suggests as $suggest) {
543  $normalized[] = $this->normalizeDep($suggest);
544  }
545 
546  return $normalized;
547  }
548 
556  private function normalizeDep($dep) {
557  switch ($dep['type']) {
558  case 'elgg_release':
559  $struct = $this->depsStructElgg;
560  break;
561 
562  case 'plugin':
563  $struct = $this->depsStructPlugin;
564  break;
565 
566  case 'priority':
567  $struct = $this->depsStructPriority;
568  break;
569 
570  case 'php_version':
571  $struct = $this->depsStructPhpVersion;
572  break;
573 
574  case 'php_extension':
575  $struct = $this->depsStructPhpExtension;
576  break;
577 
578  case 'php_ini':
579  $struct = $this->depsStructPhpIni;
580 
581  // also normalize boolean values
582  if (isset($dep['value'])) {
583  switch (strtolower($dep['value'])) {
584  case 'yes':
585  case 'true':
586  case 'on':
587  case 1:
588  $dep['value'] = 1;
589  break;
590 
591  case 'no':
592  case 'false':
593  case 'off':
594  case 0:
595  case '':
596  $dep['value'] = 0;
597  break;
598  }
599  }
600  break;
601  default:
602  // unrecognized so we just return the raw dependency
603  return $dep;
604  }
605 
606  // @todo $struct may not have been defined...
607  $normalized_dep = $this->buildStruct($struct, $dep);
608 
609  // normalize comparison operators
610  if (isset($normalized_dep['comparison'])) {
611  switch ($normalized_dep['comparison']) {
612  case '<':
613  $normalized_dep['comparison'] = 'lt';
614  break;
615 
616  case '<=':
617  $normalized_dep['comparison'] = 'le';
618  break;
619 
620  case '>':
621  $normalized_dep['comparison'] = 'gt';
622  break;
623 
624  case '>=':
625  $normalized_dep['comparison'] = 'ge';
626  break;
627 
628  case '==':
629  case 'eq':
630  $normalized_dep['comparison'] = '=';
631  break;
632 
633  case '<>':
634  case 'ne':
635  $normalized_dep['comparison'] = '!=';
636  break;
637  }
638  }
639 
640  return $normalized_dep;
641  }
642 
648  public function getConflicts() {
649  // normalize for 1.7
650  if ($this->getApiVersion() < 1.8) {
651  $conflicts = array();
652  } else {
653  $conflicts = $this->parser->getAttribute('conflicts');
654  }
655 
656  if (!$conflicts) {
657  $conflicts = array();
658  }
659 
660  $normalized = array();
661 
662  foreach ($conflicts as $conflict) {
663  $normalized[] = $this->buildStruct($this->depsConflictsStruct, $conflict);
664  }
665 
666  return $normalized;
667  }
668 
674  public function getActivateOnInstall() {
675  $activate = $this->parser->getAttribute('activate_on_install');
676  switch (strtolower($activate)) {
677  case 'yes':
678  case 'true':
679  case 'on':
680  case 1:
681  return true;
682 
683  case 'no':
684  case 'false':
685  case 'off':
686  case 0:
687  case '':
688  return false;
689  }
690  }
691 
700  protected function buildStruct(array $struct, array $array) {
701  $return = array();
702 
703  foreach ($struct as $index => $default) {
705  }
706 
707  return $return;
708  }
709 
719  static public function getFriendlyCategory($category) {
720  $cat_raw_string = "admin:plugins:category:$category";
721  if (_elgg_services()->translator->languageKeyExists($cat_raw_string)) {
722  return _elgg_services()->translator->translate($cat_raw_string);
723  }
724 
725  $category = str_replace(['-', '_'], ' ', $category);
726  return ucwords($category);
727  }
728 }
getCopyright()
Return the copyright.
getLicense()
Returns the license.
$version
getName()
Returns the plugin name.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
getRepositoryURL()
Returns the repository url.
$e
Definition: metadata.php:12
getApiVersion()
Returns the API version in use.
getScreenshots()
Return the screenshots listed.
$return
Definition: opendd.php:15
$default
Definition: checkbox.php:34
getDescription()
Return the description.
getID()
Return the plugin ID required by the author.
getPluginID()
Returns the plugin ID.
getSuggests()
Returns the suggests elements.
getBlurb()
Return the short description.
getAuthor()
Returns the plugin author.
getManifest()
Returns the manifest array.
getContributors()
Return the contributors listed.
$namespace_root
The root for plugin manifest namespaces.
getVersion()
Returns the version of the plugin.
getActivateOnInstall()
Should this plugin be activated when Elgg is installed.
$plugin_id
Definition: save.php:16
getDonationsPageURL()
Returns the donations page.
getCategories()
Return the categories listed for this plugin.
static getFriendlyCategory($category)
Returns a category&#39;s friendly name.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1375
getBugTrackerURL()
Returns the bug tracker page.
getProvides()
Return the list of provides by this plugin.
elgg_get_excerpt($text, $num_chars=250)
Returns an excerpt.
Definition: output.php:62
buildStruct(array $struct, array $array)
Normalizes an array into the structure specified.
$index
Definition: gallery.php:49
getRequires()
Returns the dependencies listed.
__construct($manifest, $plugin_id=null)
Load a manifest file, XmlElement or path to manifest.xml file.
getWebsite()
Return the website.
getConflicts()
Returns the conflicts listed.