diff --git a/res/ext.SimpleBatchUpload.js b/res/ext.SimpleBatchUpload.js index a1852cc..8e81921 100644 --- a/res/ext.SimpleBatchUpload.js +++ b/res/ext.SimpleBatchUpload.js @@ -40,6 +40,7 @@ add: function ( e, data ) { + var that = this; data.id = Date.now(); var status = $('
  • ') @@ -62,7 +63,8 @@ action: 'upload', token: token, ignorewarnings: 1, - comment: mw.message( 'simplebatchupload-comment' ).text(), + text: $( that ).fileupload( 'option', 'text' ), + comment: $( that ).fileupload( 'option', 'comment' ), filename: data.files[ 0 ].name }; diff --git a/src/ParameterProvider.php b/src/ParameterProvider.php new file mode 100644 index 0000000..f739e7d --- /dev/null +++ b/src/ParameterProvider.php @@ -0,0 +1,113 @@ +. + * + * @file + * @ingroup SimpleBatchUpload + */ + +namespace SimpleBatchUpload; + +use Message; + +/** + * Class ParameterProvider + * + * @package SimpleBatchUpload + * @ingroup SimpleBatchUpload + */ +class ParameterProvider { + + private $parameterIndex; + private $parameters = null; + + public function __construct( $parameterIndex ) { + $this->parameterIndex = $parameterIndex; + } + + public function getEscapedUploadPageText() { + return $this->getEscapedParameter( 'pagetext' ); + } + + public function getEscapedUploadComment() { + return $this->getEscapedParameter( 'comment' ); + } + + public function getSpecialPageTitle() { + return $this->getParameter( 'title' ); + } + + private function getParameter( $key ) { + if ( $this->parameters === null ) { + $this->populateParameters(); + } + return $this->parameters[ $key ]; + } + + private function setParameters( $pagetext, $comment, $title ) { + $this->parameters = [ + 'pagetext' => $pagetext, + 'comment' => $comment, + 'title' => $title, + ]; + } + + private function getEscapedParameter( $key ) { + return $this->escape( $this->getParameter( $key ) ); + } + + private function escape( $text ) { + return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8', false ); + } + + private function populateParameters() { + + if ( $this->parameterIndex === null || $this->populateParametersFromKey() === false ) { + $this->populateParametersFromDefaults(); + } + + } + + private function populateParametersFromKey() { + $paramMsg = Message::newFromKey( 'simplebatchupload-parameters' ); + + if ( $paramMsg->exists() ) { + + $paramSet = explode( "\n", $paramMsg->plain() ); + $paramSet = array_map( [ $this, 'parseParamLine' ], $paramSet ); + $paramSet = array_combine( array_column( $paramSet, 0 ), $paramSet ); + + if ( array_key_exists( $this->parameterIndex, $paramSet ) ) { + $this->setParameters( '{{' . $this->parameterIndex . '}}', $paramSet[ $this->parameterIndex ][ 1 ], $paramSet[ $this->parameterIndex ][ 2 ] ); + return true; + } + } + return false; + } + + private function populateParametersFromDefaults() { + $this->setParameters( '', Message::newFromKey( 'simplebatchupload-comment' )->text(), Message::newFromKey( 'batchupload' )->text() ); + } + + private function parseParamLine( $paramLine ) { + $paramLine = $paramLine; + return array_map( 'trim', explode( '|', $paramLine, 3 ) ); + } + +} diff --git a/src/SpecialBatchUpload.php b/src/SpecialBatchUpload.php index 81a2be2..2940e41 100644 --- a/src/SpecialBatchUpload.php +++ b/src/SpecialBatchUpload.php @@ -55,23 +55,29 @@ protected function getGroupName() { } /** - * @param null|string $par + * @param null|string $subpage * @throws \MWException */ - public function execute( $par ) { + public function execute( $subpage ) { $this->setHeaders(); $this->checkPermissions(); - $output = $this->getOutput(); + $paramProvider = new ParameterProvider( $subpage ); $html = ' ' . \Message::newFromKey( 'simplebatchupload-buttonlabel' )->escaped() . ' - + '; + $output = $this->getOutput(); + $output->setPageTitle( $paramProvider->getSpecialPageTitle() ); $output->addHTML( $html ); $output->addModules( 'ext.SimpleBatchUpload' ); $output->addModuleStyles( [ 'ext.SimpleBatchUpload', 'ext.SimpleBatchUpload.jquery-file-upload' ] );