MediaWiki  REL1_20
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 
00147         protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
00148                 $msgObj = $this->msg( $message );
00149                 $text = $msgObj->exists() ? $msgObj->text() : $message;
00150                 $attribs['value'] = $value;
00151                 if ( $value === $this->selected )
00152                         $attribs['selected'] = 'selected';
00153                 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
00154                 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
00155         }
00156 
00164         public function getLicenses() {
00165                 return $this->licenses;
00166         }
00167 
00175         public function getInputHTML( $value ) {
00176                 $this->selected = $value;
00177 
00178                 $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
00179                         (bool)$this->selected ? null : array( 'selected' => 'selected' ) );
00180                 $this->makeHtml( $this->getLicenses() );
00181 
00182                 $attribs = array(
00183                         'name' => $this->mName,
00184                         'id' => $this->mID
00185                 );
00186                 if ( !empty( $this->mParams['disabled'] ) ) {
00187                         $attibs['disabled'] = 'disabled';
00188                 }
00189 
00190                 return Html::rawElement( 'select', $attribs, $this->html );
00191         }
00192 }
00193 
00197 class License {
00201         var $template;
00202 
00206         var $text;
00207 
00213         function __construct( $str ) {
00214                 list( $text, $template ) = explode( '|', strrev( $str ), 2 );
00215 
00216                 $this->template = strrev( $template );
00217                 $this->text = strrev( $text );
00218         }
00219 }