MediaWiki  REL1_24
Licenses.php
Go to the documentation of this file.
00001 <?php
00030 class Licenses extends HTMLFormField {
00032     protected $msg;
00033 
00035     protected $licenses = array();
00036 
00038     protected $html;
00044     public function __construct( $params ) {
00045         parent::__construct( $params );
00046 
00047         $this->msg = empty( $params['licenses'] )
00048             ? wfMessage( 'licenses' )->inContentLanguage()->plain()
00049             : $params['licenses'];
00050         $this->selected = null;
00051 
00052         $this->makeLicenses();
00053     }
00054 
00058     protected function makeLicenses() {
00059         $levels = array();
00060         $lines = explode( "\n", $this->msg );
00061 
00062         foreach ( $lines as $line ) {
00063             if ( strpos( $line, '*' ) !== 0 ) {
00064                 continue;
00065             } else {
00066                 list( $level, $line ) = $this->trimStars( $line );
00067 
00068                 if ( strpos( $line, '|' ) !== false ) {
00069                     $obj = new License( $line );
00070                     $this->stackItem( $this->licenses, $levels, $obj );
00071                 } else {
00072                     if ( $level < count( $levels ) ) {
00073                         $levels = array_slice( $levels, 0, $level );
00074                     }
00075                     if ( $level == count( $levels ) ) {
00076                         $levels[$level - 1] = $line;
00077                     } elseif ( $level > count( $levels ) ) {
00078                         $levels[] = $line;
00079                     }
00080                 }
00081             }
00082         }
00083     }
00084 
00089     protected function trimStars( $str ) {
00090         $numStars = strspn( $str, '*' );
00091         return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
00092     }
00093 
00099     protected function stackItem( &$list, $path, $item ) {
00100         $position =& $list;
00101         if ( $path ) {
00102             foreach ( $path as $key ) {
00103                 $position =& $position[$key];
00104             }
00105         }
00106         $position[] = $item;
00107     }
00108 
00113     protected function makeHtml( $tagset, $depth = 0 ) {
00114         foreach ( $tagset as $key => $val ) {
00115             if ( is_array( $val ) ) {
00116                 $this->html .= $this->outputOption(
00117                     $key, '',
00118                     array(
00119                         'disabled' => 'disabled',
00120                         'style' => 'color: GrayText', // for MSIE
00121                     ),
00122                     $depth
00123                 );
00124                 $this->makeHtml( $val, $depth + 1 );
00125             } else {
00126                 $this->html .= $this->outputOption(
00127                     $val->text, $val->template,
00128                     array( 'title' => '{{' . $val->template . '}}' ),
00129                     $depth
00130                 );
00131             }
00132         }
00133     }
00134 
00142     protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
00143         $msgObj = $this->msg( $message );
00144         $text = $msgObj->exists() ? $msgObj->text() : $message;
00145         $attribs['value'] = $value;
00146         if ( $value === $this->selected ) {
00147             $attribs['selected'] = 'selected';
00148         }
00149 
00150         $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
00151         return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
00152     }
00153 
00161     public function getLicenses() {
00162         return $this->licenses;
00163     }
00164 
00172     public function getInputHTML( $value ) {
00173         $this->selected = $value;
00174 
00175         $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
00176             (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
00177         $this->makeHtml( $this->getLicenses() );
00178 
00179         $attribs = array(
00180             'name' => $this->mName,
00181             'id' => $this->mID
00182         );
00183         if ( !empty( $this->mParams['disabled'] ) ) {
00184             $attibs['disabled'] = 'disabled';
00185         }
00186 
00187         return Html::rawElement( 'select', $attribs, $this->html );
00188     }
00189 }
00190 
00194 class License {
00196     public $template;
00197 
00199     public $text;
00200 
00204     function __construct( $str ) {
00205         list( $text, $template ) = explode( '|', strrev( $str ), 2 );
00206 
00207         $this->template = strrev( $template );
00208         $this->text = strrev( $text );
00209     }
00210 }