Page MenuHomePhabricator

Make CodeEditor available in modules that are subpages of a user page
Closed, DeclinedPublic

Description

CodeEditor supports lua on pages in the "Module" namespace.

however, many times you'd like to test your module on a subpage of your user page before publishing it, like you often do with templates and articles. The Extension TemplateSandbox already support testing modules with names from the form "User:myusername/Module:modulename", but when you try to edit a page in that name, you have to do it with the regular wiki editor which is very annoying.

That problem does not exist with js and css pages because they are being identified by their extension, not their namespace.

I think that code editor should be available in modules that are subpages of a user page.

Event Timeline

Aklapper renamed this task from CodeEditor on lua subpages to Make CodeEditor available in modules that are subpages of a user page.Mar 4 2019, 6:17 PM

Well, I checked it a bit and discovered that the decision whether a page is lua is in the scribunto extension in /includes/common/Hooks.php
in the function: contentHandlerDefaultModelFor

public static function contentHandlerDefaultModelFor( Title $title, &$model ) {
	if ( $model === 'sanitized-css' ) {
		// Let TemplateStyles override Scribunto
		return true;
	}
	if ( $title->getNamespace() == NS_MODULE && !Scribunto::isDocPage( $title ) ) {
		$model = CONTENT_MODEL_SCRIBUNTO;
		return true;
	}
	return true;
}

actually in my version of mediawiki(1.32.0) is

public static function contentHandlerDefaultModelFor( Title $title, &$model ) {
	if ( $title->getNamespace() == NS_MODULE && !Scribunto::isDocPage( $title ) ) {
		$model = CONTENT_MODEL_SCRIBUNTO;
		return false;
	}
	return true;
}

I tried to change it to:

	public static function contentHandlerDefaultModelFor( Title $title, &$model ) {
		if ( $title->getNamespace() == NS_MODULE && !Scribunto::isDocPage( $title ) ) {
			$model = CONTENT_MODEL_SCRIBUNTO;
			return false;
		}
		#allow user module
		if( $title->getNamespace() === NS_USER && Title::newFromText($title->getSubpageText())->getNamespace() === NS_MODULE) {
		    $model = CONTENT_MODEL_SCRIBUNTO;
		    return false;
		}
		return true;
	}

and it seems to work!

Well, I posted a possible solution.

Is it possible to enter it to the code of Scribunto Extension?
How?
(I'm a little new in phabricator)

@nyemini: Thanks for taking a look at the code!

You are very welcome to use developer access to submit the proposed code changes as a Git branch directly into Gerrit which makes it easier to review them quickly and provide feedback.
If you don't want to set up Git/Gerrit, you can also use the Gerrit Patch Uploader. Thanks again!

Change 501940 had a related patch set uploaded (by Gerrit Patch Uploader; owner: noamy):
[mediawiki/extensions/Scribunto@master] Make CodeEditor available in modules that are subpages of a user page (phabricator T217508). CodeEditor and SyntaxHighlight does not treat modules that are subpages of a user page as lua code. That makes creating and testing modules in a personal draft (using TemplateSandbox which supports user's modules) extremely annoying, because you have to use the regular wiki editor. my suggestion is to treat pages in the form "User:John/Module:lua" as CONTENT_MODEL_SCRIBUNTO.

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

I sent a patch ( https://gerrit.wikimedia.org/r/#/c/mediawiki/extensions/Scribunto/+/501940/ )
using Gerrit Patch Uploader (wikitech account creating is currently disabled).

Ages ago I wrote editorContent which might help for the time being.

  • I do not know whether it still works, please complain if not.

The nasty thing: While JS and CSS can be recognized by so-called extension, and will get a content model by system, lua code in user pages has no content model nor .lua termination and is difficult to identify.

But you can't run modules stored in the User namespace, can you? What's the point of drafting code if you can't test it? Just save them under Module:Sandbox/USERNAME/. This strikes me as a solution in search of a problem.

You can actually use user:[user]/Module:foo to test Modules while developing them using the https://en.m.wikipedia.org/wiki/Special:TemplateSandbox

But you can't use {{#invoke:...}}, right? In any case, if you really want to store modules in the User namespace, you can run:

if (mw.config.get('wgNamespaceNumber') === 2 &&
	['edit', 'submit'].includes(mw.config.get('wgAction')) &&
	mw.config.get('wgTitle').includes('/' + mw.config.get('wgFormattedNamespaces')[828] + ':') &&
	!mw.config.get('wgTitle').endsWith('/doc')
) {
	mw.config.set('wgCodeEditorCurrentLanguage', 'lua');
	mw.loader.load('ext.codeEditor');
}

in your JS to use the code editor for them. But I would find it overkill if it were turned on by default for everybody.

Legoktm subscribed.

CodeEditor is loaded for Lua pages that use a Scribunto content model (see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/Scribunto/+/refs/heads/master/includes/Hooks.php#260). If it's not using the Scribunto content model, then it's not a Lua module and shouldn't have CodeEditor enabled.

Change 501940 had a related patch set uploaded (by Legoktm; author: Noamy):

[mediawiki/extensions/Scribunto@master] Make CodeEditor available in modules that are subpages of a user page

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

Change 501940 abandoned by Legoktm:

[mediawiki/extensions/Scribunto@master] Make CodeEditor available in modules that are subpages of a user page

Reason:

Task has been declined.

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