]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - doc/md/Download-CSS-styles-from-an-OPML-list.md
Generate HTML documentation using MkDocs (WIP)
[github/shaarli/Shaarli.git] / doc / md / Download-CSS-styles-from-an-OPML-list.md
CommitLineData
1acc87ee 1###Download CSS styles for shaarlis listed in an opml file
2Example php script:
3
992af0b9 4```php
1acc87ee 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 */
13define("SHAARLI_RSS_OPML", "https://www.ecirtam.net/shaarlirss/custom/people.opml");
14
15define("THEMES_TEMP_FOLDER", "new_themes");
16
17if(!file_exists(THEMES_TEMP_FOLDER)) {
18 mkdir(THEMES_TEMP_FOLDER);
19}
20
21function 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
30function 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) {
53ed6d7d 36 $siteUrl = siteUrl((string) $site['htmlUrl']);
37 $result[$siteUrl]=((string) $site['text']);
1acc87ee 38 }
39 return $result;
40}
41
42function getSiteFolder($url) {
43 $domain = parse_url($url, PHP_URL_HOST);
44 return THEMES_TEMP_FOLDER."/".str_replace(".", "_", $domain);
45}
46
47function get_http_response_code($theURL) {
48 $headers = get_headers($theURL);
53ed6d7d 49 return substr($headers[0], 9, 3);
1acc87ee 50}
51
52/**
53 * This makes the code PHP-5 only (particularly the call to "get_headers")
54 */
55function 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
98function getThumbnailUrl($url) {
99 return 'http://api.webthumbnail.org/?url='.$url;
100}
101
102function 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 */
114function 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 != "..") {
53ed6d7d 124 $results[] = realpath($realPath . "/" . $file);
1acc87ee 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 */
137function 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);
53ed6d7d 144 array_push($result, $iniParameters['site_url']);
1acc87ee 145 }
146 }
147 return $result;
148}
149
150$knownStyles = findKnownStyles();
151copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles);
152
153<!--- ? ---->
53ed6d7d 154```