Skip to content

Commit

Permalink
Read upload parameters from MediaWiki:Simplebatchupload-parameters
Browse files Browse the repository at this point in the history
Parameters are:
* Name of template to be stored as text on initial upload
* Upload comment
* Title line of the Special:BatchUpload page

Each line of MediaWiki:Simplebatchupload-parameters is considered as one set of
parameters. Parameters should be separated by pipes (|). The line to be used is
selected by appending the name of the template to be stored as text on initial
upload as the subpage to the URL of the Special:BatchUpload page.

Example
-------

Consider the parameter line
`Pics | These pics were uploaded using [[mw:Extension:SimpleBatchUpload{{!}}SimpleBatchUpload]] | Upload some pics!`

* This can be selected by going to `Special:BatchUpload/Pics`.
* The title of this page will be `Upload some pics!`.
* The comment for the upload will be `These pics were uploaded using [[mw:Extension:SimpleBatchUpload{{!}}SimpleBatchUpload]]`.
* If a file with that name is uploaded for the first time it will have `{{Pics}}` as wikitext.
  • Loading branch information
s7eph4n committed Oct 18, 2016
1 parent 530e90b commit d52c01f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 5 deletions.
4 changes: 3 additions & 1 deletion res/ext.SimpleBatchUpload.js
Expand Up @@ -40,6 +40,7 @@

add: function ( e, data ) {

var that = this;
data.id = Date.now();

var status = $('<li>')
Expand All @@ -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
};

Expand Down
113 changes: 113 additions & 0 deletions src/ParameterProvider.php
@@ -0,0 +1,113 @@
<?php
/**
* File containing the ParameterProvider class
*
* @copyright (C) 2016, Stephan Gambke
* @license GNU General Public License, version 2 (or any later version)
*
* This software 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 software 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, see <http://www.gnu.org/licenses/>.
*
* @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 ) );
}

}
14 changes: 10 additions & 4 deletions src/SpecialBatchUpload.php
Expand Up @@ -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 = '<span id="fileupload-dropzone" class="fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>' . \Message::newFromKey( 'simplebatchupload-buttonlabel' )->escaped() . '</span>
<!-- The file input field used as target for the file upload widget -->
<input id="fileupload" type="file" name="file" multiple data-url="' . wfScript( 'api' ) . '">
<input id="fileupload" type="file" name="file" multiple
data-url="' . wfScript( 'api' ) . '"
data-comment="' . $paramProvider->getEscapedUploadComment() .'"
data-text="' . $paramProvider->getEscapedUploadPageText() . '"
>
</span><ul id="fileupload-results"></ul>';

$output = $this->getOutput();
$output->setPageTitle( $paramProvider->getSpecialPageTitle() );
$output->addHTML( $html );
$output->addModules( 'ext.SimpleBatchUpload' );
$output->addModuleStyles( [ 'ext.SimpleBatchUpload', 'ext.SimpleBatchUpload.jquery-file-upload' ] );
Expand Down

0 comments on commit d52c01f

Please sign in to comment.