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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?