/**
  * Article save complete is called when the article is saved. We use this
  * hook to track when translated articles are saved, and switch back to current user after saving article.
  */
 static function onSaveComplete(&$article, &$user, $text, $summary, $minor, $a, $b, &$flags, $revision)
 {
     global $wgLanguageCode, $wgUser;
     if ($flags & EDIT_NEW && self::isTranslatorUser()) {
         //Switch back to our user
         global $whgOurUser;
         $user = $whgOurUser;
         $wgUser = $whgOurUser;
         //Add translation link information to database
         $toTitle = $article->getTitle();
         if (preg_match("/\\[\\[en:([^\\]]+)\\]\\]/", $text, $matches)) {
             $fromTitle = urldecode($matches[1]);
             $fromTitle = str_replace(" ", "-", $fromTitle);
             $json = json_decode(self::getArticleRevisionInfo($fromTitle), true);
             $ak = array_keys($json['query']['pages']);
             $fromAID = $ak[0];
             $fromRevisionId = $json['query']['pages'][$fromAID]['revisions'][0]['revid'];
             $tl = new TranslationLink();
             $tl->fromAID = $fromAID;
             $tl->fromLang = "en";
             $tl->toLang = $wgLanguageCode;
             $tl->toAID = $toTitle->getArticleId();
             $tl->insert();
             TranslationLink::writeLog(TranslationLink::ACTION_SAVE, "en", $fromRevisionId, $fromAID, $fromTitle, $wgLanguageCode, $toTitle->getText(), $toTitle->getArticleId());
         } else {
             // Error, we should have an interwiki link on the page. We will still log it.
             TranslationLink::writeLog(TranslationLink::ACTION_SAVE, "en", NULL, NULL, NULL, $wgLanguageCode, $toTitle->getText(), $toTitle->getArticleId());
         }
     }
     return true;
 }
 /**
  * EXECUTE
  **/
 function execute($par)
 {
     global $wgRequest, $wgOut, $wgUser, $wgLanguageCode, $isDevHost, $wgActiveLanguages;
     if ($wgUser->isBlocked()) {
         $wgOut->blockedPage();
         return;
     }
     $userGroups = $wgUser->getGroups();
     if ($wgUser->getID() == 0 || !(in_array('sysop', $userGroups) || in_array('staff', $userGroups)) || !(IS_SPARE_HOST || $isDevHost)) {
         $wgOut->setRobotpolicy('noindex,nofollow');
         $wgOut->showErrorPage('nosuchspecialpage', 'nospecialpagetext');
         return;
     }
     $action = $wgRequest->getVal('action', NULL);
     if ($wgRequest->wasPosted() && $_FILES['upload']) {
         ini_set('memory_limit', '2048M');
         $fileName = $_FILES['upload']['tmp_name'];
         $list = file_get_contents($fileName);
         set_time_limit(0);
         $lines = self::parseURLlist($list);
         self::httpDownloadHeaders();
         $urls = array();
         foreach ($lines as $line) {
             $urls[] = $line['url1'];
             $urls[] = $line['url2'];
         }
         $pageInfo = Misc::getPagesFromURLs($urls, array("page_title", "page_id"));
         $links = array();
         foreach ($lines as $line) {
             $link = new TranslationLink();
             $link->fromURL = $line['url1'];
             $link->toURL = $line['url2'];
             if (isset($pageInfo[$line['url1']]) && isset($pageInfo[$line['url2']])) {
                 $p1 = $pageInfo[$line['url1']];
                 $p2 = $pageInfo[$line['url2']];
                 $link->fromLang = $p1['lang'];
                 $link->fromAID = $p1['page_id'];
                 $link->toLang = $p2['lang'];
                 $link->toAID = $p2['page_id'];
             }
             $links[] = $link;
         }
         TranslationLink::batchUpdateTLStatus($links);
         TranslationLink::batchAddTranslationLinks($links);
         foreach ($links as $link) {
             if ($link->fromLang != NULL && $link->fromAID != NULL && $link->toLang != NULL && $link->toAID != NULL) {
                 TranslationLink::writeLog(TranslationLink::ACTION_SAVE, $link->fromLang, NULL, $link->fromAID, $link->getFromPage(), $link->toLang, $link->getToPage(), $link->toAID, "TRANSLATION_OVERRIDE");
             }
             if ($link->tlStatus == TranslationLink::TL_STATUS_SAVED) {
                 if ($link->oldTlStatus == TranslationLink::TL_STATUS_UPDATEABLE) {
                     $message = "Updated existing link";
                 } elseif ($link->oldTlStatus == TranslationLink::TL_STATUS_SAVED) {
                     $message = "Already saved";
                 } elseif ($link->oldTlStatus == TranslationLink::TL_STATUS_NEW) {
                     $message = "Saved Link";
                 } else {
                     $message = "Already saved";
                 }
             } elseif ($link->tlStatus == TranslationLink::TL_STATUS_NON_UPDATEABLE) {
                 $message = "Conflicting existings links, unable to save";
             } else {
                 $message = "Unable to save, URLs invalid or not found";
             }
             print $link->fromURL . "\t" . $link->toURL . "\t" . $message . "\n";
         }
         // Hack to override MediaWiki functionality
         exit;
         return true;
     } elseif ($action == null) {
         EasyTemplate::set_path(dirname(__FILE__));
         $tmpl = EasyTemplate::html("TranslationLinkOverride.tmpl.php");
         $wgOut->addHTML($tmpl);
         return true;
     } elseif ($action == "fetchlinks") {
         $id = $wgRequest->getVal('id', null);
         $lang = $wgRequest->getVal('lang', null);
         $this->fetchLinks($lang, $id);
         return true;
     } elseif ($action == "search") {
         EasyTemplate::set_path(dirname(__FILE__));
         $langs = $wgActiveLanguages;
         $langs[] = 'en';
         $tmpl = EasyTemplate::html("TranslationLinkOverride.delete.tmpl.php", array('langs' => $langs));
         $wgOut->addHTML($tmpl);
         return true;
     } elseif ($action == "dodelete") {
         $fromId = $wgRequest->getVal('fromId', null);
         $fromLang = $wgRequest->getVal('fromLang', null);
         $toId = $wgRequest->getVal('toId', null);
         $toLang = $wgRequest->getVal('toLang', null);
         $this->doDelete($fromLang, $fromId, $toLang, $toId);
     }
     return false;
 }