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