2005-08-29 16:43:48 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-12 20:33:02 +00:00
|
|
|
* License selector for use on Special:Upload.
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
2012-05-12 20:33:02 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2005-08-29 16:43:48 +00:00
|
|
|
*
|
2012-05-12 20:33:02 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup SpecialPage
|
2005-08-29 16:43:48 +00:00
|
|
|
* @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
|
2005-11-13 02:48:19 +00:00
|
|
|
* @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
|
2005-08-29 16:43:48 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
|
|
|
|
|
*/
|
|
|
|
|
|
2012-05-12 20:33:02 +00:00
|
|
|
/**
|
|
|
|
|
* A License class for use on Special:Upload
|
|
|
|
|
*/
|
2009-10-18 19:41:01 +00:00
|
|
|
class Licenses extends HTMLFormField {
|
2014-05-11 15:32:18 +00:00
|
|
|
/** @var string */
|
2009-10-18 19:41:01 +00:00
|
|
|
protected $msg;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2014-05-11 15:32:18 +00:00
|
|
|
/** @var array */
|
2009-10-18 19:41:01 +00:00
|
|
|
protected $licenses = array();
|
2005-08-29 16:43:48 +00:00
|
|
|
|
2014-05-11 15:32:18 +00:00
|
|
|
/** @var string */
|
2009-10-18 19:41:01 +00:00
|
|
|
protected $html;
|
2005-08-30 19:22:31 +00:00
|
|
|
/**#@-*/
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-08-29 16:43:48 +00:00
|
|
|
/**
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param array $params
|
2005-08-29 16:43:48 +00:00
|
|
|
*/
|
2009-10-18 19:41:01 +00:00
|
|
|
public function __construct( $params ) {
|
|
|
|
|
parent::__construct( $params );
|
2010-12-01 20:22:45 +00:00
|
|
|
|
2014-05-11 15:32:18 +00:00
|
|
|
$this->msg = empty( $params['licenses'] )
|
|
|
|
|
? wfMessage( 'licenses' )->inContentLanguage()->plain()
|
|
|
|
|
: $params['licenses'];
|
2009-10-18 19:41:01 +00:00
|
|
|
$this->selected = null;
|
2005-08-29 16:43:48 +00:00
|
|
|
|
|
|
|
|
$this->makeLicenses();
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2006-04-19 15:46:24 +00:00
|
|
|
* @private
|
2005-08-29 16:43:48 +00:00
|
|
|
*/
|
2009-10-18 19:41:01 +00:00
|
|
|
protected function makeLicenses() {
|
2005-08-29 16:43:48 +00:00
|
|
|
$levels = array();
|
|
|
|
|
$lines = explode( "\n", $this->msg );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-08-29 16:43:48 +00:00
|
|
|
foreach ( $lines as $line ) {
|
2011-05-28 17:52:12 +00:00
|
|
|
if ( strpos( $line, '*' ) !== 0 ) {
|
2005-08-29 16:43:48 +00:00
|
|
|
continue;
|
2011-05-28 17:52:12 +00:00
|
|
|
} else {
|
2005-08-29 16:43:48 +00:00
|
|
|
list( $level, $line ) = $this->trimStars( $line );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-08-29 16:43:48 +00:00
|
|
|
if ( strpos( $line, '|' ) !== false ) {
|
|
|
|
|
$obj = new License( $line );
|
2005-08-30 23:06:40 +00:00
|
|
|
$this->stackItem( $this->licenses, $levels, $obj );
|
2005-08-29 16:43:48 +00:00
|
|
|
} else {
|
2006-11-23 08:25:56 +00:00
|
|
|
if ( $level < count( $levels ) ) {
|
2005-08-31 14:19:01 +00:00
|
|
|
$levels = array_slice( $levels, 0, $level );
|
2006-11-23 08:25:56 +00:00
|
|
|
}
|
|
|
|
|
if ( $level == count( $levels ) ) {
|
2005-08-29 16:43:48 +00:00
|
|
|
$levels[$level - 1] = $line;
|
2011-06-17 16:03:52 +00:00
|
|
|
} elseif ( $level > count( $levels ) ) {
|
2005-08-29 16:43:48 +00:00
|
|
|
$levels[] = $line;
|
2006-11-23 08:25:56 +00:00
|
|
|
}
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-12-30 17:27:46 +00:00
|
|
|
}
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param string $str
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2009-12-19 21:23:04 +00:00
|
|
|
protected function trimStars( $str ) {
|
|
|
|
|
$numStars = strspn( $str, '*' );
|
|
|
|
|
return array( $numStars, ltrim( substr( $str, $numStars ), ' ' ) );
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param array $list
|
|
|
|
|
* @param array $path
|
|
|
|
|
* @param mixed $item
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2009-10-18 19:41:01 +00:00
|
|
|
protected function stackItem( &$list, $path, $item ) {
|
2005-08-30 23:06:40 +00:00
|
|
|
$position =& $list;
|
2011-05-28 17:52:12 +00:00
|
|
|
if ( $path ) {
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $path as $key ) {
|
2005-08-30 23:06:40 +00:00
|
|
|
$position =& $position[$key];
|
2011-05-28 17:52:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-30 23:06:40 +00:00
|
|
|
$position[] = $item;
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param array $tagset
|
|
|
|
|
* @param int $depth
|
2011-05-28 17:52:12 +00:00
|
|
|
*/
|
2009-10-18 20:06:22 +00:00
|
|
|
protected function makeHtml( $tagset, $depth = 0 ) {
|
2012-09-27 19:46:22 +00:00
|
|
|
foreach ( $tagset as $key => $val ) {
|
2005-08-29 16:43:48 +00:00
|
|
|
if ( is_array( $val ) ) {
|
|
|
|
|
$this->html .= $this->outputOption(
|
2012-08-23 07:11:21 +00:00
|
|
|
$key, '',
|
2005-08-29 16:43:48 +00:00
|
|
|
array(
|
2005-08-29 20:24:57 +00:00
|
|
|
'disabled' => 'disabled',
|
2006-01-01 01:05:21 +00:00
|
|
|
'style' => 'color: GrayText', // for MSIE
|
2005-08-29 16:43:48 +00:00
|
|
|
),
|
|
|
|
|
$depth
|
|
|
|
|
);
|
|
|
|
|
$this->makeHtml( $val, $depth + 1 );
|
|
|
|
|
} else {
|
|
|
|
|
$this->html .= $this->outputOption(
|
2012-08-23 07:11:21 +00:00
|
|
|
$val->text, $val->template,
|
2009-10-18 19:41:01 +00:00
|
|
|
array( 'title' => '{{' . $val->template . '}}' ),
|
2005-08-29 16:43:48 +00:00
|
|
|
$depth
|
|
|
|
|
);
|
|
|
|
|
}
|
2012-09-27 19:46:22 +00:00
|
|
|
}
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:52:12 +00:00
|
|
|
/**
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param string $message
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param null|array $attribs
|
|
|
|
|
* @param int $depth
|
2011-05-28 17:52:12 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-08-23 07:11:21 +00:00
|
|
|
protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
|
|
|
|
|
$msgObj = $this->msg( $message );
|
|
|
|
|
$text = $msgObj->exists() ? $msgObj->text() : $message;
|
2009-10-18 19:41:01 +00:00
|
|
|
$attribs['value'] = $value;
|
2012-09-27 19:46:22 +00:00
|
|
|
if ( $value === $this->selected ) {
|
2010-12-01 20:22:45 +00:00
|
|
|
$attribs['selected'] = 'selected';
|
2012-09-27 19:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
2009-10-18 19:41:01 +00:00
|
|
|
$val = str_repeat( /*   */ "\xc2\xa0", $depth * 2 ) . $text;
|
2008-12-14 19:14:21 +00:00
|
|
|
return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-08-29 16:43:48 +00:00
|
|
|
/**#@-*/
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-08-29 16:43:48 +00:00
|
|
|
/**
|
|
|
|
|
* Accessor for $this->licenses
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-05-28 17:52:12 +00:00
|
|
|
public function getLicenses() {
|
|
|
|
|
return $this->licenses;
|
|
|
|
|
}
|
2005-08-29 16:43:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Accessor for $this->html
|
|
|
|
|
*
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param bool $value
|
2011-05-28 17:52:12 +00:00
|
|
|
*
|
2005-08-29 16:43:48 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-10-18 19:41:01 +00:00
|
|
|
public function getInputHTML( $value ) {
|
|
|
|
|
$this->selected = $value;
|
2010-12-01 20:22:45 +00:00
|
|
|
|
2012-07-24 01:04:15 +00:00
|
|
|
$this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
|
2009-10-18 19:41:01 +00:00
|
|
|
(bool)$this->selected ? null : array( 'selected' => 'selected' ) );
|
|
|
|
|
$this->makeHtml( $this->getLicenses() );
|
2010-12-01 20:22:45 +00:00
|
|
|
|
2009-10-18 19:41:01 +00:00
|
|
|
$attribs = array(
|
|
|
|
|
'name' => $this->mName,
|
|
|
|
|
'id' => $this->mID
|
|
|
|
|
);
|
2011-05-28 17:52:12 +00:00
|
|
|
if ( !empty( $this->mParams['disabled'] ) ) {
|
2009-10-18 19:41:01 +00:00
|
|
|
$attibs['disabled'] = 'disabled';
|
2011-05-28 17:52:12 +00:00
|
|
|
}
|
2010-12-01 20:22:45 +00:00
|
|
|
|
2009-10-18 19:41:01 +00:00
|
|
|
return Html::rawElement( 'select', $attribs, $this->html );
|
|
|
|
|
}
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
|
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
|
|
|
|
* A License class for use on Special:Upload (represents a single type of license).
|
|
|
|
|
*/
|
2005-08-29 16:43:48 +00:00
|
|
|
class License {
|
2014-05-11 15:32:18 +00:00
|
|
|
/** @var string */
|
|
|
|
|
public $template;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2014-05-11 15:32:18 +00:00
|
|
|
/** @var string */
|
|
|
|
|
public $text;
|
2005-08-29 16:43:48 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $str license name??
|
2005-08-29 16:43:48 +00:00
|
|
|
*/
|
2010-08-30 16:52:51 +00:00
|
|
|
function __construct( $str ) {
|
2005-08-30 19:22:31 +00:00
|
|
|
list( $text, $template ) = explode( '|', strrev( $str ), 2 );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-08-30 19:22:31 +00:00
|
|
|
$this->template = strrev( $template );
|
|
|
|
|
$this->text = strrev( $text );
|
2005-08-29 16:43:48 +00:00
|
|
|
}
|
|
|
|
|
}
|