]>
Commit | Line | Data |
---|---|---|
7a32b172 | 1 | <!DOCTYPE html> |
2 | <html> | |
3 | <head> | |
4 | <meta charset="utf-8"> | |
5 | <meta name="generator" content="pandoc"> | |
6 | <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> | |
7 | <title></title> | |
8 | <style type="text/css">code{white-space: pre;}</style> | |
9 | <!--[if lt IE 9]> | |
10 | <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> | |
11 | <![endif]--> | |
12 | <link rel="stylesheet" href="github-markdown.css"> | |
13 | </head> | |
14 | <body> | |
15 | <h3 id="download-css-styles-for-shaarlis-listed-in-an-opml-file">Download CSS styles for shaarlis listed in an opml file</h3> | |
16 | <p>Example php script:</p> | |
17 | <pre><code><!---- ?php --> | |
18 | <!---- Copyright (c) 2014 Nicolas Delsaux (https://github.com/Riduidel) --> | |
19 | <!---- License: zlib (http://www.gzip.org/zlib/zlib_license.html) --> | |
20 | ||
21 | /** | |
22 | * Source: https://github.com/Riduidel | |
23 | * Download css styles for shaarlis listed in an opml file | |
24 | */ | |
25 | define("SHAARLI_RSS_OPML", "https://www.ecirtam.net/shaarlirss/custom/people.opml"); | |
26 | ||
27 | define("THEMES_TEMP_FOLDER", "new_themes"); | |
28 | ||
29 | if(!file_exists(THEMES_TEMP_FOLDER)) { | |
30 | mkdir(THEMES_TEMP_FOLDER); | |
31 | } | |
32 | ||
33 | function siteUrl($pathInSite) { | |
34 | $indexPos = strpos($pathInSite, "index.php"); | |
35 | if(!$indexPos) { | |
36 | return $pathInSite; | |
37 | } else { | |
38 | return substr($pathInSite, 0, $indexPos); | |
39 | } | |
40 | } | |
41 | ||
42 | function createShaarliHashFromOPMLL($opmlFile) { | |
43 | $result = array(); | |
44 | $opml = file_get_contents($opmlFile); | |
45 | $opmlXml = simplexml_load_string($opml); | |
46 | $outlineElements = $opmlXml->xpath("body/outline"); | |
47 | foreach($outlineElements as $site) { | |
48 | $siteUrl = siteUrl((string) $site['htmlUrl']); | |
49 | $result[$siteUrl]=((string) $site['text']); | |
50 | } | |
51 | return $result; | |
52 | } | |
53 | ||
54 | function getSiteFolder($url) { | |
55 | $domain = parse_url($url, PHP_URL_HOST); | |
56 | return THEMES_TEMP_FOLDER."/".str_replace(".", "_", $domain); | |
57 | } | |
58 | ||
59 | function get_http_response_code($theURL) { | |
60 | $headers = get_headers($theURL); | |
61 | return substr($headers[0], 9, 3); | |
62 | } | |
63 | ||
64 | /** | |
65 | * This makes the code PHP-5 only (particularly the call to "get_headers") | |
66 | */ | |
67 | function copyUserStyleFrom($url, $name, $knownStyles) { | |
68 | $userStyle = $url."inc/user.css"; | |
69 | if(in_array($url, $knownStyles)) { | |
70 | // TODO add log message | |
71 | } else { | |
72 | $statusCode = get_http_response_code($userStyle); | |
73 | if(intval($statusCode)<300) { | |
74 | $styleSheet = file_get_contents($userStyle); | |
75 | $siteFolder = getSiteFolder($url); | |
76 | if(!file_exists($siteFolder)) { | |
77 | mkdir($siteFolder); | |
78 | } | |
79 | if(!file_exists($siteFolder.'/user.css')) { | |
80 | // Copy stylesheet | |
81 | file_put_contents($siteFolder.'/user.css', $styleSheet); | |
82 | } | |
83 | if(!file_exists($siteFolder.'/README.md')) { | |
84 | // Then write a readme.md file | |
85 | file_put_contents($siteFolder.'/README.md', | |
86 | "User style from ".$name."\n" | |
87 | ."=============================" | |
88 | ."\n\n" | |
89 | ."This stylesheet was downloaded from ".$userStyle." on ".date(DATE_RFC822) | |
90 | ); | |
91 | } | |
92 | if(!file_exists($siteFolder.'/config.ini')) { | |
93 | // Write a config file containing useful informations | |
94 | file_put_contents($siteFolder.'/config.ini', | |
95 | "site_url=".$url."\n" | |
96 | ."site_name=".$name."\n" | |
97 | ); | |
98 | } | |
99 | if(!file_exists($siteFolder.'/home.png')) { | |
100 | // And finally copy generated thumbnail | |
101 | $homeThumb = $siteFolder.'/home.png'; | |
102 | file_put_contents($siteFolder.'/home.png', file_get_contents(getThumbnailUrl($url))); | |
103 | } | |
104 | echo 'Theme have been downloaded from <a href="'.$url.'">'.$url.'</a> into '.$siteFolder | |
105 | .'. It looks like <img src="'.$homeThumb.'"><br/>'; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | function getThumbnailUrl($url) { | |
111 | return 'http://api.webthumbnail.org/?url='.$url; | |
112 | } | |
113 | ||
114 | function copyUserStylesFrom($urlToNames, $knownStyles) { | |
115 | foreach($urlToNames as $url => $name) { | |
116 | copyUserStyleFrom($url, $name, $knownStyles); | |
117 | } | |
118 | } | |
119 | ||
120 | /** | |
121 | * Reading directory list, courtesy of http://www.laughing-buddha.net/php/dirlist/ | |
122 | * @param directory the directory we want to list files of | |
123 | * @return a simple array containing the list of absolute file paths. Notice that current file (".") and parent one("..") | |
124 | * are not listed here | |
125 | */ | |
126 | function getDirectoryList ($directory) { | |
127 | $realPath = realpath($directory); | |
128 | // create an array to hold directory list | |
129 | $results = array(); | |
130 | // create a handler for the directory | |
131 | $handler = opendir($directory); | |
132 | // open directory and walk through the filenames | |
133 | while ($file = readdir($handler)) { | |
134 | // if file isn't this directory or its parent, add it to the results | |
135 | if ($file != "." && $file != "..") { | |
136 | $results[] = realpath($realPath . "/" . $file); | |
137 | } | |
138 | } | |
139 | // tidy up: close the handler | |
140 | closedir($handler); | |
141 | // done! | |
142 | return $results; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Start in themes folder and look in all subfolders for config.ini files. | |
147 | * These config.ini files allow us not to download styles again and again | |
148 | */ | |
149 | function findKnownStyles() { | |
150 | $result = array(); | |
151 | $subFolders = getDirectoryList("themes"); | |
152 | foreach($subFolders as $folder) { | |
153 | $configFile = $folder."/config.ini"; | |
154 | if(file_exists($configFile)) { | |
155 | $iniParameters = parse_ini_file($configFile); | |
156 | array_push($result, $iniParameters['site_url']); | |
157 | } | |
158 | } | |
159 | return $result; | |
160 | } | |
161 | ||
162 | $knownStyles = findKnownStyles(); | |
163 | copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles); | |
164 | ||
165 | <!--- ? ----></code></pre> | |
166 | </body> | |
167 | </html> |