]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiTranslation.php
Remove IMDB stuff
[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     _("FrameInclude") . ',' .
99     _("FrameIncludePlugin") . ',' .
100     _("FullRecentChanges") . ',' .
101     _("FullTextSearch") . ',' .
102     _("FuzzyPages") . ',' .
103     _("GoogleMaps") . ',' .
104     _("GooglePlugin") . ',' .
105     _("GoTo") . ',' .
106     _("GraphViz") . ',' .
107     _("DebugGroupInfo") . ',' .
108     _("HelloWorld") . ',' .
109     _("HelloWorldPlugin") . ',' .
110     _("HomePageAlias") . ',' .
111     _("HtmlConverter") . ',' .
112     _("IncludePage") . ',' .
113     _("IncludePagePlugin") . ',' .
114     _("IncludePages") . ',' .
115     _("IncludeSiteMap") . ',' .
116     _("IncludeTree") . ',' .
117     _("InterWiki") . ',' .
118     _("InterWikiSearch") . ',' .
119     _("JabberPresence") . ',' .
120     _("LdapSearch") . ',' .
121     _("LikePages") . ',' .
122     _("LinkDatabase") . ',' .
123     _("LinkIcons") . ',' .
124     _("LinkSearch") . ',' .
125     _("ListPages") . ',' .
126     _("ListRelations") . ',' .
127     _("ListSubpages") . ',' .
128     _("MagicPhpWikiURLs") . ',' .
129     _("MediawikiTable") . ',' .
130     _("ModeratedPage") . ',' .
131     _("MoreAboutMechanics") . ',' .
132     _("MostPopular") . ',' .
133     _("NewPagesPerUser") . ',' .
134     _("NoCache") . ',' .
135     _("OldStyleTable") . ',' .
136     _("OldStyleTablePlugin") . ',' .
137     _("OrphanedPages") . ',' .
138     _("PageDump") . ',' .
139     _("PageGroup") . ',' .
140     _("PageHistory") . ',' .
141     _("PageInfo") . ',' .
142     _("PageTrail") . ',' .
143     _("PasswordReset") . ',' .
144     _("PhotoAlbum") . ',' .
145     _("PhotoAlbumPlugin") . ',' .
146     _("PhpHighlight") . ',' .
147     _("PhpHighlightPlugin") . ',' .
148     _("PhpWeather") . ',' .
149     _("PhpWeatherPlugin") . ',' .
150     _("PhpWiki") . ',' .
151     _("PhpWikiAdministration") . ',' .
152     _("PhpWikiDocumentation") . ',' .
153     _("PhpWikiPoll") . ',' .
154     _("Ploticus") . ',' .
155     _("PloticusPlugin") . ',' .
156     _("PluginManager") . ',' .
157     _("PopularNearby") . ',' .
158     _("PopularTags") . ',' .
159     _("PopUp") . ',' .
160     _("PreferenceApp") . ',' .
161     _("PreferencesInfo") . ',' .
162     _("PrevNext") . ',' .
163     _("Processing") . ',' .
164     _("RandomPage") . ',' .
165     _("RateIt") . ',' .
166     _("RawHtml") . ',' .
167     _("RawHtmlPlugin") . ',' .
168     _("RecentChanges") . ',' .
169     _("RecentChangesCached") . ',' .
170     _("RecentComments") . ',' .
171     _("RecentEdits") . ',' .
172     _("RecentReferrers") . ',' .
173     _("RecentVisitors") . ',' .
174     _("RedirectTo") . ',' .
175     _("RedirectToPlugin") . ',' .
176     _("RelatedChanges") . ',' .
177     _("ReleaseNotes") . ',' .
178     _("Remove") . ',' .
179     _("Rename") . ',' .
180     _("Replace") . ',' .
181     _("DebugRetransform") . ',' .
182     _("RichTable") . ',' .
183     _("RichTablePlugin") . ',' .
184     _("RssFeed") . ',' .
185     _("SearchHighlight") . ',' .
186     _("SemanticRelations") . ',' .
187     _("SemanticSearch") . ',' .
188     _("SemanticSearchAdvanced") . ',' .
189     _("SetAcl") . ',' .
190     _("SiteMap") . ',' .
191     _("SpellCheck") . ',' .
192     _("SpecialPages") . ',' .
193     _("SqlResult") . ',' .
194     _("SyncWiki") . ',' .
195     _("SyntaxHighlighter") . ',' .
196     _("SystemInfo") . ',' .
197     _("SystemInfoPlugin") . ',' .
198     _("Template") . ',' .
199     _("TeX2png") . ',' .
200     _("text2png") . ',' .
201     _("TextFormattingRules") . ',' .
202     _("TexToPng") . ',' .
203     _("TitleSearch") . ',' .
204     _("Transclude") . ',' .
205     _("TranscludePlugin") . ',' .
206     _("TranslateText") . ',' .
207     _("UnfoldSubpages") . ',' .
208     _("UnfoldSubpagesPlugin") . ',' .
209     _("UpLoad") . ',' .
210     _("UpLoadPlugin") . ',' .
211     _("UriResolver") . ',' .
212     _("UserPreferences") . ',' .
213     _("UserRatings") . ',' .
214     _("Video") . ',' .
215     _("VisualWiki") . ',' .
216     _("WabiSabi") . ',' .
217     _("WantedPages") . ',' .
218     _("WantedPagesOld") . ',' .
219     _("WatchPage") . ',' .
220     _("WhoIsOnline") . ',' .
221     _("WikiAdminChown") . ',' .
222     _("WikiAdminDeleteAcl") . ',' .
223     _("WikiAdminPurge") . ',' .
224     _("WikiAdminRemove") . ',' .
225     _("WikiAdminRename") . ',' .
226     _("WikiAdminSearchReplace") . ',' .
227     _("WikiAdminSelect") . ',' .
228     _("WikiAdminSetAcl") . ',' .
229     _("WikiAdminSetAclSimple") . ',' .
230     _("WikiAdminSetExternal") . ',' .
231     _("WikiAdminUtils") . ',' .
232     _("WikiBlog") . ',' .
233     _("WikiBlogPlugin") . ',' .
234     _("WikicreoleTable") . ',' .
235     _("WikiForm") . ',' .
236     _("WikiFormRich") . ',' .
237     _("WikiForum") . ',' .
238     _("WikiPlugin") . ',' .
239     _("WikiPoll") . ',' .
240     _("WikiTranslation") . ',' .
241     _("WikiWikiWeb") . ',' .
242     _("YouTube");
243
244 require_once 'lib/PageList.php';
245
246 class WikiPlugin_WikiTranslation
247     extends WikiPlugin
248 {
249
250     function getDescription()
251     {
252         return _("Show translations of various words or pages.");
253     }
254
255     function getDefaultArguments()
256     {
257         return array_merge
258         (
259             PageList::supportedArgs(),
260             array('languages' => '', // comma delimited string of de,en,sv,...
261                 'string' => '',
262                 'page' => '', // use a translation service
263                 'what' => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
264
265                 'match' => '*',
266                 'from_lang' => false,
267                 'include_empty' => false,
268                 //'exclude'       => '',
269                 //'sortby'        => '',
270                 //'limit'         => 0,
271                 'nolinks' => false, // don't display any links
272                 // (for development only)
273                 'noT' => false // don't display the T link
274                 // (for development only)
275             ));
276     }
277
278     function init_locale($lang)
279     {
280         if ($lang != $this->lang)
281             update_locale($lang);
282         $this->_locales[$lang] = $locale;
283     }
284
285     // reverse translation:
286     function translate_to_en($text, $lang = false)
287     {
288         if (!$lang) $lang = $this->lang; // current locale
289         if ($lang == 'en') return $text;
290
291         $this->_locales = array();
292         $this->_reverse_locales = array();
293
294         if (!isset($this->_locales[$lang])) {
295             $this->init_locale($lang);
296         }
297         assert(!empty($this->_locales[$lang]));
298         if (!isset($this->_reverse_locales[$lang])) {
299             // and now do a reverse lookup in the $locale hash
300             $this->_reverse_locales[$lang] = array_flip($this->_locales[$lang]);
301         }
302         if (!empty($this->_reverse_locales[$lang][$text])) {
303             return $this->_reverse_locales[$lang][$text];
304         } else {
305             return $text;
306         }
307     }
308
309     /**
310      * setlocale() switching with the gettext extension is by far too slow.
311      * So use the hash regardless if gettext is loaded or not.
312      */
313     function fast_translate($text, $to_lang, $from_lang = false)
314     {
315         if (!$from_lang) $from_lang = $this->lang; // current locale
316         if ($from_lang == $to_lang) return $text;
317         // setup hash from en => to_lang
318         if (!isset($this->_locales[$to_lang]))
319             $this->init_locale($to_lang);
320         if ($from_lang != 'en') {
321             // get reverse gettext: translate to english
322             $text = $this->translate_to_en($text, $from_lang);
323         }
324         return !empty($this->_locales[$to_lang][$text])
325             ? $this->_locales[$to_lang][$text]
326             : $text;
327     }
328
329     //FIXME! There's something wrong.
330     function translate($text, $to_lang, $from_lang = false)
331     {
332         if (!$from_lang) $from_lang = $this->lang; // current locale
333         if ($from_lang == $to_lang) return $text;
334         // Speed up hash lookup. Not needed for gettext module
335         if (!isset($this->_locales[$from_lang]) and !function_exists('bindtextdomain')) {
336             $this->init_locale($from_lang);
337         }
338         if ($from_lang != 'en') {
339             // get reverse gettext: translate to english
340             $en = $this->translate_to_en($text, $from_lang);
341             // and then to target
342             update_locale($to_lang);
343             $result = gettext($en);
344             update_locale($from_lang);
345         } else {
346             // locale switching is very slow with the gettext extension.
347             // better use fast_translate
348             if ($from_lang != $to_lang) {
349                 update_locale($to_lang);
350             }
351             $result = gettext($text);
352             if ($from_lang != $to_lang) {
353                 update_locale($from_lang);
354             }
355         }
356         return $result;
357     }
358
359     function run($dbi, $argstr, &$request, $basepage)
360     {
361         $this->args = $this->getArgs($argstr, $request);
362         extract($this->args);
363         $this->request = &$request;
364         if (!$from_lang) $from_lang = $request->getPref('lang');
365         if (!$from_lang) $from_lang = $GLOBALS['LANG'];
366         $this->lang = $from_lang;
367
368         if (empty($languages)) {
369             $available_languages = listAvailableLanguages();
370             if ($from_lang == 'en') {
371                 // "en" is always the first.
372                 array_shift($available_languages);
373             }
374             // put from_lang to the very end.
375             if (in_array($from_lang, $available_languages))
376                 $languages = $available_languages;
377             else
378                 $languages = array_merge($available_languages, array($from_lang));
379         } elseif (strstr($languages, ',')) {
380             $languages = explode(',', $languages);
381         } else {
382             $languages = array($languages);
383         }
384         $to_lang = $languages[0];
385         if (!empty($string) and count($languages) == 1) {
386             return $this->translate($string, $to_lang, $from_lang);
387         }
388         if (!empty($page)) {
389             $pagename = $page;
390             if ($dbi->isWikiPage($pagename)) {
391                 $url = '';
392                 // google can only translate from english and french
393                 if (in_array($from_lang, array('en', 'fr'))) {
394                     $url = "http://translate.google.com/translate";
395                     $url .= "?langpair=" . urlencode($from_lang . "|" . $to_lang);
396                     $url .= "&u=" . urlencode(WikiURL($pagename, false, true));
397                 }
398                 // redirect or transclude?
399                 if ($url) {
400                     return $request->redirect($url);
401                 }
402                 return HTML(fmt("TODO: Google can only translate from english and french. Find a translation service for %s to language %s",
403                     WikiURL($pagename, false, true),
404                     $to_lang));
405             } else {
406                 return $this->error(fmt("%s is empty.", $pagename));
407             }
408         }
409
410         $pagelist = new PageList('', $exclude, $this->args);
411         $pagelist->_columns[0]->_heading = "$from_lang";
412         foreach ($languages as $lang) {
413             if ($lang == $from_lang) continue;
414             $field = "custom:$lang";
415             $pagelist->addColumnObject(
416                 new _PageList_Column_customlang($field, $from_lang, $this));
417         }
418         if (!empty($string)) {
419             $pagelist->addPage($string);
420             return $pagelist;
421         }
422         switch ($what) {
423             case 'allpages':
424                 $pagelist->addPages($dbi->getAllPages($include_empty, $sortby,
425                     $limit, $exclude));
426                 break;
427             case 'pages':
428                 // not all pages, only the pgsrc pages
429                 if (!is_array($exclude))
430                     $exclude = $pagelist->explodePageList($exclude, false, $sortby,
431                         $limit, $exclude);
432                 $path = FindLocalizedFile(WIKI_PGSRC);
433                 $pgsrc = new fileSet($path);
434                 foreach ($pgsrc->getFiles($exclude, $sortby, $limit) as $pagename) {
435                     $pagename = urldecode($pagename);
436                     if (substr($pagename, -1, 1) == '~') continue;
437                     if (in_array($pagename, $exclude))
438                         continue; // exclude page.
439                     if ($match != '*' and !glob_match($match, $pagename))
440                         continue;
441                     $page_handle = $dbi->getPage($pagename);
442                     $pagelist->addPage($page_handle);
443                 }
444                 break;
445             case 'wikiwords':
446                 if (!isset($this->_locales[$from_lang])) {
447                     $this->init_locale($from_lang);
448                 }
449                 $locale = & $this->_locales[$from_lang];
450                 if (is_array($locale)) {
451                     $count = 0;
452                     foreach ($locale as $from => $to) {
453                         if ($match != '*' and !glob_match($match, $from))
454                             continue;
455                         if (isWikiWord($from)) {
456                             $count++;
457                             $pagelist->addPage($from);
458                             if ($limit and $count > $limit) break;
459                         }
460                     }
461                 }
462                 break;
463             // all Button texts, which need a localized .png
464             // where to get them from? templates/*.tmpl: Button()
465             // and WikiLink(?,'button')
466             // navbar links, actionpages, and admin requests
467             case 'buttons':
468                 $buttons = $GLOBALS['AllActionPages'];
469                 $fileset = new FileSet(FindFile("themes/MacOSX/buttons/en"),
470                     "*.png");
471                 foreach ($fileset->getFiles() as $file) {
472                     $b = urldecode(substr($file, 0, -4));
473                     if (!in_array($b, $buttons))
474                         $buttons[] = $b;
475                 }
476                 $count = 0;
477                 foreach ($buttons as $button) {
478                     $pagelist->addPage($button);
479                     if ($limit and ++$count > $limit) break;
480                 }
481                 break;
482         }
483         return $pagelist;
484     }
485 }
486
487 class _PageList_Column_customlang extends _PageList_Column
488 {
489     function _PageList_Column_customlang($field, $from_lang, $plugin)
490     {
491         $this->_field = $field;
492         $this->_from_lang = $from_lang;
493         $this->_plugin =& $plugin;
494         $this->_what = $plugin->args['what'];
495         $this->_noT = $plugin->args['noT'];
496         $this->_nolinks = $plugin->args['nolinks'];
497         $this->_iscustom = substr($field, 0, 7) == 'custom:';
498         if ($this->_iscustom)
499             $this->_field = substr($field, 7);
500         //$heading = $field;
501         $this->dbi = &$GLOBALS['request']->getDbh();
502         $this->_PageList_Column_base($this->_field);
503     }
504
505     function _getValue($page, &$revision_handle)
506     {
507         if (is_object($page)) $text = $page->getName();
508         else $text = $page;
509         $trans = $this->_plugin->fast_translate($text, $this->_field,
510             $this->_from_lang);
511         // how to markup untranslated words and not existing pages?
512         // untranslated: (TODO) link to translation editor
513         if ($trans == $text or // untranslated
514             (($this->_from_lang != 'en') and
515                 ($this->_field != 'en') and
516                     ($trans == $this->_plugin->fast_translate($text, 'en',
517                         $this->_from_lang))
518             )
519         ) {
520             global $WikiTheme;
521             $link = $WikiTheme->linkUnknownWikiWord($trans);
522             if (!($this->_noT or $this->_nolinks)
523                 and $this->dbi->isWikiPage($trans)
524             ) {
525                 $url = WikiURL($trans, array('action' => 'TranslateText',
526                     'lang' => $this->_field));
527                 $button = $WikiTheme->makeButton('T', $url);
528                 $button->addTooltip(sprintf(_("Define the translation for %s in %s"),
529                     $trans, $this->_field));
530                 $link = HTML::span($button);
531                 $link->setAttr('class', 'wikiunknown');
532                 $text = HTML::span($WikiTheme->maybeSplitWikiWord($trans));
533                 $text->setAttr('style', 'text-decoration:line-through');
534                 $link->pushContent($text);
535                 return $link;
536             } elseif (is_object($page))
537                 return ''; else // not existing: empty
538                 return '';
539         } elseif (is_object($page)) {
540             if (!$this->_nolinks)
541                 return WikiLink($trans, 'auto');
542             else
543                 return $trans;
544         } else {
545             return $trans;
546         }
547     }
548 }
549
550 // Local Variables:
551 // mode: php
552 // tab-width: 8
553 // c-basic-offset: 4
554 // c-hanging-comment-ender-p: nil
555 // indent-tabs-mode: nil
556 // End: