11
11
use OCP \Files \File ;
12
12
use OCP \Files \Folder ;
13
13
use OCP \Files \InvalidPathException ;
14
+ use OCP \Files \Node ;
14
15
use OCP \Files \NotPermittedException as FilesNotPermittedException ;
15
16
use OCP \Files \NotFoundException as FilesNotFoundException ;
16
17
use OCP \IConfig ;
@@ -136,35 +137,38 @@ public function getFolder(int $collectiveId, int $fileId, string $userId): Folde
136
137
137
138
/**
138
139
* @param File $file
140
+ * @param Node|null $parent
139
141
*
140
142
* @return int
141
143
* @throws NotFoundException
142
144
*/
143
- public function getParentPageId (File $ file ): int {
145
+ private function getParentPageId (File $ file, ? Node $ parent = null ): int {
144
146
try {
145
147
if (self ::isLandingPage ($ file )) {
146
148
// Return `0` for landing page
147
149
return 0 ;
148
150
}
149
151
152
+ $ parent = $ parent ?? $ file ->getParent ();
153
+
150
154
if (self ::isIndexPage ($ file )) {
151
155
// Go down two levels if index page but not landing page
152
- return $ this ->getIndexPageFile ($ file -> getParent () ->getParent ())->getId ();
156
+ return $ this ->getIndexPageFile ($ parent ->getParent ())->getId ();
153
157
}
154
158
155
- return $ this ->getIndexPageFile ($ file -> getParent () )->getId ();
159
+ return $ this ->getIndexPageFile ($ parent )->getId ();
156
160
} catch (InvalidPathException | FilesNotFoundException $ e ) {
157
161
throw new NotFoundException ($ e ->getMessage (), 0 , $ e );
158
162
}
159
163
}
160
164
161
165
/**
162
166
* @param File $file
163
- *
167
+ * @param Node|null $parent
164
168
* @return PageInfo
165
169
* @throws NotFoundException
166
170
*/
167
- private function getPageByFile (File $ file ): PageInfo {
171
+ private function getPageByFile (File $ file, ? Node $ parent = null ): PageInfo {
168
172
$ pageInfo = new PageInfo ();
169
173
try {
170
174
$ page = $ this ->pageMapper ->findByFileId ($ file ->getId ());
@@ -176,7 +180,7 @@ private function getPageByFile(File $file): PageInfo {
176
180
$ subpageOrder = ($ page !== null ) ? $ page ->getSubpageOrder () : null ;
177
181
try {
178
182
$ pageInfo ->fromFile ($ file ,
179
- $ this ->getParentPageId ($ file ),
183
+ $ this ->getParentPageId ($ file, $ parent ),
180
184
$ lastUserId ,
181
185
$ lastUserId ? $ this ->userManager ->get ($ lastUserId )->getDisplayName () : null ,
182
186
$ emoji ,
@@ -439,7 +443,7 @@ public static function folderHasSubPage(Folder $folder, string $title): int {
439
443
public function recurseFolder (Folder $ folder , string $ userId ): array {
440
444
// Find index page or create it if we have subpages, but it doesn't exist
441
445
try {
442
- $ indexPage = $ this ->getPageByFile ($ this ->getIndexPageFile ($ folder ));
446
+ $ indexPage = $ this ->getPageByFile ($ this ->getIndexPageFile ($ folder ), $ folder );
443
447
} catch (NotFoundException $ e ) {
444
448
if (!self ::folderHasSubPages ($ folder )) {
445
449
return [];
@@ -451,7 +455,7 @@ public function recurseFolder(Folder $folder, string $userId): array {
451
455
// Add subpages and recurse over subfolders
452
456
foreach ($ folder ->getDirectoryListing () as $ node ) {
453
457
if ($ node instanceof File && self ::isPage ($ node ) && !self ::isIndexPage ($ node )) {
454
- $ pageInfos [] = $ this ->getPageByFile ($ node );
458
+ $ pageInfos [] = $ this ->getPageByFile ($ node, $ folder );
455
459
} elseif ($ node instanceof Folder) {
456
460
array_push ($ pageInfos , ...$ this ->recurseFolder ($ node , $ userId ));
457
461
}
0 commit comments