Subramanya Sastry has submitted this change and it was merged.

Change subject: Move the alternate Title constructor
......................................................................


Move the alternate Title constructor

Moved the MWParserEnvironment:titleFromPrefixedText method to be
Title.fromPrefixedText, a static class method that constructs Title
objects from an environment and some prefixed text.

Change-Id: Ie4657ff0905e41c5988c866223de0bebdf1dcaa3
---
M js/lib/ext.core.LinkHandler.js
M js/lib/mediawiki.Title.js
M js/lib/mediawiki.parser.environment.js
3 files changed, 89 insertions(+), 25 deletions(-)

Approvals:
  Subramanya Sastry: Verified; Looks good to me, approved



diff --git a/js/lib/ext.core.LinkHandler.js b/js/lib/ext.core.LinkHandler.js
index fa759b2..a01b3be 100644
--- a/js/lib/ext.core.LinkHandler.js
+++ b/js/lib/ext.core.LinkHandler.js
@@ -8,6 +8,7 @@
 
 var PegTokenizer = require('./mediawiki.tokenizer.peg.js').PegTokenizer,
        WikitextConstants = 
require('./mediawiki.wikitext.constants.js').WikitextConstants,
+       Title = require( './mediawiki.Title.js' ).Title,
        Util = require('./mediawiki.Util.js').Util,
        // Why mess around? We already have a URL sanitizer.
        sanitizerLib = require( './ext.core.Sanitizer.js' ),
@@ -117,7 +118,7 @@
                hrefSrc = Util.lookupKV( token.attribs, 'href' ).vsrc,
                target = Util.lookup( attribs, 'href' ),
                href = Util.tokensToString( target ),
-               title = env.makeTitleFromPrefixedText( Util.decodeURI( href ) );
+               title = Title.fromPrefixedText( env, Util.decodeURI( href ) );
 
        if ( title.ns.isFile() ) {
                cb( this.renderFile( token, frame, cb, href, title) );
@@ -399,7 +400,7 @@
 
                if ( isImageLink ) {
                        if ( oHash.link !== undefined ) {
-                               linkTitle = env.makeTitleFromPrefixedText( 
oHash.link );
+                               linkTitle = Title.fromPrefixedText( env, 
oHash.link );
                        }
                        newAttribs.push( new KV('href', linkTitle.makeLink() ) )
                }
@@ -489,7 +490,7 @@
 WikiLinkHandler.prototype.getThumbPath = function ( key, width ) {
        var env = this.manager.env,
                // Make a relative link.
-               link = env.makeTitleFromPrefixedText( 'Special:FilePath' 
).makeLink();
+               link = Title.fromPrefixedText( env, 'Special:FilePath' 
).makeLink();
        // Simply let Special:FilePath redirect to the real thumb location
        return link + '/' + key + '?width=' + width;
 };
@@ -612,7 +613,7 @@
        var linkTitle = title;
        var isImageLink = ( oHash.link === undefined || oHash.link !== '' );
        if ( isImageLink && oHash.link !== undefined ) {
-               linkTitle = env.makeTitleFromPrefixedText( oHash.link );
+               linkTitle = Title.fromPrefixedText( env, oHash.link );
        }
 
        var thumbfile = title.key;
@@ -782,7 +783,7 @@
        var rdfaType = token.getAttribute('typeof'), magLinkRe = 
/\bmw:ExtLink\/(?:ISBN|RFC|PMID)\b/;
        if ( rdfaType && rdfaType.match( magLinkRe ) ) {
                if ( rdfaType.match( /\bmw:ExtLink\/ISBN/ ) ) {
-                       title = env.makeTitleFromPrefixedText( href );
+                       title = Title.fromPrefixedText( env, href );
                        newAttrs = [
                                new KV('href', title.makeLink()),
                                new KV('rel', rdfaType.match( magLinkRe )[0] )
diff --git a/js/lib/mediawiki.Title.js b/js/lib/mediawiki.Title.js
index 4591f05..7cffe4e 100644
--- a/js/lib/mediawiki.Title.js
+++ b/js/lib/mediawiki.Title.js
@@ -2,6 +2,17 @@
 
 var Util = require('./mediawiki.Util.js').Util;
 
+/**
+ * @class
+ *
+ * Represents a title in a wiki.
+ *
+ * @constructor
+ * @param {string} key The text of the title
+ * @param {number} ns The id of the namespace where the page is
+ * @param {string} nskey The text of the namespace name
+ * @param {MWParserEnvironment} env
+ */
 function Title ( key, ns, nskey, env ) {
        this.key = env.resolveTitle( key );
 
@@ -12,6 +23,40 @@
        this.env = env;
 }
 
+/**
+ * @method
+ * @static
+ *
+ * Take text, e.g. from a wikilink, and make a Title object from it.
+ *
+ * @param {MWParserEnvironment} env
+ * @param {string} text The prefixed text.
+ * @returns {Title}
+ */
+Title.fromPrefixedText = function ( env, text ) {
+       text = env.normalizeTitle( text );
+       var nsText = text.split( ':', 1 )[0];
+       if ( nsText && nsText !== text ) {
+               var _ns = new Namespace( 0, env );
+               var ns = _ns.namespaceIds[ nsText.toLowerCase().replace( ' ', 
'_' ) ];
+               //console.warn( JSON.stringify( [ nsText, ns ] ) );
+               if ( ns !== undefined ) {
+                       return new Title( text.substr( nsText.length + 1 ), ns, 
nsText, env );
+               } else {
+                       return new Title( text, 0, '', env );
+               }
+       } else {
+               return new Title( text, 0, '', env );
+       }
+};
+
+/**
+ * @method
+ *
+ * Make a full link out of a title.
+ *
+ * @returns {string}
+ */
 Title.prototype.makeLink = function () {
        // XXX: links always point to the canonical namespace name.
        if ( false && this.nskey ) {
@@ -28,6 +73,13 @@
        }
 };
 
+/**
+ * @method
+ *
+ * Get the text of the title, like you might see in a wikilink.
+ *
+ * @returns {string}
+ */
 Title.prototype.getPrefixedText = function () {
        // XXX: links always point to the canonical namespace name.
        if ( this.nskey ) {
@@ -42,7 +94,15 @@
        }
 };
 
-
+/**
+ * @class
+ *
+ * Represents a namespace, meant for use in the #Title class.
+ *
+ * @constructor
+ * @param {number} id The id of the namespace to represent.
+ * @param {MWParserEnvironment} env
+ */
 function Namespace( id, env ) {
        var ids = env.conf.wiki.namespaceIds;
        var names = env.conf.wiki.namespaceNames;
@@ -67,20 +127,40 @@
        };
 }
 
+/**
+ * @method
+ *
+ * Determine whether the namespace is the File namespace.
+ *
+ * @returns {boolean}
+ */
 Namespace.prototype.isFile = function ( ) {
        return this.id === this.canonicalNamespaces.file;
 };
+
+/**
+ * @method
+ *
+ * Determine whether the namespace is the Category namespace.
+ *
+ * @returns {boolean}
+ */
 Namespace.prototype.isCategory = function ( ) {
        return this.id === this.canonicalNamespaces.category;
 };
 
+/**
+ * @method
+ *
+ * Determine the default name of the namespace.
+ *
+ * @returns {string/undefined}
+ */
 Namespace.prototype.getDefaultName = function ( ) {
        return this.namespaceNames[this.id.toString()];
 };
-
 
 if (typeof module === "object") {
        module.exports.Title = Title;
        module.exports.Namespace = Namespace;
 }
-
diff --git a/js/lib/mediawiki.parser.environment.js 
b/js/lib/mediawiki.parser.environment.js
index 7f93c6d..cd25b68 100644
--- a/js/lib/mediawiki.parser.environment.js
+++ b/js/lib/mediawiki.parser.environment.js
@@ -244,23 +244,6 @@
        }
 };
 
-MWParserEnvironment.prototype.makeTitleFromPrefixedText = function ( text ) {
-               text = this.normalizeTitle( text );
-               var nsText = text.split( ':', 1 )[0];
-               if ( nsText && nsText !== text ) {
-                       var _ns = new Namespace( 0, this );
-                       var ns = _ns.namespaceIds[ 
nsText.toLowerCase().replace( ' ', '_' ) ];
-                       //console.warn( JSON.stringify( [ nsText, ns ] ) );
-                       if ( ns !== undefined ) {
-                               return new Title( text.substr( nsText.length + 
1 ), ns, nsText, this );
-                       } else {
-                               return new Title( text, 0, '', this );
-                       }
-               } else {
-                       return new Title( text, 0, '', this );
-               }
-};
-
 // XXX: move to Title!
 MWParserEnvironment.prototype.normalizeTitle = function( name, noUnderScores,
                preserveLeadingColon )

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie4657ff0905e41c5988c866223de0bebdf1dcaa3
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to