MediaWiki  REL1_19
Licenses.php
Go to the documentation of this file.
00001 <?php
00012 class Licenses extends HTMLFormField {
00016         protected $msg;
00017 
00021         protected $licenses = array();
00022 
00026         protected $html;
00034         public function __construct( $params ) {
00035                 parent::__construct( $params );
00036 
00037                 $this->msg = empty( $params['licenses'] ) ? wfMsgForContent( 'licenses' ) : $params['licenses'];
00038                 $this->selected = null;
00039 
00040                 $this->makeLicenses();
00041         }
00042 
00046         protected function makeLicenses() {
00047                 $levels = array();
00048                 $lines = explode( "\n", $this->msg );
00049 
00050                 foreach ( $lines as $line ) {
00051                         if ( strpos( $line, '*' ) !== 0 ) {
00052                                 continue;
00053                         } else {
00054                                 list( $level, $line ) = $this->trimStars( $line );
00055 
00056                                 if ( strpos( $line, '|' ) !== false ) {
00057                                         $obj = new License( $line );
00058                                         $this->stackItem( $this->licenses, $levels, $obj );
00059                                 } else {
00060                                         if ( $level < count( $levels ) ) {
00061                                                 $levels = array_slice( $levels, 0, $level );
00062                                         }
00063                                         if ( $level == count( $levels ) ) {
00064                                                 $levels[$level - 1] = $line;
00065                                         } elseif ( $level > count( $levels ) ) {
00066                                                 $levels[] = $line;
00067                                         }
00068                                 }
00069                         }
00070                 }
00071         }
00072 
00077         protected function trimStars( $str ) {
00078                 $numStars = strspn( $str, '*' );
00079                 return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
00080         }
00081 
00087         protected function stackItem( &$list, $path, $item ) {
00088                 $position =& $list;
00089                 if ( $path ) {
00090                         foreach( $path as $key ) {
00091                                 $position =& $position[$key];
00092                         }
00093                 }
00094                 $position[] = $item;
00095         }
00096 
00101         protected function makeHtml( $tagset, $depth = 0 ) {
00102                 foreach ( $tagset as $key => $val )
00103                         if ( is_array( $val ) ) {
00104                                 $this->html .= $this->outputOption(
00105                                         $this->msg( $key ), '',
00106                                         array(
00107                                                 'disabled' => 'disabled',
00108                                                 'style' => 'color: GrayText', // for MSIE
00109                                         ),
00110                                         $depth
00111                                 );
00112                                 $this->makeHtml( $val, $depth + 1 );
00113                         } else {
00114                                 $this->html .= $this->outputOption(
00115                                         $this->msg( $val->text ), $val->template,
00116                                         array( 'title' => '{{' . $val->template . '}}' ),
00117                                         $depth
00118                                 );
00119                         }
00120         }
00121 
00129         protected function outputOption( $text, $value, $attribs = null, $depth = 0 ) {
00130                 $attribs['value'] = $value;
00131                 if ( $value === $this->selected )
00132                         $attribs['selected'] = 'selected';
00133                 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
00134                 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
00135         }
00136 
00141         protected function msg( $str ) {
00142                 $msg = wfMessage( $str );
00143                 return $msg->exists() ? $msg->text() : $str;
00144         }
00145 
00153         public function getLicenses() {
00154                 return $this->licenses;
00155         }
00156 
00164         public function getInputHTML( $value ) {
00165                 $this->selected = $value;
00166 
00167                 $this->html = $this->outputOption( wfMsg( 'nolicense' ), '',
00168                         (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
00169                 $this->makeHtml( $this->getLicenses() );
00170 
00171                 $attribs = array(
00172                         'name' => $this->mName,
00173                         'id' => $this->mID
00174                 );
00175                 if ( !empty( $this->mParams['disabled'] ) ) {
00176                         $attibs['disabled'] = 'disabled';
00177                 }
00178 
00179                 return Html::rawElement( 'select', $attribs, $this->html );
00180         }
00181 }
00182 
00186 class License {
00190         var $template;
00191 
00195         var $text;
00196 
00202         function __construct( $str ) {
00203                 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
00204 
00205                 $this->template = strrev( $template );
00206                 $this->text = strrev( $text );
00207         }
00208 }