ParserStandAlone.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Copyright (C) 2008, 2009 Michael Nowak
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. **/
  20. require_once( dirname(__FILE__) .'/LBFactory_No.php' );
  21. require_once( "$IP/LocalSettings.php" );
  22. class ParserStandAlone extends Parser
  23. {
  24. const LINK_OPTION_BROKEN = 'broken';
  25. const LINK_OPTION_KNOWN = 'known';
  26. /**
  27. * @var array
  28. */
  29. static protected $languageNames = null;
  30. /**
  31. * @var array
  32. */
  33. static protected $templatesExistingCache = null;
  34. /**
  35. * @var array
  36. */
  37. static protected $templatesMissingCache = null;
  38. /**
  39. * @var array
  40. */
  41. static protected $detectedLanguageLinks = null;
  42. /**
  43. * Override the template-fetching-function of the Parser
  44. *
  45. * @global string $IP
  46. * @global string $wgTemplatePath
  47. * @global string $wgTemplateExtension
  48. * @global string $wgTemplatePrefix
  49. * @param Title $title
  50. * @return array
  51. */
  52. function fetchTemplateAndTitle( $title ) {
  53. #echo "\n--- Trying to find offline template: $title ---\n";
  54. global $wgTemplateDB, $wgTemplateFileID;
  55. $finalTitle = $title;
  56. $template_text = null;
  57. # $$$ need to fix later for all languages
  58. # We pad the title with '~' to force the database to import strings
  59. $title_orig = '~' . $wgTemplateFileID . '~' . strtolower($title);
  60. $db = new PDO('sqlite:' . $wgTemplateDB);
  61. $tl = $db->quote($title_orig);
  62. #echo "\n<!-- Orig: {$title_orig}\n SQL: {$tl} -->\n";
  63. $result = $db->query("SELECT body FROM templates WHERE title = {$tl} LIMIT 1");
  64. $data = $result->fetchAll();
  65. $max_loop_count = 25;
  66. while ($max_loop_count && sizeof($data) == 0) {
  67. $result = $db->query("SELECT redirect FROM redirects WHERE title = {$tl} LIMIT 1");
  68. $data = $result->fetchAll();
  69. if (sizeof($data) == 0) {
  70. break;
  71. }
  72. $redirect = $db->quote($data[0]['redirect']);
  73. $result = $db->query("SELECT body FROM templates WHERE title = {$redirect} LIMIT 1");
  74. $data = $result->fetchAll();
  75. --$max_loop_count;
  76. }
  77. if (sizeof($data) > 0) {
  78. $template_text = substr($data[0]['body'], 1);
  79. #echo "\n--- TT:($template_text):TT --- \n";
  80. } else {
  81. $template_text = '';
  82. }
  83. $ret = array( $template_text, $finalTitle );
  84. return $ret;
  85. }
  86. /**
  87. * @param array $mList
  88. * @return bool
  89. */
  90. static public function disableSpecialPages( &$mList ) {
  91. $mList = array();
  92. return true;
  93. }
  94. /**
  95. * @param bool $refresh default: false
  96. * @return array
  97. */
  98. static public function getLanguageNames($refresh = false) {
  99. if ($refresh || self::$languageNames === null) {
  100. self::$languageNames = Language::getLanguageNames();
  101. #var_dump(self::$languageNames);
  102. }
  103. return self::$languageNames;
  104. }
  105. /**
  106. * @global bool $wgLanguageLinks;
  107. * @param Skin $skin
  108. * @param Title $target
  109. * @param string $text
  110. * @param array $customAttribs
  111. * @param array $query
  112. * @param array $options
  113. * @param mixed $ret default: null
  114. * @return bool
  115. */
  116. static public function hookLinkBegin( $skin, $target, &$text, &$customAttribs, &$query, &$options, &$ret ) {
  117. # first make link known.
  118. $brokenKey = array_search(self::LINK_OPTION_BROKEN, $options);
  119. if ( $brokenKey !== false ) {
  120. $options[$brokenKey] = self::LINK_OPTION_KNOWN;
  121. } else if ( array_search(self::LINK_OPTION_KNOWN, $options) === false ) {
  122. $options []= self::LINK_OPTION_KNOWN;
  123. }
  124. $targetNamespace = $target->getNamespace();
  125. if ($targetNamespace == NS_FILE || $targetNamespace == NS_FILE_TALK) {
  126. global $wgFileLinks;
  127. return $wgFileLinks;
  128. } else if ($targetNamespace == NS_CATEGORY || $targetNamespace == NS_CATEGORY_TALK) {
  129. global $wgCategoryLinks;
  130. return $wgCategoryLinks;
  131. }
  132. # try detect language link
  133. $titleText = $target->getUserCaseDBKey();
  134. #echo PHP_EOL . "TitleText: {$titleText} -- NameSpace: {$target->getNamespaceKey()}";
  135. $titleExploded = explode(':', $titleText, 2);
  136. if ( array_key_exists(1, $titleExploded) ) {
  137. $languageKey = $titleExploded[0];
  138. #echo PHP_EOL . "Title maybe a LanguageLink: {$languageKey}";
  139. if ( array_key_exists($languageKey, self::getLanguageNames()) ) {
  140. global $wgLanguageLinks;
  141. self::$detectedLanguageLinks []= $target->getFullText();
  142. #echo PHP_EOL . "LanguageLink detected: {$languageKey} $titleExploded[1]";
  143. return $wgLanguageLinks;
  144. } else {
  145. return true;
  146. }
  147. } else {
  148. return true;
  149. }
  150. }
  151. /**
  152. * @param Skin $skin
  153. * @param Title $target
  154. * @param array $options
  155. * @param string $text
  156. * @param array $attribs
  157. * @param mixed $ret default: null
  158. * @return bool
  159. */
  160. static public function hookLinkEnd( $skin, $target, $options, &$text, &$attribs, &$ret ) {
  161. return true;
  162. }
  163. /**
  164. *
  165. * @param Parser $parser
  166. * @param string $text
  167. * @return void
  168. */
  169. static public function hookParserBeforeTidy( &$parser, &$text ) {
  170. $parser->mOutput->setLanguageLinks(self::$detectedLanguageLinks);
  171. self::$detectedLanguageLinks = null; # clear
  172. return true;
  173. }
  174. /**
  175. *
  176. * @param Parser $parser
  177. * @param string $text
  178. * @param string $strip_state
  179. * @return bool
  180. */
  181. static public function hookParserBeforeStrip( &$parser, &$text, &$strip_state ) {
  182. self::$detectedLanguageLinks = null; # ensure clear
  183. return true;
  184. }
  185. }