MyShaarli/doc/Download-CSS-styles-for-shaarlis-listed-in-an-opml-file.html
nodiscc 7a32b172b8 add local HTML documentation
generated with 'make htmldoc'
2015-04-05 22:37:15 +02:00

167 lines
6.1 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="github-markdown.css">
</head>
<body>
<h3 id="download-css-styles-for-shaarlis-listed-in-an-opml-file">Download CSS styles for shaarlis listed in an opml file</h3>
<p>Example php script:</p>
<pre><code>&lt;!---- ?php --&gt;
&lt;!---- Copyright (c) 2014 Nicolas Delsaux (https://github.com/Riduidel) --&gt;
&lt;!---- License: zlib (http://www.gzip.org/zlib/zlib_license.html) --&gt;
/**
* Source: https://github.com/Riduidel
* Download css styles for shaarlis listed in an opml file
*/
define(&quot;SHAARLI_RSS_OPML&quot;, &quot;https://www.ecirtam.net/shaarlirss/custom/people.opml&quot;);
define(&quot;THEMES_TEMP_FOLDER&quot;, &quot;new_themes&quot;);
if(!file_exists(THEMES_TEMP_FOLDER)) {
mkdir(THEMES_TEMP_FOLDER);
}
function siteUrl($pathInSite) {
$indexPos = strpos($pathInSite, &quot;index.php&quot;);
if(!$indexPos) {
return $pathInSite;
} else {
return substr($pathInSite, 0, $indexPos);
}
}
function createShaarliHashFromOPMLL($opmlFile) {
$result = array();
$opml = file_get_contents($opmlFile);
$opmlXml = simplexml_load_string($opml);
$outlineElements = $opmlXml-&gt;xpath(&quot;body/outline&quot;);
foreach($outlineElements as $site) {
$siteUrl = siteUrl((string) $site[&#39;htmlUrl&#39;]);
$result[$siteUrl]=((string) $site[&#39;text&#39;]);
}
return $result;
}
function getSiteFolder($url) {
$domain = parse_url($url, PHP_URL_HOST);
return THEMES_TEMP_FOLDER.&quot;/&quot;.str_replace(&quot;.&quot;, &quot;_&quot;, $domain);
}
function get_http_response_code($theURL) {
$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
}
/**
* This makes the code PHP-5 only (particularly the call to &quot;get_headers&quot;)
*/
function copyUserStyleFrom($url, $name, $knownStyles) {
$userStyle = $url.&quot;inc/user.css&quot;;
if(in_array($url, $knownStyles)) {
// TODO add log message
} else {
$statusCode = get_http_response_code($userStyle);
if(intval($statusCode)&lt;300) {
$styleSheet = file_get_contents($userStyle);
$siteFolder = getSiteFolder($url);
if(!file_exists($siteFolder)) {
mkdir($siteFolder);
}
if(!file_exists($siteFolder.&#39;/user.css&#39;)) {
// Copy stylesheet
file_put_contents($siteFolder.&#39;/user.css&#39;, $styleSheet);
}
if(!file_exists($siteFolder.&#39;/README.md&#39;)) {
// Then write a readme.md file
file_put_contents($siteFolder.&#39;/README.md&#39;,
&quot;User style from &quot;.$name.&quot;\n&quot;
.&quot;=============================&quot;
.&quot;\n\n&quot;
.&quot;This stylesheet was downloaded from &quot;.$userStyle.&quot; on &quot;.date(DATE_RFC822)
);
}
if(!file_exists($siteFolder.&#39;/config.ini&#39;)) {
// Write a config file containing useful informations
file_put_contents($siteFolder.&#39;/config.ini&#39;,
&quot;site_url=&quot;.$url.&quot;\n&quot;
.&quot;site_name=&quot;.$name.&quot;\n&quot;
);
}
if(!file_exists($siteFolder.&#39;/home.png&#39;)) {
// And finally copy generated thumbnail
$homeThumb = $siteFolder.&#39;/home.png&#39;;
file_put_contents($siteFolder.&#39;/home.png&#39;, file_get_contents(getThumbnailUrl($url)));
}
echo &#39;Theme have been downloaded from &lt;a href=&quot;&#39;.$url.&#39;&quot;&gt;&#39;.$url.&#39;&lt;/a&gt; into &#39;.$siteFolder
.&#39;. It looks like &lt;img src=&quot;&#39;.$homeThumb.&#39;&quot;&gt;&lt;br/&gt;&#39;;
}
}
}
function getThumbnailUrl($url) {
return &#39;http://api.webthumbnail.org/?url=&#39;.$url;
}
function copyUserStylesFrom($urlToNames, $knownStyles) {
foreach($urlToNames as $url =&gt; $name) {
copyUserStyleFrom($url, $name, $knownStyles);
}
}
/**
* Reading directory list, courtesy of http://www.laughing-buddha.net/php/dirlist/
* @param directory the directory we want to list files of
* @return a simple array containing the list of absolute file paths. Notice that current file (&quot;.&quot;) and parent one(&quot;..&quot;)
* are not listed here
*/
function getDirectoryList ($directory) {
$realPath = realpath($directory);
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = opendir($directory);
// open directory and walk through the filenames
while ($file = readdir($handler)) {
// if file isn&#39;t this directory or its parent, add it to the results
if ($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot;) {
$results[] = realpath($realPath . &quot;/&quot; . $file);
}
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
/**
* Start in themes folder and look in all subfolders for config.ini files.
* These config.ini files allow us not to download styles again and again
*/
function findKnownStyles() {
$result = array();
$subFolders = getDirectoryList(&quot;themes&quot;);
foreach($subFolders as $folder) {
$configFile = $folder.&quot;/config.ini&quot;;
if(file_exists($configFile)) {
$iniParameters = parse_ini_file($configFile);
array_push($result, $iniParameters[&#39;site_url&#39;]);
}
}
return $result;
}
$knownStyles = findKnownStyles();
copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles);
&lt;!--- ? ----&gt;</code></pre>
</body>
</html>