(bug 45845) Allow for docs at non-subpages

It has been claimed that some wiki might want to place module
documentation at a location other than as a subpage of the module, for
example under "Project:Module documentation/$1". It's possible to
support this, so we may as well.

This also involves renaming the "scribunto-doc-subpage-*" messages to
"scribunto-doc-page-*", since the interpretation of
scribunto-doc-subpage-name would be drastically changed.

Note that any wiki that has customized scribunto-doc-subpage-name will
need to re-customize scribunto-doc-page-name, the old value will not be
transferred.

Bug: 45845
Change-Id: Ic453561691e04b5250d219cc7d871c17e60b9912
This commit is contained in:
Brad Jorsch 2013-03-08 12:01:50 -05:00
parent 7617f25a8b
commit ded331ddc9
5 changed files with 44 additions and 35 deletions

View file

@ -22,11 +22,11 @@ $messages['en'] = array(
'scribunto-error-long' => 'Script errors:
$1',
'scribunto-doc-subpage-name' => 'doc',
'scribunto-doc-subpage-does-not-exist' => "''Documentation for this module may be created at [[$1]]''",
'scribunto-doc-subpage-show' => '{{$1}}
'scribunto-doc-page-name' => 'Module:$1/doc',
'scribunto-doc-page-does-not-exist' => "''Documentation for this module may be created at [[$1]]''",
'scribunto-doc-page-show' => '{{$1}}
<hr />',
'scribunto-doc-subpage-header' => "'''This is the documentation subpage for [[$1]]'''",
'scribunto-doc-page-header' => "'''This is the documentation page for [[$1]]'''",
'scribunto-console-intro' => '* The module exports are available as the variable "p", including unsaved modifications.
* Precede a line with "=" to evaluate it as an expression, or use print().
@ -83,10 +83,10 @@ $messages['qqq'] = array(
* $1 are the error details.',
'scribunto-error-long' => 'Error message. Parameters:
* $1 are the error details.',
'scribunto-doc-subpage-name' => 'Subpage name for module documentation.',
'scribunto-doc-subpage-does-not-exist' => 'Message displayed if the documentation subpage does not exist. $1 is the prefixed title of the subpage.',
'scribunto-doc-subpage-show' => 'Message displayed if the documentation subpage does exist. $1 is the prefixed title of the subpage. Should probably transclude that page.',
'scribunto-doc-subpage-header' => 'Message displayed at the top of the documentation subpage. $1 is the prefixed title of the module.',
'scribunto-doc-page-name' => 'Page name for module documentation. $1 is the unprefixed name of the module.',
'scribunto-doc-page-does-not-exist' => 'Message displayed if the documentation page does not exist. $1 is the prefixed title of the doc page.',
'scribunto-doc-page-show' => 'Message displayed if the documentation page does exist. $1 is the prefixed title of the doc page. Should probably transclude that page.',
'scribunto-doc-page-header' => 'Message displayed at the top of the documentation page. $1 is the prefixed title of the module.',
'scribunto-console-intro' => 'An explanatory message shown to module programmers in the debug console, where they can run Lua commands and see how they work.
"Module exports" are the names that are exported. See the chapter [http://www.lua.org/pil/15.2.html Privacy] in the book "Programming in Lua".',

View file

@ -55,7 +55,7 @@ $wgHooks['CodeEditorGetPageLanguage'][] = 'ScribuntoHooks::getCodeLanguage';
$wgHooks['EditPageBeforeEditChecks'][] = 'ScribuntoHooks::beforeEditChecks';
$wgHooks['EditPageBeforeEditButtons'][] = 'ScribuntoHooks::beforeEditButtons';
$wgHooks['EditFilterMerged'][] = 'ScribuntoHooks::validateScript';
$wgHooks['ArticleViewHeader'][] = 'ScribuntoHooks::showDocSubpageHeader';
$wgHooks['ArticleViewHeader'][] = 'ScribuntoHooks::showDocPageHeader';
$wgHooks['ContentHandlerDefaultModelFor'][] = 'ScribuntoHooks::contentHandlerDefaultModelFor';
$wgHooks['UnitTestsList'][] = 'ScribuntoHooks::unitTestsList';

View file

@ -68,32 +68,43 @@ class Scribunto {
}
/**
* Test whether the page should be considered a documentation subpage
* Test whether the page should be considered a documentation page
* @param $title Title
* @param &$forModule Title Module for which this is a doc page
* @return boolean
*/
public static function isDocSubpage( $title ) {
$docSubpage = wfMessage( 'scribunto-doc-subpage-name' )->inContentLanguage();
if ( $docSubpage->isDisabled() ) {
public static function isDocPage( $title, &$forModule = null ) {
$docPage = wfMessage( 'scribunto-doc-page-name' )->inContentLanguage();
if ( $docPage->isDisabled() ) {
return false;
}
$docSubpage = '/' . $docSubpage->plain();
return ( substr( $title->getText(), -strlen( $docSubpage ) ) === $docSubpage );
// Canonicalize the input pseudo-title. The unreplaced "$1" shouldn't
// cause a problem.
$docPage = Title::newFromText( $docPage->plain() )->getPrefixedText();
// Make it into a regex, and match it against the input title
$docPage = str_replace( '\\$1', '(.+)', preg_quote( $docPage, '/' ) );
if ( preg_match( "/^$docPage$/", $title->getPrefixedText(), $m ) ) {
$forModule = Title::makeTitleSafe( NS_MODULE, $m[1] );
return true;
} else {
return false;
}
}
/**
* Return the Title for the documentation subpage
* Return the Title for the documentation page
* @param $title Title
* @return Title|null
*/
public static function getDocSubpage( $title ) {
$docSubpage = wfMessage( 'scribunto-doc-subpage-name' )->inContentLanguage();
if ( $docSubpage->isDisabled() ) {
public static function getDocPage( $title ) {
$docPage = wfMessage( 'scribunto-doc-page-name', $title->getText() )->inContentLanguage();
if ( $docPage->isDisabled() ) {
return null;
}
return $title->getSubpage( $docSubpage->plain() );
return Title::newFromText( $docPage->plain() );
}
}

View file

@ -81,7 +81,7 @@ class ScribuntoHooks {
$moduleName = trim( $frame->expand( $args[0] ) );
$engine = Scribunto::getParserEngine( $parser );
$title = Title::makeTitleSafe( NS_MODULE, $moduleName );
if ( !$title || Scribunto::isDocSubpage( $title ) ) {
if ( !$title || Scribunto::isDocPage( $title ) ) {
throw new ScribuntoException( 'scribunto-common-nosuchmodule' );
}
$module = $engine->fetchModuleFromParser( $title );
@ -134,7 +134,7 @@ class ScribuntoHooks {
public static function getCodeLanguage( $title, &$lang ) {
global $wgScribuntoUseCodeEditor;
if( $wgScribuntoUseCodeEditor && $title->getNamespace() == NS_MODULE &&
!Scribunto::isDocSubpage( $title )
!Scribunto::isDocPage( $title )
) {
$engine = Scribunto::newDefaultEngine();
if( $engine->getCodeEditorLanguage() ) {
@ -153,7 +153,7 @@ class ScribuntoHooks {
* @return bool
*/
public static function contentHandlerDefaultModelFor( $title, &$model ) {
if( $title->getNamespace() == NS_MODULE && !Scribunto::isDocSubpage( $title ) ) {
if( $title->getNamespace() == NS_MODULE && !Scribunto::isDocPage( $title ) ) {
$model = 'Scribunto';
return false;
}
@ -195,7 +195,7 @@ class ScribuntoHooks {
return true;
}
if ( Scribunto::isDocSubpage( $editor->getTitle() ) ) {
if ( Scribunto::isDocPage( $editor->getTitle() ) ) {
return true;
}
@ -229,7 +229,7 @@ class ScribuntoHooks {
return true;
}
if ( Scribunto::isDocSubpage( $editor->getTitle() ) ) {
if ( Scribunto::isDocPage( $editor->getTitle() ) ) {
return true;
}
@ -252,7 +252,7 @@ class ScribuntoHooks {
return true;
}
if ( Scribunto::isDocSubpage( $title ) ) {
if ( Scribunto::isDocPage( $title ) ) {
return true;
}
@ -327,15 +327,13 @@ WIKI;
* @param &$pcache boolean
* @return boolean
*/
public static function showDocSubpageHeader( &$article, &$outputDone, &$pcache ) {
public static function showDocPageHeader( &$article, &$outputDone, &$pcache ) {
global $wgOut;
$title = $article->getTitle();
if( $title->getNamespace() === NS_MODULE && Scribunto::isDocSubpage( $title ) ) {
$docSubpage = wfMessage( 'scribunto-doc-subpage-name' )->inContentLanguage()->plain();
$title = substr( $title, 0, -strlen( $docSubpage ) - 1 );
if ( Scribunto::isDocPage( $title, $forModule ) ) {
$wgOut->addHTML(
wfMessage( 'scribunto-doc-subpage-header', $title )->parseAsBlock()
wfMessage( 'scribunto-doc-page-header', $forModule->getPrefixedText() )->parseAsBlock()
);
}
return true;

View file

@ -41,10 +41,10 @@ class ScribuntoContent extends TextContent {
// Get documentation, if any
$output = new ParserOutput();
$doc = Scribunto::getDocSubpage( $title );
$doc = Scribunto::getDocPage( $title );
if ( $doc ) {
$msg = wfMessage(
$doc->exists() ? 'scribunto-doc-subpage-show' : 'scribunto-doc-subpage-does-not-exist',
$doc->exists() ? 'scribunto-doc-page-show' : 'scribunto-doc-page-does-not-exist',
$doc->getPrefixedText()
)->inContentLanguage();
if ( !$msg->isDisabled() ) {
@ -53,8 +53,8 @@ class ScribuntoContent extends TextContent {
$output = $wgParser->parse( $msg->plain(), $title, $options, true, true, $revId );
}
// Mark the /doc subpage as a transclusion, so we get purged when
// it changes.
// Mark the doc page as a transclusion, so we get purged when it
// changes.
$output->addTemplate( $doc, $doc->getArticleID(), $doc->getLatestRevID() );
}