]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiTranslation.php
Remove WantedPagesOld (use WantedPages instead)
[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     _("WatchPage") . ',' .
217     _("WhoIsOnline") . ',' .
218     _("WikiAdminChown") . ',' .
219     _("WikiAdminDeleteAcl") . ',' .
220     _("WikiAdminPurge") . ',' .
221     _("WikiAdminRemove") . ',' .
222     _("WikiAdminRename") . ',' .
223     _("WikiAdminSearchReplace") . ',' .
224     _("WikiAdminSelect") . ',' .
225     _("WikiAdminSetAcl") . ',' .
226     _("WikiAdminSetAclSimple") . ',' .
227     _("WikiAdminSetExternal") . ',' .
228     _("WikiAdminUtils") . ',' .
229     _("WikiBlog") . ',' .
230     _("WikiBlogPlugin") . ',' .
231     _("WikicreoleTable") . ',' .
232     _("WikiForm") . ',' .
233     _("WikiFormRich") . ',' .
234     _("WikiForum") . ',' .
235     _("WikiPlugin") . ',' .
236     _("WikiPoll") . ',' .
237     _("WikiTranslation") . ',' .
238     _("WikiWikiWeb") . ',' .
239     _("YouTube");
240
241 require_once 'lib/PageList.php';
242
243 class WikiPlugin_WikiTranslation
244     extends WikiPlugin
245 {
246     public $lang;
247     public $_locales;
248     public $_reverse_locales;
249     public $args;
250
251     function getDescription()
252     {
253         return _("Show translations of various words or pages.");
254     }
255
256     function getDefaultArguments()
257     {
258         return array_merge
259         (
260             PageList::supportedArgs(),
261             array('languages' => '', // comma delimited string of de,en,sv,...
262                 'string' => '',
263                 'page' => '', // use a translation service
264                 'what' => 'pages', // or 'buttons', 'plugins' or 'wikiwords'
265
266                 'match' => '*',
267                 'from_lang' => false,
268                 'include_empty' => false,
269                 //'exclude'       => '',
270                 //'sortby'        => '',
271                 //'limit'         => 0,
272                 'nolinks' => false, // don't display any links
273                 // (for development only)
274                 'noT' => false // don't display the T link
275                 // (for development only)
276             ));
277     }
278
279     function init_locale($lang)
280     {
281         if ($lang != $this->lang)
282             update_locale($lang);
283         $this->_locales[$lang] = $locale;
284     }
285
286     // reverse translation:
287     function translate_to_en($text, $lang = false)
288     {
289         if (!$lang) $lang = $this->lang; // current locale
290         if ($lang == 'en') return $text;
291
292         $this->_locales = array();
293         $this->_reverse_locales = array();
294
295         if (!isset($this->_locales[$lang])) {
296             $this->init_locale($lang);
297         }
298         assert(!empty($this->_locales[$lang]));
299         if (!isset($this->_reverse_locales[$lang])) {
300             // and now do a reverse lookup in the $locale hash
301             $this->_reverse_locales[$lang] = array_flip($this->_locales[$lang]);
302         }
303         if (!empty($this->_reverse_locales[$lang][$text])) {
304             return $this->_reverse_locales[$lang][$text];
305         } else {
306             return $text;
307         }
308     }
309
310     /**
311      * setlocale() switching with the gettext extension is by far too slow.
312      * So use the hash regardless if gettext is loaded or not.
313      */
314     function fast_translate($text, $to_lang, $from_lang = false)
315     {
316         if (!$from_lang) $from_lang = $this->lang; // current locale
317         if ($from_lang == $to_lang) return $text;
318         // setup hash from en => to_lang
319         if (!isset($this->_locales[$to_lang]))
320             $this->init_locale($to_lang);
321         if ($from_lang != 'en') {
322             // get reverse gettext: translate to english
323             $text = $this->translate_to_en($text, $from_lang);
324         }
325         return !empty($this->_locales[$to_lang][$text])
326             ? $this->_locales[$to_lang][$text]
327             : $text;
328     }
329
330     //FIXME! There's something wrong.
331     function translate($text, $to_lang, $from_lang = false)
332     {
333         if (!$from_lang) $from_lang = $this->lang; // current locale
334         if ($from_lang == $to_lang) return $text;
335         // Speed up hash lookup. Not needed for gettext module
336         if (!isset($this->_locales[$from_lang]) and !function_exists('bindtextdomain')) {
337             $this->init_locale($from_lang);
338         }
339         if ($from_lang != 'en') {
340             // get reverse gettext: translate to english
341             $en = $this->translate_to_en($text, $from_lang);
342             // and then to target
343             update_locale($to_lang);
344             $result = gettext($en);
345             update_locale($from_lang);
346         } else {
347             // locale switching is very slow with the gettext extension.
348             // better use fast_translate
349             if ($from_lang != $to_lang) {
350                 update_locale($to_lang);
351             }
352             $result = gettext($text);
353             if ($from_lang != $to_lang) {
354                 update_locale($from_lang);
355             }
356         }
357         return $result;
358     }
359
360     /**
361      * @param WikiDB $dbi
362      * @param string $argstr
363      * @param WikiRequest $request
364      * @param string $basepage
365      * @return mixed
366      */
367     function run($dbi, $argstr, &$request, $basepage)
368     {
369         $this->args = $this->getArgs($argstr, $request);
370         extract($this->args);
371         $this->request = &$request;
372         if (!$from_lang) $from_lang = $request->getPref('lang');
373         if (!$from_lang) $from_lang = $GLOBALS['LANG'];
374         $this->lang = $from_lang;
375
376         if (empty($languages)) {
377             $available_languages = listAvailableLanguages();
378             if ($from_lang == 'en') {
379                 // "en" is always the first.
380                 array_shift($available_languages);
381             }
382             // put from_lang to the very end.
383             if (in_array($from_lang, $available_languages))
384                 $languages = $available_languages;
385             else
386                 $languages = array_merge($available_languages, array($from_lang));
387         } elseif (strstr($languages, ',')) {
388             $languages = explode(',', $languages);
389         } else {
390             $languages = array($languages);
391         }
392         $to_lang = $languages[0];
393         if (!empty($string) and count($languages) == 1) {
394             return $this->translate($string, $to_lang, $from_lang);
395         }
396         if (!empty($page)) {
397             $pagename = $page;
398             if ($dbi->isWikiPage($pagename)) {
399                 $url = '';
400                 // google can only translate from english and french
401                 if (in_array($from_lang, array('en', 'fr'))) {
402                     $url = "http://translate.google.com/translate";
403                     $url .= "?langpair=" . urlencode($from_lang . "|" . $to_lang);
404                     $url .= "&u=" . urlencode(WikiURL($pagename, array(), true));
405                 }
406                 // redirect or transclude?
407                 if ($url) {
408                     return $request->redirect($url);
409                 }
410                 return HTML(fmt("TODO: Google can only translate from english and french. Find a translation service for %s to language %s",
411                     WikiURL($pagename, array(), true),
412                     $to_lang));
413             } else {
414                 return $this->error(fmt("%s is empty.", $pagename));
415             }
416         }
417
418         $pagelist = new PageList('', $exclude, $this->args);
419         $pagelist->_columns[0]->_heading = "$from_lang";
420         foreach ($languages as $lang) {
421             if ($lang == $from_lang) continue;
422             $field = "custom:$lang";
423             $pagelist->addColumnObject(
424                 new _PageList_Column_customlang($field, $from_lang, $this));
425         }
426         if (!empty($string)) {
427             $pagelist->addPage($string);
428             return $pagelist;
429         }
430         switch ($what) {
431             case 'allpages':
432                 $pagelist->addPages($dbi->getAllPages($include_empty, $sortby,
433                     $limit, $exclude));
434                 break;
435             case 'pages':
436                 // not all pages, only the pgsrc pages
437                 if (!is_array($exclude))
438                     $exclude = $pagelist->explodePageList($exclude, false, $sortby,
439                         $limit, $exclude);
440                 $path = FindLocalizedFile(WIKI_PGSRC);
441                 $pgsrc = new fileSet($path);
442                 foreach ($pgsrc->getFiles($exclude, $sortby, $limit) as $pagename) {
443                     $pagename = urldecode($pagename);
444                     if (substr($pagename, -1, 1) == '~') continue;
445                     if (in_array($pagename, $exclude))
446                         continue; // exclude page.
447                     if ($match != '*' and !glob_match($match, $pagename))
448                         continue;
449                     $page_handle = $dbi->getPage($pagename);
450                     $pagelist->addPage($page_handle);
451                 }
452                 break;
453             case 'wikiwords':
454                 if (!isset($this->_locales[$from_lang])) {
455                     $this->init_locale($from_lang);
456                 }
457                 $locale = & $this->_locales[$from_lang];
458                 if (is_array($locale)) {
459                     $count = 0;
460                     foreach ($locale as $from => $to) {
461                         if ($match != '*' and !glob_match($match, $from))
462                             continue;
463                         if (isWikiWord($from)) {
464                             $count++;
465                             $pagelist->addPage($from);
466                             if ($limit and $count > $limit) break;
467                         }
468                     }
469                 }
470                 break;
471             // all Button texts, which need a localized .png
472             // where to get them from? templates/*.tmpl: Button()
473             // and WikiLink(?,'button')
474             // navbar links, actionpages, and admin requests
475             case 'buttons':
476                 $buttons = $GLOBALS['AllActionPages'];
477                 $fileset = new FileSet(FindFile("themes/MacOSX/buttons/en"),
478                     "*.png");
479                 foreach ($fileset->getFiles() as $file) {
480                     $b = urldecode(substr($file, 0, -4));
481                     if (!in_array($b, $buttons))
482                         $buttons[] = $b;
483                 }
484                 $count = 0;
485                 foreach ($buttons as $button) {
486                     $pagelist->addPage($button);
487                     if ($limit and ++$count > $limit) break;
488                 }
489                 break;
490         }
491         return $pagelist;
492     }
493 }
494
495 class _PageList_Column_customlang extends _PageList_Column
496 {
497     function _PageList_Column_customlang($field, $from_lang, $plugin)
498     {
499         /**
500          * @var WikiRequest $request
501          */
502         global $request;
503
504         $this->_field = $field;
505         $this->_from_lang = $from_lang;
506         $this->_plugin =& $plugin;
507         $this->_what = $plugin->args['what'];
508         $this->_noT = $plugin->args['noT'];
509         $this->_nolinks = $plugin->args['nolinks'];
510         $this->_iscustom = substr($field, 0, 7) == 'custom:';
511         if ($this->_iscustom)
512             $this->_field = substr($field, 7);
513         //$heading = $field;
514         $this->dbi = &$request->getDbh();
515         $this->_PageList_Column_base($this->_field);
516     }
517
518     function _getValue($page, $revision_handle)
519     {
520         if (is_object($page)) $text = $page->getName();
521         else $text = $page;
522         $trans = $this->_plugin->fast_translate($text, $this->_field,
523             $this->_from_lang);
524         // how to markup untranslated words and not existing pages?
525         // untranslated: (TODO) link to translation editor
526         if ($trans == $text or // untranslated
527             (($this->_from_lang != 'en') and
528                 ($this->_field != 'en') and
529                     ($trans == $this->_plugin->fast_translate($text, 'en',
530                         $this->_from_lang))
531             )
532         ) {
533             global $WikiTheme;
534             $link = $WikiTheme->linkUnknownWikiWord($trans);
535             if (!($this->_noT or $this->_nolinks)
536                 and $this->dbi->isWikiPage($trans)
537             ) {
538                 $url = WikiURL($trans, array('action' => 'TranslateText',
539                     'lang' => $this->_field));
540                 $button = $WikiTheme->makeButton('T', $url);
541                 $button->addTooltip(sprintf(_("Define the translation for %s in %s"),
542                     $trans, $this->_field));
543                 $link = HTML::span($button);
544                 $link->setAttr('class', 'wikiunknown');
545                 $text = HTML::span($WikiTheme->maybeSplitWikiWord($trans));
546                 $text->setAttr('style', 'text-decoration:line-through');
547                 $link->pushContent($text);
548                 return $link;
549             } elseif (is_object($page))
550                 return ''; else // not existing: empty
551                 return '';
552         } elseif (is_object($page)) {
553             if (!$this->_nolinks)
554                 return WikiLink($trans, 'auto');
555             else
556                 return $trans;
557         } else {
558             return $trans;
559         }
560     }
561 }
562
563 // Local Variables:
564 // mode: php
565 // tab-width: 8
566 // c-basic-offset: 4
567 // c-hanging-comment-ender-p: nil
568 // indent-tabs-mode: nil
569 // End: