dounieliang4712 2011-03-08 16:32
浏览 71
已采纳

使用全局变量在两个函数之间传递字符串的问题

I'm trying to pass $old from getOldText to reEdit with globals, but its not working. I can't call getOldText from inside reEdit, because by the time that occurs the old text would have been overwritten by the edit.

$old = "INITIAL";
function reEdit() {
    global $old;
            //removed code creating article object to simplify
    $new = $article->getRawText();
    $article->doEdit( "$new <-new    old -> $old");  //PROBLEM HERE, returns as INITIAL
    return true;
}

function getOldText() {
    global $old;
            //removed code creating article object to simplify
    $old = $article->getRawText();
    return true;
}

$wgHooks['EditFormInitialText'][] = array('getOldText');
$wgHooks['ArticleSaveComplete'][] = array('reEdit');

The problem is in the line indicated - $old is not passed to it despite being global.

The $wgHooks are MediaWiki code that call my functions when an article is starting to be edited and saved, respectively. For those who are familiar with mediawiki code, I'm just trying to get the text from before an edit was performed.

  • 写回答

3条回答 默认 最新

  • dptx8888 2011-03-08 17:11
    关注

    The reason your global variable is not persisting the value is because your two functions are not both being run in the same HTTP request. EditFormInitialText is called when the edit form is being generated, and may not actually be run at all in some cases. ArticleSaveComplete is called on a later submission, when the new version of the article is actually finished being saved.

    You may be able to do what you are wanting to do with the ArticleSave hook instead of EditFormInitialText.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题