Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read upload parameters from MediaWiki:Simplebatchupload-parameters
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
Showing
3 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters