/** * Imports the CSV file specified by $pathToFile * @param string $pathToFile * @return Graph */ public function import($pathToFile) { $lines = IOUtil::readCsvFile($pathToFile, $this->hasHeader, $this->encoding, $this->delimiter, $this->enclosure, null, null, $this->offset); $urls = []; $graph = new Graph(); foreach ($lines as $nr => $line) { if (!array_key_exists($this->sourceColumn, $line) || !array_key_exists($this->destinationColumn, $line)) { continue; } if (!parse_url($line[$this->sourceColumn]) || !parse_url($line[$this->destinationColumn])) { continue; } $urlFrom = trim($line[$this->sourceColumn]); if (!array_key_exists($urlFrom, $urls)) { $urls[$urlFrom] = new Node($urlFrom); } $urlTo = trim($line[$this->destinationColumn]); if (!array_key_exists($urlTo, $urls)) { $urls[$urlTo] = new Node($urlTo); } $edge = new Edge($urls[$urlFrom], $urls[$urlTo]); $graph->addEdge($edge); } return $graph; }
<?php use paslandau\IOUtility\IOUtil; use paslandau\PageRank\Calculation\PageRank; use paslandau\PageRank\Calculation\ResultFormatter; use paslandau\PageRank\Import\CsvImporter; use paslandau\PageRank\Import\ScreamingFrogCsvImporter; require_once __DIR__ . "/bootstrap.php"; $csvImporter = new ScreamingFrogCsvImporter(); $pathToFile = __DIR__ . "/resources/screaming-frog.csv"; $graph = $csvImporter->import($pathToFile); $pageRank = new PageRank(); $result = $pageRank->calculatePagerank($graph); $formatter = new ResultFormatter(4); echo $formatter->toString($result); //export result to CSV $pathToExportFolder = __DIR__ . "/export"; IOUtil::createDirectoryIfNotExists($pathToExportFolder); $pathToExportFile = IOUtil::combinePaths($pathToExportFolder, "screaming-frog-result.csv"); $finalResult = $result->getLastHistoryEntry(); $rows = $finalResult->toArray(); IOUtil::writeCsvFile($pathToExportFile, $rows, true, "utf-8", ","); echo "Exported the result of the PageRank calculation to {$pathToExportFile}.";