AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 520797
Accepted
Gerald Schneider
Gerald Schneider
Asked: 2013-07-05 01:54:24 +0800 CST2013-07-05 01:54:24 +0800 CST 2013-07-05 01:54:24 +0800 CST

如何将内容添加到 mediawiki 中的所有页面?

  • 772

TL;DR:如何向每个页面的内容(不是标题或全局模板)添加文本(通知)?

(非常)长问题的背景:我计划将 MediaWiki 迁移到另一个 wiki。wiki 的内容是从之前更旧的 wiki 迁移而来的(生成了有关格式的错误),随着时间的推移而增长,现在大部分已经过时。这就是为什么我们要从空白 wiki 开始并手动迁移内容,丢弃和/或更新过时的页面。

为了使这更容易,我想在每个现有页面的顶部添加一个文本块,特别是一个模板,通知该页面尚未迁移或丢弃,以及收集所有这些页面的类别(例如类别:migration_pending) . 然后,每个用户都应该浏览他负责的页面,将内容复制到新的 wiki 并将模板更改为另一个模板,将页面标记为已迁移 (category:migration_done) 或丢弃 (category:migration_discarded)。通过这种方式,应该可以在不忘记任何重要内容的情况下获得一个干净、最新的 wiki。

php
  • 1 1 个回答
  • 1416 Views

1 个回答

  • Voted
  1. Best Answer
    Gerald Schneider
    2014-03-13T06:16:18+08:002014-03-13T06:16:18+08:00

    Replace_Text 扩展不成功,所以我结束了编写自己的使用 MediaWiki API 的脚本。

    我从这里开始使用登录脚本并编写了这个脚本:

    #!/usr/bin/php
    <?php
    
    $settings['wikiroot'] = "https://server/mediawiki";
    $settings['user'] =  "username";
    $settings['pass'] =  "password";
    // $settings['domain'] = 'Windows';
    $settings['cookiefile'] = "cookies.tmp";
    
    $prepend = "{{migration_pending}}\n\n";
    
    
    function httpRequest($url, $post="") {
            global $settings;
    
            $ch = curl_init();
            //Change the user agent below suitably
            curl_setopt($ch, CURLOPT_USERAGENT, 'MediaWiki Migration Script 0.1');
            curl_setopt($ch, CURLOPT_URL, ($url));
            curl_setopt($ch, CURLOPT_ENCODING, "UTF-8" );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $settings['cookiefile']);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $settings['cookiefile']);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            if (!empty($post)) curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
            //UNCOMMENT TO DEBUG TO output.tmp
            //curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
            //$fp = fopen("output.tmp", "w");
            //curl_setopt($ch, CURLOPT_STDERR, $fp); // Display communication with server
    
            $xml = curl_exec($ch);
    
            if (!$xml) {
                    throw new Exception("Error getting data from server ($url): " . curl_error($ch));
            }
    
            //var_dump($xml);
    
            curl_close($ch);
    
            return $xml;
    }
    
    
    function login ($user, $pass, $token='') {
            global $settings;
    
            $url = $settings['wikiroot'] . "/api.php?action=login&format=xml";
    
            $params = "action=login&lgname=$user&lgpassword=$pass";
            if (!empty($settings['domain'])) {
                $params .= "&lgdomain=" . $settings['domain'];
            }
            if (!empty($token)) {
                    $params .= "&lgtoken=$token";
            }
    
            $data = httpRequest($url, $params);
    
            if (empty($data)) {
                    throw new Exception("No data received from server. Check that API is enabled.");
            }
    
            $xml = simplexml_load_string($data);
    
            if (!empty($token)) {
                    //Check for successful login
                    $expr = "/api/login[@result='Success']";
                    $result = $xml->xpath($expr);
    
                    if(!count($result)) {
                            throw new Exception("Login failed");
                    }
            } else {
                    $expr = "/api/login[@token]";
                    $result = $xml->xpath($expr);
    
                    if(!count($result)) {
                            throw new Exception("Login token not found in XML");
                    }
            }
    
            return $result[0]->attributes()->token;
    }
    
    
    try {
            global $settings;
            $token = login($settings['user'], $settings['pass']);
            login($settings['user'], $settings['pass'], $token);
    
            $star = "*";
            $dash1 = "-1";
    
            // get edit token
            $result = httpRequest($settings['wikiroot'] . "/api.php?action=query&format=json&prop=info|revisions&intoken=edit&titles=Main%20Page");
            $result = json_decode($result);
            $editToken = $result->query->pages->$dash1->edittoken;
    
            // only from namespace: apnamespace=100
            $result = httpRequest($settings['wikiroot'] . "/api.php?action=query&list=allpages&format=json&aplimit=5000&apnamespace=100");
            $result = json_decode($result);
            $allpages = $result->query->allpages;
    
            foreach ($allpages as $page) {
                echo "Fetching '{$page->title}' ({$page->pageid})...\n";
                $revisions = httpRequest(sprintf($settings['wikiroot'] . "/api.php?action=query&prop=revisions&rvlimit=1&format=json&rvprop=content&titles=%s", urlencode($page->title)));
                $revisions = json_decode($revisions);
                if (isset($revisions->error)) {
                    echo "ERROR: " . $revisions->error->info . "\n";
                    continue;
                }
                $content = $revisions->query->pages->{$page->pageid}->revisions[0]->$star;
                if (preg_match("/\{\{migration_/", $content)) {
                    echo "Already marked ... skipping.\n";
                    continue;
                }
                echo "Updating...";
                // add text to content and edit page
                $content = $prepend . $content;
                $post = sprintf("title=%s&text=%s&token=%s", urlencode($page->title), urlencode($content), urlencode($editToken));
                $result = httpRequest($settings['wikiroot'] . "/api.php?action=edit&format=json", $post);
                echo "done\n";
            }
            echo ("Finished (".sizeof($allpages)." pages).\n");
    } catch (Exception $e) {
            die("FAILED: " . $e->getMessage());
    }
    
    ?>
    

    该脚本的基本作用是:

    • 使用现有帐户登录
    • 获得一个允许它进行编辑操作的令牌
    • 检索给定命名空间内所有页面的列表
    • 对于每一页:
      • 从最新版本中获取内容
      • 向内容添加预定义文本
      • 用新内容保存页面

    一些附加说明:

    • 确保您要使用的用户存在并且对所有需要的命名空间具有写入权限。
    • 将用户添加到“Bot”组。这将消除一些限制,例如允许机器人以 5000 人为一组进行批量操作,而不是像普通用户那样以 500 人为一组进行批量操作。不确定这个脚本是否有必要,但它不会受到伤害。
    • 当使用 LdapAuthentication 之类的身份验证扩展时domain,必须设置该参数。并且必须将其设置为LDAP 源的名称LocalSettings.php,而不是域的实际名称。
    • 在运行脚本之前禁用电子邮件通知。否则,每个在 wiki 中观看页面的人都会收到他观看的每个更改页面的通知。对我来说,这是$wgEnableEmail = false;和$wgEnotifWatchlist = false;在 LocalSettings.php.
    • 我从命令行运行脚本,速度不快,1000 多页需要几分钟。如果我通过网络服务器运行它肯定会超时。

    最后但同样重要的是,migration_pending我添加到 MediaWiki 的模板:

    {|class=warningbox
     | [[Image:Emblem-important.png]]
     | This page hasn't been audited yet, the information on it could be outdated. If you are responsible for this page, please check it's content. If it is still current, add it to the new wiki and change this template to <nowiki>{{Migration_done}}</nowiki>. If the information on this page is not needed anymore change the template to <nowiki>{{Migration_discarded}}</nowiki>
     |}
    
    [[Category:MigrationPending]]
    

    这对我们之前使用的表格使用 CSS 类,并将页面添加到特定类别。migration_done我添加了与migration_discarded相应类别类似的模板。

    • 0

相关问题

  • 用户特定的 Php.ini 当 php 作为模块运行时?

  • 使 php mail() 函数在 ubuntu-server 上工作的步骤是什么?

  • Web 服务器和数据库服务器位于完全不同的位置

  • PHP 作为 CGI 还是 Apache 模块?

  • 通过 VPN 连接什么是远程服务器 IP?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve