Dotclear

source: plugins/popularityContest/inc/lib.popularityContest.php @ 2295

Revision 2295, 8.7 KB checked in by Moe, 14 years ago (diff)

Popularity Contest 1.4 :

  • added data in XML file :
    • display plugins URLs
    • display global results
  • added CSS rules, from the popcon website
  • make xml/ directory
  • switched to GPL v2
Line 
1<?php 
2# ***** BEGIN LICENSE BLOCK *****
3#
4# This file is part of Popularity Contest, a plugin for Dotclear 2
5# Copyright (C) 2007,2009,2010 Moe (http://gniark.net/)
6#
7# Popularity Contest is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License v2.0
9# as published by the Free Software Foundation.
10#
11# Popularity Contest is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public
17# License along with this program. If not, see
18# <http://www.gnu.org/licenses/>.
19#
20# Icon (icon.png) and images are from Silk Icons :
21# <http://www.famfamfam.com/lab/icons/silk/>
22#
23# ***** END LICENSE BLOCK *****
24
25class popularityContest
26{
27     public static $send_url = 'http://popcon.gniark.net/send.php';
28     public static $plugins_xml_url = 'http://popcon.gniark.net/raw2.xml';
29     
30     public static function getComboOptions()
31     {
32          $day_seconds = 24*3600;
33          $array = array(
34               __('weekly') => (7*$day_seconds),
35               __('every 3 days') => (3*$day_seconds),
36               __('daily') => ($day_seconds),
37          );
38          return($array);
39     }
40
41     # use : getPluginsArray(array('name','root','version')
42     public static function getPluginsArray($array_keys,$array_hidden_plugins=array())
43     {
44          $modules = $GLOBALS['core']->plugins->getModules();
45          $array = array();
46
47          foreach ($modules as $module => $module_values)
48          {
49               if (!in_array($module,$array_hidden_plugins))
50               {
51                    foreach ($array_keys as $key)
52                    {
53                         $array[$module][$key] = $module_values[$key];
54                    }
55               }
56          }
57
58          return($array);
59     }
60               
61     # send to Dotclear Popularity Contest
62     public static function send()
63     {
64          $settings =& $GLOBALS['core']->blog->settings;
65
66          $time_interval_last_try =
67               $_SERVER['REQUEST_TIME'] - $settings->popularityContest_last_try;
68          if ($time_interval_last_try < (30*60))
69          {
70               return(false);
71          }
72
73          $hidden_plugins = array();
74          if (strlen($settings->popularityContest_hidden_plugins) > 0)
75          {
76               $hidden_plugins = unserialize(base64_decode($settings->popularityContest_hidden_plugins));
77          }
78          if (!is_array($hidden_plugins)) {$hidden_plugins = array();}
79         
80          $settings->setNameSpace('popularitycontest');
81          # Popularity Contest last try
82          $settings->put('popularityContest_last_try',
83               $_SERVER['REQUEST_TIME'],'integer','Popularity Contest last try (Unix timestamp)',
84               true,true);
85
86          $url = self::$send_url;
87
88          # inspired by /dotclear/inc/core/class.dc.trackback.php
89          $client = netHttp::initClient($url,$path);
90          $client->setUserAgent('Dotclear - http://www.dotclear.net/');
91          $client->setPersistReferers(false);
92          $infos = array();
93          $infos['id'] = md5(DC_ADMIN_URL);
94          $infos['dc_version'] = DC_VERSION;
95          $data = self::getPluginsArray(array('name'),$hidden_plugins);
96          $data = array_merge($infos,$data);
97          $client->post($url,$data);
98          $content = $client->getContent();
99         
100          if (substr($content,0,4) == 'ok: ')
101          {
102               # ok
103               if (substr($content,4,4) == 'sent')
104               {
105                    $settings->setNameSpace('popularitycontest');
106                    # success : update Popularity Contest last report
107                    $settings->put('popularityContest_last_report',
108                         $_SERVER['REQUEST_TIME'],'integer',
109                         'Popularity Contest last report (Unix timestamp)',true,true);
110                    return(true);
111               }
112               return(false);
113          }
114         
115          return(false);
116     }
117
118     # create table
119     public static function getPluginsTable($editable=false)
120     {
121          global $hidden_plugins;
122
123          if (!is_array($hidden_plugins)) {$hidden_plugins = array();}
124         
125          $plugins_XML = self::getPluginsXML();
126         
127          $show_data = false;
128         
129          $plugins_data = array();
130         
131          if ($plugins_XML !== false)
132          {
133               $show_data = true;
134               
135               # inspired by daInstaller/inc/class.da.modules.parser.php
136               foreach ($plugins_XML->plugin as $p)
137               {
138                    $attrs = $p->attributes();
139                   
140                    $id = (string) $attrs['id'];
141                    $name = (string) $p->name;
142                    $url = (string) $p->url;
143                    $popularity = (int) $p->popularity;
144                   
145                    $plugins_data[$id] = array(
146                         //'name' => $name,
147                         'url' => $url,
148                         'popularity' => $popularity
149                    );
150               }
151          }
152         
153          # don't select hidden plugins or select all of it's editable
154          $array = self::getPluginsArray(array('name','root','version'),
155               (($editable) ? array() : $hidden_plugins));
156
157          $table = new table('class="clear" summary="'.
158               __('Installed plugins:').'"');
159          $table->part('head');
160          $table->row();
161          if ($editable)
162          {
163               $table->header(__('Hide'),'class="nowrap"');
164          }
165          $table->header(__('Icon'),'class="nowrap"');
166          $table->header(__('Plugin'),'class="nowrap"');
167          $table->header(__('Name'),'class="nowrap"');
168          $table->header(__('Version'),'class="nowrap"');
169          if ($show_data) {$table->header(__('Popularity'),'class="nowrap"');}
170
171          $table->part('body');
172
173          foreach ($array as $id => $v)
174          {
175               $icon = (file_exists($v['root'].'/icon.png')) ? 
176                    '<img src="index.php?pf='.$id.'/icon.png" style="height:16px;" alt="" />' : '';
177               
178               $name = $v['name'];
179               
180               $popularity = '&nbsp';
181               
182               if ($show_data && (isset($plugins_data[$id])))
183               {
184                    $url = $plugins_data[$id]['url'];
185                   
186                    if (!empty($url))
187                    {
188                         $name = '<a href="'.$url.'">'.$name.'</a>';
189                    }
190                   
191                    if ($plugins_data[$id]['popularity'] >= 0)
192                    {
193                         $popularity = $plugins_data[$id]['popularity'].' %';
194                    }
195               }
196               
197               # display
198               $table->row('class="line"');
199               
200               if ($editable)
201               {
202                    $table->cell(form::checkbox(array('hidden_plugins[]'),$id,
203                         in_array($id,$hidden_plugins)));
204               }
205               $table->cell($icon);
206               $table->cell($id);
207               $table->cell($name);
208               $table->cell($v['version']);
209               
210               if ($show_data) {
211                    $table->cell($popularity,'class="right"');
212               }
213          }
214
215          return($table->get());
216     }
217
218     # return an human readable string of $diff
219     public static function getDiff($diff)
220     {
221          if ($diff == 0) {return('0'.' '.__('second'));}
222         
223          $diff = abs($diff);
224         
225          $times = array();
226
227          $intervals = array
228          (
229               (3600*24*365.24) => array('one'=>__('year'),'more'=>__('years')),
230               (3600*24*30.4) => array('one'=>__('month'),'more'=>__('months')),
231               (3600*24) => array('one'=>__('day'),'more'=>__('days')),
232               (3600) => array('one'=>__('hour'),'more'=>__('hours')),
233               (60) => array('one'=>__('minute'),'more'=>__('minutes')),
234               (1) => array('one'=>__('second'),'more'=>__('seconds')),
235          );
236
237          foreach ($intervals as $k => $v)
238          {
239               if ($diff >= $k)
240               {
241                    $time = floor($diff/$k);
242                    $times[] = $time.' '.(($time <= 1) ? $v['one'] : $v['more']);
243                    $diff = $diff%$k;
244               }
245          }
246
247          # get times and make a string
248          if (count($times) > 1)
249          {
250               $last = array_pop($times);
251               $str = implode(__(', '),$times).' '.__('and').' '.$last;
252          }
253          else {$str = implode('',$times);}
254
255          return($str);
256     }
257     
258     public static function getPluginsXML()
259     {
260          $file = dirname(__FILE__).'/../xml/plugins.xml';
261          $dir = dirname($file);
262         
263          files::makeDir($dir);
264         
265          if (!is_writable($dir))
266          {
267               return(false);
268          }
269         
270          # update the file if it's older than one day
271          if ((!file_exists($file)) OR 
272               (filemtime($file) < ($_SERVER['REQUEST_TIME'] - 3600*24)))
273          {
274               try
275               {
276                    netHttp::quickGet(self::$plugins_xml_url,$file);
277               }
278               catch (Exception $e) {}
279          }
280         
281          if (file_exists($file) && is_readable($file))
282          {
283               $simple_xml = simplexml_load_file($file);
284               
285               return($simple_xml);
286          }
287          else
288          {
289               return(false);
290          }
291     }
292     
293     # create table
294     public static function getResultsTable()
295     {
296          global $core,$hidden_plugins;
297
298          $plugins_XML = self::getPluginsXML();
299         
300          if ($plugins_XML === false) {return;}
301         
302          $table = new table('class="clear" summary="'.
303               __('Plugins:').'"');
304          $table->part('head');
305          $table->row();
306          $table->header(__('Icon'),'class="nowrap"');
307          $table->header(__('Plugin'),'class="nowrap"');
308          $table->header(__('Name'),'class="nowrap"');
309          $table->header(__('Installed'),'class="nowrap"');
310          $table->header(__('Popularity'),'class="nowrap"');
311
312          $table->part('body');
313
314          foreach ($plugins_XML->plugin as $plugin)
315          {
316               $table->row('class="line"');
317               
318               $attrs = $plugin->attributes();
319               
320               $id = (string) $attrs['id'];
321               
322               $moduleExists = $core->plugins->moduleExists($id);
323               
324               $icon = '';
325               if ($moduleExists)
326               {
327                    $icon = '<img src="index.php?pf='.$id.'/icon.png" style="height:16px;" alt="" />';
328               }
329               
330               $name = (string) $plugin->name;
331               
332               $url = (string) $plugin->url;
333               
334               if (!empty($url))
335               {
336                    $name =  '<a href="'.$url.'">'.$name.'</a>';
337               }
338               
339               $installed = (($moduleExists)
340                    ? '<img src="images/check-on.png" alt="'.__('yes').'" />'
341                    : '');
342               
343               $popularity = (int) $plugin->popularity;
344               if ($popularity >= 0)
345               {
346                    $popularity .= ' %';
347               }
348               
349               # display
350               $table->cell($icon,'class="icon"');
351               $table->cell($id);
352               $table->cell($name);
353               $table->cell($installed);
354               $table->cell($popularity,'class="right"');
355          }
356
357          return($table->get());
358     }
359}
360
361?>
Note: See TracBrowser for help on using the repository browser.

Sites map