Skip to content

Commit 06cb8d7

Browse files
committed
feat(get): support -L with --info, show revision ID
- Follow redirects when using --info -L - Add revision ID to metadata output - go fmt cleanup
1 parent 68303cc commit 06cb8d7

4 files changed

Lines changed: 60 additions & 18 deletions

File tree

api/client.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
)
1313

1414
const (
15-
userAgent = "wikitool/1.0 (https://github.com/teal-bauer/wikitool)"
16-
apiVersion = "v1"
17-
defaultLang = "en"
18-
maxRedirects = 10
19-
redirectPrefix = "#REDIRECT [["
20-
redirectSuffix = "]]"
15+
userAgent = "wikitool/1.0 (https://github.com/teal-bauer/wikitool)"
16+
apiVersion = "v1"
17+
defaultLang = "en"
18+
maxRedirects = 10
19+
redirectPrefix = "#REDIRECT [["
20+
redirectSuffix = "]]"
2121
)
2222

2323
// Client handles Wikipedia API requests
@@ -135,7 +135,8 @@ type Page struct {
135135
URL string `json:"url"`
136136
Title string `json:"title"`
137137
} `json:"license"`
138-
HTMLURL string `json:"html_url"`
138+
HTMLURL string `json:"html_url"`
139+
RedirectTarget string `json:"redirect_target,omitempty"`
139140
}
140141

141142
// SearchResult represents a search result
@@ -293,6 +294,35 @@ func (c *Client) GetPageInfo(title string) (*Page, error) {
293294
return &page, nil
294295
}
295296

297+
// GetPageInfoFollowRedirects fetches page metadata, following redirects.
298+
// Returns the final page and a slice of titles followed (empty if no redirects).
299+
func (c *Client) GetPageInfoFollowRedirects(title string) (*Page, []string, error) {
300+
var redirectChain []string
301+
currentTitle := title
302+
303+
for range maxRedirects {
304+
page, err := c.GetPageInfo(currentTitle)
305+
if err != nil {
306+
return nil, redirectChain, err
307+
}
308+
if page.RedirectTarget == "" {
309+
return page, redirectChain, nil
310+
}
311+
redirectChain = append(redirectChain, currentTitle)
312+
// Extract title from redirect_target URL like "/w/rest.php/v1/page/ISO%2FIEC_7064/bare?redirect=no"
313+
target := page.RedirectTarget
314+
if idx := strings.Index(target, "/page/"); idx != -1 {
315+
target = target[idx+6:]
316+
if end := strings.Index(target, "/"); end != -1 {
317+
target = target[:end]
318+
}
319+
target, _ = url.PathUnescape(target)
320+
}
321+
currentTitle = target
322+
}
323+
return nil, redirectChain, fmt.Errorf("too many redirects (max %d): %v", maxRedirects, redirectChain)
324+
}
325+
296326
// Search searches Wikipedia pages
297327
func (c *Client) Search(query string, limit int) (*SearchResponse, error) {
298328
endpoint := fmt.Sprintf("/search/page?q=%s&limit=%d", url.QueryEscape(query), limit)

api/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ func TestClientGet_ErrorHandling(t *testing.T) {
215215

216216
func TestParseRedirect(t *testing.T) {
217217
tests := []struct {
218-
name string
219-
source string
220-
wantTarget string
218+
name string
219+
source string
220+
wantTarget string
221221
wantIsRedir bool
222222
}{
223223
{

cmd/get.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
)
1212

1313
var (
14-
getHTML bool
15-
getInfo bool
16-
followRedirects bool
14+
getHTML bool
15+
getInfo bool
16+
followRedirects bool
1717
)
1818

1919
var getCmd = &cobra.Command{
@@ -72,6 +72,17 @@ func runGet(cmd *cobra.Command, args []string) error {
7272

7373
// Info/metadata only
7474
if getInfo {
75+
if followRedirects {
76+
page, redirects, err := client.GetPageInfoFollowRedirects(title)
77+
if err != nil {
78+
return err
79+
}
80+
if len(redirects) > 0 {
81+
fmt.Fprintf(cmd.ErrOrStderr(), "Followed %d redirect(s): %s -> %s\n",
82+
len(redirects), strings.Join(redirects, " -> "), page.Title)
83+
}
84+
return outputPage(page, false)
85+
}
7586
page, err := client.GetPageInfo(title)
7687
if err != nil {
7788
return err
@@ -85,8 +96,8 @@ func runGet(cmd *cobra.Command, args []string) error {
8596
if err != nil {
8697
return err
8798
}
88-
if len(redirects) > 0 && output == "text" {
89-
fmt.Fprintf(cmd.ErrOrStderr(), "Followed %d redirect(s): %s -> %s\n",
99+
if len(redirects) > 0 {
100+
fmt.Fprintf(cmd.ErrOrStderr(), "Followed %d redirect(s): %s -> %s\n\n",
90101
len(redirects), strings.Join(redirects, " -> "), page.Title)
91102
}
92103
return outputPage(page, true)
@@ -119,6 +130,7 @@ func outputPage(page *api.Page, includeSource bool) error {
119130
} else {
120131
fmt.Printf("Title: %s\n", page.Title)
121132
fmt.Printf("ID: %d\n", page.ID)
133+
fmt.Printf("Revision: %d\n", page.Latest.ID)
122134
fmt.Printf("Last modified: %s\n", page.Latest.Timestamp)
123135
fmt.Printf("Content model: %s\n", page.ContentModel)
124136
if page.License.Title != "" {

cmd/search.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
)
1010

1111
var (
12-
searchLimit int
12+
searchLimit int
1313
searchAutocomplete bool
14-
searchInTitle bool
15-
searchCategory string
14+
searchInTitle bool
15+
searchCategory string
1616
)
1717

1818
var searchCmd = &cobra.Command{

0 commit comments

Comments
 (0)