2012-01-28 16:22:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Wikitext scripting infrastructure for MediaWiki: hooks.
|
|
|
|
* Copyright (C) 2009-2012 Victor Vasiliev <vasilvv@gmail.com>
|
2018-10-29 13:33:03 +00:00
|
|
|
* https://www.mediawiki.org/
|
2012-01-28 16:22:18 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
2023-08-15 11:33:26 +00:00
|
|
|
// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
|
|
|
|
|
2022-04-07 23:12:32 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto;
|
|
|
|
|
|
|
|
use Article;
|
|
|
|
use Content;
|
|
|
|
use EmptyBagOStuff;
|
2024-03-10 13:34:21 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2024-06-10 18:41:33 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2023-05-06 21:20:25 +00:00
|
|
|
use MediaWiki\EditPage\EditPage;
|
2023-08-15 11:33:26 +00:00
|
|
|
use MediaWiki\Hook\EditFilterMergedContentHook;
|
|
|
|
use MediaWiki\Hook\EditPage__showReadOnlyForm_initialHook;
|
|
|
|
use MediaWiki\Hook\EditPage__showStandardInputs_optionsHook;
|
|
|
|
use MediaWiki\Hook\EditPageBeforeEditButtonsHook;
|
|
|
|
use MediaWiki\Hook\ParserClearStateHook;
|
|
|
|
use MediaWiki\Hook\ParserClonedHook;
|
|
|
|
use MediaWiki\Hook\ParserFirstCallInitHook;
|
|
|
|
use MediaWiki\Hook\ParserLimitReportFormatHook;
|
|
|
|
use MediaWiki\Hook\ParserLimitReportPrepareHook;
|
|
|
|
use MediaWiki\Hook\SoftwareInfoHook;
|
2024-01-05 18:24:06 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2018-01-10 14:19:13 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-01-05 18:24:06 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2023-08-15 11:33:26 +00:00
|
|
|
use MediaWiki\Page\Hook\ArticleViewHeaderHook;
|
2024-06-10 18:41:33 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2024-01-05 18:24:06 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2023-08-15 11:33:26 +00:00
|
|
|
use MediaWiki\Revision\Hook\ContentHandlerDefaultModelForHook;
|
2024-01-05 18:24:06 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2023-08-19 18:18:41 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2024-01-05 18:24:06 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-04-25 09:54:46 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2022-04-07 23:12:32 +00:00
|
|
|
use PPFrame;
|
2018-03-22 10:06:15 +00:00
|
|
|
use UtfNormal\Validator;
|
2018-01-21 04:42:48 +00:00
|
|
|
use Wikimedia\PSquare;
|
2017-05-31 10:09:48 +00:00
|
|
|
|
2012-01-28 16:22:18 +00:00
|
|
|
/**
|
2012-04-06 05:04:30 +00:00
|
|
|
* Hooks for the Scribunto extension.
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
class Hooks implements
|
|
|
|
SoftwareInfoHook,
|
|
|
|
ParserFirstCallInitHook,
|
|
|
|
ParserLimitReportPrepareHook,
|
|
|
|
ParserLimitReportFormatHook,
|
|
|
|
ParserClearStateHook,
|
|
|
|
ParserClonedHook,
|
|
|
|
EditPage__showStandardInputs_optionsHook,
|
|
|
|
EditPage__showReadOnlyForm_initialHook,
|
|
|
|
EditPageBeforeEditButtonsHook,
|
|
|
|
EditFilterMergedContentHook,
|
|
|
|
ArticleViewHeaderHook,
|
|
|
|
ContentHandlerDefaultModelForHook
|
|
|
|
{
|
2024-03-10 13:34:21 +00:00
|
|
|
private Config $config;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Config $config
|
|
|
|
) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2017-06-24 00:53:28 +00:00
|
|
|
/**
|
|
|
|
* Define content handler constant upon extension registration
|
|
|
|
*/
|
|
|
|
public static function onRegistration() {
|
|
|
|
define( 'CONTENT_MODEL_SCRIBUNTO', 'Scribunto' );
|
|
|
|
}
|
|
|
|
|
2013-03-20 16:30:26 +00:00
|
|
|
/**
|
|
|
|
* Get software information for Special:Version
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param array &$software
|
2013-03-20 16:30:26 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onSoftwareInfo( &$software ) {
|
2013-03-20 16:30:26 +00:00
|
|
|
$engine = Scribunto::newDefaultEngine();
|
|
|
|
$engine->setTitle( Title::makeTitle( NS_SPECIAL, 'Version' ) );
|
|
|
|
$engine->getSoftwareInfo( $software );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-28 16:22:18 +00:00
|
|
|
/**
|
|
|
|
* Register parser hooks.
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
2019-11-15 06:10:39 +00:00
|
|
|
* @param Parser $parser
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onParserFirstCallInit( $parser ) {
|
2024-03-10 13:34:21 +00:00
|
|
|
$parser->setFunctionHook( 'invoke', [ $this, 'invokeHook' ], Parser::SFH_OBJECT_ARGS );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-04 06:10:32 +00:00
|
|
|
* Called when the interpreter is to be reset.
|
2014-07-07 18:46:59 +00:00
|
|
|
*
|
2019-11-15 06:10:39 +00:00
|
|
|
* @param Parser $parser
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onParserClearState( $parser ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
Scribunto::resetParserEngine( $parser );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 00:13:19 +00:00
|
|
|
/**
|
|
|
|
* Called when the parser is cloned
|
|
|
|
*
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param Parser $parser
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2012-11-15 00:13:19 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onParserCloned( $parser ) {
|
2012-11-15 00:13:19 +00:00
|
|
|
$parser->scribunto_engine = null;
|
|
|
|
}
|
|
|
|
|
2012-01-28 16:22:18 +00:00
|
|
|
/**
|
2012-04-04 06:10:32 +00:00
|
|
|
* Hook function for {{#invoke:module|func}}
|
2012-01-28 16:46:15 +00:00
|
|
|
*
|
2019-11-15 06:10:39 +00:00
|
|
|
* @param Parser $parser
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param PPFrame $frame
|
|
|
|
* @param array $args
|
2012-01-28 16:22:18 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-10 13:34:21 +00:00
|
|
|
public function invokeHook( Parser $parser, PPFrame $frame, array $args ) {
|
2012-01-28 16:22:18 +00:00
|
|
|
try {
|
2012-05-22 03:56:07 +00:00
|
|
|
if ( count( $args ) < 2 ) {
|
|
|
|
throw new ScribuntoException( 'scribunto-common-nofunction' );
|
|
|
|
}
|
|
|
|
$moduleName = trim( $frame->expand( $args[0] ) );
|
2012-04-06 05:04:30 +00:00
|
|
|
$engine = Scribunto::getParserEngine( $parser );
|
2014-09-16 03:25:53 +00:00
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
$title = Title::makeTitleSafe( NS_MODULE, $moduleName );
|
2014-09-16 03:25:53 +00:00
|
|
|
if ( !$title || !$title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
|
2015-06-26 16:37:34 +00:00
|
|
|
throw new ScribuntoException( 'scribunto-common-nosuchmodule',
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'args' => [ $moduleName ] ] );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-05 07:58:02 +00:00
|
|
|
$module = $engine->fetchModuleFromParser( $title );
|
Added more Lua environment features
Package library:
* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
retrieved from the wiki, using require('Module:Foo'), or from files
distributed with Scribunto, using require('foo'). The "Module:" prefix allows
for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
exception, to more easily support the desired behaviour of the package loader,
which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
in library names, since LuaSandbox doesn't support this.
Improved module isolation and related refactoring:
* Expose restricted versions of getfenv() and setfenv() to user Lua code.
Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
calls. This breaks isolation of #invoke calls, since the local variables are
persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
a purpose if it can't cache anything. Instead, invoke functions using a module
method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
array. Use the formatting facilities of the Status class.
Other:
* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
where the initial error will be caught and ignored, and the user will see the
error from checkValid() instead. In this case, rethrow the original error for
a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
the error and exit. This avoids a protocol violation when an error is
encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.
Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 03:46:18 +00:00
|
|
|
if ( !$module ) {
|
2015-06-26 16:37:34 +00:00
|
|
|
throw new ScribuntoException( 'scribunto-common-nosuchmodule',
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'args' => [ $moduleName ] ] );
|
2012-04-05 07:58:02 +00:00
|
|
|
}
|
2012-05-22 03:56:07 +00:00
|
|
|
$functionName = trim( $frame->expand( $args[1] ) );
|
|
|
|
|
2014-09-06 19:32:28 +00:00
|
|
|
$bits = $args[1]->splitArg();
|
2012-05-22 03:56:07 +00:00
|
|
|
unset( $args[0] );
|
|
|
|
unset( $args[1] );
|
2014-09-06 19:32:28 +00:00
|
|
|
|
|
|
|
// If $bits['index'] is empty, then the function name was parsed as a
|
|
|
|
// key=value pair (because of an equals sign in it), and since it didn't
|
|
|
|
// have an index, we don't need the index offset.
|
|
|
|
$childFrame = $frame->newChild( $args, $title, $bits['index'] === '' ? 0 : 1 );
|
2015-10-24 23:54:11 +00:00
|
|
|
|
2024-03-10 13:34:21 +00:00
|
|
|
if ( $this->config->get( 'ScribuntoGatherFunctionStats' ) ) {
|
2015-10-24 23:54:11 +00:00
|
|
|
$u0 = $engine->getResourceUsage( $engine::CPU_SECONDS );
|
|
|
|
$result = $module->invoke( $functionName, $childFrame );
|
|
|
|
$u1 = $engine->getResourceUsage( $engine::CPU_SECONDS );
|
|
|
|
|
|
|
|
if ( $u1 > $u0 ) {
|
2017-07-08 13:42:23 +00:00
|
|
|
$timingMs = (int)( 1000 * ( $u1 - $u0 ) );
|
2022-04-16 21:09:10 +00:00
|
|
|
// Since the overhead of stats is worst when #invoke
|
2015-10-24 23:54:11 +00:00
|
|
|
// calls are very short, don't process measurements <= 20ms.
|
|
|
|
if ( $timingMs > 20 ) {
|
2024-03-10 13:34:21 +00:00
|
|
|
$this->reportTiming( $moduleName, $functionName, $timingMs );
|
2015-10-24 23:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = $module->invoke( $functionName, $childFrame );
|
|
|
|
}
|
|
|
|
|
2018-03-22 10:06:15 +00:00
|
|
|
return Validator::cleanUp( strval( $result ) );
|
2015-06-26 16:37:34 +00:00
|
|
|
} catch ( ScribuntoException $e ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$trace = $e->getScriptTraceHtml( [ 'msgOptions' => [ 'content' ] ] );
|
|
|
|
$html = Html::element( 'p', [], $e->getMessage() );
|
2012-04-23 11:36:13 +00:00
|
|
|
if ( $trace !== false ) {
|
2012-10-06 23:42:39 +00:00
|
|
|
$html .= Html::element( 'p',
|
2017-06-15 17:19:00 +00:00
|
|
|
[],
|
2012-10-06 23:42:39 +00:00
|
|
|
wfMessage( 'scribunto-common-backtrace' )->inContentLanguage()->text()
|
|
|
|
) . $trace;
|
2014-06-30 17:38:46 +00:00
|
|
|
} else {
|
|
|
|
$html .= Html::element( 'p',
|
2017-06-15 17:19:00 +00:00
|
|
|
[],
|
2014-06-30 17:38:46 +00:00
|
|
|
wfMessage( 'scribunto-common-no-details' )->inContentLanguage()->text()
|
|
|
|
);
|
2012-04-23 11:36:13 +00:00
|
|
|
}
|
2023-08-07 22:11:46 +00:00
|
|
|
|
|
|
|
// Index this error by a uniq ID so that we are independent of
|
|
|
|
// page parse order. (T300979)
|
|
|
|
// (The only way this will conflict is if two exceptions have
|
|
|
|
// exactly the same backtrace, in which case we really only need
|
|
|
|
// one copy of the backtrace!)
|
|
|
|
$uuid = substr( sha1( $html ), -8 );
|
2022-01-12 16:31:31 +00:00
|
|
|
$parserOutput = $parser->getOutput();
|
2023-08-07 22:11:46 +00:00
|
|
|
$parserOutput->appendExtensionData( 'ScribuntoErrors', $uuid );
|
|
|
|
$parserOutput->setExtensionData( "ScribuntoErrors-$uuid", $html );
|
|
|
|
|
|
|
|
$parserOutput->appendJsConfigVar( 'ScribuntoErrors', $uuid );
|
|
|
|
$parserOutput->setJsConfigVar( "ScribuntoErrors-$uuid", $html );
|
|
|
|
|
|
|
|
// These methods are idempotent; doesn't hurt to call them every
|
|
|
|
// time.
|
|
|
|
$parser->addTrackingCategory( 'scribunto-common-error-category' );
|
|
|
|
$parserOutput->addModules( [ 'ext.scribunto.errors' ] );
|
|
|
|
|
|
|
|
$id = "mw-scribunto-error-$uuid";
|
2014-06-30 17:38:46 +00:00
|
|
|
$parserError = htmlspecialchars( $e->getMessage() );
|
2012-04-23 11:36:13 +00:00
|
|
|
|
|
|
|
// #iferror-compatible error element
|
2014-07-07 18:46:59 +00:00
|
|
|
return "<strong class=\"error\"><span class=\"scribunto-error\" id=\"$id\">" .
|
2018-09-03 18:08:53 +00:00
|
|
|
$parserError . "</span></strong>";
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 23:54:11 +00:00
|
|
|
/**
|
|
|
|
* Record stats on slow function calls.
|
|
|
|
*
|
|
|
|
* @param string $moduleName
|
|
|
|
* @param string $functionName
|
|
|
|
* @param int $timing Function execution time in milliseconds.
|
|
|
|
*/
|
2024-03-10 13:34:21 +00:00
|
|
|
private function reportTiming( $moduleName, $functionName, $timing ) {
|
|
|
|
if ( !$this->config->get( 'ScribuntoGatherFunctionStats' ) ) {
|
2015-10-24 23:54:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-10 13:34:21 +00:00
|
|
|
$threshold = $this->config->get( 'ScribuntoSlowFunctionThreshold' );
|
2015-10-29 22:53:10 +00:00
|
|
|
if ( !( is_float( $threshold ) && $threshold > 0 && $threshold < 1 ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-08 23:26:58 +00:00
|
|
|
$objectcachefactory = MediaWikiServices::getInstance()->getObjectCacheFactory();
|
2015-10-24 23:54:11 +00:00
|
|
|
static $cache;
|
|
|
|
if ( !$cache ) {
|
2024-05-08 23:26:58 +00:00
|
|
|
$cache = $objectcachefactory->getLocalServerInstance( CACHE_NONE );
|
2018-01-10 14:19:13 +00:00
|
|
|
|
2015-10-24 23:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// To control the sampling rate, we keep a compact histogram of
|
2015-10-29 22:53:10 +00:00
|
|
|
// observations in APC, and extract the Nth percentile (specified
|
|
|
|
// via $wgScribuntoSlowFunctionThreshold; defaults to 0.90).
|
2022-07-19 19:58:17 +00:00
|
|
|
// We need a non-empty local server cache for that (e.g. php-apcu).
|
|
|
|
if ( $cache instanceof EmptyBagOStuff ) {
|
2015-10-24 23:54:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-16 19:06:05 +00:00
|
|
|
$cacheVersion = '3';
|
Hooks: Bump scribunto-stats cache version
The Wikimedia\PSquare class has changed such that its serialization
is incompatible with its prior version. We should probably refactor
this to use an actually supported serialization format, possibly
by enhancing PSquare with some kind of getter method that provides
a plain array that its constructor can accept again instead of relying
on internal PHP serialisation format, but for now we can bump the
cache version.
The side-effect of this will be that the performance stats captured
by Scribunto for visualiation in Grafana, will briefly split its
sampling logic between two versions instead of being global across
all wikis until the train is fully deployed. This does not have any
effect on its business logic or runtime behaviour, and is presumably
by design as that's the only way to bump the cache version, which was
a pre-existing factor in its cache key.
Basically, this means that during the deployment days on weeks where
this is bumped, we will briefly capture fewer samples as there are then
two separate counters trying to reach 1000 before flushing a median
to Graphite. We can remedy that by backporting and deploying to both.
While at it, I'm changing the cache key to conform to our conventions
and make it an explicit, greppable, lowercase and descriptive name
(scribuntu-stats) instead of the implicit __METHOD__ which expanded
to "Scribunto\Hooks::reportTiming". I note that this means the split
brain sampling actually happened at least once before during the week
where the namespace was introduced as that will have implicily changed
the cache-key. Another reason not to use __METHOD__ in a cache key.
Bug: T313341
Change-Id: Ic9dad0f55cba18ec03272b87366a091a396beb74
2022-07-19 19:40:06 +00:00
|
|
|
$key = $cache->makeGlobalKey( 'scribunto-stats', $cacheVersion, (string)$threshold );
|
2015-10-24 23:54:11 +00:00
|
|
|
|
|
|
|
// This is a classic "read-update-write" critical section with no
|
|
|
|
// mutual exclusion, but the only consequence is that some samples
|
|
|
|
// will be dropped. We only need enough samples to estimate the
|
2022-04-16 21:09:10 +00:00
|
|
|
// shape of the data, so that's fine.
|
2017-05-31 10:09:48 +00:00
|
|
|
$ps = $cache->get( $key ) ?: new PSquare( $threshold );
|
2015-10-24 23:54:11 +00:00
|
|
|
$ps->addObservation( $timing );
|
|
|
|
$cache->set( $key, $ps, 60 );
|
|
|
|
|
|
|
|
if ( $ps->getCount() < 1000 || $timing < $ps->getValue() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static $stats;
|
|
|
|
if ( !$stats ) {
|
2024-05-05 05:34:03 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsFactory();
|
2015-10-24 23:54:11 +00:00
|
|
|
}
|
2024-05-05 05:34:03 +00:00
|
|
|
$statAction = WikiMap::getCurrentWikiId() . '__' . $moduleName . '__' . $functionName;
|
|
|
|
$stats->getTiming( 'scribunto_traces_seconds' )
|
|
|
|
->setLabel( 'action', $statAction )
|
|
|
|
->copyToStatsdAt( 'scribunto.traces.' . $statAction )
|
|
|
|
->observe( $timing );
|
2015-10-24 23:54:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-28 16:22:18 +00:00
|
|
|
/**
|
2013-02-20 22:00:42 +00:00
|
|
|
* Set the Scribunto content handler for modules
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @param string &$model
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onContentHandlerDefaultModelFor( $title, &$model ) {
|
2018-10-18 13:37:23 +00:00
|
|
|
if ( $model === 'sanitized-css' ) {
|
|
|
|
// Let TemplateStyles override Scribunto
|
2024-08-26 15:27:06 +00:00
|
|
|
return;
|
2018-10-18 13:37:23 +00:00
|
|
|
}
|
2022-09-22 06:19:39 +00:00
|
|
|
if ( $title->getNamespace() === NS_MODULE ) {
|
|
|
|
if ( str_ends_with( $title->getText(), '.json' ) ) {
|
|
|
|
$model = CONTENT_MODEL_JSON;
|
|
|
|
} elseif ( !Scribunto::isDocPage( $title ) ) {
|
|
|
|
$model = CONTENT_MODEL_SCRIBUNTO;
|
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 12:42:42 +00:00
|
|
|
/**
|
|
|
|
* Adds report of number of evaluations by the single wikitext page.
|
|
|
|
*
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param Parser $parser
|
2022-01-12 16:31:31 +00:00
|
|
|
* @param ParserOutput $parserOutput
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2013-03-14 12:42:42 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onParserLimitReportPrepare( $parser, $parserOutput ) {
|
2013-03-14 12:42:42 +00:00
|
|
|
if ( Scribunto::isParserEnginePresent( $parser ) ) {
|
|
|
|
$engine = Scribunto::getParserEngine( $parser );
|
2022-01-12 16:31:31 +00:00
|
|
|
$engine->reportLimitData( $parserOutput );
|
2013-03-14 12:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats the limit report data
|
|
|
|
*
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param string $key
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param mixed &$value
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param string &$report
|
|
|
|
* @param bool $isHTML
|
|
|
|
* @param bool $localize
|
2013-03-14 12:42:42 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onParserLimitReportFormat( $key, &$value, &$report, $isHTML, $localize ) {
|
2013-03-14 12:42:42 +00:00
|
|
|
$engine = Scribunto::newDefaultEngine();
|
|
|
|
return $engine->formatLimitData( $key, $value, $report, $isHTML, $localize );
|
|
|
|
}
|
|
|
|
|
2014-07-07 18:46:59 +00:00
|
|
|
/**
|
2017-03-30 16:57:38 +00:00
|
|
|
* EditPage::showStandardInputs:options hook
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param EditPage $editor
|
2017-03-30 16:57:38 +00:00
|
|
|
* @param OutputPage $output
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param int &$tab Current tabindex
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2012-07-03 02:56:55 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onEditPage__showStandardInputs_options( $editor, $output, &$tab ) {
|
2014-09-16 03:25:53 +00:00
|
|
|
if ( $editor->getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
|
2017-03-30 16:57:38 +00:00
|
|
|
$output->addModules( 'ext.scribunto.edit' );
|
2015-08-24 20:38:46 +00:00
|
|
|
$editor->editFormTextAfterTools .= '<div id="mw-scribunto-console"></div>';
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
2015-03-27 16:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EditPage::showReadOnlyForm:initial hook
|
|
|
|
*
|
|
|
|
* @param EditPage $editor
|
|
|
|
* @param OutputPage $output
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2015-03-27 16:32:19 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onEditPage__showReadOnlyForm_initial( $editor, $output ) {
|
2014-09-16 03:25:53 +00:00
|
|
|
if ( $editor->getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
|
|
|
|
$output->addModules( 'ext.scribunto.edit' );
|
2015-08-24 21:13:31 +00:00
|
|
|
$editor->editFormTextAfterContent .= '<div id="mw-scribunto-console"></div>';
|
2015-03-27 16:32:19 +00:00
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EditPageBeforeEditButtons hook
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
2019-11-15 06:10:39 +00:00
|
|
|
* @param EditPage $editor
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param array &$buttons Button array
|
|
|
|
* @param int &$tabindex Current tabindex
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2012-07-14 04:23:42 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onEditPageBeforeEditButtons( $editor, &$buttons, &$tabindex ) {
|
2014-09-16 03:25:53 +00:00
|
|
|
if ( $editor->getTitle()->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
|
|
|
|
unset( $buttons['preview'] );
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
2012-07-03 02:56:55 +00:00
|
|
|
}
|
|
|
|
|
2012-06-15 23:34:35 +00:00
|
|
|
/**
|
2016-10-13 18:39:33 +00:00
|
|
|
* @param IContextSource $context
|
|
|
|
* @param Content $content
|
|
|
|
* @param Status $status
|
2023-08-15 11:33:26 +00:00
|
|
|
* @param string $summary
|
|
|
|
* @param User $user
|
|
|
|
* @param bool $minoredit
|
2012-06-15 23:34:35 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onEditFilterMergedContent( IContextSource $context, Content $content,
|
|
|
|
Status $status, $summary, User $user, $minoredit
|
2016-10-13 18:39:33 +00:00
|
|
|
) {
|
|
|
|
$title = $context->getTitle();
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2016-10-13 18:39:33 +00:00
|
|
|
if ( !$content instanceof ScribuntoContent ) {
|
2013-02-20 22:00:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-27 12:13:37 +00:00
|
|
|
$contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory();
|
|
|
|
$contentHandler = $contentHandlerFactory->getContentHandler( $content->getModel() );
|
2013-02-20 22:00:42 +00:00
|
|
|
|
2021-10-27 12:13:37 +00:00
|
|
|
'@phan-var ScribuntoContentHandler $contentHandler';
|
|
|
|
$validateStatus = $contentHandler->validate( $content, $title );
|
2016-10-13 18:39:33 +00:00
|
|
|
if ( $validateStatus->isOK() ) {
|
2012-07-03 02:56:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2016-10-13 18:39:33 +00:00
|
|
|
$status->merge( $validateStatus );
|
|
|
|
|
2023-02-25 00:57:56 +00:00
|
|
|
if ( isset( $validateStatus->value->params['module'] ) ) {
|
|
|
|
$module = $validateStatus->value->params['module'];
|
|
|
|
$line = $validateStatus->value->params['line'];
|
2012-07-03 02:56:55 +00:00
|
|
|
if ( $module === $title->getPrefixedDBkey() && preg_match( '/^\d+$/', $line ) ) {
|
2016-10-13 18:39:33 +00:00
|
|
|
$out = $context->getOutput();
|
2024-04-26 11:19:42 +00:00
|
|
|
$out->addInlineScript( 'window.location.hash = ' . Html::encodeJsVar( "#mw-ce-l$line" ) );
|
2012-04-23 11:36:13 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2021-04-17 10:09:22 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
// @todo Remove this line after this extension do not support mediawiki version 1.36 and before
|
|
|
|
$status->value = EditPage::AS_HOOK_ERROR_EXPECTED;
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-01-28 16:22:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
|
2013-02-20 22:00:42 +00:00
|
|
|
/**
|
2019-11-15 06:10:39 +00:00
|
|
|
* @param Article $article
|
2022-09-28 14:34:34 +00:00
|
|
|
* @param bool|ParserOutput|null &$outputDone
|
2014-10-10 09:02:03 +00:00
|
|
|
* @param bool &$pcache
|
2024-08-26 15:27:06 +00:00
|
|
|
* @return void
|
2013-02-20 22:00:42 +00:00
|
|
|
*/
|
2023-08-15 11:33:26 +00:00
|
|
|
public function onArticleViewHeader( $article, &$outputDone, &$pcache ) {
|
2013-02-20 22:00:42 +00:00
|
|
|
$title = $article->getTitle();
|
2013-03-08 17:01:50 +00:00
|
|
|
if ( Scribunto::isDocPage( $title, $forModule ) ) {
|
2016-08-31 07:31:26 +00:00
|
|
|
$article->getContext()->getOutput()->addHTML(
|
2013-03-08 17:01:50 +00:00
|
|
|
wfMessage( 'scribunto-doc-page-header', $forModule->getPrefixedText() )->parseAsBlock()
|
2013-02-20 22:00:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|