/**
     * Generates the XML description of all terms in the data source that match 
     * the input policy.
     * @param string $dataSourceSpec
     * 		The XML structure from getSourceSpecification, filled with the data 
     * 		the user entered.
     * @param string $importSet
     * 		One of the <importSet>-elements from the XML structure from 
     * 		getImportSets() or empty.
     * @param string $inputPolicy
     * 		The XML structure of the input policy. It contains the specification
     * 		of the terms to import and their properties.
     * @param string $conflictPolicy
     * 		The XML structure of the conflict policy. It defines if existing articles
     * 		are overwritten or not.
     *  
     * @param boolean $createTermList
     * 		If <true>, the XML structure for <getTermList> is created otherwise
     * 		the one for <getTerms>
     * 
     * @return string
     *		An XML structure that contains all requested terms.
     * 		If the operation fails, an error message is returned.
     * 		Example:
     * 		<?xml version="1.0"?>
     *		<ReturnValue xmlns="http://www.ontoprise.de/smwplus#">
     *		    <value>false</value>
     *		    <message>The specified data source does not exist.</message>
     * 		</ReturnValue>
     *  
     */
    private function createTerms($dataSourceSpec, $importSet, $inputPolicy, $conflictPolicy, $createTermList)
    {
        $wiki = $this->getSourceWikiFromSpec($dataSourceSpec);
        if (!array_key_exists($wiki, $this->mWikiAPIs)) {
            echo "Unknown wiki: {$wiki}\n";
            return '<?xml version="1.0"?>
					<ReturnValue xmlns="http://www.ontoprise.de/smwplus#">
						<value>false</value>
						<message>The specified data source "' . $wiki . '" does not exist.</message>
					</ReturnValue>';
        }
        $iai = new IAIArticleImporter($this->mWikiAPIs[$wiki]);
        $importSets = $this->parseImportSets($importSet);
        $policy = $this->parseInputPolicy($inputPolicy);
        $terms = '';
        // Find the terms in the source wiki closest to the requested terms
        $ipTerms = $policy['terms'];
        foreach ($ipTerms as $k => $term) {
            if (empty($term)) {
                continue;
            }
            // check if the term is available in the source wiki
            $term = trim($term);
            $ns = 0;
            $t = Title::newFromText($term);
            if ($t) {
                $ns = $t->getNamespace();
                $term = $t->getText();
            }
            $matches = $iai->getArticles($term, $ns, 1);
            $ipTerms[$k] = $matches[0];
        }
        if ($createTermList) {
            foreach ($ipTerms as $t) {
                $terms .= "<articleName>" . $t . "</articleName>\n";
            }
        } else {
            // Conflict policy
            $overwrite = true;
            preg_match('/<overwriteExistingTerms.*?>(.*?)<\\/overwriteExistingTerms>/i', $conflictPolicy, $overwriteMatch);
            if (count($overwriteMatch) == 2) {
                $overwrite = $overwriteMatch[1] == 'true';
                echo "\nSkip existing articles: " . ($overwrite ? "false\n" : "true\n");
            }
            echo "Importing from wiki: {$wiki}\n";
            echo "Articles to import: \n";
            foreach ($ipTerms as $t) {
                echo "{$t}\n";
            }
            echo "\n";
            $importArticles = true;
            $importTemplates = true;
            $importImages = true;
            echo "Import sets: ";
            if (!empty($importSets)) {
                if (!in_array('Articles', $importSets)) {
                    $importArticles = false;
                }
                if (!in_array('Templates', $importSets)) {
                    $importTemplates = false;
                }
                if (!in_array('Images', $importSets)) {
                    $importImages = false;
                }
            }
            if ($importArticles) {
                echo "Articles, ";
            }
            if ($importTemplates) {
                echo "Templates, ";
            }
            if ($importImages) {
                echo "Images ";
            }
            echo "\n";
            try {
                $iai->startReport();
                if ($importArticles) {
                    $iai->importArticles($ipTerms, $importTemplates, $importImages, !$overwrite);
                } else {
                    if ($importTemplates) {
                        $iai->importTemplates($ipTerms);
                    }
                    if ($importImages) {
                        $iai->importImagesForArticle($ipTerms);
                    }
                }
                if (!empty($images)) {
                    $iai->importImages($images, true);
                }
            } catch (Exception $e) {
                echo "Caught an exception: \n" . $e->getMessage();
                return '<?xml version="1.0"?>
						<ReturnValue xmlns="http://www.ontoprise.de/smwplus#">
							<value>false</value>
							<message>Caught an exception: ' . $e->getMessage() . '</message>
						</ReturnValue>';
            }
            $report = $iai->createReport(true);
            echo "Saved report for articles in: {$report}\n";
            $terms .= "<term>\n";
            $terms .= "<articleName>{$report}_TIF</articleName>\n";
            $terms .= "<linkToReport>{$report}</linkToReport>\n";
            $terms .= "</term>\n";
        }
        return '<?xml version="1.0"?>' . "\n" . '<terms xmlns="http://www.ontoprise.de/smwplus#">' . "\n" . $terms . '</terms>' . "\n";
    }
Beispiel #2
0
 /**
  * This method is called by the bot framework. 
  */
 public function run($paramArray, $isAsync, $delay)
 {
     echo "...started!\n";
     var_dump($paramArray);
     $result = "";
     global $iaigIP;
     $iaigLog = fopen("{$iaigIP}/IAIBot.log", "a");
     fprintf($iaigLog, "Import Bot started.\n");
     $log = SGAGardeningIssuesAccess::getGardeningIssuesAccess();
     $this->setNumberOfTasks(1);
     // Two sub-tasks: update template, update images
     $this->addSubTask(2);
     global $iaigWikiApi;
     $ai = new IAIArticleImporter($iaigWikiApi);
     try {
         echo "Importing from {$iaigWikiApi}\n";
         // Import templates of article
         $article = $paramArray["article"];
         echo "Start report\n";
         $ai->startReport();
         echo "Import templates\n";
         $ai->importTemplates(array($article));
         $this->worked(1);
         $log->addGardeningIssueAboutValue($this->id, IAI_IMPORTBOT_PROCESSED_TASK, Title::newFromText($article), "Templates");
         echo "import Images\n";
         // Import images of article
         $ai->importImagesForArticle(array($article));
         $log->addGardeningIssueAboutValue($this->id, IAI_IMPORTBOT_PROCESSED_TASK, Title::newFromText($article), "Images");
         echo "Create report\n";
         $this->worked(1);
     } catch (Exception $e) {
         echo "Caught an exception: \n" . $e->getMessage();
     }
     $report = $ai->createReport(true);
     $log->addGardeningIssueAboutArticle($this->id, IAI_IMPORTBOT_REPORT, Title::newFromText($report));
     echo "...done.\n";
     fclose($iaigLog);
     return $result;
 }