PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/wiki/pluginslib.php

https://gitlab.com/ElvisAns/tiki
PHP | 380 lines | 250 code | 23 blank | 107 comment | 59 complexity | ff7756f23b582ebbbb2c506a2d9fb53a MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /**
  8. * Plugin Lib
  9. *
  10. * A port of PhpWiki WikiPlugin class
  11. * Principal use is port PhpWiki plugins, but can be used to make new ones.
  12. * Use:
  13. * - Extends PluginsLib with your class
  14. * - add the lines
  15. * <code>
  16. * include "pluginslib.php";
  17. *
  18. * function wikiplugin_backlinks($data, $params) {
  19. * $plugin = new BackLinks();
  20. * return $plugin->run($data, $params);
  21. * }
  22. * function wikiplugin_backlinks_help() {
  23. * $plugin = new BackLinks();
  24. * return $plugin->getDescription();
  25. * } * </code>
  26. * @package Tiki
  27. * @subpackage Plugins
  28. * @author Claudio Bustos
  29. * @version $Revision: 1.12 $
  30. */
  31. //this script may only be included - so its better to die if called directly.
  32. if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
  33. header('location: index.php');
  34. exit;
  35. }
  36. class PluginsLib extends TikiLib
  37. {
  38. public $_errors;
  39. public $_data;
  40. public $_params;
  41. /**
  42. * Array of params to be expanded as arrays. Explode the string with {@link $separator}
  43. * @var array
  44. */
  45. public $expanded_params = [];
  46. /**
  47. * Separator used to explote params listed on {@link $expanded_params}
  48. * @var string
  49. */
  50. public $separator = "|";
  51. /**
  52. * List of fields retrieved from {@link TikiLib::list_pages()}
  53. * Keys are the name of the fields and values the names for tra();
  54. * @var array
  55. */
  56. public $aInfoPresetNames = [
  57. 'hits' => 'Hits', 'lastModif' => 'Last mod', 'user' => 'Last author', 'len' => 'Size', 'comment' => 'Com',
  58. 'creator' => 'Creator', 'version' => 'Last ver', 'flag' => 'Status', 'versions' => 'Vers', 'links' => 'Links',
  59. 'backlinks' => 'Backlinks'];
  60. /**
  61. * Process the params, in this order:
  62. * - default values, asigned on {@link PluginsLib::getDefaultArguments()}
  63. * - request values, sended by GET or POST method, if $request is put to true
  64. * - explicit values, asigned on the Wiki
  65. * @param array sended to wikiplugin_name($data, $params)
  66. * @param bool if set to true, accept values from $_REQUEST
  67. * @param bool if set to true, assign default values from {@link PluginsLib::getDefaultArguments()}
  68. * @return array list of params
  69. */
  70. public function getParams($params, $request = false, $defaults = false)
  71. {
  72. if ($defaults === false) {
  73. $defaults = $this->getDefaultArguments();
  74. }
  75. $args = [];
  76. foreach ($defaults as $arg => $default_val) {
  77. if (isset($params[$arg])) {
  78. $args[$arg] = $params[$arg];
  79. } elseif (isset($_REQUEST[$arg])) {
  80. $args[$arg] = $_REQUEST[$arg];
  81. } elseif (isset($_REQUEST['page'])) {
  82. // maybe this kind of transformation can be grouped on a external function
  83. if ($default_val === '[pagename]') {
  84. $default_val = $_REQUEST['page'];
  85. }
  86. $args[$arg] = $default_val;
  87. }
  88. if (in_array($arg, $this->expanded_params) && ! (isset($args[$arg]) && is_array($args[$arg]))) {
  89. if (isset($args[$arg]) && $args[$arg]) {
  90. $args[$arg] = explode($this->separator, $args[$arg]);
  91. foreach ($args[$arg] as $id => $value) {
  92. $args[$arg][$id] = trim($value);
  93. }
  94. } else {
  95. $args[$arg] = [];
  96. }
  97. }
  98. }
  99. return $args;
  100. }
  101. /**
  102. * Returns the name of the Plugin
  103. * By default, erase the first 'WikiPlugin'
  104. * Made for overload it.
  105. * @return string
  106. */
  107. public function getName()
  108. {
  109. return preg_replace('/^WikiPlugin/', '', get_class($this));
  110. }
  111. /**
  112. * Returns a description of the Plugin
  113. * Made for overload it.
  114. * @return string
  115. */
  116. public function getDescription()
  117. {
  118. return $this->getName();
  119. }
  120. /**
  121. * Returns the version of the version
  122. * Made for overload it.
  123. * @return string
  124. */
  125. public function getVersion()
  126. {
  127. return tra('No version indicated');
  128. //return preg_replace("/[Revision: $]/", '',
  129. // "\$Revision: 1.12 $");
  130. }
  131. /**
  132. * Returns the default arguments for the plugin
  133. * Use keys as the arguments and values as ... the default values
  134. * @return array
  135. */
  136. public function getDefaultArguments()
  137. {
  138. return ['description' => $this->getDescription()];
  139. }
  140. /**
  141. * Run the plugin
  142. * For sake of God, overload it!
  143. * @param string
  144. * @param array
  145. */
  146. public function run($data, $params)
  147. {
  148. /**
  149. * UGLY ERROR!.
  150. */
  151. return $this->error('PluginsLib::run: pure virtual function. Don\'t be so lazy!');
  152. }
  153. public function error($message)
  154. {
  155. return '~np~<span class="warn">' . tra('Plugin ') . $this->getName() . ' ' . tra('failed')
  156. . ' : ' . tra($message) . '</span>~/np~';
  157. }
  158. public function getErrorDetail()
  159. {
  160. return $this->_errors;
  161. }
  162. public function _error($message)
  163. {
  164. $this->_errors = $message;
  165. return false;
  166. }
  167. }
  168. /**
  169. * Class with utilities for Plugins
  170. */
  171. class PluginsLibUtil
  172. {
  173. /**
  174. * Create a table with information from pages
  175. * @param array key ["data"] from one of the functions that retrieve informaci�n about pages
  176. * @param array list of keys to show.
  177. * @param array definition of the principal field. By default:
  178. * array("field"=>"pageName","name"=>"Page")
  179. * @return string
  180. */
  181. public static function createTable($aData, $aInfo = false, $aPrincipalField = false)
  182. {
  183. // contract
  184. if (! $aPrincipalField or ! is_array($aPrincipalField)) {
  185. $aPrincipalField = ['field' => 'pageName', 'name' => 'Page'];
  186. }
  187. if (! is_array($aInfo)) {
  188. $aInfo = false;
  189. }
  190. // ~contract
  191. $sOutput = '';
  192. if ($aInfo) {
  193. $iNumCol = count($aInfo) + 1;
  194. $sStyle = '';
  195. if (in_array('parameters', $aInfo)) {
  196. $sOutput .= '<em>' . tra('Required parameters are in</em> <b>bold</b>') . '<br />';
  197. }
  198. // Header for info
  199. $sOutput .= '<table class="table table-striped table-hover">' . "\n\t" . '<tr>' . "\n\t\t"
  200. . '<td class="heading"' . $sStyle . '>' . tra($aPrincipalField['name']) . '</td>';
  201. foreach ($aInfo as $iInfo => $sHeader) {
  202. if ($sHeader == 'paraminfo') {
  203. $sHeader = tra('Parameter Info');
  204. } else if ($sHeader == 'sourcecode') {
  205. $sHeader = tra('Source Code');
  206. }
  207. $sOutput .= "\n\t\t" . '<th class="heading"' . $sStyle . '>' . ucfirst(tra($sHeader)) . '</th>';
  208. }
  209. $sOutput .= "\n\t" . '</tr>';
  210. }
  211. $iCounter = 1;
  212. //Primary row
  213. foreach ($aData as $aPage) {
  214. $rowspan = '';
  215. if ($aPrincipalField['field'] == 'plugin') {
  216. $openlink = '';
  217. $closelink = '';
  218. } else {
  219. $openlink = '((';
  220. $closelink = '))';
  221. }
  222. if (! $aInfo) {
  223. $sOutput .= '*' . $openlink . $aPage[$aPrincipalField['field']] . $closelink . "\n";
  224. //First column
  225. } elseif (isset($aPage[$aPrincipalField['field']])) {
  226. if (is_array($aPage[$aPrincipalField['field']])) {
  227. $fieldval = $aPage[$aPrincipalField['field']][$aPrincipalField['field']];
  228. if (isset($aPage[$aPrincipalField['field']]['rowspan']) && $aPage[$aPrincipalField['field']]['rowspan'] != 0) {
  229. $rowspan = ' rowspan="' . $aPage[$aPrincipalField['field']]['rowspan'] . '" ';
  230. } else {
  231. $rowspan = '';
  232. }
  233. } else {
  234. $fieldval = $aPage[$aPrincipalField['field']];
  235. $rowspan = '';
  236. }
  237. $sClass = ($iCounter % 2) ? 'odd' : 'even';
  238. $sOutput .= "\n\t" . '<tr>' . "\n\t\t" . '<td class="' . $sClass . '"' . $rowspan . '>'
  239. . $openlink . $fieldval . $closelink . '</td>';
  240. $colcounter = 2;
  241. //Subsequent columns
  242. foreach ($aInfo as $sInfo) {
  243. if (isset($aPage[$sInfo])) {
  244. if (is_array($aPage[$sInfo])) {
  245. $rowspan2 = '';
  246. if (isset($aPage[$sInfo]['rowspan']) && $aPage[$sInfo]['rowspan'] > 0) {
  247. $rowspan2 = ' rowspan="' . $aPage[$sInfo]['rowspan'] . '" ';
  248. $pcount = count($aPage[$sInfo]) - 1;
  249. } else {
  250. $pcount = count($aPage[$sInfo]);
  251. }
  252. $i = $pcount;
  253. foreach ($aPage[$sInfo] as $sInfokey => $sInfoitem) {
  254. //Potential sub-rows
  255. if ($i < $pcount && strpos($sInfokey, 'rowspan') === false) {
  256. $begrow = "\n\t" . '<tr>';
  257. $endrow = "\n\t" . '</tr>';
  258. } else {
  259. $begrow = '';
  260. if ($colcounter == $iNumCol && strpos($sInfokey, 'rowspan') === false) {
  261. $endrow = "\n\t" . '</tr>';
  262. } else {
  263. $endrow = '';
  264. }
  265. }
  266. //Ignore field added to hold rowspan
  267. if (strpos($sInfokey, 'rowspan') !== false) {
  268. $sOutput .= '';
  269. } else {
  270. $sOutput .= $begrow . "\n\t\t" . '<td class="' . $sClass . '"' . $rowspan2 . '>';
  271. if (strpos($sInfokey, 'onekey') !== false) {
  272. $sOutput .= $sInfoitem;
  273. } else {
  274. $sOutput .= $sInfokey;
  275. }
  276. $sOutput .= '</td>';
  277. if (in_array('paraminfo', $aInfo) && $sInfo == 'parameters') {
  278. $sOutput .= "\n\t\t" . '<td class="' . $sClass . '">';
  279. if (count($aPage['parameters']) > 0) {
  280. $sOutput .= $sInfoitem;
  281. }
  282. $sOutput .= '</td>';
  283. }
  284. }
  285. $sOutput .= $endrow;
  286. $i--;
  287. }
  288. $colcounter++;
  289. } else {
  290. $sOutput .= "\n\t\t" . '<td class="' . $sClass . '">' . $aPage[$sInfo] . '</td>';
  291. if ($colcounter == $iNumCol) {
  292. $sOutput .= "\n\t" . '</tr>';
  293. }
  294. $colcounter++;
  295. }
  296. }
  297. }
  298. }
  299. $iCounter++;
  300. }
  301. if ($aInfo) {
  302. $sOutput .= '</table>';
  303. }
  304. return $sOutput;
  305. }
  306. public static function createList($aData)
  307. {
  308. $aPrincipalField = ['field' => 'pageName', 'name' => 'Pages'];
  309. // Header for info
  310. $sOutput = '<table class="table table-striped table-hover"><tr><th class="heading">'
  311. . tra($aPrincipalField['name']) . '</th></tr><tr><td>';
  312. $iCounter = 0;
  313. // create a comma-separated list of entries
  314. foreach ($aData as $aPage) {
  315. if ($iCounter > 0) {
  316. $sOutput .= ', ';
  317. }
  318. $sOutput .= '((' . $aPage[$aPrincipalField['field']] . '))';
  319. $iCounter++;
  320. }
  321. $sOutput .= '</td></tr></table>';
  322. return $sOutput;
  323. }
  324. public static function handleDownload($query, $index, $matches, $request = null)
  325. {
  326. if (empty($request)) {
  327. $request = $_REQUEST;
  328. }
  329. if (empty($request['download'])) {
  330. return;
  331. }
  332. $builder = new Search_Formatter_Builder();
  333. $builder->setDownload(true);
  334. $builder->apply($matches);
  335. $filename = $builder->getDownloadName();
  336. if (! $filename) {
  337. $filename = 'report.csv';
  338. }
  339. $formatter = $builder->getFormatter();
  340. $offset = 0;
  341. $output = '';
  342. do {
  343. $query->setRange($offset, 100);
  344. $result = $query->search($index);
  345. $chunk = $formatter->format($result);
  346. if ($offset > 0) {
  347. $chunk = substr($chunk, strpos($chunk, "\n") + 1);
  348. }
  349. $output .= $chunk;
  350. $offset += 100;
  351. } while ($offset < $result->count());
  352. header('Content-Type: text/csv; charset=utf8');
  353. header("Content-Disposition: attachment; filename=$filename");
  354. echo $output;
  355. exit();
  356. }
  357. }