jenkins-bot has submitted this change and it was merged.
Change subject: re-factor save metadata mapping
......................................................................
re-factor save metadata mapping
Bryan Davis and Gergő Tisza mentioned concern about the special page handler
AjaxHandler
use of exit() and set_error_handler(), which is used to save the metadata
mapping created by the user.
after reviewing the code and the possibility of extending the API instead i was
able to instead
make use of the existing API action=edit call to achieve what is needed; the
saving of the
metadata mapping created by the user. this change-set addresses those issues.
Change-Id: I56b661cb58b85eaac7571100e25ab6c47d1cca0c
---
M includes/Adapters/Php/MappingPhpAdapter.php
M includes/Config.php
M includes/Forms/MetadataMappingForm.php
D includes/Handlers/Ajax/AjaxHandler.php
D includes/Handlers/Ajax/MetadataMappingSaveHandler.php
M includes/Specials/SpecialGWToolset.php
M resources/js/ext.gwtoolset.js
7 files changed, 77 insertions(+), 224 deletions(-)
Approvals:
BryanDavis: Looks good to me, but someone else must approve
Gergő Tisza: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/Adapters/Php/MappingPhpAdapter.php
b/includes/Adapters/Php/MappingPhpAdapter.php
index f6629bd..ad9e690 100644
--- a/includes/Adapters/Php/MappingPhpAdapter.php
+++ b/includes/Adapters/Php/MappingPhpAdapter.php
@@ -8,76 +8,15 @@
*/
namespace GWToolset\Adapters\Php;
-use ContentHandler,
- GWToolset\Adapters\DataAdapterInterface,
- GWToolset\Config,
+use GWToolset\Adapters\DataAdapterInterface,
GWToolset\GWTException,
- GWToolset\Helpers\FileChecks,
- GWToolset\Helpers\WikiPages,
- MWException,
- Php\Filter,
Revision,
Title,
WikiPage;
class MappingPhpAdapter implements DataAdapterInterface {
- /**
- * @param {array} $options
- * @throws {MWException}
- * @return {Status}
- */
public function create( array $options = array() ) {
- $result = false;
-
- if ( empty( $options['mapping-json'] ) ) {
- throw new MWException(
- wfMessage( 'gwtoolset-developer-issue' )
- ->params( wfMessage(
'gwtoolset-no-mapping-json' )->parse() )
- ->parse()
- );
- }
-
- if ( empty( $options['mapping-name'] ) ) {
- throw new MWException(
- wfMessage( 'gwtoolset-developer-issue' )
- ->params( wfMessage(
'gwtoolset-no-mapping' )->parse() )
- ->parse()
- );
- }
-
- if ( empty( $options['user'] ) ) {
- throw new MWException(
- wfMessage( 'gwtoolset-developer-issue' )
- ->params( wfMessage(
'gwtoolset-no-user' )->escaped() )
- ->parse()
- );
- }
-
- // nb: cannot filter the json - might need to test it as valid
by converting it back and
- // forth with json_decode/encode
- $options['text'] = $options['mapping-json'];
- $options['mapping-user-name'] = $options['user']->getName();
-
- $options['summary'] =
- wfMessage( 'gwtoolset-create-mapping' )
- ->params( Config::$name,
$options['mapping-user-name'] )
- ->escaped();
-
- $options['title'] = \GWToolset\getTitle(
- \GWToolset\stripIllegalTitleChars(
- Config::$metadata_mapping_subpage . '/' .
- $options['mapping-user-name'] . '/' .
- $options['mapping-name'] . '.json',
- array( 'allow-subpage' => true )
- ),
- Config::$metadata_namespace,
- array( 'must-be-known' => false )
- );
-
- $result = $this->saveMapping( $options );
-
- return $result;
}
public function delete( array $options = array() ) {
@@ -109,37 +48,6 @@
// need to remove line breaks from the mapping
otherwise the json_decode will error out
$result = str_replace( PHP_EOL, '', $result );
}
-
- return $result;
- }
-
- /**
- * attempts to save the mapping to the wiki as content
- *
- * @todo does ContentHandler filter $options['text']
- * @todo does ContentHandler filter $options['summary']
- * @todo figure out issue with the db ErrorException
- * Transaction idle or pre-commit callbacks still pending.
- * triggered by $db->__destruct because there is a mTrxIdleCallbacks
waiting
- * not sure why though
- * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=47375
- *
- * @param {array} $options
- * @return {Status}
- */
- protected function saveMapping( array &$options ) {
- $result = false;
-
- $Mapping_Content = ContentHandler::makeContent(
$options['text'], $options['title'] );
- $Mapping_Page = new WikiPage( $options['title'] );
-
- $result = $Mapping_Page->doEditContent(
- $Mapping_Content,
- $options['summary'],
- 0,
- false,
- $options['user']
- );
return $result;
}
diff --git a/includes/Config.php b/includes/Config.php
index a6d70a7..dee23a0 100644
--- a/includes/Config.php
+++ b/includes/Config.php
@@ -64,9 +64,6 @@
'GWToolset\Forms\MetadataMappingForm' =>
'/includes/Forms/MetadataMappingForm.php',
'GWToolset\Forms\PreviewForm' =>
'/includes/Forms/PreviewForm.php',
- 'GWToolset\Handlers\Ajax\AjaxHandler' =>
'/includes/Handlers/Ajax/AjaxHandler.php',
- 'GWToolset\Handlers\Ajax\MetadataMappingSaveHandler' =>
'/includes/Handlers/Ajax/MetadataMappingSaveHandler.php',
-
'GWToolset\Handlers\Forms\FormHandler' =>
'/includes/Handlers/Forms/FormHandler.php',
'GWToolset\Handlers\Forms\MetadataDetectHandler' =>
'/includes/Handlers/Forms/MetadataDetectHandler.php',
'GWToolset\Handlers\Forms\MetadataMappingHandler' =>
'/includes/Handlers/Forms/MetadataMappingHandler.php',
@@ -240,16 +237,17 @@
'resources/css/ext.gwtoolset.css'
),
'messages' => array(
+ 'gwtoolset-back-text-link',
+ 'gwtoolset-cancel',
+ 'gwtoolset-create-mapping',
'gwtoolset-developer-issue',
'gwtoolset-loading',
+ 'gwtoolset-save',
'gwtoolset-save-mapping',
'gwtoolset-save-mapping-name',
'gwtoolset-save-mapping-failed',
'gwtoolset-save-mapping-succeeded',
- 'gwtoolset-step-2-heading',
- 'gwtoolset-back-text-link',
- 'gwtoolset-save',
- 'gwtoolset-cancel'
+ 'gwtoolset-step-2-heading'
),
'dependencies' => array(
'jquery.json',
diff --git a/includes/Forms/MetadataMappingForm.php
b/includes/Forms/MetadataMappingForm.php
index 65a3ba8..5c8f152 100644
--- a/includes/Forms/MetadataMappingForm.php
+++ b/includes/Forms/MetadataMappingForm.php
@@ -217,6 +217,27 @@
'input',
array(
'type' => 'hidden',
+ 'id' => 'metadata-namespace',
+ 'name' => 'metadata-namespace',
+ 'value' => Filter::evaluate(
\GWToolset\getNamespaceName( Config::$metadata_namespace ) )
+ )
+ ) .
+
+ Html::rawElement(
+ 'input',
+ array(
+ 'type' => 'hidden',
+ 'id' => 'metadata-mapping-subpage',
+ 'name' => 'metadata-mapping-subpage',
+ 'value' => Filter::evaluate(
Config::$metadata_mapping_subpage )
+ )
+ ) .
+
+
+ Html::rawElement(
+ 'input',
+ array(
+ 'type' => 'hidden',
'id' => 'wpEditToken',
'name' => 'wpEditToken',
'value' =>
$Handler->User->getEditToken()
diff --git a/includes/Handlers/Ajax/AjaxHandler.php
b/includes/Handlers/Ajax/AjaxHandler.php
deleted file mode 100644
index fb1d4a9..0000000
--- a/includes/Handlers/Ajax/AjaxHandler.php
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-/**
- * GWToolset
- *
- * @file
- * @ingroup Extensions
- * @license GNU General Public License 3.0 http://www.gnu.org/licenses/gpl.html
- */
-
-namespace GWToolset\Handlers\Ajax;
-use GWToolset\Handlers\SpecialPageHandler,
- GWToolset\Helpers\WikiChecks,
- Status;
-
-abstract class AjaxHandler extends SpecialPageHandler {
-
- /**
- * gets an html form.
- * not needed in this class
- */
- public function getHtmlForm() {
- }
-
- /**
- * entry point
- * a control method that acts as an entry point for the
- * SpecialPageHandler and handles execution of the class methods
- */
- public function execute() {
- $result = WikiChecks::doesEditTokenMatch( $this->SpecialPage );
-
- if ( $result->ok ) {
- $result = $this->processRequest();
- }
-
- set_error_handler( array( $this, 'swallowErrors' ) );
- header( 'Content-Type: application/json; charset=utf-8' );
- echo json_encode( $result );
- exit();
- }
-
- /**
- * intended to swallow notice and warnings when display errors is set
to true
- * this should not be the case in a production environment
- *
- * @see http://php.net/manual/en/function.set-error-handler.php
- */
- function swallowErrors() {
- }
-}
diff --git a/includes/Handlers/Ajax/MetadataMappingSaveHandler.php
b/includes/Handlers/Ajax/MetadataMappingSaveHandler.php
deleted file mode 100644
index 233d63c..0000000
--- a/includes/Handlers/Ajax/MetadataMappingSaveHandler.php
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php
-/**
- * GWToolset
- *
- * @file
- * @ingroup Extensions
- * @license GNU General Public License 3.0 http://www.gnu.org/licenses/gpl.html
- */
-namespace GWToolset\Handlers\Ajax;
-use GWToolset\Adapters\Php\MappingPhpAdapter,
- GWToolset\Models\Mapping;
-
-class MetadataMappingSaveHandler extends AjaxHandler {
-
- /**
- * @var {Mapping}
- */
- protected $_Mapping;
-
- /**
- * a control method that processes a SpecialPage request
- * and returns a response, typically an html form
- *
- * @return {Status}
- */
- protected function processRequest() {
- $mapping_result = false;
-
- $this->_Mapping = new Mapping( new MappingPhpAdapter() );
- $this->_Mapping->mapping_array =
$this->SpecialPage->getRequest()->getArray( 'metadata-mappings' );
-
- // create takes care of new and existing pages
- $mapping_result = $this->_Mapping->create(
- array(
- 'created' => date( 'Y-m-d H:i:s' ),
- 'mapping-json' => json_encode(
$this->_Mapping->mapping_array ),
- 'mapping-name' =>
$this->SpecialPage->getRequest()->getVal( 'mapping-name-to-use' ),
- 'mediawiki-template-name' =>
$this->SpecialPage->getRequest()->getVal( 'mediawiki-template-name' ),
- 'user' => $this->SpecialPage->getUser()
- )
- );
-
- return $mapping_result;
- }
-}
diff --git a/includes/Specials/SpecialGWToolset.php
b/includes/Specials/SpecialGWToolset.php
index 937b613..d6ad479 100644
--- a/includes/Specials/SpecialGWToolset.php
+++ b/includes/Specials/SpecialGWToolset.php
@@ -46,10 +46,6 @@
'handler' =>
'\GWToolset\Handlers\Forms\MetadataMappingHandler',
'form' => '\GWToolset\Forms\MetadataMappingForm'
),
- 'metadata-mapping-save' => array(
- 'allow-get' => false,
- 'handler' =>
'\GWToolset\Handlers\Ajax\MetadataMappingSaveHandler'
- ),
'metadata-preview' => array(
'allow-get' => false,
'handler' =>
'\GWToolset\Handlers\Forms\MetadataMappingHandler',
diff --git a/resources/js/ext.gwtoolset.js b/resources/js/ext.gwtoolset.js
index 8c7a938..9b49453 100644
--- a/resources/js/ext.gwtoolset.js
+++ b/resources/js/ext.gwtoolset.js
@@ -293,20 +293,32 @@
},
handleAjaxError: function () {
- gwtoolset.openDialog( { msg: mw.message(
'gwtoolset-developer-issue' ).text() } );
+ gwtoolset.openDialog( {
+ msg: mw.message( 'gwtoolset-developer-issue',
'' ).text()
+ } );
+
mw.log( arguments );
},
/**
* @param {Object} data
- * @param {string} textStatus
- * @param {Object} jqXHR
*/
- handleAjaxSuccess: function ( data, textStatus, jqXHR ) {
- if ( data.ok !== true || !textStatus || !jqXHR ) {
- gwtoolset.openDialog( { msg: mw.message(
'gwtoolset-save-mapping-failed' ).text() } );
+ handleAjaxSuccess: function ( data ) {
+ if ( !data.edit || !data.edit.result ||
data.edit.result !== 'Success' ) {
+ if ( data.error && data.error.info ) {
+ gwtoolset.openDialog( {
+ msg: mw.message(
'gwtoolset-save-mapping-failed' ).text() +
+ ' ( ' + data.error.info
+ ' )'
+ } );
+ } else {
+ gwtoolset.openDialog( {
+ msg: mw.message(
'gwtoolset-save-mapping-failed' ).text()
+ } );
+ }
} else {
- gwtoolset.openDialog( { msg: mw.message(
'gwtoolset-save-mapping-succeeded' ).text() } );
+ gwtoolset.openDialog( {
+ msg: mw.message(
'gwtoolset-save-mapping-succeeded' ).text()
+ } );
}
},
@@ -519,10 +531,24 @@
* @param {Event} evt
*/
saveMapping: function ( evt ) {
- var mappingNameToUse = $( '#mapping-name-to-use'
).val(),
- mediawikiTemplateName = $(
'#mediawiki-template-name' ).val(),
- wpEditToken = mw.user.tokens.get( 'editToken' ),
- metadataMappings =
gwtoolset.getFormSectionValues( gwtoolset.$templateTableTbody, 'select' );
+ var Api = new mw.Api(),
+ mappingNameToUse = $( '#mapping-name-to-use'
).val(),
+ metadataMappings = gwtoolset
+ .getFormSectionValues(
+ gwtoolset.$templateTableTbody,
+ 'select'
+ ),
+ summary = mw.message(
+ 'gwtoolset-create-mapping',
+ 'GWToolset',
+ mw.user.getName()
+ )
+ .text(),
+ title = $( '#metadata-namespace' ).val() +
+ $( '#metadata-mapping-subpage' ).val()
+ '/' +
+ mw.user.getName() + '/' +
+ mappingNameToUse + '.json',
+ wpEditToken = mw.user.tokens.get( 'editToken' );
if ( evt ) {
gwtoolset.$dialog.dialog( 'close' );
@@ -533,21 +559,20 @@
return;
}
- $.ajax( {
- type: 'POST',
- url: mw.util.wikiGetlink( 'Special:GWToolset' ),
- data: {
- 'gwtoolset-form':
'metadata-mapping-save',
- 'mapping-name-to-use': mappingNameToUse,
- 'metadata-mappings': metadataMappings,
- 'mediawiki-template-name':
mediawikiTemplateName,
- 'wpEditToken': wpEditToken
+ Api.post(
+ {
+ action: 'edit',
+ summary: summary,
+ text: $.toJSON( metadataMappings ),
+ title: title,
+ token: wpEditToken
},
- error: gwtoolset.handleAjaxError,
- success: gwtoolset.handleAjaxSuccess,
- complete: gwtoolset.handleAjaxComplete,
- timeout: 5000
- } );
+ {
+ error: gwtoolset.handleAjaxError,
+ success: gwtoolset.handleAjaxSuccess,
+ timeout: 5000
+ }
+ );
}
};
--
To view, visit https://gerrit.wikimedia.org/r/94128
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I56b661cb58b85eaac7571100e25ab6c47d1cca0c
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/GWToolset
Gerrit-Branch: master
Gerrit-Owner: Dan-nl <[email protected]>
Gerrit-Reviewer: BryanDavis <[email protected]>
Gerrit-Reviewer: Gergő Tisza <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits