OSDN Git Service

Correct strtr() related. Cleanup.
authorhenoheno <henoheno>
Sun, 23 Jan 2005 09:32:03 +0000 (18:32 +0900)
committerhenoheno <henoheno>
Sun, 23 Jan 2005 09:32:03 +0000 (18:32 +0900)
plugin/update_entities.inc.php

index a2be438..e429a79 100644 (file)
@@ -1,12 +1,12 @@
 <?php
-/////////////////////////////////////////////////
-// PukiWiki - Yet another WikiWikiWeb clone.
-//
-// $Id: update_entities.inc.php,v 1.5 2004/07/31 03:09:20 henoheno Exp $
+// PukiWiki - Yet another WikiWikiWeb clone
+// $Id: update_entities.inc.php,v 1.6 2005/01/23 09:32:03 henoheno Exp $
 //
+// Update entities plugin - Update XHTML entities from DTD
+// (for admin)
 
 // DTD¤Î¾ì½ê
-define('W3C_XHTML_DTD_LOCATION','http://www.w3.org/TR/xhtml1/DTD/');
+define('W3C_XHTML_DTD_LOCATION', 'http://www.w3.org/TR/xhtml1/DTD/');
 
 // ¥á¥Ã¥»¡¼¥¸ÀßÄê
 function plugin_update_entities_init()
@@ -30,8 +30,7 @@ PHP
 * ¼Â¹Ô
 ´ÉÍý¼Ô¥Ñ¥¹¥ï¡¼¥É¤òÆþÎϤ·¤Æ¡¢[¼Â¹Ô]¥Ü¥¿¥ó¤ò¥¯¥ê¥Ã¥¯¤·¤Æ¤¯¤À¤µ¤¤¡£
 '
-               )
-       );
+               ));
        set_plugin_messages($messages);
 }
 
@@ -40,10 +39,11 @@ function plugin_update_entities_action()
        global $script, $vars;
        global $_entities_messages;
 
-       if (empty($vars['action']) or empty($vars['adminpass']) or ! pkwk_login($vars['adminpass']))
-       {
+       $msg = $body = '';
+       if (empty($vars['action']) || empty($vars['adminpass']) || ! pkwk_login($vars['adminpass'])) {
+               $msg   = & $_entities_messages['title_update'];
                $items = plugin_update_entities_create();
-               $body = convert_html(sprintf($_entities_messages['msg_usage'],join("\n-",$items)));
+               $body  = convert_html(sprintf($_entities_messages['msg_usage'], join("\n" . '-', $items)));
                $body .= <<<EOD
 <form method="POST" action="$script">
  <div>
@@ -55,69 +55,62 @@ function plugin_update_entities_action()
  </div>
 </form>
 EOD;
-               return array(
-                       'msg'=>$_entities_messages['title_update'],
-                       'body'=>$body
-               );
-       }
-       else if ($vars['action'] == 'update')
-       {
+       } else if ($vars['action'] == 'update') {
                plugin_update_entities_create(TRUE);
-               return array(
-                       'msg'=>$_entities_messages['title_update'],
-                       'body'=>$_entities_messages['msg_done']
-               );
+               $msg  = & $_entities_messages['title_update'];
+               $body = & $_entities_messages['msg_done'    ];
+       } else {
+               $msg  = & $_entities_messages['title_update'];
+               $body = & $_entities_messages['err_invalid' ];
        }
+       return array('msg'=>$msg, 'body'=>$body);
+}
 
-       return array(
-               'msg'=>$_entities_messages['title_update'],
-               'body'=>$_entities_messages['err_invalid']
-       );
+// Remove &amp; => amp
+function plugin_update_entities_strtr($entity){
+       return strtr($entity, array('&'=>'', ';'=>''));
 }
 
-function plugin_update_entities_create($do=FALSE)
+function plugin_update_entities_create($do = FALSE)
 {
-       $files = array('xhtml-lat1.ent','xhtml-special.ent','xhtml-symbol.ent');
-       $entities = strtr(
-               array_values(get_html_translation_table(HTML_ENTITIES)),
-               array('&'=>'',';'=>'')
-       );
-       $items = array('php:html_translation_table');
-       foreach ($files as $file)
-       {
-               $source = file(W3C_XHTML_DTD_LOCATION.$file);
-//                     or die_message('cannot receive '.W3C_XHTML_DTD_LOCATION.$file.'.');
-               if (!is_array($source))
-               {
-                       $items[] = "w3c:$file COLOR(red):not found.";
+       $files = array('xhtml-lat1.ent', 'xhtml-special.ent', 'xhtml-symbol.ent');
+       
+       $entities = array_map('plugin_update_entities_strtr',
+               array_values(get_html_translation_table(HTML_ENTITIES)));
+       $items   = array('php:html_translation_table');
+       $matches = array();
+       foreach ($files as $file) {
+               $source = file(W3C_XHTML_DTD_LOCATION . $file);
+//                     or die_message('cannot receive ' . W3C_XHTML_DTD_LOCATION . $file . '.');
+               if (! is_array($source)) {
+                       $items[] = 'w3c:' . $file . ' COLOR(red):not found.';
                        continue;
                }
-               $items[] = "w3c:$file";
+               $items[] = 'w3c:' . $file;
                if (preg_match_all('/<!ENTITY\s+([A-Za-z0-9]+)/',
-                       join('',$source),$matches,PREG_PATTERN_ORDER))
+                       join('', $source), $matches, PREG_PATTERN_ORDER))
                {
-                       $entities = array_merge($entities,$matches[1]);
+                       $entities = array_merge($entities, $matches[1]);
                }
        }
-       if (!$do)
-       {
-               return $items;
-       }
+       if (! $do) return $items;
+
        $entities = array_unique($entities);
-       sort($entities,SORT_STRING);
+       sort($entities, SORT_STRING);
        $min = 999;
        $max = 0;
-       foreach ($entities as $entity)
-       {
+       foreach ($entities as $entity) {
                $len = strlen($entity);
-               $max = max($max,$len);
-               $min = min($min,$len);
+               $max = max($max, $len);
+               $min = min($min, $len);
        }
 
-       $pattern = "(?=[a-zA-Z0-9]\{$min,$max})".get_autolink_pattern_sub($entities,0,count($entities),0);
-       $fp = fopen(CACHE_DIR.'entities.dat','w')
-               or die_message('cannot write file '.CAHCE_DIR.'entities.dat<br />maybe permission is not writable or filename is too long');
-       fwrite($fp,$pattern);
+       $pattern = "(?=[a-zA-Z0-9]\{$min,$max})" .
+               get_autolink_pattern_sub($entities, 0, count($entities), 0);
+       $fp = fopen(CACHE_DIR  .'entities.dat', 'w')
+               or die_message('cannot write file ' . CAHCE_DIR . 'entities.dat<br />' . "\n" .
+                       'maybe permission is not writable or filename is too long');
+       fwrite($fp, $pattern);
        fclose($fp);
 
        return $items;