/_ATTIC/peachy/Plugins/template.php

https://bitbucket.org/magnusmanske/magnustools · PHP · 326 lines · 177 code · 42 blank · 107 comment · 75 complexity · 591a7d1ad0a59dd7071b89ba8e002365 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Sam Korn <smoddy@gmail.com>
  4. * @author Soxred93 <soxred93@gmail.com>
  5. * @copyright Copyright (c) 2009, Sam Korn
  6. * @license http://opensource.org/licenses/mit-license.php MIT License
  7. */
  8. /**
  9. * Modify and use templates
  10. *
  11. * Facilitate the isolation and editing of templates.
  12. *
  13. * @todo Convert this to Peachy standards
  14. */
  15. class Template {
  16. /**
  17. * Text preceeding the template
  18. * @var string
  19. */
  20. private $before;
  21. /**
  22. * Text of the template on creation
  23. * @var string
  24. */
  25. private $templatestring;
  26. /**
  27. * Text that follows the template
  28. * @var string
  29. */
  30. private $after;
  31. /**
  32. * Double curly-bracket and any white-space before the template's name
  33. * @var string
  34. */
  35. private $open;
  36. /**
  37. * Name of template
  38. * @var string
  39. */
  40. private $name;
  41. /**
  42. * Array of fields of the template
  43. *
  44. * The keys of the array are the names of the fields as they appear to MediaWiki.
  45. *
  46. * @var array
  47. */
  48. private $fields;
  49. /**
  50. * The double curly-brakced that closes the template
  51. * @var string
  52. */
  53. private $end;
  54. /**
  55. * Extract a template from a string
  56. *
  57. * Find the template $name in the string $text and build template from this. Only
  58. * the first occurence will be found -- others must be found using {@link Template::$after}.
  59. *
  60. * @param string $text Text to find template in
  61. * @param string $name Name of the temlate to find
  62. */
  63. public function __construct($text,$name) {
  64. if (false === ($from = stripos($text,'{{' . $name))) {
  65. unset ($this->name);
  66. return;
  67. }
  68. $name = "(?i:" . substr(preg_quote($name,'/'),0,1) . ")" . substr($name,1);
  69. preg_match("/\{\{" . $name . "\s*(?:(?:\|.*)|(?:\}.*))/s",$text,$match);
  70. if (isset($match[0])) {
  71. $from = stripos($text,$match[0]);
  72. } else {
  73. unset ($this->name);
  74. return;
  75. }
  76. $i = 2;
  77. $counter = 2;
  78. while (strlen($match[0]) > $i) {
  79. if ($match[0][$i] == '{') {
  80. $counter++;
  81. }
  82. elseif ($match[0][$i] == '}') {
  83. $counter--;
  84. }
  85. if ($counter == 1) {
  86. $end = $i + 2;
  87. $this->before = substr($text,0,$from);
  88. $this->templatestring = substr($text,$from,$end);
  89. $this->after = substr($match[0],$end);
  90. break;
  91. }
  92. $i++;
  93. }
  94. preg_match('/(\{\{\s*)([^|}]*)(.*)/s',$this->templatestring,$match);
  95. $this->open = $match[1];
  96. $this->name = $match[2];
  97. if (false === strpos($this->templatestring,'|')) {
  98. $this->fields = array();
  99. $this->end = '}}';
  100. return;
  101. }
  102. $subtemplate = 0;
  103. $current = '';
  104. $link = false;
  105. $lastletter = '';
  106. for ($i = 0 ; $i < strlen($match[3]) ; $i++) {
  107. if ($current && !$subtemplate && !$link && (($match[3][$i] == '|') || ($match[3][$i] == '}'))) {
  108. $fields[] = $current;
  109. $current = '';
  110. $lastletter = $match[3][$i];
  111. continue;
  112. }
  113. if (!$current && !$subtemplate && !$link && ($lastletter == '}') && ($match[3][$i] == '}')) {
  114. break;
  115. }
  116. if (($lastletter == '{') && ($match[3][$i] == '{')) {
  117. $subtemplate++;
  118. }
  119. if (($lastletter == '}') && ($match[3][$i] == '}')) {
  120. $subtemplate--;
  121. }
  122. if ($link && ($lastletter == ']') && ($match[3][$i] == ']')) {
  123. $link = false;
  124. }
  125. if (!$link && ($lastletter == '[') && ($match[3][$i] == '[')) {
  126. $link = true;
  127. }
  128. if (!$subtemplate && !$current && !$link && ($match[3][$i] == '|') && ($lastletter == '|')) {
  129. $fields[] = '';
  130. }
  131. if ((!$subtemplate && !$link && ($match[3][$i] != '|')) || $subtemplate || $link) {
  132. $current .= $match[3][$i];
  133. }
  134. $lastletter = $match[3][$i];
  135. }
  136. $last = (count($fields)) - 1;
  137. if ($fields[$last] == '}') { //this is actually impossible to set, so it will always indicate an empty field
  138. $fields[$last] = '';
  139. }
  140. $i = 0;
  141. if ($fields) {
  142. foreach ($fields as $field) {
  143. if (preg_match('/\s*([^=\|\}]*?)\s*=/',$field,$match)) {
  144. $this->fields[$match[1]] = $field;
  145. } else {
  146. $this->fields[++$i] = $field;
  147. }
  148. }
  149. }
  150. $this->end = '}}';
  151. }
  152. public function __get($var) {
  153. return $this->$var;
  154. }
  155. public function __set($var,$val) {
  156. return;
  157. }
  158. public function __toString() {
  159. $return = $this->open;
  160. $return .= $this->name;
  161. if (count($this->fields != 0)) {
  162. foreach ($this->fields as $field) {
  163. $return .= "|$field";
  164. }
  165. }
  166. $return .= $this->end;
  167. return $return;
  168. }
  169. /**
  170. * Return the string used in {@link __construct()} with the new template.
  171. *
  172. * @return string
  173. */
  174. public function wholePage() {
  175. return $this->before . ((string) $this) . $this->after;
  176. }
  177. /**
  178. * Get the value of a field
  179. *
  180. * @param string $fieldname Name of the field to find
  181. * @return string|boolean Value of template if it exists, otherwise boolean false
  182. */
  183. public function fieldvalue($fieldname) {
  184. if( is_numeric($fieldname)) {
  185. return trim($this->fields[$fieldname]);
  186. }
  187. if (isset($this->fields[$fieldname])) {
  188. return trim(substr($this->fields[$fieldname],strpos($this->fields[$fieldname],'=') + 1));
  189. } else {
  190. return false;
  191. }
  192. }
  193. /**
  194. * Change the name of a field
  195. *
  196. * @param string $oldname Name of the field to migrate
  197. * @param string $newname New name of the field
  198. */
  199. public function renamefield($oldname,$newname) {
  200. foreach ($this->fields as $name => $field) {
  201. if ($name != $oldname) {
  202. $newfields[$name] = $field;
  203. continue;
  204. }
  205. $newfields[$newname] = preg_replace('/^(\s*)' . preg_quote($oldname,'/') . '(\s*=)/is',"$1" . $newname . "$2",$field);
  206. }
  207. $this->fields = $newfields;
  208. }
  209. /**
  210. * Delete a field
  211. *
  212. * @param string $fieldname Name of field to delete
  213. */
  214. public function removefield($fieldname) {
  215. unset($this->fields[$fieldname]);
  216. }
  217. /**
  218. * Rename template
  219. *
  220. * @param string $newname New name of template
  221. */
  222. public function rename($newname) {
  223. $this->name = $newname;
  224. }
  225. /**
  226. * Add a field to the template
  227. *
  228. * If the fieldname is not given, the parameter will be added effectively as
  229. * a numbered parameter.
  230. *
  231. * @param string $value Value of parameter
  232. * @param string fieldname Name of parameter
  233. */
  234. public function addfield($value,$fieldname = '') {
  235. if (!$fieldname) {
  236. $fieldname = (max(array_keys($this->fields)) + 1);
  237. } else {
  238. $this->fields[$fieldname] = $fieldname . ' = ';
  239. }
  240. $this->fields[$fieldname] .= $value;
  241. }
  242. /**
  243. * Does the field exist?
  244. *
  245. * If a field with the name specified by $fieldname exists, return true. Else, return false/
  246. *
  247. * @param string $fieldname Name of field to search for
  248. * @return boolean
  249. */
  250. public function fieldisset($fieldname) {
  251. if (isset($this->fields[$fieldname])) {
  252. return true;
  253. } else {
  254. return false;
  255. }
  256. }
  257. /**
  258. * Update field value
  259. *
  260. * Update the value of field $fieldname to $value.
  261. *
  262. * If the field does not exist, add it.
  263. *
  264. * @param string $fieldname Name of field to update
  265. * @param string $value Value to update to
  266. */
  267. public function updatefield($fieldname,$value) {
  268. if (!$this->fieldisset($fieldname)) {
  269. $this->addfield($value,$fieldname);
  270. return;
  271. }
  272. $oldvalue = $this->fieldvalue($fieldname);
  273. $this->fields[$fieldname] = preg_replace('/^(.*?=\s*)' . preg_quote($oldvalue,'(\s*)/') . '\s*/is',"$1$value$2",$this->fields[$fieldname]);
  274. }
  275. public static function extract ($text, $name) {
  276. $template = new Template ($text,$name);
  277. if (!isset($template->name) or !$template->name) {
  278. unset($template);
  279. return false;
  280. } else {
  281. return $template;
  282. }
  283. }
  284. }