-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
1177 lines (1034 loc) · 26.4 KB
/
code.power
File metadata and controls
1177 lines (1034 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The local path
*
* @var string|null
* @since 3.2.0
**/
public ?string $path;
/**
* All approved paths
*
* @var array|null
* @since 3.2.0
**/
public ?array $paths;
/**
* The Grep target [network]
*
* @var string
* @since 5.0.4
**/
protected ?string $target = null;
/**
* The Grep target [entity]
*
* @var string
* @since 5.0.4
**/
protected string $entity;
/**
* The target branch field name ['read_branch', 'write_branch']
*
* @var string
* @since 3.2.2
**/
protected string $branch_field = 'read_branch';
/**
* Order of global search
*
* @var array
* @since 3.2.1
**/
protected array $order = ['local', 'remote'];
/**
* The target default branch name
*
* @var string|null
* @since 3.2.2
**/
protected ?string $branch_name = null;
/**
* The VDM global API base
*
* @var string
* @since 5.0.4
**/
protected string $api_base = '//git.vdm.dev/';
/**
* The Config Class.
*
* @var Config
* @since 5.1.1
*/
protected Config $config;
/**
* Gitea Repository Contents
*
* @var Contents
* @since 3.2.0
**/
protected Contents $contents;
/**
* The Resolve Class.
*
* @var Resolve
* @since 5.0.4
*/
protected Resolve $resolve;
/**
* The Tracker Class.
*
* @var Tracker
* @since 5.1.1
*/
protected Tracker $tracker;
/**
* Joomla Application object
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/**
* Constructor.
*
* @param Config $config The Config Class.
* @param Contents $contents The Contents Class.
* @param Resolve $resolve The Resolve Class.
* @param Tracker $tracker The Tracker Class.
* @param array $paths The approved paths
* @param string|null $path The local path
* @param CMSApplication|null $app The Application Class.
*
* @since 3.2.1
*/
public function __construct(Config $config, Contents $contents,
Resolve $resolve, Tracker $tracker, array $paths,
?string $path = null, ?CMSApplication $app = null)
{
$this->entity = $config->getTable();
$this->config = $config;
$this->contents = $contents;
$this->resolve = $resolve;
$this->tracker = $tracker;
$this->paths = $paths;
$this->path = $path;
$this->app = $app ?: Factory::getApplication();
$this->initializeInstances();
}
/**
* Get an item
*
* @param string $guid The global unique id of the item
* @param array|null $order The search order
* @param object|null $repo The repository object to search. If null, all repos will be searched.
*
* @return object|null
* @since 3.2.2
*/
public function get(string $guid, ?array $order = null, ?object $repo = null): ?object
{
$order = $order ?? $this->order;
if ($repo !== null)
{
return $this->searchSingleRepo($guid, $order, $repo);
}
return $this->searchAllRepos($guid, $order);
}
/**
* Validate any repository
*
* @param object $repository The target repository object.
* @param string|null $networkTarget The network target name
*
* @return bool True if valid path
* @since 5.1.4
*/
public function validRepo(object &$repository, ?string $networkTarget = null): bool
{
if (($repository->grep_validated ?? false))
{
return true;
}
$repository->organisation = trim($repository->organisation ?? '');
$repository->repository = trim($repository->repository ?? '');
if (empty($repository->organisation) || empty($repository->repository))
{
return false;
}
$networkTarget ??= $this->getNetworkTarget();
$target = trim($repository->target ?? 'gitea');
// resolve API if a gitea (core) endpoint
if (!empty($repository->base) && $target === 'gitea')
{
$this->resolve->api($networkTarget ?? $repository->repository, $repository->base, $repository->organisation, $repository->repository);
}
// build the path
$repository->path = $repository->organisation . '/' . $repository->repository;
// get the branch field name
$branch_field = $this->getBranchField();
// get the branch name
$branch = $this->getBranchName($repository);
if ($branch === 'default' || empty($branch))
{
// will allow us to target the default branch as set by the git system
$repository->{$branch_field} = null;
}
// set local path
if ($this->path && is_dir($this->path . '/' . $repository->path))
{
$repository->full_path = $this->path . '/' . $repository->path;
}
$repository->grep_validated = true;
return true;
}
/**
* Get the path/repo object
*
* @param string $guid The target repository guid.
*
* @return object|null
* @since 5.1.1
*/
public function getPath(string $guid): ?object
{
if (!is_array($this->paths) || $this->paths === [] || empty($guid))
{
return null;
}
foreach ($this->paths as $path)
{
if (!isset($path->guid) || $guid !== $path->guid)
{
continue;
}
return $path;
}
return null;
}
/**
* Get all the available repos
*
* @return array|null
* @since 5.1.1
*/
public function getPaths(): ?array
{
if (!is_array($this->paths) || $this->paths === [])
{
return null;
}
return $this->paths;
}
/**
* Get all paths + indexes (the active set)
*
* @return array|null
* @since 5.1.1
*/
public function getPathsIndexes(): ?array
{
if (!is_array($this->paths) || $this->paths === [])
{
return null;
}
$powers = [];
foreach ($this->paths as $path)
{
// Get remote index
$this->indexRemote($path);
if (is_array($path->index ?? null) && is_object($path->index[$this->entity] ?? null))
{
$powers[] = $path;
}
}
return $powers;
}
/**
* Get the a path + indexes
*
* @param string $guid The unique identifier for the repo.
* @param bool $reload The switch to reload the index, and not return from cache.
*
* @return object|null
* @since 5.1.1
*/
public function getPathIndexes(string $guid, bool $reload = false): ?object
{
if (!is_array($this->paths) || $this->paths === [] || empty($guid))
{
return null;
}
foreach ($this->paths as $path)
{
if (!isset($path->guid) || $guid !== $path->guid)
{
continue;
}
if ($reload)
{
unset($path->index[$this->entity]);
}
// Get remote index
$this->indexRemote($path);
if (is_array($path->index ?? null) && is_object($path->index[$this->entity] ?? null))
{
return $path;
}
}
return null;
}
/**
* Get the index of a repo
*
* @param string $guid The unique identifier for the repo.
* @param bool $reload The switch to reload the index, and not return from cache.
*
* @return object|null
* @since 3.2.2
*/
public function getRemoteIndex(string $guid, bool $reload = false): ?object
{
if (!is_array($this->paths) || $this->paths === [] || empty($guid))
{
return null;
}
foreach ($this->paths as $path)
{
if (!isset($path->guid) || $guid !== $path->guid)
{
continue;
}
if ($reload)
{
unset($path->index[$this->entity]);
}
// Get remote index
$this->indexRemote($path);
if (is_array($path->index ?? null) && is_object($path->index[$this->entity] ?? null))
{
return $path->index[$this->entity];
}
}
return null;
}
/**
* Reset the index of a entity
*
* @return void
* @since 5.1.2
*/
public function resetEntityIndex(): void
{
foreach ($this->paths as &$path)
{
unset($path->index[$this->entity]);
}
}
/**
* Get the network target name
*
* @return string|null
* @since 5.1.1
*/
public function getNetworkTarget(): ?string
{
return $this->target ?? null;
}
/**
* Check if an item exists in any repo or in a specific repo.
*
* @param string $guid The unique identifier for the item.
* @param object|null $repo The repository object to check against. If null, all repos will be checked.
* @param array|null $order The order of the targets to check. If null, the default order will be used.
*
* @return bool True if the item exists, false otherwise.
* @since 3.2.2
*/
public function exists(string $guid, ?object $repo = null, ?array $order = null): bool
{
$order = $order ?? $this->order;
if ($repo !== null)
{
return $this->itemExistsInRepo($guid, $repo, $order);
}
return $this->itemExistsInAllRepos($guid, $order);
}
/**
* Set the branch field
*
* @param string $field The field to use to get the branch name from the data set
*
* @return void
* @since 3.2.2
*/
public function setBranchField(string $field): void
{
$this->branch_field = $field;
}
/**
* Set the DEFAULT branch name (only used if branch field is not found)
*
* @param string|null $name The default branch to use if no name could be found
*
* @return void
* @since 3.2.2
*/
public function setBranchDefaultName(?string $name): void
{
$this->branch_name = $name;
}
/**
* Set the index path
*
* @param string $indexPath The repository index path
*
* @return void
* @since 3.2.2
*/
public function setIndexPath(string $indexPath): void
{
$this->config->setIndexPath($indexPath);
}
/**
* Loads API config using the provided base URL and token.
*
* This method checks if the base URL contains 'https://git.vdm.dev/'.
* If it does, it uses the token as is (which may be null).
* If not, it ensures the token is not null by defaulting to an empty string.
*
* @param Api $api The api object with a load_ method.
* @param string|null $base The base URL path.
* @param string|null $token The token for authentication (can be null).
*
* @return void
* @since 5.0.4
*/
public function loadApi(Api $api, ?string $base, ?string $token): void
{
// If we have global tokens for a base system we must not reset on an empty token
if ($base && (
strpos($base. '/', $this->api_base) !== false ||
strpos($base, 'api.github.com') !== false
))
{
// If base contains $this->api_base = https://git.vdm.dev/, use the token as is
// If base contains api.github.com, use the token as is
$tokenToUse = $token;
}
else
{
// Otherwise, ensure the token is not null (use empty string if null)
$tokenToUse = $token ?? '';
}
// Load the content with the determined base and token
$api->load_($base, $tokenToUse);
}
/**
* Resolve and validate entity GUID values.
*
* - Empty values are ignored.
* - If the entity uses a GUID field, each value is validated:
* - Valid GUIDs are accepted as-is.
* - Invalid GUIDs are resolved via a helper field when available.
* - If the entity does not use GUIDs, values are returned unchanged.
*
* @param array $values The values to resolve.
* @param object|null $repo The repository object to search. If null, all repositories are searched.
*
* @return array An array of valid GUID values.
* @since 5.1.4
*/
public function getValidGuids(array $values, ?object $repo = null): array
{
// If this entity does not use GUIDs, return values untouched
if ($this->getGuidField() !== 'guid')
{
return $values;
}
$helperField = $this->getGuidHelperField();
$validGuids = [];
foreach ($values as $value)
{
$value = trim((string) $value);
if ($value === '')
{
continue;
}
// Accept already-valid GUIDs
if (GuidHelper::valid($value))
{
$validGuids[] = $value;
continue;
}
// No helper available to resolve invalid GUIDs
if ($helperField === null)
{
continue;
}
$resolved = $this->getEntityGuid($value, $helperField, $repo);
if ($resolved !== null && GuidHelper::valid($resolved))
{
$validGuids[] = $resolved;
}
}
return $validGuids;
}
/**
* Set repository messages and errors based on given conditions.
*
* @param string $message The message to set (if error)
* @param string $path Path value
* @param string $repository Repository name
* @param string $organisation Organisation name
* @param string|null $base Base URL
*
* @return void
* @since 3.2.0
*/
abstract protected function setRemoteIndexMessage(string $message, string $path, string $repository, string $organisation, ?string $base): void;
/**
* Injects metadata SHA into the power params object.
*
* @param object $power The object to modify
* @param object $path The repository path
* @param string $targetPath The target path inside the repo
* @param string $branch The branch to use
* @param string $sourceKey The key to set inside params->source
*
* @return void
* @since 5.1.1
*/
protected function setRepoItemSha(object &$power, object $path,
string $targetPath, string $branch, string $sourceKey): void
{
try {
$meta = $this->contents->metadata(
$path->organisation,
$path->repository,
$targetPath,
$branch
);
} catch (\Throwable $e) {
$meta = null;
}
if ($meta === null || !isset($meta->sha))
{
return;
}
if (!isset($power->params) || !is_object($power->params))
{
$power->params = (object) ['source' => [$sourceKey => $meta->sha]];
return;
}
if (!isset($power->params->source) || !is_array($power->params->source))
{
$power->params->source = [];
}
$power->params->source[$sourceKey] = $meta->sha;
}
/**
* Get function name
*
* @param string $name The targeted area name
* @param string $type The type of function name
*
* @return string|null
* @since 3.2.0
*/
protected function getFunctionName(string $name, string $type = 'search'): ?string
{
$function_name = $type . ucfirst(strtolower($name));
return method_exists($this, $function_name) ? $function_name : null;
}
/**
* Search a single repository for an item
*
* @param string $guid The unique identifier for the item.
* @param array $order The order of the targets to check.
* @param object $repo The repository object to check against.
*
* @return object|null
* @since 3.2.2
*/
protected function searchSingleRepo(string $guid, array $order, object $repo): ?object
{
foreach ($order as $target)
{
if ($this->itemExists($guid, $repo, $target))
{
$functionName = $this->getFunctionName($target, 'get');
if ($functionName !== null && ($power = $this->{$functionName}($repo, $guid)) !== null)
{
return $power;
}
}
}
return null;
}
/**
* Search all repositories for an item
*
* @param string $guid The unique identifier for the item.
* @param array $order The order of the targets to check.
*
* @return object|null
* @since 3.2.2
*/
protected function searchAllRepos(string $guid, array $order): ?object
{
if (is_array($this->paths) && $this->paths !== [])
{
foreach ($order as $target)
{
$functionName = $this->getFunctionName($target);
if ($functionName !== null && ($power = $this->{$functionName}($guid)) !== null)
{
return $power;
}
}
}
return null;
}
/**
* Check if an item exists in a specific repository.
*
* @param string $guid The unique identifier for the item.
* @param object $repo The repository object to check against.
* @param array $order The order of the targets to check.
*
* @return bool True if the item exists, false otherwise.
* @since 3.2.2
*/
protected function itemExistsInRepo(string $guid, object $repo, array $order): bool
{
foreach ($order as $target)
{
if ($this->itemExists($guid, $repo, $target))
{
return true;
}
}
return false;
}
/**
* Check if an item exists in any of the repositories.
*
* @param string $guid The unique identifier for the item.
* @param array $order The order of the targets to check.
*
* @return bool True if the item exists, false otherwise.
* @since 3.2.2
*/
protected function itemExistsInAllRepos(string $guid, array $order): bool
{
// We can only search if we have paths
if (is_array($this->paths) && $this->paths !== [])
{
foreach ($order as $target)
{
foreach ($this->paths as $path)
{
if ($this->itemExists($guid, $path, $target))
{
return true;
}
}
}
}
return false;
}
/**
* Get the branch field
*
* @return string
* @since 3.2.2
*/
protected function getBranchField(): string
{
return $this->branch_field;
}
/**
* Get the branch default name
*
* @return string|null
* @since 3.2.2
*/
protected function getBranchDefaultName(): ?string
{
return $this->branch_name;
}
/**
* Get the branch name
*
* @param object $item The item path
*
* @return string|null
* @since 3.2.2
*/
protected function getBranchName(object $item): ?string
{
// get the branch field name
$branch_field = $this->getBranchField();
return $item->{$branch_field} ?? $this->getBranchDefaultName();
}
/**
* Get the index path
*
* @return string
* @since 3.2.2
*/
protected function getIndexPath(): string
{
return $this->config->getIndexPath();
}
/**
* Get the settings name
*
* @return string
* @since 3.2.2
*/
protected function getSettingsName(): string
{
return $this->config->getSettingsName();
}
/**
* Get GUID field
*
* @return string
* @since 5.1.1
*/
protected function getGuidField(): string
{
return $this->config->getGuidField();
}
/**
* Get GUID Helper field
*
* @return string|null
* @since 5.1.4
*/
public function getGuidHelperField(): ?string
{
return $this->config->getGuidHelperField();
}
/**
* Get GUID field
*
* @return string
* @since 5.1.1
*/
protected function getItemReadmeName(): string
{
return $this->config->getItemReadmeName();
}
/**
* Has item readme
*
* @return bool
* @since 5.1.1
*/
protected function hasItemReadme(): bool
{
return $this->config->hasItemReadme();
}
/**
* Check if an item exists in a specific repo and target.
*
* @param string $guid The unique identifier for the item.
* @param object $repo The repository object to check against.
* @param string $target The target to check within the repo.
*
* @return bool True if the item exists, false otherwise.
* @since 3.2.2
*/
protected function itemExists(string $guid, object &$repo, string $target): bool
{
if (($function_name = $this->getFunctionName($target, 'index')) !== null)
{
$this->{$function_name}($repo);
if (($function_name = $this->getFunctionName($target, 'exists')) !== null &&
$this->{$function_name}($guid, $repo))
{
return true;
}
}
return false;
}
/**
* Check if item exists locally
*
* @param string $guid The global unique id of the item
*
* @return object|null return path object
* @since 3.2.2
*/
protected function existsLocally(string $guid): ?object
{
// we can only search if we have paths
if ($this->path && $this->paths)
{
foreach ($this->paths as $path)
{
// get local index
$this->indexLocal($path);
if ($this->existsLocal($guid, $path))
{
return $path;
}
}
}
return null;
}
/**
* Check if item exists remotely
*
* @param string $guid The global unique id of the item
*
* @return object|null return path object
* @since 3.2.2
*/
protected function existsRemotely(string $guid): ?object
{
// we can only search if we have paths
if ($this->paths)
{
foreach ($this->paths as $path)
{
// get local index
$this->indexRemote($path);
if ($this->existsRemote($guid, $path))
{
return $path;
}
}
}
return null;
}
/**
* Determine whether an item exists in the local index.
*
* @param string $guid The global unique identifier of the item.
* @param object $path The path object containing the local entities.
*
* @return bool True if the item exists locally, false otherwise.
* @since 3.2.2
*/
protected function existsLocal(string $guid, object $path): bool
{
$local = $path->local ?? null;
if (!is_array($local))
{
return false;
}
$indexedEntities = $local[$this->entity] ?? null;
if (!is_object($indexedEntities))
{
return false;
}
return isset($indexedEntities->{$guid});
}
/**
* Determine whether an item exists in the remote index.
*
* @param string $guid The global unique identifier of the item.
* @param object $path The path object containing the indexed entities.
*
* @return bool True if the item exists remotely, false otherwise.
* @since 3.2.2
*/
protected function existsRemote(string $guid, object $path): bool
{
$index = $path->index ?? null;
if (!is_array($index))
{
return false;
}
$indexedEntities = $index[$this->entity] ?? null;
if (!is_object($indexedEntities))
{
return false;
}
return isset($indexedEntities->{$guid});
}
/**
* Load the remote repository index of powers
*
* @param object $path The repository path details
*
* @return void
* @since 3.2.0
*/
protected function indexRemote(object &$path): void
{
if (is_array($path->index ?? null) && isset($path->index[$this->entity]))
{
return; // already set
}
if (!is_array($path->index ?? null))
{
$path->index = [];
}
try
{
// set the target system
$target = $path->target ?? 'gitea';
$this->contents->setTarget($target);
// load the base and token if set
$this->loadApi(
$this->contents,
$path->base ?? null,