Elgg  Version 1.9
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_version and 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_echo('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_echo('PluginException:NoAvailableParser',
203  array($this->apiVersion, $this->getPluginID())));
204  }
205 
206  if (!$this->parser->parse()) {
207  throw new PluginException(elgg_echo('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_echo('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('aalborg_theme', 'blog', 'bookmarks', 'categories', 'ckeditor',
385  'custom_index', 'dashboard', 'developers', 'diagnostics',
386  'embed', 'externalpages', 'file', 'garbagecollector',
387  'groups', 'htmlawed', 'invitefriends', 'legacy_urls', 'likes',
388  'logbrowser', 'logrotate', 'members', 'messageboard',
389  'messages', 'notifications', 'pages', 'profile',
390  'reportedcontent', 'search', 'site_notifications','tagcloud', 'thewire',
391  'twitter_api', 'uservalidationbyemail', 'web_services', 'zaudio',
392  );
393 
394  $cats = $this->parser->getAttribute('category');
395 
396  if (!$cats) {
397  $cats = array();
398  }
399 
400  if (in_array('bundled', $cats) && !in_array($this->getPluginID(), $bundled_plugins)) {
401  unset($cats[array_search('bundled', $cats)]);
402  }
403 
404  return $cats;
405  }
406 
412  public function getScreenshots() {
413  $ss = $this->parser->getAttribute('screenshot');
414 
415  if (!$ss) {
416  $ss = array();
417  }
418 
419  $normalized = array();
420  foreach ($ss as $s) {
421  $normalized[] = $this->buildStruct($this->screenshotStruct, $s);
422  }
423 
424  return $normalized;
425  }
426 
432  public function getContributors() {
433  $ss = $this->parser->getAttribute('contributor');
434 
435  if (!$ss) {
436  $ss = array();
437  }
438 
439  $normalized = array();
440  foreach ($ss as $s) {
441  $normalized[] = $this->buildStruct($this->contributorStruct, $s);
442  }
443 
444  return $normalized;
445  }
446 
452  public function getProvides() {
453  // normalize for 1.7
454  if ($this->getApiVersion() < 1.8) {
455  $provides = array();
456  } else {
457  $provides = $this->parser->getAttribute('provides');
458  }
459 
460  if (!$provides) {
461  $provides = array();
462  }
463 
464  // always provide ourself if we can
465  if ($this->pluginID) {
466  $provides[] = array(
467  'type' => 'plugin',
468  'name' => $this->getPluginID(),
469  'version' => $this->getVersion()
470  );
471  }
472 
473  $normalized = array();
474  foreach ($provides as $provide) {
475  $normalized[] = $this->buildStruct($this->depsProvidesStruct, $provide);
476  }
477 
478  return $normalized;
479  }
480 
486  public function getRequires() {
487  // rewrite the 1.7 style elgg_version as a real requires.
488  if ($this->apiVersion < 1.8) {
489  $elgg_version = $this->parser->getAttribute('elgg_version');
490  if ($elgg_version) {
491  $reqs = array(
492  array(
493  'type' => 'elgg_version',
494  'version' => $elgg_version,
495  'comparison' => 'ge'
496  )
497  );
498  } else {
499  $reqs = array();
500  }
501  } else {
502  $reqs = $this->parser->getAttribute('requires');
503  }
504 
505  if (!$reqs) {
506  $reqs = array();
507  }
508 
509  $normalized = array();
510  foreach ($reqs as $req) {
511  $normalized[] = $this->normalizeDep($req);
512  }
513 
514  return $normalized;
515  }
516 
522  public function getSuggests() {
523  $suggests = $this->parser->getAttribute('suggests');
524 
525  if (!$suggests) {
526  $suggests = array();
527  }
528 
529  $normalized = array();
530  foreach ($suggests as $suggest) {
531  $normalized[] = $this->normalizeDep($suggest);
532  }
533 
534  return $normalized;
535  }
536 
544  private function normalizeDep($dep) {
545  switch ($dep['type']) {
546  case 'elgg_version':
547  case 'elgg_release':
548  $struct = $this->depsStructElgg;
549  break;
550 
551  case 'plugin':
552  $struct = $this->depsStructPlugin;
553  break;
554 
555  case 'priority':
556  $struct = $this->depsStructPriority;
557  break;
558 
559  case 'php_version':
560  $struct = $this->depsStructPhpVersion;
561  break;
562 
563  case 'php_extension':
564  $struct = $this->depsStructPhpExtension;
565  break;
566 
567  case 'php_ini':
568  $struct = $this->depsStructPhpIni;
569 
570  // also normalize boolean values
571  if (isset($dep['value'])) {
572  switch (strtolower($dep['value'])) {
573  case 'yes':
574  case 'true':
575  case 'on':
576  case 1:
577  $dep['value'] = 1;
578  break;
579 
580  case 'no':
581  case 'false':
582  case 'off':
583  case 0:
584  case '':
585  $dep['value'] = 0;
586  break;
587  }
588  }
589  break;
590  default:
591  // unrecognized so we just return the raw dependency
592  return $dep;
593  }
594 
595  // @todo $struct may not have been defined...
596  $normalized_dep = $this->buildStruct($struct, $dep);
597 
598  // normalize comparison operators
599  if (isset($normalized_dep['comparison'])) {
600  switch ($normalized_dep['comparison']) {
601  case '<':
602  $normalized_dep['comparison'] = 'lt';
603  break;
604 
605  case '<=':
606  $normalized_dep['comparison'] = 'le';
607  break;
608 
609  case '>':
610  $normalized_dep['comparison'] = 'gt';
611  break;
612 
613  case '>=':
614  $normalized_dep['comparison'] = 'ge';
615  break;
616 
617  case '==':
618  case 'eq':
619  $normalized_dep['comparison'] = '=';
620  break;
621 
622  case '<>':
623  case 'ne':
624  $normalized_dep['comparison'] = '!=';
625  break;
626  }
627  }
628 
629  return $normalized_dep;
630  }
631 
637  public function getConflicts() {
638  // normalize for 1.7
639  if ($this->getApiVersion() < 1.8) {
640  $conflicts = array();
641  } else {
642  $conflicts = $this->parser->getAttribute('conflicts');
643  }
644 
645  if (!$conflicts) {
646  $conflicts = array();
647  }
648 
649  $normalized = array();
650 
651  foreach ($conflicts as $conflict) {
652  $normalized[] = $this->buildStruct($this->depsConflictsStruct, $conflict);
653  }
654 
655  return $normalized;
656  }
657 
663  public function getActivateOnInstall() {
664  $activate = $this->parser->getAttribute('activate_on_install');
665  switch (strtolower($activate)) {
666  case 'yes':
667  case 'true':
668  case 'on':
669  case 1:
670  return true;
671 
672  case 'no':
673  case 'false':
674  case 'off':
675  case 0:
676  case '':
677  return false;
678  }
679  }
680 
689  protected function buildStruct(array $struct, array $array) {
690  $return = array();
691 
692  foreach ($struct as $index => $default) {
693  $return[$index] = elgg_extract($index, $array, $default);
694  }
695 
696  return $return;
697  }
698 
708  static public function getFriendlyCategory($category) {
709  $cat_raw_string = "admin:plugins:category:$category";
710  $cat_display_string = elgg_echo($cat_raw_string);
711  if ($cat_display_string == $cat_raw_string) {
712  $category = str_replace(array('-', '_'), ' ', $category);
713  $cat_display_string = ucwords($category);
714  }
715  return $cat_display_string;
716  }
717 }
getCopyright()
Return the copyright.
getLicense()
Returns the license.
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:36
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1464
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.
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
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.
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:72
buildStruct(array $struct, array $array)
Normalizes an array into the structure specified.
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.
$version
Definition: version.php:14
getConflicts()
Returns the conflicts listed.