的情况是,我有一个私人wiki,在http://mysite.com/wiki,这是一个密码后面。我想做的是,在同一服务器上有一个单独的位置,可以用wiki文本(代码)读取任意文本文件,并使用http://mysite.com/wiki的特定的引擎从它呈现HTML(因为安装了模板/插件)。
作为示例,我将在http://mysite.com上有一个/tmppub目录;在它里面,我会有一个文本文件里面有wiki文本源代码,比如Example。Wiki和process.php页面;那我就叫:
http://mysite.com/tmppub/process.php?file=Example.wiki
…其中process.php将读取文件示例。在同一目录下的Wiki,以某种方式将内容传递给../wiki安装,检索HTML输出并显示它。
我想,我想要的是类似的例子在Mediawiki2HTML - gwtwiki -如何将Mediawiki文本转换为HTML - Java维基百科API (Bliki引擎)-除了这个Mediawiki2HTML是在Java(我想要PHP),并可能使用内部渲染引擎(我想要一个已经存在的Mediawiki的具体安装)。
问题是,我可以编写一个PHP脚本,它将读取文件,处理/wiki的密码,并传递GET和POST变量-除了我不确定如何解决Mediawiki安装:
- 我可以试着打电话给
&action=edit
(例如编辑Wikipedia:Sandbox)并要求预览;但这将返回编辑按钮和文本字段,我必须手动清理-不像 - 我可以尝试解决API,但正如我在API:解析wikitext - MediaWiki中看到的那样,它只会与MediaWiki安装中的页面一起工作-而不是与它的页面断开。
最后,我想获得内容的原始HTML(没有边栏之类的HTML),就像使用动作参数render
(示例)时一样。
,
如果已经有这样一个PHP应用程序可用,如果没有,解决Mediawiki安装的正确方法是什么,以获得wiki文本源的"原始"HTML呈现?提前感谢您的任何回答,
干杯!
您实际上可以使用API甚至使用parse
动作来解析自定义wikittext。(title
参数可能有点误导,但是当使用{{PAGENAME}}
时,它实际上只是解析器的指针。)要解析现有页面,使用render
操作。
如果身份验证是基于http的,并且您可以访问MediaWiki安装,那么您可以滥用用于维护脚本的代码来加载重要内容并在其上进行解析。(不过这可能有点脏。)以下代码摘自includes/api/ApiParse.php
并进行了一些编辑(当然,根据您的需要调整文件路径):
require_once dirname( __FILE__ ) . '/w/maintenance/commandLine.inc';
$text = "* [[foo]]'n* [[Example|bar]]'n* [http://example.com/ an outside link]";
$titleObj = Title::newFromText( 'Example' );
$parserOptions = new ParserOptions();
$parserOptions->setTidy( true );
$parserOutput = $wgParser->parse( $text, $titleObj, $parserOptions );
$parsedText = $parserOutput->getText();
解析后的HTML现在在$parsedText
变量中。如果您需要对文本执行预保存转换(展开{{subst:}}
s,将波浪扩展到签名等),请查看ApiParse.php
文件以供参考。
有许多wiki解析器可用- http://www.mediawiki.org/wiki/Alternative_parsers
你可以选择其中任何一个。您所需要做的就是在它们周围放置一个简单的身份验证包装器,然后就可以将其用作服务。
感谢@ mat
mwparse.php
,并尝试从web浏览器调用它-答案:"这个脚本必须从命令行运行"。啊,好吧:)这原来是使用commandLine.inc
的要求。
所以,我登录到服务器外壳,我尝试从CLI执行,我得到:
$ cd /path/to/mwparse/
$ php -f mwparse.php
...
Exception caught inside exception handler: exception 'DBQueryError' with message 'A database error has occurred
Query: SELECT /* MessageCache::loadFromDB 127.0.0.1 * / page_title FROM MWPREFIX_page WHERE page_is_redirect = '0' AND page_namespace = '8' AND (page_title not like '%%/%%') AND (page_len > 10000)
Function: doQuery
Error: HY000 no such table: MWPREFIX_page
' in /path/to/MyWiki/includes/db/Database.php:606
Stack trace:
....
…这是胡说八道,因为MyWiki
的安装工作时,从浏览器调用-我还打开了sqlitebrowser
中的数据库,以确认,确实,表MWPREFIX_page
存在。(在matj的回答中所指的/w
,我在这里称之为/MyWiki
)
因此,在尝试安装xdebug
并使用它调试脚本之后(这对我来说无法与Mediawiki一起工作,似乎是因为内存不断耗尽),我只是试图运行这个命令:
php -r "require_once dirname( __FILE__ ) . 'PREFIX/maintenance/commandLine.inc';"
…在不同的目录中,使用适当的PREFIX
。结果是,在根Mediawiki安装中,只能 执行这一行—也就是说,在本例中,在MyWiki
文件夹中:
$ cd /path/to/MyWiki
$ php -r "require_once dirname( __FILE__ ) . '/maintenance/commandLine.inc';"
$
知道了这一点,我将mat
<?
//~ error_reporting(E_ALL);
//~ ini_set('display_errors', '1');
chdir('../MyWiki);
//echo getcwd() . "'n"; // for debug check
require_once './maintenance/commandLine.inc';
$text = "* [[foo]]'n* [[Example|bar]]'n* [http://example.com/ an outside link]";
$titleObj = Title::newFromText( 'Example' );
$parserOptions = new ParserOptions();
$parserOptions->setTidy( true );
$parserOutput = $wgParser->parse( $text, $titleObj, $parserOptions );
$parsedText = $parserOutput->getText();
echo $parsedText;
?>
现在我可以从自己的目录运行脚本;但是,以下内容:
PHP Notice: Undefined index: SERVER_NAME in /path/to/MyWiki/includes/Linker.php on line 888
Notice: Undefined index: SERVER_NAME in /path/to/MyWiki/includes/Linker.php on line 888
…可以在输出中看到。如果启用了error_reporting
,则Notice
是- PHP Notice
实际上是在标准错误中。因此,要从脚本中获取输出,我将在脚本的目录中调用:
php -f mwparse.php 2>/dev/null
要联机,现在我只需要编写一个PHP页面,在CLI中调用这个脚本(可能使用exec),这应该不是问题(除了require_once ... commandLine.inc
确实需要几秒钟来执行,所以它将在某种程度上影响性能)。
很高兴看到这个问题解决了-再次感谢,
干杯!
,
PS:由于我花了相当多的时间在这上面,我将在下面转储一些命令行日志(主要与xdebug
的安装有关)。
from web: This script must be run from the command line
from remote terminal:
Exception caught inside exception handler: exception 'DBQueryError' with message 'A database error has occurred
Query: SELECT /* MessageCache::loadFromDB 127.0.0.1 * / page_title FROM MWPREFIX_page WHERE page_is_redirect = '0' AND page_namespace = '8' AND (page_title not like '%%/%%') AND (page_len > 10000)
Function: doQuery
Error: HY000 no such table: MWPREFIX_page
' in /path/to/MyWiki/includes/db/Database.php:606
Stack trace:
....
PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/mcrypt.ini on line 1 in Unknown on line 0
sdf
MediaWiki internal error.
Original exception: exception 'DBQueryError' with message 'A database error has occurred
Query: SELECT /* MediaWikiBagOStuff::_doquery 127.0.0.1 * / value,exptime FROM PREFIX_objectcache WHERE keyname='wikidb-MWPREFIX_:messages:en'
Function: doQuery
Error: HY000 no such table: MWPREFIX_objectcache
' in /path/to/MyWiki/includes/db/Database.php:606
http://www.apaddedcell.com/easy-php-debugging-ubuntu-using-xdebug-and-vim
https://stackoverflow.com/questions/1947395/how-can-i-debug-a-php-cli-script-with-xdebug
sudo apt-get install php-pear # pecl
sudo pecl install xdebug-beta # sh: phpize: not found
sudo apt-get install php5-dev # phpize; The following extra packages will be installed: autoconf automake autotools-dev binutils gcc gcc-4.4 libc-dev-bin libc6-dev libltdl-dev libssl-dev libtool linux-libc-dev m4 manpages-dev shtool zlib1g-dev
sudo pecl install xdebug-beta # Installing '/usr/lib/php5/20090626+lfs/xdebug.so'
sudo nano /etc/php5/apache2/php.ini # zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so and paste
sudo service apache2 restart # sudo /etc/init.d/apache2 restart
wget http://xdebug.org/files/xdebug-2.1.1.tgz # for debugclient
tar xzvf xdebug-2.1.1.tgz
rm package*.xml
cd xdebug-2.1.1/
$ cd debugclient
$ ./configure --with-libedit # configure: error: "libedit was not found on your system."
sudo apt-get install libedit2 # libedit2 is already the newest version.
sudo apt-get install libedit-dev # The following extra packages will be installed: libbsd-dev libncurses5-dev
$ ./configure --with-libedit
$ make
# make install
./debugclient # Waiting for debug server to connect.
# open another remote terminal
export XDEBUG_CONFIG="idekey=session_name"
php mwparse.php
# flies by
# mediawiki started crashing upon adding ?XDEBUG_SESSION_START=1 to url, restart server
# now different errors:
# Deprecated: Call-time pass-by-reference has been deprecated in /path/to/MyWiki/includes/Article.php on line 1658 (http://www.emmajane.net/php-what-call-time-pass-reference-story)
# Notice: Undefined variable: wgBibPath in /path/to/MyWiki/extensions/Bibwiki/Bibwiki.i18n.php on line 116
# Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 16 bytes) in /path/to/MyWiki/includes/GlobalFunctions.php on line 337
http://www.mediawiki.org/wiki/Manual:Errors_and_symptoms#Fatal_error:_Allowed_memory_size_of_nnnnnnn_bytes_exhausted_.28tried_to_allocate_nnnnnnnn_bytes.29
sudo nano /etc/php5/apache2/php.ini # comment out xdebug stuff
sudo service apache2 restart # now mediawiki works fine...
,
编辑注:
- 请注意,即使您在
LocalSettings.php
中设置了$wgDefaultUserOptions ['editsection'] = false;
,也不会对上述脚本产生影响(尽管它将在Mediawiki适当的中产生影响)-如果您想禁用API脚本渲染的编辑部分列表,脚本必须包含$parserOptions->setEditSection( false );
(通过Mediawiki: ParserOptions Class设置) - 由于在生产服务器上,似乎我没有权限运行PHP: exec()(或者更确切地说,PHP: passthru()),或者可能没有权限运行
php-cli
-所以我不能逐字使用上述解决方案,因为commandLine.inc
将需要一个终端。但是,可以复制commandLine.inc
,并使用$argv = array();unset($_SERVER);
"破解"它,然后上述解析器可以从web服务器上下文中完全工作(然而,我不确定commandLine.inc
的这种复制是否可能代表安全风险?)