$dba = wfGetDB(DB_MASTER);
# Do nothing if the tables exist
if ($dba->tableExists('dympage') || $dba->tableExists('dymnorm')) {
    echo "The tables already exist. No action was taken.\n";
} else {
    $sql = $dba->getType() == 'postgres' ? dirname(__FILE__) . '/didyoumean.pg.sql' : dirname(__FILE__) . '/didyoumean.sql';
    echo "Sourcing: {$sql}\n";
    $res = $dba->sourceFile($sql);
    echo "Result: {$res}\n";
    if ($res) {
        echo "The tables have been set up correctly.\n";
        require_once 'DYMNorm.php';
        $result = $dba->select('page', array('page_title', 'page_id'), array('page_namespace' => 0, 'page_is_redirect' => 0));
        foreach ($result as $row) {
            #echo "$row->page_title\n";
            $norm = wfDymNormalise($row->page_title);
            # *new* table using numeric columns where possible
            $theid = $dba->selectField('dymnorm', 'dn_normid', array('dn_normtitle' => $norm));
            if ($theid) {
                echo "old: {$row->page_title} ->\t{$norm} = {$theid}\n";
            } else {
                $normid = $dba->nextSequenceValue('dymnorm_dn_normid_seq');
                $dba->insert('dymnorm', array('dn_normid' => $normid, 'dn_normtitle' => $norm));
                $theid = $dba->insertId();
                echo "NEW: {$row->page_title} ->\t{$norm} = {$theid}\n";
            }
            $dba->insert('dympage', array('dp_pageid' => $row->page_id, 'dp_normid' => $theid));
        }
        $dba->freeResult($result);
    }
}
 public static function doUpdate($pageid, $title)
 {
     wfDebug('HIPP: ' . __METHOD__ . " MOVE\n");
     $dbw = wfGetDB(DB_MASTER);
     $norm = wfDymNormalise($title);
     $normid = $dbw->selectField('dymnorm', 'dn_normid', array('dn_normtitle' => $norm));
     if ($normid) {
         wfDebug("HIPP: old: {$title} ->\t{$norm} = {$normid}\n");
     } else {
         $nsvid = $dbw->nextSequenceValue('dymnorm_dn_normid_seq');
         $dbw->insert('dymnorm', array('dn_normid' => $nsvid, 'dn_normtitle' => $norm));
         $normid = $dbw->insertId();
         wfDebug("HIPP: NEW: {$title} ->\t{$norm} = {$normid}\n");
     }
     $oldnormid = $dbw->selectField('dympage', 'dp_normid', array('dp_pageid' => $pageid));
     if ($oldnormid != $normid) {
         $dbw->update('dympage', array('dp_normid' => $normid), array('dp_pageid' => $pageid));
         $count = $dbw->selectField('dympage', 'COUNT(*)', array('dp_normid' => $oldnormid));
         if ($count == 0) {
             $dbw->delete('dymnorm', array('dn_normid' => $oldnormid));
         }
         # touch all pages which linked to the old name or will link to the new one
         self::touchPages("(dp_normid={$normid} OR dp_normid={$oldnormid})");
     }
 }