Skip to content

Commit ef7ba7d

Browse files
authored
Merge pull request #6485 from WoltLab/6.2-list-view-canonical-url
Set `canonicalURL` in `AbstractListViewPage`
2 parents b812cc5 + 44c46b3 commit ef7ba7d

2 files changed

Lines changed: 75 additions & 13 deletions

File tree

wcfsetup/install/files/lib/page/AbstractGridViewPage.class.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use wcf\system\request\LinkHandler;
77
use wcf\system\gridView\AbstractGridView;
88
use wcf\system\WCF;
9+
use wcf\util\StringUtil;
910

1011
/**
1112
* Abstract implementation of a page that is rendering a grid view.
@@ -49,6 +50,8 @@ public function readParameters()
4950
if (isset($_REQUEST['filters']) && \is_array($_REQUEST['filters'])) {
5051
$this->filters = $_REQUEST['filters'];
5152
}
53+
54+
$this->canonicalURL = $this->getCanonicalUrl();
5255
}
5356

5457
#[\Override]
@@ -66,6 +69,7 @@ public function assignVariables()
6669

6770
WCF::getTPL()->assign([
6871
'gridView' => $this->gridView,
72+
'headContent' => $this->getHeadContent(),
6973
]);
7074
}
7175

@@ -88,7 +92,65 @@ protected function initGridView(): void
8892
if ($this->pageNo != 1) {
8993
$this->gridView->setPageNo($this->pageNo);
9094
}
91-
$this->gridView->setBaseUrl(LinkHandler::getInstance()->getControllerLink(static::class));
95+
$this->gridView->setBaseUrl(
96+
LinkHandler::getInstance()->getControllerLink(static::class, $this->getBaseUrlParameters())
97+
);
98+
}
99+
100+
/**
101+
* @return array<string, mixed>
102+
*/
103+
protected function getBaseUrlParameters(): array
104+
{
105+
return [];
106+
}
107+
108+
protected function getHeadContent(): string
109+
{
110+
$linkTags = [];
111+
if ($this->gridView->getPageNo() < $this->gridView->countPages()) {
112+
$linkTags[] = \sprintf(
113+
'<link rel="next" href="%s">',
114+
StringUtil::encodeHTML(
115+
LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
116+
$this->getBaseUrlParameters(),
117+
[
118+
'pageNo' => $this->gridView->getPageNo() + 1,
119+
'sortField' => $this->sortField ?: null,
120+
'sortOrder' => $this->sortOrder ?: null,
121+
]
122+
))
123+
)
124+
);
125+
}
126+
127+
if ($this->gridView->getPageNo() > 1) {
128+
$linkTags[] = \sprintf(
129+
'<link rel="prev" href="%s">',
130+
StringUtil::encodeHTML(
131+
LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
132+
$this->getBaseUrlParameters(),
133+
[
134+
'pageNo' => $this->gridView->getPageNo() !== 2 ? $this->gridView->getPageNo() - 1 : null,
135+
'sortField' => $this->sortField ?: null,
136+
'sortOrder' => $this->sortOrder ?: null,
137+
]
138+
))
139+
)
140+
);
141+
}
142+
143+
return \implode("\n", $linkTags);
144+
}
145+
146+
protected function getCanonicalUrl(): string
147+
{
148+
return LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
149+
$this->getBaseUrlParameters(),
150+
[
151+
'pageNo' => $this->pageNo !== 1 ? $this->pageNo : null,
152+
]
153+
));
92154
}
93155

94156
/**

wcfsetup/install/files/lib/page/AbstractListViewPage.class.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function readParameters()
5151
if (isset($_REQUEST['filters']) && \is_array($_REQUEST['filters'])) {
5252
$this->filters = $_REQUEST['filters'];
5353
}
54+
55+
$this->canonicalURL = $this->getCanonicalUrl();
5456
}
5557

5658
#[\Override]
@@ -139,21 +141,19 @@ protected function getHeadContent(): string
139141
);
140142
}
141143

142-
$linkTags[] = \sprintf(
143-
'<link rel="canonical" href="%s">',
144-
StringUtil::encodeHTML(
145-
LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
146-
$this->getBaseUrlParameters(),
147-
[
148-
'pageNo' => $this->listView->getPageNo() !== 1 ? $this->listView->getPageNo() : null,
149-
]
150-
))
151-
)
152-
);
153-
154144
return \implode("\n", $linkTags);
155145
}
156146

147+
protected function getCanonicalUrl(): string
148+
{
149+
return LinkHandler::getInstance()->getControllerLink(static::class, \array_merge(
150+
$this->getBaseUrlParameters(),
151+
[
152+
'pageNo' => $this->pageNo !== 1 ? $this->pageNo : null,
153+
]
154+
));
155+
}
156+
157157
/**
158158
* Returns the list view instance for the rendering of this page.
159159
*

0 commit comments

Comments
 (0)