MediaWiki  REL1_22
Licenses.php
Go to the documentation of this file.
00001 <?php
00030 class Licenses extends HTMLFormField {
00034     protected $msg;
00035 
00039     protected $licenses = array();
00040 
00044     protected $html;
00052     public function __construct( $params ) {
00053         parent::__construct( $params );
00054 
00055         $this->msg = empty( $params['licenses'] ) ? wfMessage( 'licenses' )->inContentLanguage()->plain() : $params['licenses'];
00056         $this->selected = null;
00057 
00058         $this->makeLicenses();
00059     }
00060 
00064     protected function makeLicenses() {
00065         $levels = array();
00066         $lines = explode( "\n", $this->msg );
00067 
00068         foreach ( $lines as $line ) {
00069             if ( strpos( $line, '*' ) !== 0 ) {
00070                 continue;
00071             } else {
00072                 list( $level, $line ) = $this->trimStars( $line );
00073 
00074                 if ( strpos( $line, '|' ) !== false ) {
00075                     $obj = new License( $line );
00076                     $this->stackItem( $this->licenses, $levels, $obj );
00077                 } else {
00078                     if ( $level < count( $levels ) ) {
00079                         $levels = array_slice( $levels, 0, $level );
00080                     }
00081                     if ( $level == count( $levels ) ) {
00082                         $levels[$level - 1] = $line;
00083                     } elseif ( $level > count( $levels ) ) {
00084                         $levels[] = $line;
00085                     }
00086                 }
00087             }
00088         }
00089     }
00090 
00095     protected function trimStars( $str ) {
00096         $numStars = strspn( $str, '*' );
00097         return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
00098     }
00099 
00105     protected function stackItem( &$list, $path, $item ) {
00106         $position =& $list;
00107         if ( $path ) {
00108             foreach ( $path as $key ) {
00109                 $position =& $position[$key];
00110             }
00111         }
00112         $position[] = $item;
00113     }
00114 
00119     protected function makeHtml( $tagset, $depth = 0 ) {
00120         foreach ( $tagset as $key => $val ) {
00121             if ( is_array( $val ) ) {
00122                 $this->html .= $this->outputOption(
00123                     $key, '',
00124                     array(
00125                         'disabled' => 'disabled',
00126                         'style' => 'color: GrayText', // for MSIE
00127                     ),
00128                     $depth
00129                 );
00130                 $this->makeHtml( $val, $depth + 1 );
00131             } else {
00132                 $this->html .= $this->outputOption(
00133                     $val->text, $val->template,
00134                     array( 'title' => '{{' . $val->template . '}}' ),
00135                     $depth
00136                 );
00137             }
00138         }
00139     }
00140 
00148     protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
00149         $msgObj = $this->msg( $message );
00150         $text = $msgObj->exists() ? $msgObj->text() : $message;
00151         $attribs['value'] = $value;
00152         if ( $value === $this->selected ) {
00153             $attribs['selected'] = 'selected';
00154         }
00155 
00156         $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
00157         return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
00158     }
00159 
00167     public function getLicenses() {
00168         return $this->licenses;
00169     }
00170 
00178     public function getInputHTML( $value ) {
00179         $this->selected = $value;
00180 
00181         $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
00182             (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
00183         $this->makeHtml( $this->getLicenses() );
00184 
00185         $attribs = array(
00186             'name' => $this->mName,
00187             'id' => $this->mID
00188         );
00189         if ( !empty( $this->mParams['disabled'] ) ) {
00190             $attibs['disabled'] = 'disabled';
00191         }
00192 
00193         return Html::rawElement( 'select', $attribs, $this->html );
00194     }
00195 }
00196 
00200 class License {
00204     var $template;
00205 
00209     var $text;
00210 
00216     function __construct( $str ) {
00217         list( $text, $template ) = explode( '|', strrev( $str ), 2 );
00218 
00219         $this->template = strrev( $template );
00220         $this->text = strrev( $text );
00221     }
00222 }