]>
code.delx.au - gnu-emacs/blob - test/etags/php-src/lce_functions.php
2 if(!defined("LCE_FUNCTIONS"))
4 define("LCE_FUNCTIONS", 1);
6 include("lce_config.php");
9 define("LCE_UNKNOWN", 0);
12 // a unqualified comment
13 define("LCE_COMMENT", 2);
14 // a user/translator comment
15 define("LCE_COMMENT_USER", 3);
16 // a tool-generated comment
17 define("LCE_COMMENT_TOOL", 4);
18 // A line containing a MSGID
19 define("LCE_MSGID", 5);
20 // A line containing a MSGSTR
21 define("LCE_MSGSTR", 6);
22 // A quoted text string
23 define("LCE_TEXT", 7);
25 define("STATE_ABORT", 0);
26 define("STATE_OK", 1);
27 define("STATE_LOOP", 2);
29 class POEntryAD
extends AD
31 function validate($value)
33 // print '"<pre>' . $value . '"<br></pre>';
34 $result = AD
::validate(trim($value));
38 $lines = explode("\n", ereg_replace("\r", "", $result[1]));
39 //$lines = explode("\n", $result[1]);
44 for($i = 0; $i < count($lines); $i++
)
46 if(trim($lines[$i]) != "")
49 $result[1] = join("\n", $res);
54 $result[0] = $this->checkQuotation($result[1]);
59 function checkQuotation($str)
61 $rex = "\\\\n|\\\\t|\\\\r|\\\\\"";
62 $str = ereg_replace($rex, "", $str);
63 $str = ereg_replace("\\\\\\\\", "", $str);
64 return !(strstr($str, "\"")
65 ||
strstr($str, "\\"));
70 class CommentAD
extends AD
74 $name, // the name of the variable
76 $type = "", // as returned by gettype
79 $this->prefix
= $prefix;
80 AD
::AD($name, $not_null, $type);
83 function validate($value)
85 $res = AD
::validate($value);
87 if($res[0] && $res[1] != "")
90 $lines = explode("\n", $res[1]);
92 for($i = 0; $i < count($lines); $i++
)
95 if(substr($line, 0, 1) != "#")
96 $line = $this->prefix
. $line;
99 $res[1] = join("\n", $mod_lines);
105 class POEntry
extends HtmlValidator
115 var $user_comment_lc = 0;
116 var $sys_comment_lc = 0;
117 var $unk_comment_lc = 0;
123 new POEntryAD("msgstr", REQUIRED_ATTRIBUTE
),
124 new CommentAD("user_comment"),
125 new POEntryAD("sys_comment"),
126 new POEntryAD("unk_comment"),
127 new AD("msgid_lc", NOT_REQUIRED_ATTRIBUTE
, 0),
128 new AD("msgstr_lc", NOT_REQUIRED_ATTRIBUTE
, 0),
129 new AD("user_comment_lc", NOT_REQUIRED_ATTRIBUTE
, 0),
130 new AD("sys_comment_lc", NOT_REQUIRED_ATTRIBUTE
, 0),
131 new AD("unk_comment_lc", NOT_REQUIRED_ATTRIBUTE
, 0)
135 function lineCount($entry)
137 $lc = count(explode("\n", $entry));
141 function serializeToVars($prefix)
143 $this->user_comment_lc
= $this->lineCount($this->user_comment
);
144 $this->unk_comment_lc
= $this->lineCount($this->sys_comment
);
145 $this->sys_comment_lc
= $this->lineCount($this->unk_comment
);
146 $this->msgid_lc
= $this->lineCount($this->msgid
);
147 $this->msgstr_lc
= $this->lineCount($this->msgstr
);
148 return HtmlValidator
::serializeToVars($prefix);
154 $content .= $this->user_comment
. "\n";
155 $content .= $this->unk_comment
. "\n";
156 $content .= $this->sys_comment
. "\n";
157 $content .= "msgid \"" . $this->msgid
. "\"\n";
158 $content .= 'msgstr "' . join("\"\n\"", explode("\n", $this->msgstr
)) . "\"" . "\n\n";
163 class POReader
extends HTMLValidator
177 function gettext($msgid)
179 if(isset($this->po_entries
[$msgid]))
181 $po = $this->po_entries
[$msgid];
182 return StripCSlashes(join("", explode("\n", $po->msgstr
)));
183 //return $po->msgstr;
189 function parseFromVars($prefix)
191 $res = HtmlValidator
::parseFromVars($prefix);
195 $this->po_entries
= array();
196 for($i = 0; $i < $this->poe_num
; $i++
)
199 $res = $poe->parseFromVars($prefix . "_POE$i");
202 $msgid = $prefix . "_POE" . $i . "_MSGID";
204 $this->po_entries
[$prefix . "_POE" . $i . "_MSGID"] = $res[1];
211 $GLOBALS[$prefix . "_ERR"] = 1;
212 return array($poe_res, $this);
215 function serializeToVars($prefix)
217 HtmlValidator
::serializeToVars($prefix);
218 reset($this->po_entries
);
220 while($poe = each($this->po_entries
))
223 $poe->serializeToVars($prefix . "_POE$i");
229 function POReader($domain, $filename)
231 $this->domain
= $domain;
232 $this->filename
= $filename;
233 $this->ignore_ws
= true;
234 $this->po_entries
= array();
236 new AD("domain", REQUIRED_ATTRIBUTE
),
237 new AD("filename", REQUIRED_ATTRIBUTE
),
238 new AD("poe_num", REQUIRED_ATTRIBUTE
, 0)
245 if($fh = fopen($this->filename
, "r"))
247 $this->lines
= array();
250 $line = fgets($fh, 4096);
251 $this->lines
[] = $line;
255 $this->createPOEntries();
256 $this->poe_num
= count($this->po_entries
);
259 function write($save="yes")
261 reset($this->po_entries
);
263 while($poe = each($this->po_entries
))
266 $content .= $poe->write();
269 if(($fh = fopen($this->filename
, "w"))
272 fwrite($fh, $content);
277 function isComment($class)
279 if($class == LCE_COMMENT ||
$class == LCE_COMMENT_USER ||
$class == LCE_COMMENT_TOOL
)
284 function comment($line, $class)
286 if($this->isComment($class))
288 if($class == LCE_COMMENT_USER
)
289 $this->user_comment
.= $line;
290 else if($class == LCE_COMMENT_TOOL
)
291 $this->sys_comment
.= $line;
293 $this->unk_comment
.= $line;
296 if($class == LCE_MSGID
)
298 $this->state
= "msgid";
304 function msgid($line, $class)
306 if($class == LCE_MSGID ||
$class == LCE_TEXT
)
308 $line = $this->stripLine($line, LCE_MSGID
);
309 $this->msgid
.= $line;
312 if($class == LCE_MSGSTR
)
314 $this->state
= "msgstr";
320 function msgstr($line, $class)
322 if($class == LCE_MSGSTR ||
$class == LCE_TEXT
)
324 $line = $this->stripLine($line, $class);
325 $this->msgstr
.= $line;
328 // We have a different state, so we have to create a POEntry
330 $poe->user_comment
= trim($this->user_comment
);
331 $poe->sys_comment
= trim($this->sys_comment
);
332 $poe->unk_comment
= trim($this->unk_comment
);
333 $poe->msgid
= trim($this->msgid
);
334 $poe->msgstr
= trim($this->msgstr
);
335 $this->po_entries
[trim($this->msgid
)] = $poe;
336 $this->state
= "start";
340 function start($line, $class)
342 $this->user_comment
= "";
343 $this->sys_comment
= "";
344 $this->unk_comment
= "";
347 if($this->isComment($class))
349 $this->state
= "comment";
352 if($class == LCE_MSGID
)
354 $this->state
= "msgid";
360 function createPOEntries()
364 $this->user_comment
= "";
365 $this->sys_comment
= "";
366 $this->state
= "start";
369 for($i = 0; $i < count($this->lines
); $i++
)
371 $line = $this->lines
[$i];
372 $class = $this->classifyLine($line);
373 if($class != LCE_WS ||
!$this->ignore_ws
)
375 $state_ret = STATE_LOOP
;
376 while($state_ret == STATE_LOOP
)
378 $state = $this->state
;
379 //print "$this->state $class:$line <br>";
380 $state_ret = $this->$state($line, $class);
382 //print "state_ret = $state_ret <br>";
384 if($state_ret == STATE_ABORT
)
387 // Get the last entry
388 if($state_ret != STATE_ABORT
)
390 $this->msgstr("", LCE_UNKNOWN
);
394 function stripLine($line, $class)
399 ereg('^"(.*)"', $line, $regs);
400 $line = $regs[1] . "\n";
403 if(substr($line, strlen("msgid")) == "msgid")
405 $line = substr($line, strlen("msgid") +
1);
407 ereg('"(.*)"', $line, $regs);
411 // TODO: Check if ^ can be removed
412 $line = substr($line, strlen("msgstr") +
1);
413 ereg('^"(.*)"', $line, $regs);
414 $line = $regs[1] . "\n";
421 function printClassification()
424 for($i = 0; $i < count($this->lines
); $i++
)
426 $line = $this->lines
[$i];
427 $class = $this->classifyLine($line);
428 print "#$i: $class $line<br>";
432 function classifyLine($line)
434 if(ereg("^[ \n\r\t]*$", $line))
436 if(ereg("^#.*\$", $line))
438 if(ereg("^[,:-~].*", substr($line, 1)))
440 return LCE_COMMENT_TOOL
;
442 if(ereg("^[ \n\r\t].*", substr($line, 1)))
444 return LCE_COMMENT_USER
;
448 if(ereg("^msgid (.*)\$", $line, $regs))
451 if($this->classifyLine($line) == LCE_TEXT
)
454 if(ereg("^msgstr (.*)\$", $line, $regs))
457 if($this->classifyLine($line) == LCE_TEXT
)
460 if(ereg('^".*"', $line))
462 // TODO: Check correct escapes
471 function getTextDomains($lines)
473 $default_domain = "";
475 while($gl = each($GLOBALS))
480 for($i = 0; $i < count($lines); $i++
)
482 if(ereg("bindtextdomain\(([^,]+),([^\)]+)\)", $lines[$i], $regs))
484 //print "Line:" . $lines[$i] . " <br>";
486 $ev = "\$directory = ". $regs[2] . ";";
489 $domains[] = array($name, $directory);
491 if(ereg("textdomain\(([^\)]+)\)", $lines[$i], $regs))
492 $default_domain = $regs[1];
494 return array($default_domain, $domains);
498 class PORManager
extends HtmlValidator
502 function PORManager()
504 $this->por_a
= array();
507 function addPOReader($d_name, &$por)
509 $this->por_a
[$d_name] = &$por;
512 function &getPOReader($domain)
514 return $this->por_a
[$domain];
517 function getDomainNames()
519 return array_keys($this->por_a
);
523 function &loadPORManager()
526 if(!isset($LCE_PORMAN))
528 $LCE_PORMAN = new PORManager();
534 // More or less intelligent filename joining
535 // As available in PYTHONs os.path
538 $numargs = func_num_args();
539 $args = func_get_args();
540 for($i = 0; $i < $numargs - 1; $i++
)
542 if(substr($args[$i], -1) != "/")
543 $args[$i] = $args[$i] . "/";
546 if(substr($args[$i],0 , 1) == "/")
547 $args[$i] = substr($args[$i], 1);
551 return join("", $args);
554 if(defined("LCE_TESTSERVER"))
557 function lce_bindtextdomain($d_name, $d_path)
559 global $LANG, $LC_MESSAGES, $LC_ALL, $LCE_LANG;
561 global $LCE_PO_SUFFIX;
564 $path_orig = $d_path;
565 // This is not complete and reflects
566 // my not very far going understanding of the
567 // different $LC_x thingies.
568 if(isset($LC_MESSAGES))
570 //print "LC_MESSAGES<br>";
571 $lang_suffix = $LC_MESSAGES;
573 else if(isset($LC_ALL))
575 //print "LC_ALL<br>";
576 $lang_suffix = $LC_ALL;
578 else if(isset($LANG))
581 $lang_suffix = $LANG;
585 //print "LCE_LANG<br>";
586 $lang_suffix = $LCE_LANG;
589 //print "LangSuffix: $lang_suffix \n";
590 //print "D_Path: " . fileJoin($d_path, $lang_suffix, "LC_MESSAGES", $d_name . $LCE_PO_SUFFIX) . "<br>";
591 // First try: the whole lang_suffix
593 if(file_exists(fileJoin($d_path, $lang_suffix, "LC_MESSAGES", $d_name . $LCE_PO_SUFFIX)))
594 $d_path = fileJoin($d_path, $lang_suffix, "LC_MESSAGES", $d_name . $LCE_PO_SUFFIX);
597 $lang_suffix = substr($lang_suffix, 0, 2);
598 if(file_exists(fileJoin($d_path, $lang_suffix, "LC_MESSAGES", $d_name. $LCE_PO_SUFFIX)))
599 $d_path = fileJoin(fileJoin($d_path, $lang_suffix, "LC_MESSAGES", $d_name . $LCE_PO_SUFFIX));
602 $LCE_ERR = "No PO-file found";
606 //print "D_Path: $d_path \n";
607 $por = new POReader($d_name, $d_path, $path_orig);
609 $porman =& loadPORManager();
610 $porman->addPOReader($d_name, $por);
614 function lce_textdomain($domain)
617 $LCE_DOMAIN = $domain;
620 function lce_gettext($msgid)
623 return lce_dgettext($LCE_DOMAIN, $msgid);
626 function lce_dgettext($domain, $msgid)
628 $porman =& loadPORManager();
629 if($por = &$porman->getPOReader($domain))
630 return $por->gettext($msgid);
636 global $LCE_LCEDITLOC;
637 $porman =& loadPORManager();
638 $domains = $porman->getDomainNames();
639 for($i = 0; $i < count($domains); $i++
)
641 $por =& $porman->getPOReader($domains[$i]);
642 $domain = "domain=" . urlencode($por->domain
);
643 $filename = "filename=" . urlencode($por->filename
);
644 $url = $LCE_LCEDITLOC . "?" . $domain . "&" . $filename;
645 print "<a target=\"_blank\" href=\"" . $url . "\">Domain: $por->domain</a><br>";
651 function lce_bindtextdomain($domain, $path)
653 bindtextdomain($domain, $path);
656 function lce_textdomain($domain)
661 function lce_gettext($msgid)
663 return gettext($msgid);
666 function lce_dgettext($domain, $msgid)
668 return dgettext($domain, $msgid);
676 function lce_geteditcode($type, $name, $text, $rows=2)
678 global $LCE_EDIT_LEVEL;
679 $level_map = array("msgid" => 4,
684 if($level_map[$type] > $LCE_EDIT_LEVEL)
686 return "<input type=\"hidden\" name=\"" . $name . "\" value=\"" . $text . "\"><pre>\n" . $text . "\n</pre>";
690 return "<textarea name=\"" . $name . "\" rows=\"" . $rows . "\" cols=\"60\">" . $text . "</textarea>";
695 ;;; Local Variables: ***