docfile.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Utility for finding and parsing documentation files
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Documentation
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Utility for finding and parsing documentation files
  37. *
  38. * @category Documentation
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class DocFile
  46. {
  47. protected $filename;
  48. protected $contents;
  49. function __construct($filename)
  50. {
  51. $this->filename = $filename;
  52. }
  53. static function forTitle($title, $paths)
  54. {
  55. if (!is_array($paths)) {
  56. $paths = array($paths);
  57. }
  58. $filename = null;
  59. if (Event::handle('StartDocFileForTitle', array($title, &$paths, &$filename))) {
  60. foreach ($paths as $path) {
  61. $def = $path.'/'.$title;
  62. if (!file_exists($def)) {
  63. $def = null;
  64. }
  65. $lang = glob($path.'/'.$title.'.*');
  66. if ($lang === false) {
  67. $lang = array();
  68. }
  69. if (!empty($lang) || !empty($def)) {
  70. $filename = self::negotiateLanguage($lang, $def);
  71. break;
  72. }
  73. }
  74. Event::handle('EndDocFileForTitle', array($title, $paths, &$filename));
  75. }
  76. if (empty($filename)) {
  77. return null;
  78. } else {
  79. return new DocFile($filename);
  80. }
  81. }
  82. function toHTML($args=null)
  83. {
  84. if (is_null($args)) {
  85. $args = array();
  86. }
  87. if (empty($this->contents)) {
  88. $this->contents = file_get_contents($this->filename);
  89. }
  90. return common_markup_to_html($this->contents, $args);
  91. }
  92. static function defaultPaths()
  93. {
  94. $paths = array(INSTALLDIR.'/local/doc-src/',
  95. INSTALLDIR.'/doc-src/');
  96. $site = GNUsocial::currentSite();
  97. if (!empty($site)) {
  98. array_unshift($paths, INSTALLDIR.'/local/doc-src/'.$site.'/');
  99. }
  100. return $paths;
  101. }
  102. static function mailPaths()
  103. {
  104. $paths = array(INSTALLDIR.'/local/mail-src/',
  105. INSTALLDIR.'/mail-src/');
  106. $site = GNUsocial::currentSite();
  107. if (!empty($site)) {
  108. array_unshift($paths, INSTALLDIR.'/local/mail-src/'.$site.'/');
  109. }
  110. return $paths;
  111. }
  112. static function negotiateLanguage($filenames, $defaultFilename=null)
  113. {
  114. // XXX: do this better
  115. $langcode = common_language();
  116. foreach ($filenames as $filename) {
  117. if (preg_match('/\.'.$langcode.'$/', $filename)) {
  118. return $filename;
  119. }
  120. }
  121. return $defaultFilename;
  122. }
  123. }