Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
js_api_management.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_management.inc';
18 
31 {
32 
33 
39  {
40  Asset_Management::__construct($pm);
41 
42 
43  $this->vars = Array (
44  'types' => Array(
45  'added' => '0.1',
46  'type' => 'serialise',
47  'default' => Array(),
48  ),
49  'types_restriction' => Array(
50  'added' => '0.1',
51  'type' => 'serialise',
52  'default' => Array(),
53  ),
54  'name' => Array(
55  'added' => '0.1',
56  'type' => 'text',
57  'default' => '',
58  'description' => 'The full name of the page',
59  'update_fn' => '',
60  'is_admin' => FALSE,
61  ),
62  'short_name' => Array(
63  'added' => '0.1',
64  'type' => 'text',
65  'default' => '',
66  'description' => 'The short name of the page',
67  'update_fn' => '',
68  'is_admin' => FALSE,
69  ),
70  'root_node' => Array(
71  'added' => '0.2',
72  'type' => 'assetid',
73  'default' => '',
74  'description' => 'The root node to restrict to. DEPRECIATED in v0.2 and up',
75  'update_fn' => '',
76  'is_admin' => FALSE,
77  ),
78  'api_key' => Array(
79  'added' => '0.1',
80  'type' => 'text',
81  'default' => '',
82  'description' => 'The API Key',
83  'update_fn' => '',
84  'is_admin' => FALSE,
85  ),
86  'create_attributes' => Array(
87  'added' => '0.1',
88  'type' => 'boolean',
89  'default' => FALSE,
90  'parameters' => Array(
91  'allow_empty' => FALSE,
92  ),
93  ),
94  'allow_json' => Array(
95  'added' => '0.1',
96  'type' => 'boolean',
97  'default' => TRUE,
98  'parameters' => Array(
99  'allow_empty' => FALSE,
100  ),
101  ),
102  'ignore_permissions' => Array(
103  'added' => '0.2',
104  'type' => 'boolean',
105  'default' => FALSE,
106  'parameters' => Array(
107  'allow_empty' => FALSE,
108  ),
109  ),
110  'get_general' => Array(
111  'added' => '0.1',
112  'type' => 'boolean',
113  'default' => FALSE,
114  'parameters' => Array(
115  'allow_empty' => FALSE,
116  ),
117  ),
118  'get_attributes' => Array(
119  'added' => '0.1',
120  'type' => 'boolean',
121  'default' => FALSE,
122  'parameters' => Array(
123  'allow_empty' => FALSE,
124  ),
125  ),
126  'set_attribute' => Array(
127  'added' => '0.1',
128  'type' => 'boolean',
129  'default' => FALSE,
130  'parameters' => Array(
131  'allow_empty' => FALSE,
132  ),
133  ),
134  'get_metadata' => Array(
135  'added' => '0.1',
136  'type' => 'boolean',
137  'default' => FALSE,
138  'parameters' => Array(
139  'allow_empty' => FALSE,
140  ),
141  ),
142  'set_metadata' => Array(
143  'added' => '0.1',
144  'type' => 'boolean',
145  'default' => FALSE,
146  'parameters' => Array(
147  'allow_empty' => FALSE,
148  ),
149  ),
150  'trash_asset' => Array(
151  'added' => '0.1',
152  'type' => 'boolean',
153  'default' => FALSE,
154  'parameters' => Array(
155  'allow_empty' => FALSE,
156  ),
157  ),
158  'get_parents' => Array(
159  'added' => '0.1',
160  'type' => 'boolean',
161  'default' => FALSE,
162  'parameters' => Array(
163  'allow_empty' => FALSE,
164  ),
165  ),
166  'get_children' => Array(
167  'added' => '0.1',
168  'type' => 'boolean',
169  'default' => FALSE,
170  'parameters' => Array(
171  'allow_empty' => FALSE,
172  ),
173  ),
174  'get_permissions' => Array(
175  'added' => '0.1',
176  'type' => 'boolean',
177  'default' => FALSE,
178  'parameters' => Array(
179  'allow_empty' => FALSE,
180  ),
181  ),
182  'create_asset' => Array(
183  'added' => '0.1',
184  'type' => 'boolean',
185  'default' => FALSE,
186  'parameters' => Array(
187  'allow_empty' => FALSE,
188  ),
189  ),
190  'get_asset_types' => Array(
191  'added' => '0.1',
192  'type' => 'boolean',
193  'default' => FALSE,
194  'parameters' => Array(
195  'allow_empty' => FALSE,
196  ),
197  ),
198  'get_locks_info' => Array(
199  'added' => '0.4',
200  'type' => 'boolean',
201  'default' => FALSE,
202  'parameters' => Array(
203  'allow_empty' => FALSE,
204  ),
205  ),
206  'acquire_lock' => Array(
207  'added' => '0.1',
208  'type' => 'boolean',
209  'default' => FALSE,
210  'parameters' => Array(
211  'allow_empty' => FALSE,
212  ),
213  ),
214  'release_lock' => Array(
215  'added' => '0.1',
216  'type' => 'boolean',
217  'default' => FALSE,
218  'parameters' => Array(
219  'allow_empty' => FALSE,
220  ),
221  ),
222  'create_link' => Array(
223  'added' => '0.1',
224  'type' => 'boolean',
225  'default' => FALSE,
226  'parameters' => Array(
227  'allow_empty' => FALSE,
228  ),
229  ),
230  'remove_link' => Array(
231  'added' => '0.1',
232  'type' => 'boolean',
233  'default' => FALSE,
234  'parameters' => Array(
235  'allow_empty' => FALSE,
236  ),
237  ),
238  'move_link' => Array(
239  'added' => '0.1',
240  'type' => 'boolean',
241  'default' => FALSE,
242  'parameters' => Array(
243  'allow_empty' => FALSE,
244  ),
245  ),
246  'update_link' => Array(
247  'added' => '0.1',
248  'type' => 'boolean',
249  'default' => FALSE,
250  'parameters' => Array(
251  'allow_empty' => FALSE,
252  ),
253  ),
254  'get_link_id' => Array(
255  'added' => '0.1',
256  'type' => 'boolean',
257  'default' => FALSE,
258  'parameters' => Array(
259  'allow_empty' => FALSE,
260  ),
261  ),
262  'get_asset_tree' => Array(
263  'added' => '0.1',
264  'type' => 'boolean',
265  'default' => FALSE,
266  'parameters' => Array(
267  'allow_empty' => FALSE,
268  ),
269  ),
270  'get_keywords_replacements' => Array(
271  'added' => '0.4',
272  'type' => 'boolean',
273  'default' => FALSE,
274  'parameters' => Array(
275  'allow_empty' => FALSE,
276  ),
277  ),
278  'set_asset_status' => Array(
279  'added' => '0.4',
280  'type' => 'boolean',
281  'default' => FALSE,
282  'parameters' => Array(
283  'allow_empty' => FALSE,
284  ),
285  ),
286  'get_web_path' => Array(
287  'added' => '0.5',
288  'type' => 'boolean',
289  'default' => FALSE,
290  'parameters' => Array(
291  'allow_empty' => FALSE,
292  ),
293  ),
294  'set_web_path' => Array(
295  'added' => '0.5',
296  'type' => 'boolean',
297  'default' => FALSE,
298  'parameters' => Array(
299  'allow_empty' => FALSE,
300  ),
301  ),
302  'force_simple_edit' => Array(
303  'added' => '0.5',
304  'type' => 'boolean',
305  'default' => FALSE,
306  'parameters' => Array(
307  'allow_empty' => FALSE,
308  ),
309  ),
310  'get_workflow_schema' => Array(
311  'added' => '0.5',
312  'type' => 'boolean',
313  'default' => FALSE,
314  'parameters' => Array(
315  'allow_empty' => FALSE,
316  ),
317  ),
318  'set_file_content' => Array(
319  'added' => '0.6',
320  'type' => 'boolean',
321  'default' => FALSE,
322  'parameters' => Array(
323  'allow_empty' => FALSE,
324  ),
325  ),
326  'import_assets' => Array(
327  'added' => '0.6',
328  'type' => 'boolean',
329  'default' => FALSE,
330  'parameters' => Array(
331  'allow_empty' => FALSE,
332  ),
333  ),
334  'get_roles' => Array(
335  'added' => '0.5',
336  'type' => 'boolean',
337  'default' => FALSE,
338  'parameters' => Array(
339  'allow_empty' => FALSE,
340  ),
341  ),
342  'execute_html_tidy' => Array(
343  'added' => '0.6',
344  'type' => 'boolean',
345  'default' => FALSE,
346  'parameters' => Array(
347  'allow_empty'=> FALSE,
348  ),
349  ),
350  'use_enhanced_js' => Array(
351  'added' => '0.6',
352  'type' => 'boolean',
353  'default' => FALSE,
354  'parameters' => Array(
355  'allow_empty'=> FALSE,
356  ),
357  ),
358  'get_lineage' => Array(
359  'added' => '0.6',
360  'type' => 'boolean',
361  'default' => FALSE,
362  'parameters' => Array(
363  'allow_empty'=> FALSE,
364  ),
365  ),
366  'get_lineage_from_url' => Array(
367  'added' => '0.6',
368  'type' => 'boolean',
369  'default' => FALSE,
370  'parameters' => Array(
371  'allow_empty'=> FALSE,
372  ),
373  ),
374  'get_url_from_lineage' => Array(
375  'added' => '0.6',
376  'type' => 'boolean',
377  'default' => FALSE,
378  'parameters' => Array(
379  'allow_empty'=> FALSE,
380  ),
381  ),
382 
383  'batch_requests' => Array(
384  'added' => '0.6',
385  'type' => 'boolean',
386  'default' => FALSE,
387  'parameters' => Array(
388  'allow_empty'=> FALSE,
389  ),
390  ),
391  'clone_asset' => Array(
392  'added' => '0.6',
393  'type' => 'boolean',
394  'default' => FALSE,
395  'parameters' => Array(
396  'allow_empty'=> FALSE,
397  ),
398  ),
399  'show_diff' => Array(
400  'added' => '0.6',
401  'type' => 'boolean',
402  'default' => FALSE,
403  'parameters' => Array(
404  'allow_empty'=> FALSE,
405  ),
406  ),
407  'get_context' => Array(
408  'added' => '0.6',
409  'type' => 'boolean',
410  'default' => FALSE,
411  'parameters' => Array(
412  'allow_empty'=> FALSE,
413  ),
414  ),
415  'set_context' => Array(
416  'added' => '0.6',
417  'type' => 'boolean',
418  'default' => FALSE,
419  'parameters' => Array(
420  'allow_empty'=> FALSE,
421  ),
422  ),
423  'get_metadata_schema' => Array(
424  'added' => '0.6',
425  'type' => 'boolean',
426  'default' => FALSE,
427  'parameters' => Array(
428  'allow_empty'=> FALSE,
429  ),
430  ),
431  'sync_context' => Array(
432  'added' => '0.6',
433  'type' => 'boolean',
434  'default' => FALSE,
435  'parameters' => Array(
436  'allow_empty'=> FALSE,
437  ),
438  ),
439  );
440 
441  }//end constructor
442 
443 
452  function _upgrade($current_version)
453  {
454  if (!parent::_upgrade($current_version)) return FALSE;
455 
456  $asset_name = strtoupper($this->_pm->getTypeInfo($this->getAssetType(), 'name'));
457 
458  if (version_compare($current_version, '0.2', '<')) {
459  // version 0.1 -> 0.2
460  // Root nodes changed from assetid to links
461  pre_echo('STARTING '.$asset_name.' UPGRADE - FROM VERSION 0.1');
462  $apis = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('js_api', FALSE, TRUE);
463  $count = 0;
464  foreach ($apis as $assetid => $type_code) {
465  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, 'js_api');
466  if (is_null($asset)) continue;
467 
468  // Current links if applicable
469  $root_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_NOTICE, '', FALSE, 'major', 'root');
470  $current_root_node_id = $asset->attr('root_node');
471 
472  // Set a new notice link for the current root node if not already
473  if (!empty($current_root_node_id)) {
474  $exists = FALSE;
475  foreach ($root_links as $root_link) {
476  if ($root_link['minorid'] == $current_root_node_id) {
477  $exists = TRUE;
478  }//end if
479  }//end foreach
480 
481  if (!$exists) {
482  $current_root_node = $GLOBALS['SQ_SYSTEM']->am->getAsset($current_root_node_id);
483  if (!is_null($current_root_node)) {
484  $GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'links');
485  $new_link = $GLOBALS['SQ_SYSTEM']->am->createAssetLink($asset, $current_root_node, SQ_LINK_NOTICE, 'root');
486  if (!empty($new_link)) $count++;
487  $GLOBALS['SQ_SYSTEM']->am->releaseLock($asset->id, 'links');
488  }//end if
489  }//end if
490  }//end if
491 
492  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
493  unset($asset);
494 
495  }//end foreach
496  pre_echo('Updated - '.$count.' Javascript Api(s)...');
497  pre_echo($asset_name.' UPGRADE COMPLETE - FROM VERSION 0.1');
498 
499  }//end if - upgrade to version 0.2
500 
501  if (version_compare($current_version, '0.3', '<')) {
502 
503  // Generate API key for those had empty default key
504  pre_echo('STARTING '.$asset_name.' UPGRADE - TO VERSION 0.3 - FIXING Default API Key');
505  $apis = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('js_api', FALSE, TRUE);
506  $count = 0;
507  foreach ($apis as $assetid => $type_code) {
508  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, 'js_api');
509  if (is_null($asset)) continue;
510  $key = $asset->attr('api_key');
511  if(empty($key)) {
512  // regenerate api key
513  $new_key = rand(1000000000, 9999999999);
514  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
515  $asset->setAttrValue('api_key', $new_key);
516  $asset->saveAttributes();
517  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
518  pre_echo('JS API Asset #'.$asset->id.' has a new API key '.$new_key);
519  $count ++;
520  }
521 
522  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
523  unset($asset);
524 
525  }//end foreach
526  pre_echo('Updated - '.$count.' Javascript Api(s)...');
527  pre_echo($asset_name.' UPGRADE COMPLETE - TO VERSION 0.3');
528 
529  }//end if - upgrade to version 0.3
530 
531  if (version_compare($current_version, '0.4', '<')) {
532 
533  pre_echo('STARTING UPGRADE - FROM VERSION '.$current_version.' TO 0.4');
534  $root_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_folder');
535  $children = $GLOBALS['SQ_SYSTEM']->am->getChildren($root_folder->id, $this->getAssetType(), FALSE);
536 
537  pre_echo('Upgrading '.count($children).' assets...');
538  foreach ($children as $assetid => $type) {
539  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $this->getAssetType());
540  if (is_null($asset)) continue;
541 
542  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'attributes')) {
543  trigger_localised_error('CMS0025', E_USER_WARNING, '0.2', $asset->name, $asset->id);
544  return FALSE;
545  }
546 
547 
548  $asset->setAttrValue('get_locks_info', FALSE);
549 
550  $asset->setAttrValue('get_keywords_replacements', FALSE);
551 
552  $asset->setAttrValue('set_asset_status', FALSE);
553 
554  if (!$asset->saveAttributes()) {
555  trigger_localised_error('CMS0026', E_USER_WARNING, '0.2');
556  return FALSE;
557  }
558 
559  $GLOBALS['SQ_SYSTEM']->am->releaseLock($asset->id, 'attributes');
560  }
561  pre_echo('UPGRADE COMPLETE - FROM VERSION '.$current_version.' TO 0.4');
562 
563  }//end if - upgrade to version 0.4
564 
565  if (version_compare($current_version, '0.5', '<')) {
566 
567  pre_echo('STARTING UPGRADE - FROM VERSION '.$current_version.' TO 0.5');
568  $root_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_folder');
569  $children = $GLOBALS['SQ_SYSTEM']->am->getChildren($root_folder->id, $this->getAssetType(), FALSE);
570 
571  pre_echo('Upgrading '.count($children).' assets...');
572  foreach ($children as $assetid => $type) {
573  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $this->getAssetType());
574  if (is_null($asset)) continue;
575 
576  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'attributes')) {
577  trigger_localised_error('CMS0025', E_USER_WARNING, '0.5', $asset->name, $asset->id);
578  return FALSE;
579  }
580 
581 
582  $asset->setAttrValue('get_web_path', FALSE);
583 
584  $asset->setAttrValue('get_web_path', FALSE);
585 
586  $asset->setAttrValue('force_simple_edit', FALSE);
587 
588  $asset->setAttrValue('get_workflow_schema', FALSE);
589 
590 
591  if (!$asset->saveAttributes()) {
592  trigger_localised_error('CMS0026', E_USER_WARNING, '0.5');
593  return FALSE;
594  }
595 
596  $GLOBALS['SQ_SYSTEM']->am->releaseLock($asset->id, 'attributes');
597  }
598  pre_echo('UPGRADE COMPLETE - FROM VERSION '.$current_version.' TO 0.5');
599 
600  }//end if - upgrade to version 0.5
601 
602  if (version_compare($current_version, '0.6', '<')) {
603 
604  pre_echo('STARTING UPGRADE - FROM VERSION '.$current_version.' TO 0.6');
605  $root_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_folder');
606  $children = $GLOBALS['SQ_SYSTEM']->am->getChildren($root_folder->id, $this->getAssetType(), FALSE);
607 
608  pre_echo('Upgrading '.count($children).' assets...');
609  foreach ($children as $assetid => $type) {
610  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $this->getAssetType());
611  if (is_null($asset)) continue;
612 
613  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'attributes')) {
614  trigger_localised_error('CMS0025', E_USER_WARNING, '0.6', $asset->name, $asset->id);
615  return FALSE;
616  }
617 
618  $asset->setAttrValue('set_file_content', FALSE);
619 
620  $asset->setAttrValue('import_assets', FALSE);
621 
622  if (!$asset->saveAttributes()) {
623  trigger_localised_error('CMS0026', E_USER_WARNING, '0.6');
624  return FALSE;
625  }
626 
627  $GLOBALS['SQ_SYSTEM']->am->releaseLock($asset->id, 'attributes');
628  }
629  pre_echo('UPGRADE COMPLETE - FROM VERSION '.$current_version.' TO 0.6');
630 
631  }//end if - upgrade to version 0.6
632 
633  return TRUE;
634 
635  }//end _upgrade()
636 
637 
638 }//end class
639 ?>