00001 <?php
00012 class Licenses extends HTMLFormField {
00016 protected $msg;
00017
00021 protected $licenses = array();
00022
00026 protected $html;
00032 public function __construct( $params ) {
00033 parent::__construct( $params );
00034
00035 $this->msg = empty( $params['licenses'] ) ? wfMsgForContent( 'licenses' ) : $params['licenses'];
00036 $this->selected = null;
00037
00038 $this->makeLicenses();
00039 }
00040
00044 protected function makeLicenses() {
00045 $levels = array();
00046 $lines = explode( "\n", $this->msg );
00047
00048 foreach ( $lines as $line ) {
00049 if ( strpos( $line, '*' ) !== 0 )
00050 continue;
00051 else {
00052 list( $level, $line ) = $this->trimStars( $line );
00053
00054 if ( strpos( $line, '|' ) !== false ) {
00055 $obj = new License( $line );
00056 $this->stackItem( $this->licenses, $levels, $obj );
00057 } else {
00058 if ( $level < count( $levels ) ) {
00059 $levels = array_slice( $levels, 0, $level );
00060 }
00061 if ( $level == count( $levels ) ) {
00062 $levels[$level - 1] = $line;
00063 } else if ( $level > count( $levels ) ) {
00064 $levels[] = $line;
00065 }
00066 }
00067 }
00068 }
00069 }
00070
00071 protected function trimStars( $str ) {
00072 $i = $count = 0;
00073
00074 $numStars = strspn( $str, '*' );
00075 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
00076 }
00077
00078 protected function stackItem( &$list, $path, $item ) {
00079 $position =& $list;
00080 if ( $path )
00081 foreach( $path as $key )
00082 $position =& $position[$key];
00083 $position[] = $item;
00084 }
00085
00086 protected function makeHtml( $tagset, $depth = 0 ) {
00087 foreach ( $tagset as $key => $val )
00088 if ( is_array( $val ) ) {
00089 $this->html .= $this->outputOption(
00090 $this->msg( $key ), '',
00091 array(
00092 'disabled' => 'disabled',
00093 'style' => 'color: GrayText',
00094 ),
00095 $depth
00096 );
00097 $this->makeHtml( $val, $depth + 1 );
00098 } else {
00099 $this->html .= $this->outputOption(
00100 $this->msg( $val->text ), $val->template,
00101 array( 'title' => '{{' . $val->template . '}}' ),
00102 $depth
00103 );
00104 }
00105 }
00106
00107 protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
00108 $attribs['value'] = $value;
00109 if ( $value === $this->selected )
00110 $attribs['selected'] = 'selected';
00111 $val = str_repeat( "\xc2\xa0", $depth * 2 ) . $text;
00112 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
00113 }
00114
00115 protected function msg( $str ) {
00116 $out = wfMsg( $str );
00117 return wfEmptyMsg( $str, $out ) ? $str : $out;
00118 }
00119
00127 public function getLicenses() { return $this->licenses; }
00128
00134 public function getInputHTML( $value ) {
00135 $this->selected = $value;
00136
00137 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
00138 (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
00139 $this->makeHtml( $this->getLicenses() );
00140
00141 $attribs = array(
00142 'name' => $this->mName,
00143 'id' => $this->mID
00144 );
00145 if ( !empty( $this->mParams['disabled'] ) )
00146 $attibs['disabled'] = 'disabled';
00147
00148 return Html::rawElement( 'select', $attribs, $this->html );
00149 }
00150 }
00151
00155 class License {
00159 var $template;
00160
00164 var $text;
00165
00171 function License( $str ) {
00172 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
00173
00174 $this->template = strrev( $template );
00175 $this->text = strrev( $text );
00176 }
00177 }