我目前正在制作一个嵌套标签扩展,其中父标签(在我的情况下<tabs>
)需要能够影响子标签的内容(在我的情况下<tab>
)。我已经尝试通过运行标签的内容来做到这一点$parser->recursiveTagParse($input)
,$input
但是<tabs>
在返回的字符串中,似乎不可能匹配任何东西。为了以最基本的方式对此进行测试,我执行了以下操作:
添加到 LocalSettings.php:
require_once "$IP\\extensions\\Test\\Test.php";
在其测试目录中创建 Test.php:
<?php
$wgAutoloadClasses['Test'] = __DIR__ . '/Test.body.php';
$wgHooks['ParserFirstCallInit'][] = 'Test::init';
使用以下命令创建 Test.body.php:
<?php
class Test {
public static function init( &$parser ) {
$parser->setHook( 'testfoo', array( 'Test', 'firstTag' ) );
$parser->setHook( 'testbar', array( 'Test', 'secondTag' ) );
return true;
}
public static function firstTag($input, $attr = array(), $parser, $frame) {
$newinput = $parser->recursiveTagParse($input);
return preg_match_all("/ /", $newinput);
}
public static function secondTag($input, $attr = array(), $parser, $frame) {
return htmlspecialchars($input);
}
}
最后,将以下内容放在 wiki 的页面上:
<testfoo>three spaces out here<testbar>and four more spaces here</testbar></testfoo>
这应该是 be 的最终输出7
,因为输出字符串中有 7 个空格。问题是这反而返回标签3
外的空格数量。<testbar>
我想知道的是是否有可能匹配和/或替换解析嵌套标签的结果(嵌套标签输出的内容),如果不可能,是否可以制作<testbar>
标签嵌套在<testfoo>
标签内时表现不同?
我还尝试将PPFrame
第二个参数添加到部分$parser->recursiveTagParse($input)
以尝试检测嵌套,但是 PPFrame 的文档非常不清楚,以至于我不知道我需要做什么。我现在所做的,根据文档所说的是:
public static function firstTag($input, $attr = array(), $parser, $frame) {
$newframe = $frame->newChild(array('nested'=>'true'), $frame->getTitle(), 0);
$newinput = $parser->recursiveTagParse($input, $newframe);
return preg_match_all("/ /", $newinput);
}
但这会导致错误。当我这样做时它说的是:
注意:尝试在第 984 行的 C:\hiddenpath\w\includes\parser\Preprocessor_DOM.php 中获取非对象的属性
可捕获的致命错误:传递给 DOMXPath::__construct() 的参数 1 必须是 DOMDocument 的实例,给定 null,在第 10 行的 C:\hiddenpath\w\extensions\Test\Test.body.php 中调用并在 C 中定义: \hiddenpath\w\includes\parser\Preprocessor_DOM.php 在第 984 行
但我不知道我需要做什么来解决这个问题。我需要让我的第一个论点成为一个特殊的对象吗?如果是这样,为什么文档说我需要将数组作为第一个参数?