mediawiki 调用外站内容方法

我使用的skin是 Timeless。在TimelessTemplate.php文件中做了修改

一、加入以下代码,读取远程接口api内容后进行处理

 

    private  function  handleNews($cateId=0)
    {
        $content=file_get_contents('http://szxw/index.php?s=article&c=search&cateId='.$cateId.'&api_call_function=module_list&appid=1&appsecret=ABDf');
        if(strpos($content,'code'))
        {
          $jscontent=json_decode($content,true);
           $strs='';
            foreach ($jscontent['data'] as $item) {
                $strs.="<li><a target='_blank' href=".$item['url'].">".$item['title']."</a></li>";
            }
            return "<ul>$strs</ul>";
        }
    }

使用以上代码生成了ul内容列表

二、修改方法 getContentBlock 加入了以下内容

protected function getContentBlock() {
        $templateData = $this->getSkin()->getTemplateData();
        $content=$this->get( 'bodytext' );
        if(preg_match('/\$catid(\d+)\$/',$content, $matches ) && $_GET['action']!='edit')
        {
            $content=str_replace($matches[0],$this->handleNews($matches[1]),$content);
        }
...................

    Html::rawElement( 'div', [ 'id' => 'bodyContent' ],
                    $this->getContentSub() .
                    $content.
                    $this->getClear()
                )
            )
        );

        return Html::rawElement( 'div', [ 'id' => 'mw-content' ], $html);
    }

 

第三步,在创建词条时,加入如下使用

==相关资讯==

$catid59$

  其中传入参数59为需要处理的目录ID

 

posted @ 2022-12-22 17:09  fogwu  阅读(29)  评论(0编辑  收藏  举报