public function run()
 {
     global $wgServerName, $wgScriptPath, $wgServer, $wgScriptExtension;
     wfProfileIn('DSMWUpdateJob::run()');
     $urlServer = 'http://' . $wgServerName;
     $revids = array();
     $revids1 = array();
     $page_ids = array();
     // Getting all the revision ids of pages having been logootized
     $db = wfGetDB(DB_SLAVE);
     $model_table = $db->tableName('model');
     $sql = "SELECT `rev_id` FROM {$model_table}";
     $res = $db->query($sql);
     while ($row = $db->fetchObject($res)) {
         $revids[] = $row->rev_id;
     }
     $db->freeResult($res);
     // if (count($revids)==0) $revids=array();
     // Getting all the revision ids without the pages in the DSMW namespaces and
     // Administration_pull_site_addition and Administration_pull_site_addition pages
     $rev_table = $db->tableName('revision');
     $page_table = $db->tableName('page');
     $sql = "SELECT {$rev_table}.`rev_id` FROM {$rev_table}, {$page_table} WHERE\n     `rev_page`=`page_id` and `page_namespace`!= 110 and `page_namespace`!= 200\n        and `page_namespace`!= 210 and `page_namespace`!= 220\nand `page_title`!= \"Administration_pull_site_addition\"\nand `page_title` != \"Administration_push_site_addition\"";
     $res1 = $db->query($sql);
     while ($row = $db->fetchObject($res1)) {
         $revids1[] = $row->rev_id;
     }
     $db->freeResult($res1);
     // if (count($revids1)==0) $revids1=array();
     // Array_diff returns an array containing all the entries from $revids1 that are
     // not present in $revids.
     $diff = array_diff($revids1, $revids);
     // get page ids of these revisions (each id must be unique in the array)
     foreach ($diff as $id) {
         $page_id = $db->selectField('revision', 'rev_page', array('rev_id' => $id));
         $page_ids[] = $page_id;
     }
     $page_ids = array_unique($page_ids);
     sort($page_ids);
     // Now we can logootize:
     if (count($page_ids) != 0) {
         foreach ($page_ids as $pageid) {
             $title = Title::newFromID($pageid);
             $ns = $title->getNamespace();
             $lastRev = Revision::loadFromPageId($db, $pageid);
             $pageText = $lastRev->getText();
             // load an empty model
             $model = DSMWRevisionManager::loadModel(0);
             $logoot = new logootEngine($model);
             $listOp = $logoot->generate("", $pageText);
             $modelAfterIntegrate = $logoot->getModel();
             $tmp = serialize($listOp);
             $patchid = sha1($tmp);
             if ($ns == NS_FILE || $ns == NS_IMAGE || $ns == NS_MEDIA) {
                 $apiUrl = $wgServer . $wgScriptPath . "/api" . $wgScriptExtension;
                 $onPage = str_replace(array(' '), array('%20'), $lastRev->getTitle()->getText());
                 $download = $apiUrl . "?action=query&titles=File:" . $onPage . "&prop=imageinfo&format=php&iiprop=mime|url|size";
                 $resp = Http::get($download);
                 $resp = unserialize($resp);
                 $a = $resp['query']['pages'];
                 $a = current($a);
                 $a = $a['imageinfo'];
                 $a = current($a);
                 $mime = $a['mime'];
                 $size = $a['size'];
                 $url = $a['url'];
                 $patch = new DSMWPatch(false, true, null, $urlServer . $wgScriptPath, 0, null, null, null, $mime, $size, $url, null);
                 $patch->storePage('File:' . $lastRev->getTitle()->getText(), $lastRev->getId());
             } else {
                 $patch = new DSMWPatch(false, false, $listOp, $urlServer, 0);
                 $patch->storePage($lastRev->getTitle()->getText(), $lastRev->getId());
             }
             DSMWRevisionManager::storeModel($lastRev->getId(), $sessionId = session_id(), $modelAfterIntegrate, $blobCB = 0);
         }
     }
     wfProfileOut('DSMWUpdateJob::run()');
     return true;
 }
 /**
  * @since 1.1
  *
  * @param UploadForm $image
  *
  * @return true
  */
 public function onUploadComplete(UploadForm $image)
 {
     global $wgServerName, $wgScriptPath, $wgServer, $wgVersion;
     $urlServer = 'http://' . $wgServerName . $wgScriptPath;
     // $classe = get_class($image);
     if (compareMWVersion($wgVersion, '1.16.0') == -1) {
         $localfile = $image->mLocalFile;
     } else {
         $localfile = $image->getLocalFile();
     }
     $path = utils::prepareString($localfile->mime, $localfile->size, $wgServer . urldecode($localfile->url));
     if (!file_exists($path)) {
         $dbr = wfGetDB(DB_SLAVE);
         $lastRevision = Revision::loadFromTitle($dbr, $localfile->getTitle());
         if ($lastRevision->getPrevious() == null) {
             $rev_id = 0;
         } else {
             $rev_id = $lastRevision->getPrevious()->getId();
         }
         $revID = $lastRevision->getId();
         $model = DSMWRevisionManager::loadModel($rev_id);
         $patch = new DSMWPatch(false, true, null, $urlServer, $rev_id, null, null, null, $localfile->mime, $localfile->size, urldecode($localfile->url), null);
         $patch->storePage($localfile->getTitle(), $revID);
         // stores the patch in a wikipage
         DSMWRevisionManager::storeModel($revID, $sessionId = session_id(), $model, $blobCB = 0);
     }
     return true;
 }
/**
 * A ChangeSet has patches which has operations
 * this function is used to integrate these operations
 * It's a local changeSet (downloaded from a remote site)
 * @param <String> $changeSetId with NS
 */
function integrate($changeSetId, $patchIdList, $relatedPushServer, $csName)
{
    // global $wgScriptExtension;
    // $patchIdList = getPatchIdList($changeSetId);
    //  $lastPatch = utils::getLastPatchId($pageName);
    global $wgServerName, $wgScriptPath, $wgScriptExtension, $wgOut;
    $urlServer = 'http://' . $wgServerName . $wgScriptPath . "/index.php/{$csName}";
    wfDebugLog('p2p', ' - function integrate : ' . $changeSetId);
    $i = 1;
    $j = count($patchIdList);
    $pages = array();
    foreach ($patchIdList as $patchId) {
        $name = 'patch';
        $sub = substr($patchId, 6, 3);
        wfDebugLog('p2p', '  -> patchId : ' . $patchId);
        if (!utils::pageExist($patchId)) {
            // if this patch exists already, don't apply it
            wfDebugLog('p2p', '      -> patch unexist');
            $url = utils::lcfirst($relatedPushServer) . "/api.php?action=query&meta=patch&papatchId=" . $patchId . '&format=xml';
            wfDebugLog('p2p', '      -> getPatch request url ' . $url);
            $patch = utils::file_get_contents_curl($url);
            /*test if it is a xml file. If not, the server is not reachable via the url
             * Then we try to reach it with the .php5 extension
             */
            if (strpos($patch, "<?xml version=\"1.0\"?>") === false) {
                $url = utils::lcfirst($relatedPushServer) . "/api.php5?action=query&meta=patch&papatchId=" . $patchId . '&format=xml';
                wfDebugLog('p2p', '      -> getPatch request url ' . $url);
                $patch = utils::file_get_contents_curl($url);
            }
            if (strpos($patch, "<?xml version=\"1.0\"?>") === false) {
                $patch = false;
            }
            if ($patch === false) {
                throw new MWException(__METHOD__ . ': Cannot connect to Push Server (Patch API)');
            }
            $patch = trim($patch);
            wfDebugLog('p2p', '      -> patch content :' . $patch);
            $dom = new DOMDocument();
            $dom->loadXML($patch);
            $patchs = $dom->getElementsByTagName($name);
            // when the patch is not found, mostly when the id passed
            // through the url is wrong
            if (empty($patchs) || is_null($patchs)) {
                throw new MWException(__METHOD__ . ': Error: Patch not found!');
            }
            //        $patchID = null;
            foreach ($patchs as $p) {
                if ($p->hasAttribute("onPage")) {
                    $onPage = $p->getAttribute('onPage');
                }
                if ($p->hasAttribute("previous")) {
                    $previousPatch = $p->getAttribute('previous');
                }
                if ($p->hasAttribute("siteID")) {
                    $siteID = $p->getAttribute('siteID');
                }
                if ($p->hasAttribute("mime")) {
                    $Mime = $p->getAttribute('mime');
                }
                if ($p->hasAttribute("size")) {
                    $Size = $p->getAttribute('size');
                }
                if ($p->hasAttribute("url")) {
                    $Url = $p->getAttribute('url');
                }
                if ($p->hasAttribute("DateAtt")) {
                    $Date = $p->getAttribute('DateAtt');
                }
                if ($p->hasAttribute("siteUrl")) {
                    $SiteUrl = $p->getAttribute('siteUrl');
                }
                if ($p->hasAttribute("causal")) {
                    $causal = $p->getAttribute('causal');
                }
            }
            $operations = null;
            $op = $dom->getElementsByTagName('operation');
            foreach ($op as $o) {
                $operations[] = $o->firstChild->nodeValue;
            }
            $lastPatch = utils::getLastPatchId($onPage);
            if ($lastPatch == false) {
                $lastPatch = 'none';
            }
            //            foreach ($operations as $operation) {
            //                $operation = operationToLogootOp($operation);
            //                if ($operation!=false && is_object($operation)) {
            //                    logootIntegrate($operation, $onPage);
            //                }
            //            }
            if (!in_array($onPage, $pages)) {
                $onPage1 = str_replace(array(' '), array('_'), $onPage);
                utils::writeAndFlush("<span style=\"margin-left:60px;\">Page: <A HREF=" . 'http://' . $wgServerName . $wgScriptPath . "/index.php/{$onPage1}>" . $onPage . "</A></span><br/>");
                $pages[] = $onPage;
            }
            if ($sub === 'ATT') {
                touch(utils::prepareString($Mime, $Size, $Url));
                $DateLastPatch = utils::getLastAttPatchTimestamp($onPage);
                // $DateOtherPatch = utils::getOtherAttPatchTimestamp($patchIdList);
                if ($DateLastPatch == null) {
                    downloadFile($Url);
                    $edit = true;
                    utils::writeAndFlush("<span style=\"margin-left:98px;\">download attachment (" . round($Size / 1000000, 2) . "Mo)</span><br/>");
                } elseif ($DateLastPatch < $Date) {
                    downloadFile($Url);
                    $edit = true;
                    utils::writeAndFlush("<span style=\"margin-left:98px;\">download attachment (" . round($Size / 1000000, 2) . "Mo)</span><br/>");
                } else {
                    newRev($onPage);
                    $edit = false;
                }
                unlink(utils::prepareString($Mime, $Size, $Url));
            }
            utils::writeAndFlush("<span style=\"margin-left:80px;\">" . $i . "/" . $j . ": Integration of Patch: <A HREF=" . 'http://' . $wgServerName . $wgScriptPath . "/index.php/{$patchId}>" . $patchId . "</A></span><br/>");
            if ($sub === 'ATT') {
                $rev = logootIntegrateAtt($onPage, $edit);
                if ($rev > 0) {
                    $patch = new DSMWPatch(true, true, $operations, $SiteUrl, $causal, $patchId, $lastPatch, $siteID, $Mime, $Size, $Url, $Date);
                    $patch->storePage($onPage, $rev);
                } else {
                    throw new MWException(__METHOD__ . ': article not saved!');
                }
            } else {
                $rev = logootIntegrate($operations, $onPage, $sub);
                if ($rev > 0) {
                    $patch = new DSMWPatch(true, false, $operations, $SiteUrl, $causal, $patchId, $lastPatch, $siteID, null, null, null, null);
                    $patch->storePage($onPage, $rev);
                } else {
                    throw new MWException(__METHOD__ . ': article not saved!');
                }
            }
        }
        // end if pageExists
        $i++;
    }
    utils::writeAndFlush("<span style=\"margin-left:30px;\">Go to <A HREF=" . $urlServer . ">ChangeSet</A></span> <br/>");
}