PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/WikiZam/extensions/CarZam/CarZam.hooks.php

https://github.com/Seizam/seizamcore
PHP | 246 lines | 123 code | 41 blank | 82 comment | 30 complexity | 2e53ae78b8a492c343c1ff9b22bde579 MD5 | raw file
  1. <?php
  2. /**
  3. * Hooks for CarZam extension
  4. *
  5. * @file
  6. * @ingroup Extensions
  7. */
  8. if (!defined('MEDIAWIKI')) {
  9. die(-1);
  10. }
  11. class CarZamHooks {
  12. /**
  13. * BeforePageDisplay hook
  14. *
  15. * Adds the modules to the page
  16. *
  17. * @param OutputPage $out output page
  18. * @param Skin $skin current skin
  19. */
  20. public static function beforePageDisplay($out, $skin) {
  21. $out->addModules(array('ext.carzam.carrousel'));
  22. return true;
  23. }
  24. /**
  25. *
  26. * @param Parser $parser
  27. * @return boolean
  28. */
  29. public static function onParserFirstCallInit(&$parser) {
  30. $parser->setHook('carrousel', array('CarZamHooks', 'renderCarrouselTag'));
  31. $parser->setHook('slideshow', array('CarZamHooks', 'renderSlideshowTag'));
  32. return true;
  33. }
  34. /**
  35. * Takes a line and parse it into title, titleLink, alt & caption parameters
  36. * @todo Make arguments to paremeters parsing less ugly & more flexible, with widgetsFramework for example.
  37. *
  38. * @param string $line
  39. * @param Parser $parser
  40. * @return array title, titleLink, alt, caption
  41. */
  42. private static function parseLine($line, $parser, $frame) {
  43. $explosion = explode('|', $line);
  44. $parameters = array();
  45. $parameters['title'] = null;
  46. $parameters['titleLink'] = null; #Title to link to
  47. $parameters['alt'] = ''; #html alternative text
  48. $parameters['caption'] = ''; #html caption
  49. #arguments are parsed by position... :,-(
  50. #first arg is file title
  51. $titleText = array_shift($explosion);
  52. if (is_null($titleText)) {
  53. # Empty line or something went wrong
  54. return false;
  55. }
  56. if (strpos($titleText, '%') !== false) {
  57. #fix possible url encoding of title
  58. $titleText = rawurldecode($titleText);
  59. }
  60. $title = Title::newFromText($titleText, NS_FILE);
  61. if (is_null($title)) {
  62. # Bogus title. Ignore these so we don't bomb out later.
  63. return false;
  64. } else {
  65. $parameters['title'] = $title;
  66. }
  67. #second arg could be link
  68. $titleLinkText = count($explosion) > 0 ? self::identifyByName('link', $explosion[0]) : false;
  69. if ($titleLinkText === false) {
  70. #title link is not set
  71. } else {
  72. if ($titleLinkText === true || $titleLinkText == '') {
  73. #title link is set to empty (no linking)
  74. } else {
  75. #title link is set
  76. $titleLink = Title::newFromText($titleLinkText);
  77. if (!is_null($titleLink) && $titleLink->isKnown()) {
  78. #isKnown, register to the output for the link table
  79. $parser->getOutput()->addLink($titleLink);
  80. $parameters['titleLink'] = $titleLink;
  81. }
  82. }
  83. array_shift($explosion);
  84. }
  85. #third arg could be alt
  86. $altText = count($explosion) > 0 ? self::identifyByname('alt', $explosion[0]) : false;
  87. if ($altText === false) {
  88. #alt is not set
  89. } else {
  90. $parameters['alt'] = htmlspecialchars($altText);
  91. array_shift($explosion);
  92. }
  93. #the rest is the caption, we can reconstruct the WikiText broken by explode('|').
  94. $captionText = implode('|', $explosion);
  95. $parameters['caption'] = $parser->recursiveTagParse($captionText, $frame);
  96. return $parameters;
  97. }
  98. /**
  99. * Analyses the argument, and look for this parameter name, case
  100. * insensitive.
  101. *
  102. * @todo copied from WidgetsFramework\Parameter, move all this logic to Wfmk
  103. *
  104. * @param string $name The parameter name.
  105. * @param string $argument The raw argument.
  106. * @return string|boolean <ul>
  107. * <li>If its name is found followed by equal sign, returns the string
  108. * that follow the equal sign (the value).</li>
  109. * <li>If the name is found, without anything else, returns boolean
  110. * <i>true</i>.</li>
  111. * <li>Else, returns boolean <i>false</i>.</li>
  112. * </ul>
  113. */
  114. private static function identifyByName($name, $argument) {
  115. $name_length = strlen($name);
  116. if (strlen($argument) < $name_length) {
  117. return false; // too short, name cannot be found
  118. }
  119. // the comparison is case insensitive
  120. if (0 != substr_compare(
  121. $argument, $name, 0, $name_length, true)) {
  122. return false; // name not found
  123. }
  124. // else: name has been found
  125. // remove the name, and any space just after
  126. $argument_without_name = ltrim(substr($argument, $name_length));
  127. if (strlen($argument_without_name) == 0) {
  128. return true; // no value, only the name
  129. }
  130. // the next char must be '='
  131. if ($argument_without_name[0] != '=') {
  132. // this is not the name of this parameter
  133. return false;
  134. }
  135. // get the value by removing '=' and any spaces just after
  136. $value = ltrim(substr($argument_without_name, 1));
  137. return $value;
  138. }
  139. /**
  140. * @todo better argument parsing within the tag.
  141. *
  142. * @param $in
  143. * @param array $param
  144. * @param Parser $parser
  145. * @param bool $frame
  146. * @return string
  147. */
  148. public static function renderCarrouselTag($in, $param=array(), $parser=null, $frame=false) {
  149. $c = new CarZamCarrousel();
  150. $c->setParser($parser);
  151. $c->setContextTitle($parser->getTitle());
  152. $c->setHideBadImages();
  153. if (isset($param['height'])) {
  154. $explosion = explode('px', strtolower($param['height']));
  155. $c->setPhotoHeight($explosion[0]);
  156. }
  157. # Reading inside the tag, right now takes arguments by order
  158. /** @todo make less ugly */
  159. $lines = StringUtils::explode("\n", $in);
  160. foreach ($lines as $line) {
  161. $parameters = self::parseLine($line, $parser, $frame);
  162. if ($parameters === false)
  163. continue;
  164. else {
  165. $c->add($parameters['title'], $parameters['caption'], $parameters['alt'], $parameters['titleLink']);
  166. }
  167. }
  168. return $c->toHTML();
  169. }
  170. /**
  171. * @param $in
  172. * @param array $param
  173. * @param Parser $parser
  174. * @param bool $frame
  175. * @return string
  176. */
  177. public static function renderSlideshowTag($in, $param=array(), $parser=null, $frame=false) {
  178. $s = new CarZamSlideshow();
  179. $s->setParser($parser);
  180. $s->setContextTitle($parser->getTitle());
  181. $s->setHideBadImages();
  182. if (isset($param['height'])) {
  183. $explosion = explode('px', strtolower($param['height']));
  184. $s->setPhotoHeight($explosion[0]);
  185. }
  186. if (isset($param['width'])) {
  187. $explosion = explode('px', strtolower($param['width']));
  188. $s->setPhotoWidth($explosion[0]);
  189. }
  190. if (isset($param['float']))
  191. $s->setFloat($param['float']);
  192. # Reading inside the tag, right now takes arguments by order
  193. /** @todo make less ugly */
  194. $lines = StringUtils::explode("\n", $in);
  195. foreach ($lines as $line) {
  196. $parameters = self::parseLine($line, $parser, $frame);
  197. if ($parameters === false)
  198. continue;
  199. else {
  200. $s->add($parameters['title'], $parameters['caption'], $parameters['alt'], $parameters['titleLink']);
  201. }
  202. }
  203. return $s->toHTML();
  204. }
  205. }