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

/lib/importer/tikiimporter_blog.php

https://gitlab.com/ElvisAns/tiki
PHP | 475 lines | 261 code | 68 blank | 146 comment | 31 complexity | 07ce137a8b0aa52148ba50aa2bc10c73 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. * Class to provide basic functionalities to blog importers. So far
  9. * used only for the Wordpress importer. For more information
  10. * see http://dev.tiki.org/Wordpress+importer and http://doc.tiki.org/Wordpress+importer
  11. *
  12. * @author Rodrigo Sampaio Primo <rodrigo@utopia.org.br>
  13. * @package tikiimporter
  14. */
  15. require_once('tikiimporter.php');
  16. /**
  17. * Class to provide basic functionalities to blog importers. So far
  18. * used only for the Wordpress importer. For more information
  19. * see http://dev.tiki.org/Wordpress+importer and http://doc.tiki.org/Wordpress+importer
  20. *
  21. * This class has the methods to insert data into Tiki blog. Probably they can
  22. * be reused by all the importers. Child classes must only implement the functions
  23. * to extract and prepare the data (validateInput(), parseData())
  24. *
  25. * @package tikiimporter
  26. */
  27. class TikiImporter_Blog extends TikiImporter
  28. {
  29. /**
  30. * Blog information extracted from the XML file (title, description, created etc)
  31. * @var array
  32. */
  33. public $blogInfo = [];
  34. /**
  35. * Instance of TikiImporter_Wiki
  36. * @var TikiImporter_Wiki
  37. */
  38. public $importerWiki = '';
  39. /**
  40. * The id of the blog created by the importer
  41. * @var int
  42. */
  43. public $blogId = '';
  44. /**
  45. * The data extracted and parsed from the Wordpress
  46. * XML file.
  47. * @var array
  48. */
  49. public $parsedData = [];
  50. /**
  51. * @see lib/importer/TikiImporter#importOptions()
  52. */
  53. public static function importOptions()
  54. {
  55. $options = [
  56. ['name' => 'setAsHomePage', 'type' => 'checkbox', 'label' => tra('Set new blog as Tiki homepage')],
  57. ];
  58. return $options;
  59. }
  60. /**
  61. * Main function that starts the importing proccess
  62. *
  63. * Set the import options based on the options the user selected
  64. * and start the importing proccess by calling the functions to
  65. * validate, parse and insert the data.
  66. *
  67. * @return null
  68. */
  69. public function import($filePath = null)
  70. {
  71. $this->setupTiki();
  72. // child classes must implement this method
  73. // and it should set $this->parsedData
  74. $this->parseData();
  75. $importFeedback = $this->insertData();
  76. $this->saveAndDisplayLog("\n" . tra('Importation completed!'));
  77. echo "\n\n<b>" . tra('<a href="tiki-importer.php">Click here</a> to finish the import process') . '</b>';
  78. flush();
  79. $_SESSION['tiki_importer_feedback'] = $importFeedback;
  80. $_SESSION['tiki_importer_log'] = $this->log;
  81. $_SESSION['tiki_importer_errors'] = $this->errors;
  82. }
  83. /**
  84. * This function change all the necessary Tiki preferences
  85. * in order to setup the site for the data that will be imported.
  86. * Also implemented by child classes.
  87. *
  88. * @return void
  89. */
  90. public function setupTiki()
  91. {
  92. $tikilib = TikiLib::lib('tiki');
  93. $tikilib->set_preference('feature_blogs', 'y');
  94. }
  95. /**
  96. * Insert the imported data into Tiki.
  97. *
  98. * @param array $parsedData the return of $this->parseData() (all the data that will be imported)
  99. *
  100. * @return array $countData stats about the content that has been imported
  101. */
  102. public function insertData($parsedData = null)
  103. {
  104. $countData = [];
  105. $countPosts = count($this->parsedData['posts']);
  106. $countPages = count($this->parsedData['pages']);
  107. $countTags = count($this->parsedData['tags']);
  108. $countCategories = count($this->parsedData['categories']);
  109. $this->saveAndDisplayLog(
  110. "\n" . tr(
  111. 'Found %0 posts, %1 pages, %2 tags and %3 categories. Inserting them into Tiki:',
  112. $countPosts,
  113. $countPages,
  114. $countTags,
  115. $countCategories
  116. ) . "\n"
  117. );
  118. if (! empty($this->parsedData['posts'])) {
  119. $this->createBlog();
  120. }
  121. if (! empty($this->parsedData)) {
  122. if (! empty($this->parsedData['tags'])) {
  123. $this->createTags($this->parsedData['tags']);
  124. }
  125. if (! empty($this->parsedData['categories'])) {
  126. $this->createCategories($this->parsedData['categories']);
  127. }
  128. $items = array_merge($this->parsedData['posts'], $this->parsedData['pages']);
  129. if (! empty($items)) {
  130. foreach ($items as $key => $item) {
  131. if ($objId = $this->insertItem($item)) {
  132. // discover the item key in the $this->parsedData array
  133. $itemKey = array_search($item, $this->parsedData[$item['type'] . 's']);
  134. $this->parsedData[$item['type'] . 's'][$itemKey]['objId'] = $objId;
  135. } else {
  136. //TODO: improve feedback reporting the difference between the number of items found and items imported
  137. if ($item['type'] == 'page') {
  138. $countPages--;
  139. } else {
  140. $countPosts--;
  141. }
  142. $this->saveAndDisplayLog(tr('Item "%0" NOT imported (there was already a item with the same name)', $item['name']) . "\n");
  143. }
  144. }
  145. }
  146. }
  147. $countData['importedPages'] = $countPages;
  148. $countData['importedPosts'] = $countPosts;
  149. $countData['importedTags'] = $countTags;
  150. $countData['importedCategories'] = $countCategories;
  151. return $countData;
  152. }
  153. /**
  154. * Insert a page or post and its comments and link it with
  155. * categories and tags.
  156. *
  157. * @param array $item a page or post
  158. * @return int|string page name or post id
  159. */
  160. public function insertItem($item)
  161. {
  162. $methodName = 'insert' . ucfirst($item['type']);
  163. if ($objId = $this->$methodName($item)) {
  164. if ($item['type'] == 'page') {
  165. $type = 'wiki page';
  166. $msg = tr('Page "%0" sucessfully imported', $item['name']);
  167. } elseif ($item['type'] == 'post') {
  168. $type = 'blog post';
  169. $msg = tr('Post "%0" sucessfully imported', $item['name']);
  170. }
  171. if (! empty($item['comments'])) {
  172. $this->insertComments($objId, $type, $item['comments']);
  173. }
  174. if (! empty($item['tags'])) {
  175. $this->linkObjectWithTags($objId, $type, $item['tags']);
  176. }
  177. if (! empty($item['categories'])) {
  178. $this->linkObjectWithCategories($objId, $type, $item['categories']);
  179. }
  180. $this->saveAndDisplayLog($msg . "\n");
  181. return $objId;
  182. }
  183. }
  184. /**
  185. * Create blog based on $this->blogInfo and
  186. * set new blog as Tiki home page if option selected.
  187. *
  188. * @return void
  189. */
  190. public function createBlog()
  191. {
  192. global $user;
  193. $bloglib = TikiLib::lib('blog');
  194. $tikilib = TikiLib::lib('tiki');
  195. //TODO: refactor replace_blog() to have default values
  196. //TODO: blog user can be different that the user logged in the system
  197. if (isset($this->blogInfo['created'])) {
  198. $created = $this->blogInfo['created'];
  199. } else {
  200. $created = $tikilib->now;
  201. }
  202. $this->blogId = $bloglib->replace_blog(
  203. $this->blogInfo['title'],
  204. $this->blogInfo['desc'],
  205. $user,
  206. 'y',
  207. 10,
  208. false,
  209. '',
  210. 'y',
  211. 'n',
  212. 'y',
  213. 'n',
  214. 'y',
  215. 'y',
  216. 'y',
  217. 'y',
  218. 'y',
  219. 'n',
  220. '',
  221. 'y',
  222. 5,
  223. 'n',
  224. $created,
  225. $this->blogInfo['lastModif']
  226. );
  227. if (isset($_REQUEST['setAsHomePage']) && $_REQUEST['setAsHomePage'] == 'on') {
  228. $tikilib->set_preference('home_blog', $this->blogId);
  229. $tikilib->set_preference('tikiIndex', 'tiki-view_blog.php?blogId=' . $this->blogId);
  230. }
  231. }
  232. /**
  233. * Create all existing tags for a blog. Tags here
  234. * are just created, not related yet with any object (post or page)
  235. *
  236. * @param array $tags
  237. * @return void
  238. */
  239. public function createTags($tags)
  240. {
  241. $freetaglib = TikiLib::lib('freetag');
  242. foreach ($tags as $tag) {
  243. $freetaglib->find_or_create_tag($tag);
  244. }
  245. }
  246. /**
  247. * Link an object with its tags
  248. *
  249. * @param int|string $objId
  250. * @param string $type
  251. * @param array $tags
  252. * @return void
  253. */
  254. public function linkObjectWithTags($objId, $type, $tags)
  255. {
  256. $freetaglib = TikiLib::lib('freetag');
  257. global $user;
  258. $freetaglib->_tag_object_array($user, $objId, $type, $tags);
  259. }
  260. /**
  261. * Create all existing categories for a blog.
  262. *
  263. * @param array $categories
  264. * @return void
  265. */
  266. public function createCategories($categories)
  267. {
  268. $categlib = TikiLib::lib('categ');
  269. foreach ($categories as $categ) {
  270. if (! empty($categ['parent'])) {
  271. $categ['parentId'] = $categlib->get_category_id($categ['parent']);
  272. } else {
  273. $categ['parentId'] = 0;
  274. }
  275. $categlib->add_category($categ['parentId'], $categ['name'], $categ['description']);
  276. }
  277. }
  278. /**
  279. * Link an object with its categories
  280. *
  281. * @param int|string $objId
  282. * @param string $type
  283. * @param array $categories
  284. * @return void
  285. */
  286. public function linkObjectWithCategories($objId, $type, $categories)
  287. {
  288. $categlib = TikiLib::lib('categ');
  289. foreach ($categories as $categName) {
  290. $categId = $categlib->get_category_id($categName);
  291. //$catObjId is the id on tiki_objects table and $objId the id on object own table
  292. $catObjId = $categlib->get_object_id($type, $objId);
  293. // apparently this is needed only to create an entry on tiki_categorized_objects
  294. $categlib->add_categorized_object($type, $objId, '', '', '');
  295. $categlib->categorize($catObjId, $categId);
  296. }
  297. }
  298. /**
  299. * Insert page into Tiki using its builtin methods
  300. *
  301. * @param array $page
  302. * @return string|bool page name if was possible to create the new page or false
  303. */
  304. public function insertPage($page)
  305. {
  306. $objectlib = TikiLib::lib('object');
  307. $this->instantiateImporterWiki();
  308. $pageName = $this->importerWiki->insertPage($page);
  309. // maybe this should go to TikiImporter_Wiki::insertPage()
  310. if ($pageName) {
  311. $objectlib->insert_object('wiki page', $pageName, '', $pageName, 'tiki-index.php?page=' . urlencode($pageName));
  312. }
  313. return $pageName;
  314. }
  315. /**
  316. * Insert post into Tiki using its builtin methods
  317. *
  318. * @param array $post
  319. * @return int|bool post id if one was created or false
  320. */
  321. public function insertPost($post)
  322. {
  323. $bloglib = TikiLib::lib('blog');
  324. $objectlib = TikiLib::lib('object');
  325. $post = array_merge(['content' => '', 'excerpt' => '', 'author' => '', 'name' => '', 'created' => 0], $post); // set defaults
  326. $postId = $bloglib->blog_post(
  327. $this->blogId,
  328. $post['content'],
  329. $post['excerpt'],
  330. $post['author'],
  331. $post['name'],
  332. '',
  333. 'n',
  334. $post['created']
  335. );
  336. if ($postId) {
  337. $objectlib->insert_object(
  338. 'blog post',
  339. $postId,
  340. '',
  341. $post['name'],
  342. 'tiki-view_blog_post.php?postId=' . urlencode($postId)
  343. );
  344. }
  345. return $postId;
  346. }
  347. /**
  348. * Insert comments for a wiki page or post
  349. *
  350. * @param int|string $objId int for a post id or string for a wiki page name (used as id)
  351. * @param string $objType 'blog post' or 'wiki page'
  352. * @param array $comments
  353. * @return void
  354. */
  355. public function insertComments($objId, $objType, $comments)
  356. {
  357. $commentslib = TikiLib::lib('comments');
  358. $objRef = $objType . ':' . $objId;
  359. // not used but required by $commentslib->post_new_comment() as is passed by reference
  360. $message_id = '';
  361. foreach ($comments as $comment) {
  362. // set empty values for comments properties if they are not set
  363. if (! isset($comment['author'])) {
  364. $comment['author'] = '';
  365. }
  366. if (! isset($comment['author_email'])) {
  367. $comment['author_email'] = '';
  368. }
  369. if (! isset($comment['author_url'])) {
  370. $comment['author_url'] = '';
  371. }
  372. $commentId = $commentslib->post_new_comment(
  373. $objRef,
  374. 0,
  375. null,
  376. '',
  377. $comment['data'],
  378. $message_id,
  379. '',
  380. 'n',
  381. '',
  382. '',
  383. '',
  384. $comment['author'],
  385. $comment['created'],
  386. $comment['author_email'],
  387. $comment['author_url']
  388. );
  389. if ($comment['approved'] == 0) {
  390. $commentslib->approve_comment($commentId, 'n');
  391. }
  392. }
  393. }
  394. /**
  395. * This function just create an instance of
  396. * TikiImporter_Wiki and set some default values
  397. *
  398. * @return void
  399. */
  400. public function instantiateImporterWiki()
  401. {
  402. require_once('tikiimporter_wiki.php');
  403. $this->importerWiki = new TikiImporter_Wiki();
  404. $this->importerWiki->alreadyExistentPageName = 'appendPrefix';
  405. $this->importerWiki->softwareName = $this->softwareName;
  406. }
  407. }