感谢@Matěj Grabovský 提供answer;但是,我在让它工作的时候绊倒了几次,所以这里有一篇文章。
首先,我刚刚将答案中的代码保存为mwparse.php,并尝试从网络浏览器中调用它 - 答案:“此脚本必须从命令行运行 ”。嗯嗯 :) 原来这是使用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。 (Matěj's answer中提到的/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ěj's script修改为:
<?
//~ 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
... 可以在输出中看到。 Notice 是如果启用了error_reporting - 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...
编辑注释: