OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@825 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_ChoppedDisc / NP_ChoppedDisc.php
1 <?
2 /* plugin needs to work on Nucleus versions <=2.0 as well
3 if (!function_exists('sql_table')){
4         function sql_table($name) {
5                 return 'nucleus_' . $name;
6         }
7 }*/
8
9 class NP_ChoppedDisc extends NucleusPlugin
10 {
11         function getEventList()
12         {
13                 return array();
14         }
15
16         function getName()
17         {
18                 return 'Chopped description';
19         }
20
21         function getAuthor()
22         {
23                 return 'nakahara21';
24         }
25
26         function getURL()
27         {
28                 return 'http://nakahara21.com';
29         }
30
31         function getVersion()
32         {
33                 return '0.7';
34         }
35
36         function getDescription()
37         {
38                 return 'Chopped description. &lt;%ChoppedDisc(250,1)%&gt;';
39         }
40
41         function supportsFeature($what)
42         {
43                 switch ($what) {
44                         case 'SqlTablePrefix':
45                                 return 1;
46                         default:
47                                 return 0;
48                 }
49         }
50
51         function parseHighlight($query)
52         {
53         
54                 // get rid of quotes
55                 $query = preg_replace('/\'|"/','',$query);
56         
57                 if (!query) return array();
58         
59                 $aHighlight = explode(' ', $query);
60         
61                 for ($i = 0; $i<count($aHighlight); $i++) {
62                         $aHighlight[$i] = trim($aHighlight[$i]);
63 //                      if (strlen($aHighlight[$i]) < 3)
64 //                              unset($aHighlight[$i]);
65                 }
66         
67 //              if (count($aHighlight) == 1)
68 //                      return $aHighlight[0];
69 //              else
70                         return $aHighlight;
71         }
72
73         function splitLastStr($str, $width=5)
74         {
75                 $posn = (mb_strwidth($str) > $width)? mb_strwidth($str) - $width: 0;
76                 $resArray[0] = ($posn) ? mb_strcut($str, 0, $posn, _CHARSET) : '';
77                 $resArray[1] = ($posn) ? mb_strcut($str, $posn, $width + 2, _CHARSET) : $str;
78                 return $resArray;
79         }
80
81         function chopStr($str, $query, $maxLength)
82         {
83                 
84                 $searchclass =& new SEARCH($query);
85                 $highlight       = $searchclass->inclusive;
86                 $this->highlights = $this->parseHighlight($highlight);
87
88                 if(mb_strwidth($str) <= $maxLength)
89                         return $str;
90                 
91                 $toated = "...";
92                 $tLength = mb_strwidth($toated);
93                 $maxLength = $maxLength - $tLength;
94
95                 $text = highlight($str, $this->highlights, '<\0>');
96                 $text = '< >' . $text;
97                 preg_match_all('/(<[^>]+>)([^<>]*)/', $text, $matches);
98                 for($i=0;$i<count($matches[1]);$i++){
99                         $matches[1][$i] = ereg_replace("<|>", '', $matches[1][$i]);
100                 }
101 /*
102                 print_r($matches);
103                 echo "<hr />";
104 */
105                 for ($i=0;$i<count($this->highlights);$i++) {
106                         for ($e=0;$e<count($matches[1]);$e++) {
107                                 if (eregi($this->highlights[$i], $matches[1][$e])) {
108                                         if (!$hitkey[$i]) {
109                                                 $hitkey[$i] = $e;
110                                         }
111                                 }
112                         }
113                 }
114                 
115
116                 if(!$hitkey){
117                         $tt = mb_strcut($matches[2][0], 0, $maxLength, _CHARSET);
118                         if(mb_strwidth($matches[2][0]) > $maxLength) {
119                                 $tt .= $toated;
120                         }
121                 } elseif($hitkey[1]) {
122                         sort($hitkey);
123                         foreach ($hitkey as $keyval) {
124                                 $hitWordArray[] = $matches[1][$keyval];
125                         }
126
127                         $list[0] = array("qlen" => 0, "q" => '');
128                         $trimLength = intval(($maxLength - mb_strwidth(join("",$hitWordArray))) / (count($hitWordArray) +1));
129                         
130                         $left = $str;
131                         $i=0;
132                         while ($i <= count($hitWordArray)) {
133 //                              $hitWord = $hitWordArray[$i];
134                                 $tempArray = ($hitWord = $hitWordArray[$i]) ? explode($hitWord, $left, 2) : array($left, '');
135                                 $preStr = ($hitWord) ? $this->splitLastStr($tempArray[0], 5) : array($left, '');
136
137                                 $left = $preStr[1].$hitWord.$tempArray[1];
138
139                                 $list[$i]['str'] = $preStr[0];
140                                 $list[$i]['len'] = mb_strwidth($preStr[0]);
141
142                                 $tempTrimLen = $trimLength + $list[$i]['qlen'];
143
144                                 if($list[$i]['len'] < $tempTrimLen){
145                                         $list[$i]['trimlen'] = 0;
146                                         $addsum += $tempTrimLen - $list[$i]['len'];
147                                 }else{
148                                         $list[$i]['trimlen'] = $list[$i]['len'] - $tempTrimLen;
149                                 }
150
151                                 if (!$hitWord) break;
152                                 $i++;
153                                 $list[$i]['q'] = $hitWord;
154                                 $list[$i]['qlen'] = mb_strwidth($hitWord);
155                         }
156 //      print_r($list);
157
158                         for ($i=0;$i<count($list);$i++) {
159                                 if ($list[$i]['trimlen'] && ($addsum > 0)) {
160                                         $list[$i]['trimlen'] = min($list[$i]['trimlen'], $addsum);
161                                         $addsum -= $list[$i]['trimlen'];
162                                         $list[$i]['trimlen'] = $trimLength + $list[$i]['trimlen'] + $list[$i]['qlen'];
163                                 } elseif($list[$i]['trimlen']) {
164                                         $list[$i]['trimlen'] = $trimLength + $list[$i]['qlen'];
165                                 } else {
166                                         $list[$i]['trimlen'] = $list[$i]['len'];
167                                 }
168                         }
169 //      print_r($list);
170
171                         $tt = mb_strcut(
172                                  $list[0]['str'],
173                                  $list[0]['len'] - $list[0]['trimlen'],
174                                  $list[0]['trimlen'] + 2,
175                                  _CHARSET);
176                         if ($list[0]['len'] > $list[0]['trimlen']) {
177                                 $tt = $toated.$tt;
178                         }
179
180                         for ($i=1;$i<count($list);$i++) {
181                                 $tt .= mb_strcut($list[$i]['str'], 0, $list[$i]['trimlen'], _CHARSET);
182                                 if ($list[$i]['len'] > $list[$i]['trimlen']) {
183                                         $tt .= $toated;
184                                 }
185                         }
186                 } else {
187                         $hitWord = $this->highlights[0];
188                         $keyLength = mb_strwidth($hitWord);
189                         
190                         $splitStr = preg_quote($hitWord);
191                         list($preStr, $hStr) = preg_split("/$splitStr/i",$str,2);
192
193                         $preStrLength = mb_strwidth($preStr);
194                         $hStrLength = mb_strwidth($hStr);
195                         $halfLength = intval(($maxLength - $keyLength) / 2);
196
197                         $hTrimLength = $preTrimLength = $halfLength;
198                         $minLength = min($preStrLength, $hStrLength, $halfLength);
199                         if ($preStrLength == $minLength) {
200                                 $hTrimLength = $maxLength - $keyLength - $preStrLength;
201                                 $preTrimLength = $preStrLength;
202                         }
203                         if ($hStrLength == $minLength) {
204                                 $preTrimLength = $maxLength - $keyLength - $hStrLength;
205                                 $hTrimLength = $hStrLength;
206                         }
207
208                         $tt = mb_strcut($preStr, $preStrLength - $preTrimLength, $preStrLength, _CHARSET);
209                         $tt .= $matches[1][1];
210                         $tt .= mb_strcut($hStr, 0, $hTrimLength,_CHARSET);
211
212                         if ($preTrimLength < $preStrLength) {
213                                 $tt = $toated . $tt;
214                         }
215                         if ($hTrimLength < $hStrLength) {
216                                 $tt .= $toated;
217                         }
218
219                 }
220
221                 return $tt;
222
223 /*
224                 $temp_str_array = mb_split($query,$str);
225                 $num = count($temp_str_array);
226                 foreach($temp_str_array as $key => $temp_str){
227                         $lastp = mb_strwidth($temp_str);
228                         if($key>0)
229                                 $temp_s  = mb_strcut($temp_str, 0, 40, _CHARSET); 
230                         if($key>0 || ($key==0 && $lastp>40))
231                                 $temp_s .= "..."; 
232                         $temp_s .= mb_strcut($temp_str, $lastp-40, $lastp, _CHARSET); 
233                         $str_array[$key] = $temp_s; 
234 //                      if($key>0 && $lastp<83)
235                         if(($key>0 && $lastp<83) || $key == $num-1)
236                                 $str_array[$key] = $temp_str; 
237                         
238                         $temp_str_length += mb_strwidth($str_array[$key]);
239                         if($temp_str_length > $maxLength)
240                                 $str_array[$key] = $temp_str; 
241                 }
242                 $str = @join($query,$str_array);
243                 return $str;
244 */
245         }
246
247         function doTemplateVar(&$item, $maxLength = 250, $addHighlight = 0)
248         {
249                 global $CONF, $manager, $member, $catid;
250                 global $query;
251
252                 if ($manager->pluginInstalled('NP_HighlightSource')) {
253                         $tempPlugin =& $manager->getPlugin('NP_HighlightSource');
254                         preg_replace_callback("#<hs(|:[^>]+?)>(.*?)</hs>#s", array(&$tempPlugin, 'phpHighlight'), $item->body); 
255                         preg_replace_callback("#<hs(|:[^>]+?)>(.*?)</hs>#s", array(&$tempPlugin, 'phpHighlight'), $item->more); 
256                 }
257
258                 $syndicated = strip_tags($item->body);
259                 $syndicated .= strip_tags($item->more);
260                 $syndicated = preg_replace("/[\r\n]/", "", $syndicated);
261
262                 $syndicated = $this->chopStr($syndicated, $query, $maxLength);
263
264 /*
265                 if ($addHighlight) {
266                         echo highlight(htmlspecialchars($syndicated), $this->highlights, '<span class="highlight">\0</span>');
267                 } else {
268                         echo htmlspecialchars($syndicated);
269                 }
270         }
271 */
272                 if ($addHighlight) {
273                         global $currentTemplateName;
274                         $template =& $manager->getTemplate($currentTemplateName);
275                         echo highlight($syndicated, $this->highlights, $template['SEARCH_HIGHLIGHT']);
276                 } else {
277                         echo $syndicated;
278                 }
279         }
280
281 }