You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

  1. <?php
  2. function ajax_wikipedia ($param) {
  3. if (preg_match("/^([^:]+):(.*)$/", $param['page'], $m)) {
  4. $wp_lang = $m[1];
  5. $wp_page = $m[2];
  6. }
  7. if (!isset($wp_lang) || !isset($wp_page)) {
  8. return false;
  9. }
  10. $wp_url = "https://{$wp_lang}.wikipedia.org/wiki/" . urlencode(strtr($wp_page, array(" " => "_")));
  11. $content = file_get_contents($wp_url);
  12. $langList = array($wp_lang => $wp_url);
  13. $dom = new DOMDocument();
  14. $dom->loadHTML($content);
  15. $langDiv = $dom->getElementsByTagName('li');//interlanguage-link interwiki-bar');
  16. for ($i = 0; $i < $langDiv->length; $i++) {
  17. $li = $langDiv->item($i);
  18. if (preg_match('/^interlanguage-link interwiki-([a-z\-]+)( .*|)$/', $li->getAttribute('class'), $m)) {
  19. $a = $li->firstChild;
  20. $langList[$m[1]] = $a->getAttribute('href');
  21. }
  22. }
  23. if ($wp_lang !== $param['lang'] && array_key_exists($param['lang'], $langList)) {
  24. $content = file_get_contents($langList[$param['lang']]);
  25. $wp_lang = $param['lang'];
  26. }
  27. return array(
  28. 'content' => $content,
  29. 'languages' => $langList,
  30. 'language' => $wp_lang,
  31. );
  32. }