]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiTranslation.php
Use real list instead of middot
[SourceForge/phpwiki.git] / lib / plugin / WikiTranslation.php
1 <?php
2
3 /*
4  * Copyright 2004,2005 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * _WikiTranslation:  Display pagenames and other strings in various languages.
25  * Can also be used to let a favorite translation service translate a whole page.
26  * Current favorite: translate.google.com if from_lang = en or fr
27  *
28  * Examples:
29  *  <<WikiTranslation page=HomePage languages=fr >>
30  *     Translation service for HomePage into french (redirect to translate.google.com)
31  *  <<WikiTranslation what=pages >>
32  *     Translation matrix of all pages with proper translations (all in pgsrc)
33  *  <<WikiTranslation what=wikiwords match="W*" limit=20 >>
34  *     Translation matrix of the first 20 wikiwords matching "W*"
35  *  <<WikiTranslation string=HomePage languages=fr,de,sv >>
36  *     Translation matrix for all given languages
37  *  <<WikiTranslation string=HomePage >>
38  *     Translation matrix for all supported languages
39  *  <<WikiTranslation string=HomePage languages=fr >>
40  *     Just return the translated string for this language.
41  *
42  * @author:  Reini Urban
43  */
44
45 /* Container for untranslated pagenames. Needed to show up in locale/po/phpwiki.pot */
46 $pgsrc_container =
47     _("AddComment") . ',' .
48     _("AddCommentPlugin") . ',' .
49     _("AddingPages") . ',' .
50     _("AllPages") . ',' .
51     _("AllPagesCreatedByMe") . ',' .
52     _("AllPagesLastEditedByMe") . ',' .
53     _("AllPagesOwnedByMe") . ',' .
54     _("AllUsers") . ',' .
55     _("AnalyseAccessLogSql") . ',' .
56     _("AppendText") . ',' .
57     _("AsciiMath") . ',' .
58     _("AsciiSVG") . ',' .
59     _("AtomFeed") . ',' .
60     _("DebugAuthInfo") . ',' .
61     _("AuthorHistory") . ',' .
62     _("AuthorHistoryPlugin") . ',' .
63     _("DebugBackendInfo") . ',' .
64     _("BackLinks") . ',' .
65     _("BlogArchives") . ',' .
66     _("BlogJournal") . ',' .
67     _("BoxRight") . ',' .
68     _("CacheTest") . ',' .
69     _("Calendar") . ',' .
70     _("CalendarList") . ',' .
71     _("CalendarListPlugin") . ',' .
72     _("CalendarPlugin") . ',' .
73     _("CategoryCategory") . ',' .
74     _("CategoryHomePages") . ',' .
75     _("CategoryPage") . ',' .
76     _("Chart") . ',' .
77     _("Chown") . ',' .
78     _("Comment") . ',' .
79     _("CommentPlugin") . ',' .
80     _("CreateBib") . ',' .
81     _("CreatePage") . ',' .
82     _("CreateToc") . ',' .
83     _("CreateTocPlugin") . ',' .
84     _("CurrentTime") . ',' .
85     _("DeadEndPages") . ',' .
86     _("DebugInfo") . ',' .
87     _("Diff") . ',' .
88     _("DynamicIncludePage") . ',' .
89     _("EditMetaData") . ',' .
90     _("EditMetaDataPlugin") . ',' .
91     _("ExternalSearch") . ',' .
92     _("ExternalSearchPlugin") . ',' .
93     _("FacebookLike") . ',' .
94     _("FileInfo") . ',' .
95     _("FindPage") . ',' .
96     _("FoafViewer") . ',' .
97     _("FoafViewerPlugin") . ',' .
98     _("FullRecentChanges") . ',' .
99     _("FullTextSearch") . ',' .
100     _("FuzzyPages") . ',' .
101     _("GoogleMaps") . ',' .
102     _("GooglePlugin") . ',' .
103     _("GoTo") . ',' .
104     _("GraphViz") . ',' .
105     _("DebugGroupInfo") . ',' .
106     _("HelloWorld") . ',' .
107     _("HelloWorldPlugin") . ',' .
108     _("HomePageAlias") . ',' .
109     _("HtmlConverter") . ',' .
110     _("IncludePage") . ',' .
111     _("IncludePagePlugin") . ',' .
112     _("IncludePages") . ',' .
113     _("IncludeSiteMap") . ',' .
114     _("IncludeTree") . ',' .
115     _("InterWiki") . ',' .
116     _("InterWikiSearch") . ',' .
117     _("JabberPresence") . ',' .
118     _("LdapSearch") . ',' .
119     _("LikePages") . ',' .
120     _("LinkDatabase") . ',' .
121     _("LinkIcons") . ',' .
122     _("LinkSearch") . ',' .
123     _("ListPages") . ',' .
124     _("ListRelations") . ',' .
125     _("ListSubpages") . ',' .
126     _("MagicPhpWikiURLs") . ',' .
127     _("MediawikiTable") . ',' .
128     _("ModeratedPage") . ',' .
129     _("MoreAboutMechanics") . ',' .
130     _("MostPopular") . ',' .
131     _("NewPagesPerUser") . ',' .
132     _("NoCache") . ',' .
133     _("OldStyleTable") . ',' .
134     _("OldStyleTablePlugin") . ',' .
135     _("OrphanedPages") . ',' .
136     _("PageDump") . ',' .
137     _("PageGroup") . ',' .
138     _("PageHistory") . ',' .
139     _("PageInfo") . ',' .
140     _("PageTrail") . ',' .
141     _("PasswordReset") . ',' .
142     _("PhotoAlbum") . ',' .
143     _("PhotoAlbumPlugin") . ',' .
144     _("PhpHighlight") . ',' .
145     _("PhpHighlightPlugin") . ',' .
146     _("PhpWeather") . ',' .
147     _("PhpWeatherPlugin") . ',' .
148     _("PhpWiki") . ',' .
149     _("PhpWikiAdministration") . ',' .
150     _("PhpWikiDocumentation") . ',' .
151     _("PhpWikiPoll") . ',' .
152     _("Ploticus") . ',' .
153     _("PloticusPlugin") . ',' .
154     _("PluginManager") . ',' .
155     _("PopularNearby") . ',' .
156     _("PopularTags") . ',' .
157     _("PopUp") . ',' .
158     _("PreferenceApp") . ',' .
159     _("PreferencesInfo") . ',' .
160     _("PrevNext") . ',' .
161     _("Processing") . ',' .
162     _("RandomPage") . ',' .
163     _("RateIt") . ',' .
164     _("RawHtml") . ',' .
165     _("RawHtmlPlugin") . ',' .
166     _("RecentChanges") . ',' .
167     _("RecentChangesCached") . ',' .
168     _("RecentComments") . ',' .
169     _("RecentEdits") . ',' .
170     _("RecentReferrers") . ',' .
171     _("RecentVisitors") . ',' .
172     _("RedirectTo") . ',' .
173     _("RedirectToPlugin") . ',' .
174     _("RelatedChanges") . ',' .
175     _("ReleaseNotes") . ',' .
176     _("Remove") . ',' .
177     _("Rename") . ',' .
178     _("Replace") . ',' .
179     _("DebugRetransform") . ',' .
180     _("RichTable") . ',' .
181     _("RichTablePlugin") . ',' .
182     _("RssFeed") . ',' .
183     _("SearchHighlight") . ',' .
184     _("SemanticRelations") . ',' .
185     _("SemanticSearch") . ',' .
186     _("SemanticSearchAdvanced") . ',' .
187     _("SetAcl") . ',' .
188     _("SiteMap") . ',' .
189     _("SpellCheck") . ',' .
190     _("SpecialPages") . ',' .
191     _("SqlResult") . ',' .
192     _("SyncWiki") . ',' .
193     _("SyntaxHighlighter") . ',' .
194     _("SystemInfo") . ',' .
195     _("SystemInfoPlugin") . ',' .
196     _("Template") . ',' .
197     _("TeX2png") . ',' .
198     _("text2png") . ',' .
199     _("TextFormattingRules") . ',' .
200     _("TexToPng") . ',' .
201     _("TitleSearch") . ',' .
202     _("Transclude") . ',' .
203     _("TranscludePlugin") . ',' .
204     _("TranslateText") . ',' .
205     _("UnfoldSubpages") . ',' .
206     _("UnfoldSubpagesPlugin") . ',' .
207     _("UpLoad") . ',' .
208     _("UpLoadPlugin") . ',' .
209     _("UriResolver") . ',' .
210     _("UserPreferences") . ',' .
211     _("UserRatings") . ',' .
212     _("Video") . ',' .
213     _("VisualWiki") . ',' .
214     _("WabiSabi") . ',' .
215     _("WantedPages") . ',' .
216     _("WantedPagesOld") . ',' .
217     _("WatchPage") . ',' .
218     _("WhoIsOnline") . ',' .
219     _("WikiAdminChown") . ',' .
220     _("WikiAdminDeleteAcl") . ',' .
221     _("WikiAdminPurge") . ',' .
222     _("WikiAdminRemove") . ',' .
223     _("WikiAdminRename") . ',' .
224     _("WikiAdminSearchReplace") . ',' .
225     _("WikiAdminSelect") . ',' .
226     _("WikiAdminSetAcl") . ',' .
227     _("WikiAdminSetAclSimple") . ',' .
228     _("WikiAdminSetExternal") . ',' .
229     _("WikiAdminUtils") . ',' .
230     _("WikiBlog") . ',' .
231     _("WikiBlogPlugin") . ',' .
232     _("WikicreoleTable") . ',' .
233     _("WikiForm") . ',' .
234     _("WikiFormRich") . ',' .
235     _("WikiForum") . ',' .
236     _("WikiPlugin") . ',' .
237     _("WikiPoll") . ',' .
238     _("WikiTranslation") . ',' .
239     _("WikiWikiWeb") . ',' .
240     _("YouTube");
241
242 require_once 'lib/PageList.php';
243
244 class WikiPlugin_WikiTranslation
245     extends WikiPlugin
246 {
247     public $lang;
248     public $_locales;
249     public $_reverse_locales;
250     public $args;
251
252     function getDescription()
253     {
254         return _("Show translations of various words or pages.");
255     }
256
257     function getDefaultArguments()
258     {
259         return array_merge
260         (
261             PageList::supportedArgs(),
262             array('languages' => '', // comma delimited string of de,en,sv,...
263                 'string' => '',
264                 'page' => '', // use a translation service
265                 'what' => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
266
267                 'match' => '*',
268                 'from_lang' => false,
269                 'include_empty' => false,
270                 //'exclude'       => '',
271                 //'sortby'        => '',
272                 //'limit'         => 0,
273                 'nolinks' => false, // don't display any links
274                 // (for development only)
275                 'noT' => false // don't display the T link
276                 // (for development only)
277             ));
278     }
279
280     function init_locale($lang)
281     {
282         if ($lang != $this->lang)
283             update_locale($lang);
284         $this->_locales[$lang] = $locale;
285     }
286
287     // reverse translation:
288     function translate_to_en($text, $lang = false)
289     {
290         if (!$lang) $lang = $this->lang; // current locale
291         if ($lang == 'en') return $text;
292
293         $this->_locales = array();
294         $this->_reverse_locales = array();
295
296         if (!isset($this->_locales[$lang])) {
297             $this->init_locale($lang);
298         }
299         assert(!empty($this->_locales[$lang]));
300         if (!isset($this->_reverse_locales[$lang])) {
301             // and now do a reverse lookup in the $locale hash
302             $this->_reverse_locales[$lang] = array_flip($this->_locales[$lang]);
303         }
304         if (!empty($this->_reverse_locales[$lang][$text])) {
305             return $this->_reverse_locales[$lang][$text];
306         } else {
307             return $text;
308         }
309     }
310
311     /**
312      * setlocale() switching with the gettext extension is by far too slow.
313      * So use the hash regardless if gettext is loaded or not.
314      */
315     function fast_translate($text, $to_lang, $from_lang = false)
316     {
317         if (!$from_lang) $from_lang = $this->lang; // current locale
318         if ($from_lang == $to_lang) return $text;
319         // setup hash from en => to_lang
320         if (!isset($this->_locales[$to_lang]))
321             $this->init_locale($to_lang);
322         if ($from_lang != 'en') {
323             // get reverse gettext: translate to english
324             $text = $this->translate_to_en($text, $from_lang);
325         }
326         return !empty($this->_locales[$to_lang][$text])
327             ? $this->_locales[$to_lang][$text]
328             : $text;
329     }
330
331     //FIXME! There's something wrong.
332     function translate($text, $to_lang, $from_lang = false)
333     {
334         if (!$from_lang) $from_lang = $this->lang; // current locale
335         if ($from_lang == $to_lang) return $text;
336         // Speed up hash lookup. Not needed for gettext module
337         if (!isset($this->_locales[$from_lang]) and !function_exists('bindtextdomain')) {
338             $this->init_locale($from_lang);
339         }
340         if ($from_lang != 'en') {
341             // get reverse gettext: translate to english
342             $en = $this->translate_to_en($text, $from_lang);
343             // and then to target
344             update_locale($to_lang);
345             $result = gettext($en);
346             update_locale($from_lang);
347         } else {
348             // locale switching is very slow with the gettext extension.
349             // better use fast_translate
350             if ($from_lang != $to_lang) {
351                 update_locale($to_lang);
352             }
353             $result = gettext($text);
354             if ($from_lang != $to_lang) {
355                 update_locale($from_lang);
356             }
357         }
358         return $result;
359     }
360
361     /**
362      * @param WikiDB $dbi
363      * @param string $argstr
364      * @param WikiRequest $request
365      * @param string $basepage
366      * @return mixed
367      */
368     function run($dbi, $argstr, &$request, $basepage)
369     {
370         $this->args = $this->getArgs($argstr, $request);
371         extract($this->args);
372         $this->request = &$request;
373         if (!$from_lang) $from_lang = $request->getPref('lang');
374         if (!$from_lang) $from_lang = $GLOBALS['LANG'];
375         $this->lang = $from_lang;
376
377         if (empty($languages)) {
378             $available_languages = listAvailableLanguages();
379             if ($from_lang == 'en') {
380                 // "en" is always the first.
381                 array_shift($available_languages);
382             }
383             // put from_lang to the very end.
384             if (in_array($from_lang, $available_languages))
385                 $languages = $available_languages;
386             else
387                 $languages = array_merge($available_languages, array($from_lang));
388         } elseif (strstr($languages, ',')) {
389             $languages = explode(',', $languages);
390         } else {
391             $languages = array($languages);
392         }
393         $to_lang = $languages[0];
394         if (!empty($string) and count($languages) == 1) {
395             return $this->translate($string, $to_lang, $from_lang);
396         }
397         if (!empty($page)) {
398             $pagename = $page;
399             if ($dbi->isWikiPage($pagename)) {
400                 $url = '';
401                 // google can only translate from english and french
402                 if (in_array($from_lang, array('en', 'fr'))) {
403                     $url = "http://translate.google.com/translate";
404                     $url .= "?langpair=" . urlencode($from_lang . "|" . $to_lang);
405                     $url .= "&u=" . urlencode(WikiURL($pagename, array(), true));
406                 }
407                 // redirect or transclude?
408                 if ($url) {
409                     return $request->redirect($url);
410                 }
411                 return HTML(fmt("TODO: Google can only translate from english and french. Find a translation service for %s to language %s",
412                     WikiURL($pagename, array(), true),
413                     $to_lang));
414             } else {
415                 return $this->error(fmt("%s is empty.", $pagename));
416             }
417         }
418
419         $pagelist = new PageList('', $exclude, $this->args);
420         $pagelist->_columns[0]->_heading = "$from_lang";
421         foreach ($languages as $lang) {
422             if ($lang == $from_lang) continue;
423             $field = "custom:$lang";
424             $pagelist->addColumnObject(
425                 new _PageList_Column_customlang($field, $from_lang, $this));
426         }
427         if (!empty($string)) {
428             $pagelist->addPage($string);
429             return $pagelist;
430         }
431         switch ($what) {
432             case 'allpages':
433                 $pagelist->addPages($dbi->getAllPages($include_empty, $sortby,
434                     $limit, $exclude));
435                 break;
436             case 'pages':
437                 // not all pages, only the pgsrc pages
438                 if (!is_array($exclude))
439                     $exclude = $pagelist->explodePageList($exclude, false, $sortby,
440                         $limit, $exclude);
441                 $path = FindLocalizedFile(WIKI_PGSRC);
442                 $pgsrc = new fileSet($path);
443                 foreach ($pgsrc->getFiles($exclude, $sortby, $limit) as $pagename) {
444                     $pagename = urldecode($pagename);
445                     if (substr($pagename, -1, 1) == '~') continue;
446                     if (in_array($pagename, $exclude))
447                         continue; // exclude page.
448                     if ($match != '*' and !glob_match($match, $pagename))
449                         continue;
450                     $page_handle = $dbi->getPage($pagename);
451                     $pagelist->addPage($page_handle);
452                 }
453                 break;
454             case 'wikiwords':
455                 if (!isset($this->_locales[$from_lang])) {
456                     $this->init_locale($from_lang);
457                 }
458                 $locale = & $this->_locales[$from_lang];
459                 if (is_array($locale)) {
460                     $count = 0;
461                     foreach ($locale as $from => $to) {
462                         if ($match != '*' and !glob_match($match, $from))
463                             continue;
464                         if (isWikiWord($from)) {
465                             $count++;
466                             $pagelist->addPage($from);
467                             if ($limit and $count > $limit) break;
468                         }
469                     }
470                 }
471                 break;
472             // all Button texts, which need a localized .png
473             // where to get them from? templates/*.tmpl: Button()
474             // and WikiLink(?,'button')
475             // navbar links, actionpages, and admin requests
476             case 'buttons':
477                 $buttons = $GLOBALS['AllActionPages'];
478                 $fileset = new FileSet(FindFile("themes/MacOSX/buttons/en"),
479                     "*.png");
480                 foreach ($fileset->getFiles() as $file) {
481                     $b = urldecode(substr($file, 0, -4));
482                     if (!in_array($b, $buttons))
483                         $buttons[] = $b;
484                 }
485                 $count = 0;
486                 foreach ($buttons as $button) {
487                     $pagelist->addPage($button);
488                     if ($limit and ++$count > $limit) break;
489                 }
490                 break;
491         }
492         return $pagelist;
493     }
494 }
495
496 class _PageList_Column_customlang extends _PageList_Column
497 {
498     function _PageList_Column_customlang($field, $from_lang, $plugin)
499     {
500         /**
501          * @var WikiRequest $request
502          */
503         global $request;
504
505         $this->_field = $field;
506         $this->_from_lang = $from_lang;
507         $this->_plugin =& $plugin;
508         $this->_what = $plugin->args['what'];
509         $this->_noT = $plugin->args['noT'];
510         $this->_nolinks = $plugin->args['nolinks'];
511         $this->_iscustom = substr($field, 0, 7) == 'custom:';
512         if ($this->_iscustom)
513             $this->_field = substr($field, 7);
514         //$heading = $field;
515         $this->dbi = &$request->getDbh();
516         $this->_PageList_Column_base($this->_field);
517     }
518
519     function _getValue($page, &$revision_handle)
520     {
521         if (is_object($page)) $text = $page->getName();
522         else $text = $page;
523         $trans = $this->_plugin->fast_translate($text, $this->_field,
524             $this->_from_lang);
525         // how to markup untranslated words and not existing pages?
526         // untranslated: (TODO) link to translation editor
527         if ($trans == $text or // untranslated
528             (($this->_from_lang != 'en') and
529                 ($this->_field != 'en') and
530                     ($trans == $this->_plugin->fast_translate($text, 'en',
531                         $this->_from_lang))
532             )
533         ) {
534             global $WikiTheme;
535             $link = $WikiTheme->linkUnknownWikiWord($trans);
536             if (!($this->_noT or $this->_nolinks)
537                 and $this->dbi->isWikiPage($trans)
538             ) {
539                 $url = WikiURL($trans, array('action' => 'TranslateText',
540                     'lang' => $this->_field));
541                 $button = $WikiTheme->makeButton('T', $url);
542                 $button->addTooltip(sprintf(_("Define the translation for %s in %s"),
543                     $trans, $this->_field));
544                 $link = HTML::span($button);
545                 $link->setAttr('class', 'wikiunknown');
546                 $text = HTML::span($WikiTheme->maybeSplitWikiWord($trans));
547                 $text->setAttr('style', 'text-decoration:line-through');
548                 $link->pushContent($text);
549                 return $link;
550             } elseif (is_object($page))
551                 return ''; else // not existing: empty
552                 return '';
553         } elseif (is_object($page)) {
554             if (!$this->_nolinks)
555                 return WikiLink($trans, 'auto');
556             else
557                 return $trans;
558         } else {
559             return $trans;
560         }
561     }
562 }
563
564 // Local Variables:
565 // mode: php
566 // tab-width: 8
567 // c-basic-offset: 4
568 // c-hanging-comment-ender-p: nil
569 // indent-tabs-mode: nil
570 // End: