PageRenderTime 26ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

/ontowiki/src/extensions/components/history/HistoryController.php

https://bitbucket.org/nihilus/ontowiki
PHP | 343 lines | 237 code | 60 blank | 46 comment | 38 complexity | 5ecf87eeace2213ad468b601f55e5565 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * History component controller.
  4. *
  5. * @category OntoWiki
  6. * @package OntoWiki_extensions_components_history
  7. * @author Christoph Rie?&#x; <c.riess.dev@googlemail.com>
  8. * @copyright Copyright (c) 2009, {@link http://aksw.org AKSW}
  9. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  10. * @version $Id: HistoryController.php 4090 2009-08-19 22:10:54Z christian.wuerker $
  11. */
  12. class HistoryController extends OntoWiki_Controller_Component
  13. {
  14. /**
  15. * Listing history for selected Resource
  16. */
  17. public function listAction()
  18. {
  19. $model = $this->_owApp->selectedModel;
  20. $translate = $this->_owApp->translate;
  21. $store = $this->_erfurt->getStore();
  22. $resource = $this->_owApp->selectedResource;
  23. $ac = $this->_erfurt->getAc();
  24. $params = $this->_request->getParams();
  25. $limit = 20;
  26. // redirecting to home if no model/resource is selected
  27. if (empty($model) || (empty($this->_owApp->selectedResource) && empty($params['r']) && $this->_owApp->lastRoute !== 'instances')) {
  28. $this->_abort('No model/resource selected.', OntoWiki_Message::ERROR);
  29. }
  30. // getting page (from and for paging)
  31. if (!empty($params['p']) && (int) $params['p'] > 0) {
  32. $page = (int) $params['p'];
  33. } else {
  34. $page = 1;
  35. }
  36. // enabling versioning
  37. $versioning = $this->_erfurt->getVersioning();
  38. $versioning->setLimit($limit);
  39. if (!$versioning->isVersioningEnabled()) {
  40. $this->_abort('Versioning/History is currently disabled', null, false);
  41. }
  42. $singleResource = true;
  43. // setting if class or instances
  44. if ($this->_owApp->lastRoute === 'instances') {
  45. // setting default title
  46. $title = $resource->getTitle() ? $resource->getTitle() : OntoWiki_Utils::contractNamespace($resource->getIri());
  47. $windowTitle = $translate->_('Versions for elements of the list');
  48. $instances = $this->_session->instances;
  49. if(!($instances instanceof OntoWiki_Model_Instances)){
  50. throw new OntoWiki_Exception("Something went wrong with list creation. Probably your session timed out. <a href='".$this->_config->urlBase."'>Start again</a>");
  51. exit;
  52. }
  53. $query = $instances->getResourceQuery();
  54. $query->setLimit(0);
  55. $query->setOffset(0);
  56. $results = $model->sparqlQuery($query);
  57. $resourceVar = $instances->getResourceVar()->getName();
  58. $resources = array();
  59. foreach ($results as $result) {
  60. $resources[] = $result[$resourceVar];
  61. }
  62. $historyArray = $versioning->getHistoryForResourceList(
  63. $resources,
  64. (string) $this->_owApp->selectedModel,
  65. $page
  66. );
  67. $singleResource = false;
  68. } else {
  69. // setting default title
  70. $title = $resource->getTitle() ? $resource->getTitle() : OntoWiki_Utils::contractNamespace($resource->getIri());
  71. $windowTitle = sprintf($translate->_('Versions for %1$s'), $title);
  72. $historyArray = $versioning->getHistoryForResource(
  73. (string)$resource,
  74. (string)$this->_owApp->selectedModel,
  75. $page
  76. );
  77. }
  78. if (sizeof($historyArray) == ( $limit + 1 ) ) {
  79. $count = $page * $limit + 1;
  80. unset($historyArray[$limit]);
  81. } else {
  82. $count = ($page - 1) * $limit + sizeof($historyArray);
  83. }
  84. $idArray = array();
  85. $userArray = $this->_erfurt->getUsers();
  86. $titleHelper = new OntoWiki_Model_TitleHelper();
  87. // Load IDs for rollback and Username Labels for view
  88. foreach ($historyArray as $key => $entry) {
  89. $idArray[] = (int) $entry['id'];
  90. if(!$singleResource){
  91. $historyArray[$key]['url'] = $this->_config->urlBase . "view?r=" . urlencode($entry['resource']);
  92. $titleHelper->addResource($entry['resource']);
  93. }
  94. if ($entry['useruri'] == $this->_erfurt->getConfig()->ac->user->anonymousUser) {
  95. $userArray[$entry['useruri']] = 'Anonymous';
  96. } elseif ($entry['useruri'] == $this->_erfurt->getConfig()->ac->user->superAdmin) {
  97. $userArray[$entry['useruri']] = 'SuperAdmin';
  98. } elseif (
  99. is_array($userArray[$entry['useruri']]) &&
  100. array_key_exists('userName',$userArray[$entry['useruri']])
  101. ) {
  102. $userArray[$entry['useruri']] = $userArray[$entry['useruri']]['userName'];
  103. }
  104. }
  105. $this->view->userArray = $userArray;
  106. $this->view->idArray = $idArray;
  107. $this->view->historyArray = $historyArray;
  108. $this->view->singleResource = $singleResource;
  109. $this->view->titleHelper = $titleHelper;
  110. if (empty($historyArray)) {
  111. $this->_owApp->appendMessage(
  112. new OntoWiki_Message(
  113. 'No matches.' ,
  114. OntoWiki_Message::INFO
  115. )
  116. );
  117. }
  118. if ($this->_erfurt->getAc()->isActionAllowed('Rollback')) {
  119. $this->view->rollbackAllowed = true;
  120. // adding submit button for rollback-action
  121. $toolbar = $this->_owApp->toolbar;
  122. $toolbar->appendButton(
  123. OntoWiki_Toolbar::SUBMIT,
  124. array('name' => $translate->_('Rollback changes'), 'id' => 'history-rollback')
  125. );
  126. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  127. } else {
  128. $this->view->rollbackAllowed = false;
  129. }
  130. // paging
  131. $statusBar = $this->view->placeholder('main.window.statusbar');
  132. $statusBar->append(OntoWiki_Pager::get($count,$limit));
  133. // setting view variables
  134. $url = new OntoWiki_Url(array('controller' => 'history', 'action' => 'rollback'));
  135. $this->view->placeholder('main.window.title')->set($windowTitle);
  136. $this->view->formActionUrl = (string) $url;
  137. $this->view->formMethod = 'post';
  138. // $this->view->formName = 'instancelist';
  139. $this->view->formName = 'history-rollback';
  140. $this->view->formEncoding = 'multipart/form-data';
  141. }
  142. /**
  143. * Restoring actions that are specified within the POST parameter
  144. */
  145. public function rollbackAction()
  146. {
  147. $resource = $this->_owApp->selectedResource;
  148. $graphuri = (string) $this->_owApp->selectedModel;
  149. $translate = $this->_owApp->translate;
  150. $params = $this->_request->getParams();
  151. // abort on missing parameters
  152. if (!array_key_exists('actionid',$params) || empty($resource) || empty($graphuri)) {
  153. $this->_abort('missing parameters.', OntoWiki_Message::ERROR);
  154. }
  155. // set active tab to history
  156. Ontowiki_Navigation::setActive('history');
  157. // setting default title
  158. $title = $resource->getTitle() ? $resource->getTitle() : OntoWiki_Utils::contractNamespace($resource->getIri());
  159. $windowTitle = sprintf($translate->_('Versions for %1$s'), $title);
  160. $this->view->placeholder('main.window.title')->set($windowTitle);
  161. // setting more view variables
  162. $url = new OntoWiki_Url(array('controller' => 'view', 'action' => 'index' ), null);
  163. $this->view->backUrl = (string) $url;
  164. // set translate on view
  165. $this->view->translate = $this->_owApp->translate;
  166. // abort on insufficient rights
  167. if (!$this->_erfurt->getAc()->isActionAllowed('Rollback')) {
  168. $this->_abort('not allowed.', OntoWiki_Message::ERROR);
  169. }
  170. // enabling versioning
  171. $versioning = $this->_erfurt->getVersioning();
  172. if (!$versioning->isVersioningEnabled()) {
  173. $this->_abort('versioning / history is currently disabled.', null, false);
  174. }
  175. $successIDs = array();
  176. $errorIDs = array();
  177. $actionids = array();
  178. // starting rollback action
  179. $actionSpec = array(
  180. 'modeluri' => $graphuri ,
  181. 'type' => Erfurt_Versioning::STATEMENTS_ROLLBACK,
  182. 'resourceuri' => (string) $resource
  183. );
  184. $versioning->startAction($actionSpec);
  185. // Trying to rollback actions from POST parameters (style: serialized in actionid)
  186. foreach (unserialize($params['actionid']) as $id) {
  187. if ( $versioning->rollbackAction($id) ) {
  188. $successIDs[] = $id;
  189. } else {
  190. $errorIDs[] = $id;
  191. }
  192. }
  193. // ending rollback action
  194. $versioning->endAction();
  195. // adding messages for errors and success
  196. if (!empty($successIDs)) {
  197. $this->_owApp->appendMessage(
  198. new OntoWiki_Message(
  199. 'Rolled back action(s): ' . implode(', ',$successIDs) ,
  200. OntoWiki_Message::SUCCESS
  201. )
  202. );
  203. }
  204. if (!empty($errorIDs)) {
  205. $this->_owApp->appendMessage(
  206. new OntoWiki_Message(
  207. 'Error on rollback of action(s): ' . implode(', ',$errorIDs) ,
  208. OntoWiki_Message::ERROR
  209. )
  210. );
  211. }
  212. }
  213. /**
  214. * Service to generate small HTML for Action-Details-AJAX-Integration inside Ontowiki
  215. */
  216. public function detailsAction()
  217. {
  218. $params = $this->_request->getParams();
  219. if (empty($params['id'])) {
  220. $this->_abort('missing parameters.');
  221. } else {
  222. $actionID = (int) $params['id'];
  223. }
  224. // disabling layout as it is used as a service
  225. $this->_helper->layout()->disableLayout();
  226. $this->view->isEmpty = true;
  227. // enabling versioning
  228. $versioning = $this->_erfurt->getVersioning();
  229. $detailsArray = $versioning->getDetailsForAction($actionID);
  230. $stAddArray = array();
  231. $stDelArray = array();
  232. $stOtherArray = array();
  233. function toFlatArray($serializedString) {
  234. $walkArray = unserialize($serializedString);
  235. foreach ($walkArray as $subject => $a) {
  236. foreach ($a as $predicate => $b) {
  237. foreach ($b as $object) {
  238. return array($subject, $predicate, $object['value']);
  239. }
  240. }
  241. }
  242. }
  243. foreach ($detailsArray as $entry) {
  244. $this->view->isEmpty = false;
  245. $type = (int) $entry['action_type'];
  246. if ( $type === Erfurt_Versioning::STATEMENT_ADDED ) {
  247. $stAddArray[] = toFlatArray($entry['statement_hash']);
  248. } elseif ( $type === Erfurt_Versioning::STATEMENT_REMOVED ) {
  249. $stDelArray[] = toFlatArray($entry['statement_hash']);
  250. } else {
  251. $stOtherArray[] = toFlatArray($entry['statement_hash']);
  252. }
  253. }
  254. $this->view->translate = $this->_owApp->translate;
  255. $this->view->actionID = $actionID;
  256. $this->view->stAddArray = $stAddArray;
  257. $this->view->stDelArray = $stDelArray;
  258. $this->view->stOtherArray = $stOtherArray;
  259. }
  260. /**
  261. * Shortcut for adding messages
  262. */
  263. private function _abort($msg, $type = null, $redirect = null)
  264. {
  265. if (empty($type)) {
  266. $type = OntoWiki_Message::INFO;
  267. }
  268. $this->_owApp->appendMessage(
  269. new OntoWiki_Message(
  270. $msg ,
  271. $type
  272. )
  273. );
  274. if (empty($redirect)) {
  275. if ($redirect !== false) {
  276. $this->_redirect($this->_config->urlBase);
  277. }
  278. } else {
  279. $this->redirect((string)$redirect);
  280. }
  281. return true;
  282. }
  283. }