PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax-recreateobjects.php

https://github.com/juho-jaakkola/kaltura_video
PHP | 253 lines | 144 code | 26 blank | 83 comment | 42 complexity | 0db6db5b971fc880dd2d6f865d125291 MD5 | raw file
  1. <?php
  2. /**
  3. * Kaltura video client
  4. * @package ElggKalturaVideo
  5. * @license http://www.gnu.org/licenses/gpl.html GNU Public License version 3
  6. * @author Ivan Vergés <ivan@microstudi.net>
  7. * @copyright Ivan Vergés 2010
  8. * @link http://microstudi.net/elgg/
  9. **/
  10. require_once(dirname(__FILE__)."/kaltura/api_client/includes.php");
  11. if(!elgg_is_admin_logged_in()) die('');
  12. $page = intval(get_input('page',0));
  13. $load_text = elgg_echo('kalturavideo:recreate:initiating');
  14. $total = 1;
  15. $page_size = 10;
  16. $continue = $page + 1;
  17. //0 does nothing
  18. if($page > 0) {
  19. try {
  20. //open the kaltura list
  21. $kmodel = KalturaModel::getInstance();
  22. //we only work with mixing entries
  23. $results = $kmodel->listMixEntries($page_size, $page);
  24. $total = $results->totalCount;
  25. $ok_guids = array();
  26. if(count($results->objects)>0) {
  27. foreach($results->objects as $result) {
  28. /*
  29. A object is:
  30. *
  31. [hasRealThumbnail] =>
  32. [editorType] => 1
  33. [dataContent] => all this data in a xml text
  34. [plays] =>
  35. [views] =>
  36. [width] =>
  37. [height] =>
  38. [duration] => 4
  39. [id] => entryId
  40. [name] => New Elgg Video
  41. [description] =>
  42. [partnerId] => partner_id
  43. [userId] => Elgg_user
  44. [tags] =>
  45. [adminTags] =>
  46. [status] => 2
  47. [type] => 2
  48. [createdAt] => 1254667488 timestamp
  49. [rank] => 0
  50. [totalRank] =>
  51. [votes] =>
  52. [groupId] =>
  53. [partnerData] =>
  54. [downloadUrl] =>
  55. [searchText] =>
  56. [licenseType] => -1
  57. [version] => 100001
  58. [thumbnailUrl] => entry thumbnail url
  59. * */
  60. //process object
  61. $prefix = KALTURA_ELGG_USER_PREFIX;
  62. $body = 'FOUND: <img src="'.$result->thumbnailUrl.'" style="height:20px;vertical-align:middle;" /> '.$result->id.' ('.$result->name.')';
  63. /**
  64. * @fixme This is apparently supposed to find only the videos of current user.
  65. * Not working though. The username is not stored like this in Kaltura Server.
  66. */
  67. // Remove username prefix
  68. //$user = preg_replace("/$prefix/", '', $result->userId);
  69. // Remove underscores
  70. //$user = preg_replace("/_/", '', $user);
  71. $user = 'admin';
  72. // This is not working
  73. //$user = substr($result->userId,strlen($prefix));
  74. $groupid = 0;
  75. if(!$user) {
  76. //old configuration option
  77. if(elgg_get_plugin_setting('uid_prefix', 'kaltura_video')) {
  78. $user = substr($result->userId,strlen($prefix));
  79. }
  80. }
  81. $uob = false;
  82. //check import from kaltura
  83. $taguser = '';
  84. $admintags = explode(" ",trim($result->adminTags));
  85. if($admintags[0] == KALTURA_ADMIN_TAGS) {
  86. $taguser = $admintags[1];
  87. }
  88. $result->adminTags = KALTURA_ADMIN_TAGS;
  89. foreach(array($taguser,$user) as $u) {
  90. if(strpos($u,":")!==false) {
  91. list($u,$groupid) = explode(":",$u);
  92. }
  93. if($uob = get_user_by_username($u)) {
  94. $user = $u;
  95. break;
  96. }
  97. }
  98. if($uob) {
  99. $user_guid = $uob->getGUID();
  100. $container_guid = null;
  101. //print_r($result);continue;
  102. $ob = kaltura_get_entity($result->id);
  103. if($ob->title) $result->name = $ob->title;
  104. if($ob->description) $result->description = $ob->description;
  105. if($ob->tags) $result->tags = implode(", ",$ob->tags);
  106. if($ob) {
  107. $metadata = kaltura_get_metadata($ob);
  108. $container_guid = $ob->container_guid;
  109. $privacy = $ob->access_id;
  110. //check for duplicates (and delete it)
  111. $objs = elgg_get_entities_from_metadata(array('metadata_name_value_pairs' => array(
  112. "kaltura_video_id" => $result->id
  113. ), 'types' => 'object', 'subtypes' => 'kaltura_video'));
  114. if(count($objs) > 1) {
  115. foreach($objs as $_ob) {
  116. delete_entity($_ob->guid);
  117. }
  118. }
  119. }
  120. else {
  121. $metadata = false;
  122. $privacy = null;
  123. }
  124. //remove all metadata (to clean old configs not needed)
  125. if($metadata) {
  126. foreach($metadata as $name => $value) {
  127. //do no remove this id because the kaltura_update_object needs this
  128. if($name == 'kaltura_video_id') continue;
  129. remove_metadata($ob->guid,$name);
  130. }
  131. }
  132. //privacity
  133. ////////////////////////////////////////////
  134. //for compatability with old versions,
  135. //will not used in future because Elgg provides more complets types of privacity now
  136. if($metadata->kaltura_video_privacity == 'loggedin') {
  137. $privacy = ACCESS_LOGGED_IN;
  138. }
  139. if($metadata->kaltura_video_privacity == 'public') {
  140. $privacy = ACCESS_PUBLIC;
  141. }
  142. if($metadata->kaltura_video_privacity == 'friends') {
  143. $privacy = ACCESS_FRIENDS;
  144. }
  145. if($metadata->kaltura_video_privacity == 'thisgroup') {
  146. $group = get_entity($ob->container_guid);
  147. if ($group instanceof ElggGroup) {
  148. $ob->kaltura_video_isgroup = 1;
  149. if($group->group_acl) {
  150. $privacy = $group->group_acl;
  151. $ob->kaltura_video_group_visible = 1;
  152. }
  153. }
  154. }
  155. ////////////////////////////////////////////
  156. //get the current container
  157. $user_ID = KALTURA_ELGG_USER_PREFIX.$user;
  158. if($groupid) {
  159. $user_guid = $uob->getGUID();
  160. $container_guid = $uob->getGUID();
  161. if($groupid) {
  162. $group = get_entity($groupid);
  163. if($group instanceof ElggGroup) {
  164. $container_guid = $group->getGUID();
  165. $user_ID .= ":".$groupid;
  166. }
  167. }
  168. }
  169. //create new object
  170. $result->userId = $user_ID;
  171. try {
  172. $kmodel = KalturaModel::getInstance();
  173. }
  174. catch(Exception $e) {
  175. $body .= ' ERROR: '.$e->getMessage();
  176. continue;
  177. }
  178. //$body .= print_r($result,1).print_r($kmodel->client->user->getConfig(),1);
  179. $ob = kaltura_update_object($result,$kmodel,$privacy,$user_guid,$container_guid);
  180. if(!$ob) {
  181. die("Fatal error while creating new object, ABORTING\n");
  182. }
  183. //create metadata
  184. if($metadata) {
  185. /*
  186. //If this is uncommented that will mantain the old players
  187. //Otherwise (commented) the player will be recreated to the plugin admin definitions
  188. if($metadata->kaltura_video_widget_id) {
  189. $ob->kaltura_video_widget_id = $metadata->kaltura_video_widget_id;
  190. $ob->kaltura_video_widget_uid = $metadata->kaltura_video_widget_uid;
  191. $ob->kaltura_video_widget_width = $metadata->kaltura_video_widget_width;
  192. $ob->kaltura_video_widget_height = $metadata->kaltura_video_widget_height;
  193. $ob->kaltura_video_widget_html = $metadata->kaltura_video_widget_html;
  194. }
  195. * */
  196. if($metadata->kaltura_video_collaborative) $ob->kaltura_video_collaborative = $metadata->kaltura_video_collaborative;
  197. if($metadata->kaltura_video_comments_on) $ob->kaltura_video_comments_on = $metadata->kaltura_video_comments_on;
  198. //echo htmlspecialchars(print_r($metadata,true));
  199. }
  200. if($ob->save()) {
  201. $ok_guids[] = $ob->guid;
  202. }
  203. else {
  204. die("Fatal error while saving metadata or object, ABORTING\n");
  205. }
  206. $body .= ' <strong style="color:#0b0;">'.$result->userId.' imported to '.$user.($groupid?" group $groupid":'').'.</strong>';
  207. }
  208. else {
  209. $body .= ' <strong style="color:#b00;">Not imported!</strong> (Expected user: '.$u.')';
  210. }
  211. $body .= "<br />\n";
  212. echo $body;
  213. }
  214. echo "<p>".str_replace(array("%NUMRANGE%","%TOTAL%"),array( (($page-1)*$page_size+1) . "-" . (count($results->objects)+($page-1)*$page_size),$total),elgg_echo('kalturavideo:recreate:processedvideos')).'</p>';
  215. }
  216. else $continue = 'end';
  217. }
  218. catch(Exception $e) {
  219. $error = $e->getMessage();
  220. echo $error;
  221. }
  222. $load_text = str_replace(array("%NUM%","%TOTAL%"),array($page,ceil($total/$page_size)),elgg_echo('kalturavideo:recreate:stepof'));
  223. }
  224. //show the next loading until the end
  225. echo '<div class="loaded" rel="'.$continue.'" style="height:20px;width:100%;background:url('.$CONFIG->wwwroot.'mod/kaltura_video/kaltura/editor/images/loadingAnimation.gif) no-repeat center left;padding:1px 1px 1px 215px;font-style:italic;">'.$load_text.'</div>';
  226. ?>