Mvolz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/154962

Change subject: WILL NOT WORK UNTIL DEPENDENCIES ARE RESOLVED
......................................................................

WILL NOT WORK UNTIL DEPENDENCIES ARE RESOLVED

Uses as-of-yet uncommitted changes to Extension:
TemplateData and Extension:VisualEditor to insert
citations using template data.

Also a giant mess.

Change-Id: I3f8abf4cdfadb363df1ac83d9cbebd81d565f487
---
M modules/ve.ui.CiteFromURLDialog.js
1 file changed, 53 insertions(+), 277 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Citoid 
refs/changes/62/154962/1

diff --git a/modules/ve.ui.CiteFromURLDialog.js 
b/modules/ve.ui.CiteFromURLDialog.js
index 123b1db..2b6f6b1 100644
--- a/modules/ve.ui.CiteFromURLDialog.js
+++ b/modules/ve.ui.CiteFromURLDialog.js
@@ -25,7 +25,8 @@
         */
        ve.ui.CiteFromURLDialog.prototype.insertTemplate = function ( 
searchResults ) {
 
-               var transclusion, template, templateName, fieldValue, 
partPromise,
+               var transclusion, template, spec, map, maps, applicationType, 
templateName,
+                       fieldValue, templateField, partPromise, i, j,
                        citation = $.parseJSON( JSON.stringify( searchResults ) 
)[0], //uses the first citation result for the time being
                        templateTypeMap = $.parseJSON( mw.message( 
'citoid-template-type-map.json' ).plain() ),
                        citationParams = {
@@ -184,24 +185,58 @@
                        transclusion = new ve.dm.MWTransclusionModel();
 
                        templateName = templateTypeMap[citation.itemType];
+
                        // Create a template
                        template = ve.dm.MWTemplateModel.newFromName( 
transclusion, templateName );
 
-                       // Add a template to a transclusion
+                       //Promise for template being added to the transclusion
                        partPromise = transclusion.addPart( template );
-                       // Add parameters to the template
-                       //template.addParameter( new ve.dm.MWTemplateParameter( 
template, 'author', 'Randall Munroe' ) );
-                       //template.addParameter( new ve.dm.MWTemplateParameter( 
template, 'url', 'http://www.xkcd.com' ) );
 
-                       $.each( templateParamMap[templateName], function ( 
templateField, citoidField ) {
-                               //adds empty string to selected important 
parameters
-                               //so that template will reopen with those 
fields for manual entry
-                               fieldValue = citation[citoidField] !== 
undefined ? citation[citoidField] : '';
+                       //use template definitions above
+                       // $.each( templateParamMap[templateName], function ( 
templateField, citoidField ) {
+                       //      //adds empty string to selected important 
parameters
+                       //      //so that template will reopen with those 
fields for manual entry
+                       //      fieldValue = citation[citoidField] !== 
undefined ? citation[citoidField] : '';
 
-                               template.addParameter ( new 
ve.dm.MWParameterModel( template, templateField, fieldValue ) );
-                       } );
+                       //      template.addParameter ( new 
ve.dm.MWParameterModel( template, templateField, fieldValue ) );
+                       // });
+
+                       //spec = template.getSpec();
+                       //console.log(template.spec);
+                       //console.log(template.spec.maps);
+                       //
+                       applicationType = this.applicationType;
 
                        partPromise.done(function () {
+                               spec = template.getSpec();
+                               maps = spec.getMaps();
+                               map = maps[applicationType];
+                               for (var citoidField in map) {
+                                       templateField = map[citoidField];
+                                       //construct parameters
+                                       if ( typeof templateField === 'string' 
) {
+                                               if ( citation[citoidField] !== 
undefined ) {
+                                                       template.addParameter ( 
new ve.dm.MWParameterModel( template, templateField, citation[citoidField] ) );
+                                               }
+                                       }else if ( citation[citoidField] 
instanceof Array ) {
+                                               //if the citoidfield contains 
an array
+                                               //iterate through the array
+                                               for ( i = 0; i < 
citation[citoidField].length; i++ ) {
+                                                       //if element i of the 
citoidfield is a string
+                                                       //this has never been 
tested... test this
+                                                       if ( typeof 
citation[citoidField][i] === 'string' && templateField[i] !== undefined) {
+                                                               
template.addParameter ( new ve.dm.MWParameterModel( template, templateField[i], 
citation[citoidField][i] ) );
+                                                       //uh oh! it's a 2d 
array. let's go deeper
+                                                       } else if 
(citation[citoidField][i] instanceof Array) {
+                                                               for ( j = 0; j 
< citation[citoidField][i].length; j++ ) {
+                                                                       if 
(typeof citation[citoidField][i][j] === 'string' && templateField[i][j] !== 
undefined) {
+                                                                               
template.addParameter ( new ve.dm.MWParameterModel( template, 
templateField[i][j], citation[citoidField][i][j]) );
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
                                transclusion.insertTransclusionNode( fragment );
                                // HACK: Scorch the earth - this is only needed 
because without it, the reference list won't
                                // re-render properly, and can be removed once 
someone fixes that
@@ -219,239 +254,14 @@
                }
        };
 
-       /**When adding a template part, the template spec is automatically 
loaded from the TemplateData API, and cached for future use, this is the reason 
for loading and adding processes being asynchronous
-Adding parameters is synchronous, as the spec is already present
-        * Returns full template given search results
-        * @param  {Object} searchResults json response object
-        * @return {Object}               filled in template
-        */
-       ve.ui.CiteFromURLDialog.prototype.getPlainObject = function ( 
searchResults ) {
-
-               var content, plainObject, d, templateHref, templateName,
-                       citation = jQuery.parseJSON( JSON.stringify( 
searchResults ) )[0], //uses the first citation result for the time being
-
-                       //Parameter map for Template:Citation on en-wiki
-                       //In the format citation-template-field:citoid-field
-                       citationParams = {
-                               'first1': 'author1-first',
-                               'last1': 'author1-last',
-                               'first2': 'author2-first',
-                               'last2': 'author2-last',
-                               'first3': 'author3-first',
-                               'last3': 'author3-last',
-                               'first4': 'author4-first',
-                               'last4': 'author4-last',
-                       //      'accessdate': 'accessDate',
-                               'title': 'title',
-                               'url': 'url',
-                               'publisher': 'publisher',
-                               //a large number of Zotero types have the field 
publicationTitle
-                               //however, in setting journal to 
publicationTitle, the citation
-                               //will be formatted as a journal article, which 
may not always be
-                               //desirable.
-                               'journal': 'publicationTitle',
-                       //      'newspaper': 'publicationTitle',
-                               'date': 'date',
-                               'location': 'place',
-                               'issn': 'ISSN',
-                               'isbn': 'ISBN',
-                               'pages': 'pages',
-                               'volume': 'volume',
-                               'series': 'series',
-                               'issue': 'issue',
-                               'doi': 'DOI'
-                       },
-
-                       webParams = {
-                               'first1': 'author1-first',
-                               'last1': 'author1-last',
-                               'first2': 'author2-first',
-                               'last2': 'author2-last',
-                               'first3': 'author3-first',
-                               'last3': 'author3-last',
-                               'first4': 'author4-first',
-                               'last4': 'author4-last',
-                       //      'accessdate': 'accessDate',
-                               'title': 'title',
-                               'url': 'url',
-                               'date': 'date',
-                               'publisher': 'publisher',
-                               'website': 'publicationTitle'
-                       },
-
-                       newsParams = {
-                               'first1': 'author1-first',
-                               'last1': 'author1-last',
-                               'first2': 'author2-first',
-                               'last2': 'author2-last',
-                               'first3': 'author3-first',
-                               'last3': 'author3-last',
-                               'first4': 'author4-first',
-                               'last4': 'author4-last',
-                       //      'accessdate': 'accessDate',
-                               'title': 'title',
-                               'url': 'url',
-                               'publisher': 'publisher',
-                               'newspaper': 'publicationTitle',
-                               'date': 'date',
-                               'location': 'place',
-                               'issn': 'ISSN',
-                               'isbn': 'ISBN',
-                               'pages': 'pages',
-                               'volume': 'volume',
-                               'series': 'series',
-                               'issue': 'issue',
-                               'doi': 'DOI'
-                       },
-
-                       bookParams = {
-                               'first1': 'author1-first',
-                               'last1': 'author1-last',
-                               'first2': 'author2-first',
-                               'last2': 'author2-last',
-                               'first3': 'author3-first',
-                               'last3': 'author3-last',
-                               'first4': 'author4-first',
-                               'last4': 'author4-last',
-                       //      'accessdate': 'accessDate',
-                               'title': 'title',
-                               'url': 'url',
-                               'publisher': 'publisher',
-                               'journal': 'publicationTitle',
-                               'date': 'date',
-                               'location': 'place',
-                               'issn': 'ISSN',
-                               'isbn': 'ISBN',
-                               'pages': 'pages',
-                               'volume': 'volume',
-                               'series': 'series',
-                               'issue': 'issue',
-                               'doi': 'DOI'
-                       },
-
-                       journalParams = {
-                               'first1': 'author1-first',
-                               'last1': 'author1-last',
-                               'first2': 'author2-first',
-                               'last2': 'author2-last',
-                               'first3': 'author3-first',
-                               'last3': 'author3-last',
-                               'first4': 'author4-first',
-                               'last4': 'author4-last',
-                       //      'accessdate': 'accessDate',
-                               'title': 'title',
-                               'url': 'url',
-                               'publisher': 'publisher',
-                               'journal': 'publicationTitle',
-                               'date': 'date',
-                               'location': 'place',
-                               'issn': 'ISSN',
-                               'isbn': 'ISBN',
-                               'pages': 'pages',
-                               'volume': 'volume',
-                               'series': 'series',
-                               'issue': 'issue',
-                               'doi': 'DOI'
-                       },
-
-                       //format 'template name':parameter obj name
-                       templateParamMap = {
-                               'Citation': citationParams,
-                               'Cite web': webParams,
-                               'Cite news': newsParams,
-                               'Cite journal': journalParams,
-                               'Cite book': bookParams
-                       },
-
-                       //This will contain the correct template with the 
fields filled out
-                       paramObj = {};
-
-               try {
-                       //Must use mw.message to avoid JSON being parsed as 
Wikitext
-                       templateTypeMap = jQuery.parseJSON( mw.message( 
'citoid-template-type-map.json' ).plain() );
-               } catch ( e ) { 
-                       templateTypeMap = {
-                               book: 'Cite book',
-                               bookSection: 'Cite book',
-                               journalArticle: 'Cite journal',
-                               magazineArticle: 'Cite news',
-                               newspaperArticle: 'Cite news',
-                               thesis: 'Cite journal',
-                               letter: 'Citation',
-                               manuscript: 'Cite book',
-                               interview: 'Citation',
-                               film: 'Citation',
-                               artwork: 'Citation',
-                               webpage: 'Cite web',
-                               report: 'Cite journal',
-                               bill: 'Citation',
-                               hearing: 'Citation',
-                               patent: 'Citation',
-                               statute: 'Citation',
-                               email: 'Cite web',
-                               map: 'Citation',
-                               blogPost: 'Cite web',
-                               instantMessage: 'Citation',
-                               forumPost: 'Cite web',
-                               audioRecording: 'Citation',
-                               presentation: 'Cite journal',
-                               videoRecording: 'Citation',
-                               tvBroadcast: 'Citation',
-                               radioBroadcast: 'Citation',
-                               podcast: 'Citation',
-                               computerProgram: 'Citation',
-                               conferencePaper: 'Cite journal',
-                               'document': 'Citation',
-                               encyclopediaArticle: 'Cite journal',
-                               dictionaryEntry: 'Cite journal'
-                       };
-               }
-
-               templateName = templateTypeMap[citation.itemType];
-
-               templateHref = 'Template:' + templateName;
-
-               $.each( templateParamMap[templateName], function ( key, value ) 
{
-                       var objString = citation[value] !== undefined ? 
citation[value] : '';
-                       paramObj[key] = { 'wt': objString };
-               } );
-
-               d = new Date();
-               paramObj.accessdate = { 'wt': d.toDateString() };
-
-               plainObject = { //before paren put get plain object
-                       'parts': [ {
-
-                               'template': {
-                                       'target': {
-                                               'href': templateHref,
-                                               'wt': templateName.toLowerCase()
-                                       },
-                                       'params': paramObj
-                               }
-                       } ]
-               };
-
-               content = [
-                       {
-                               'type': 'mwTransclusionInline',
-                               'attributes': {
-                                       'mw': plainObject
-                               }
-                       },
-                       { 'type': '/mwTransclusionInline' }
-               ];
-
-               //this.getFragment().insertContent( content );
-               return content;
-       };
-
        /**
         * [initialize description]
         * @return {[type]} [description]
         */
        ve.ui.CiteFromURLDialog.prototype.initialize = function () {
                
ve.ui.CiteFromURLDialog.super.super.super.prototype.initialize.call( this );
+
+               this.applicationType = 'application/x-citoid+json';
 
                //not actually using this//hack for inheriting from 
mwtemplatedialog
                this.bookletLayout = new OO.ui.BookletLayout(
@@ -483,7 +293,7 @@
                panel.$element.append( inputsFieldset.$element );
                this.$body.append( panel.$element );
 
-               this.modules= ['ext.visualEditor.data'];
+               this.modules = ['ext.visualEditor.data'];
                this.modulesLoaded = $.Deferred();
 
        };
@@ -503,53 +313,19 @@
 
                                citoidConfig = mw.config.get('wgCitoidConfig');
                                citoidService = citoidConfig.citoidServiceUrl;
-
                                $.ajax( {
                                        beforeSend: function (request) {
                                                
request.setRequestHeader('Content-Type', 'application/json');
                                        },
                                        url: citoidService,
                                        type: 'POST',
-                                       data: JSON.stringify( { url: 
this.searchInput.getValue() } ),
+                                       data: JSON.stringify( {
+                                               url: 
this.searchInput.getValue(),
+                                               contentType: 
this.applicationType
+                                       } ),
                                        dataType: 'json',
                                        success: function ( result ) {
-                                               // that.close();
-
-                                               // var item, fragment,
-                                               //      surfaceModel = 
that.getFragment().getSurface(),
-                                               //      doc = 
surfaceModel.getDocument(),
-                                               //      internalList = 
doc.getInternalList();
-
-                                               // //sets up referencemodel 
with blank stuff
-                                               // if ( !that.referenceModel ) {
-                                               //      // Collapse returns a 
new fragment, so update this.fragment
-                                               //      that.fragment = 
that.getFragment().collapseRangeToEnd();
-                                               //      that.referenceModel = 
new ve.dm.MWReferenceModel();
-                                               //      
that.referenceModel.insertInternalItem( surfaceModel );
-                                               //      
that.referenceModel.insertReferenceNode( that.getFragment() );
-                                               // }
-                                               // //gets bank stuff again
-                                               // item = 
that.referenceModel.findInternalItem( surfaceModel );
-                                               // if ( item ) {
-                                               //      fragment = 
that.getFragment().clone( item.getChildren()[0].getRange());
-                                               //      //actually inserts full 
transclusion model here!
-                                               //      
//fragment.insertContent(that.getPlainObject( result ) );
-                                               //      //using transclusion 
methods...
-                                               //      
that.insertTemplate(fragment, result);
-                                               // }
                                                that.insertTemplate( result );
-                                               // // HACK: Scorch the earth - 
this is only needed because without it, the reference list won't
-                                               // // re-render properly, and 
can be removed once someone fixes that
-                                               // 
that.referenceModel.setDocument(
-                                               //      doc.cloneFromRange(
-                                               //              
internalList.getItemNode( that.referenceModel.getListIndex() ).getRange()
-                                               //      )
-                                               // );
-
-                                               // 
that.referenceModel.updateInternalItem( surfaceModel );
-
-                                               // //hack- doesn't seem to be 
working in always
-                                               // that.popPending();
                                        },
                                        error: function ( XMLHttpRequest, 
textStatus, errorThrown) {
                                                that.popPending();

-- 
To view, visit https://gerrit.wikimedia.org/r/154962
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3f8abf4cdfadb363df1ac83d9cbebd81d565f487
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Citoid
Gerrit-Branch: master
Gerrit-Owner: Mvolz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to