Licenses.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * A License class for use on Special:Upload
  4. *
  5. * @ingroup SpecialPage
  6. *
  7. * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
  8. * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  10. */
  11. class Licenses {
  12. /**#@+
  13. * @private
  14. */
  15. /**
  16. * @var string
  17. */
  18. var $msg;
  19. /**
  20. * @var array
  21. */
  22. var $licenses = array();
  23. /**
  24. * @var string
  25. */
  26. var $html;
  27. /**#@-*/
  28. /**
  29. * Constructor
  30. *
  31. * @param $str String: the string to build the licenses member from, will use
  32. * wfMsgForContent( 'licenses' ) if null (default: null)
  33. */
  34. function __construct( $str = null ) {
  35. // PHP sucks, this should be possible in the constructor
  36. $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str;
  37. $this->html = '';
  38. $this->makeLicenses();
  39. $tmp = $this->getLicenses();
  40. $this->makeHtml( $tmp );
  41. }
  42. /**#@+
  43. * @private
  44. */
  45. function makeLicenses() {
  46. $levels = array();
  47. $lines = explode( "\n", $this->msg );
  48. foreach ( $lines as $line ) {
  49. if ( strpos( $line, '*' ) !== 0 )
  50. continue;
  51. else {
  52. list( $level, $line ) = $this->trimStars( $line );
  53. if ( strpos( $line, '|' ) !== false ) {
  54. $obj = new License( $line );
  55. $this->stackItem( $this->licenses, $levels, $obj );
  56. } else {
  57. if ( $level < count( $levels ) ) {
  58. $levels = array_slice( $levels, 0, $level );
  59. }
  60. if ( $level == count( $levels ) ) {
  61. $levels[$level - 1] = $line;
  62. } else if ( $level > count( $levels ) ) {
  63. $levels[] = $line;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. function trimStars( $str ) {
  70. $i = $count = 0;
  71. wfSuppressWarnings();
  72. while ($str[$i++] == '*')
  73. ++$count;
  74. wfRestoreWarnings();
  75. return array( $count, ltrim( $str, '* ' ) );
  76. }
  77. function stackItem( &$list, $path, $item ) {
  78. $position =& $list;
  79. if ( $path )
  80. foreach( $path as $key )
  81. $position =& $position[$key];
  82. $position[] = $item;
  83. }
  84. function makeHtml( &$tagset, $depth = 0 ) {
  85. foreach ( $tagset as $key => $val )
  86. if ( is_array( $val ) ) {
  87. $this->html .= $this->outputOption(
  88. $this->msg( $key ),
  89. array(
  90. 'value' => '',
  91. 'disabled' => 'disabled',
  92. 'style' => 'color: GrayText', // for MSIE
  93. ),
  94. $depth
  95. );
  96. $this->makeHtml( $val, $depth + 1 );
  97. } else {
  98. $this->html .= $this->outputOption(
  99. $this->msg( $val->text ),
  100. array(
  101. 'value' => $val->template,
  102. 'title' => '{{' . $val->template . '}}'
  103. ),
  104. $depth
  105. );
  106. }
  107. }
  108. function outputOption( $val, $attribs = null, $depth ) {
  109. $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $val;
  110. return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
  111. }
  112. function msg( $str ) {
  113. $out = wfMsg( $str );
  114. return wfEmptyMsg( $str, $out ) ? $str : $out;
  115. }
  116. /**#@-*/
  117. /**
  118. * Accessor for $this->licenses
  119. *
  120. * @return array
  121. */
  122. function getLicenses() { return $this->licenses; }
  123. /**
  124. * Accessor for $this->html
  125. *
  126. * @return string
  127. */
  128. function getHtml() { return $this->html; }
  129. }
  130. /**
  131. * A License class for use on Special:Upload (represents a single type of license).
  132. */
  133. class License {
  134. /**
  135. * @var string
  136. */
  137. var $template;
  138. /**
  139. * @var string
  140. */
  141. var $text;
  142. /**
  143. * Constructor
  144. *
  145. * @param $str String: license name??
  146. */
  147. function License( $str ) {
  148. list( $text, $template ) = explode( '|', strrev( $str ), 2 );
  149. $this->template = strrev( $template );
  150. $this->text = strrev( $text );
  151. }
  152. }