/**
* Prepares a passed in data array with img elements that need to be fetched from remote server and changed to point to a local resource.
*
* @param DOMDocument $doc The DOMDocument to evaluate img elements for
* @param \RollingCurl\RollingCurl $rollingCurl The RollingCurl instance to add our requests to
* @param string $tempDirPath The directory to store images
* @param array $imgData The data array to populate
*/
private function prepareImageRequests($doc, $rollingCurl, $tempDirPath, $imgData)
{
global $wgServer;
// Ensure there's a trailing slash after our $wgServer name
if (substr($wgServer, -1) !== '/') {
$search = $wgServer .= '/';
} else {
$search = $wgServer;
}
$imgElements = $doc->getElementsByTagName('img');
foreach ($imgElements as $imgElement) {
$src = $imgElement->getAttribute('src');
// Strip the server and slash from our image src
$localPath = str_replace($search, '', $src);
$pathInfo = pathinfo($localPath);
// Create the directory locally to mimic the directory structure of the server
$localPath = $tempDirPath . '/' . $pathInfo['dirname'];
if (!is_dir($localPath)) {
$result = mkdir($localPath, 0777, true);
if (!$result) {
throw new Exception("Failed to create temp directory: {$localPath}");
}
}
$localPath = $localPath .= '/' . $pathInfo['basename'];
$zipPath = $pathInfo['dirname'] . '/' . $pathInfo['basename'];
$request = new \RollingCurl\Request($src);
$imgData[] = array('src' => $src, 'element' => $imgElement, 'extension' => $pathInfo['extension'], 'local_path' => $localPath, 'new_path' => $zipPath, 'request' => $request);
$rollingCurl->add($request);
}
}