2 # Page inlining and blogging.
3 package IkiWiki
::Plugin
::inline
;
17 hook
(type
=> "getopt", id
=> "inline", call
=> \
&getopt
);
18 hook
(type
=> "getsetup", id
=> "inline", call
=> \
&getsetup
);
19 hook
(type
=> "checkconfig", id
=> "inline", call
=> \
&checkconfig
);
20 hook
(type
=> "sessioncgi", id
=> "inline", call
=> \
&sessioncgi
);
21 hook
(type
=> "preprocess", id
=> "inline",
22 call
=> \
&IkiWiki
::preprocess_inline
);
23 hook
(type
=> "pagetemplate", id
=> "inline",
24 call
=> \
&IkiWiki
::pagetemplate_inline
);
25 hook
(type
=> "format", id
=> "inline", call
=> \
&format
, first
=> 1);
26 # Hook to change to do pinging since it's called late.
27 # This ensures each page only pings once and prevents slow
28 # pings interrupting page builds.
29 hook
(type
=> "change", id
=> "inline", call
=> \
&IkiWiki
::pingurl
);
33 eval q{use Getopt::Long};
35 Getopt
::Long
::Configure
('pass_through');
37 "rss!" => \
$config{rss
},
38 "atom!" => \
$config{atom
},
39 "allowrss!" => \
$config{allowrss
},
40 "allowatom!" => \
$config{allowatom
},
42 push @
{$config{pingurl
}}, $_[1];
57 description
=> "enable rss feeds by default?",
64 description
=> "enable atom feeds by default?",
71 description
=> "allow rss feeds to be used?",
78 description
=> "allow atom feeds to be used?",
84 example
=> "http://rpc.technorati.com/rpc/ping",
85 description
=> "urls to ping (using XML-RPC) on feed update",
92 if (($config{rss
} || $config{atom
}) && ! length $config{url
}) {
93 error
(gettext
("Must specify url to wiki with --url when using --rss or --atom"));
96 push @
{$config{wiki_file_prune_regexps
}}, qr/\.rss$/;
99 push @
{$config{wiki_file_prune_regexps
}}, qr/\.atom$/;
101 if (! exists $config{pingurl
}) {
109 # Fill in the inline content generated earlier. This is actually an
111 $params{content
}=~s
{<div
class="inline" id
="([^"]+)"></div>}{
114 return $params{content};
117 sub sessioncgi ($$) {
121 if ($q->param('do') eq 'blog') {
122 my $page=titlepage(decode_utf8($q->param('title')));
123 $page=~s/(\/)/"__
".ord($1)."__
"/eg; # don't create subdirs
124 # if the page already exists, munge it to be unique
125 my $from=$q->param('from');
127 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
128 $add=1 unless length $add;
131 $q->param('page', "/$from/$page$add");
132 # now go create the page
133 $q->param('do', 'create');
134 # make sure the editpage plugin is loaded
135 if (IkiWiki->can("cgi_editpage
")) {
136 IkiWiki::cgi_editpage($q, $session);
139 error(gettext("page editing
not allowed
"));
145 # Back to ikiwiki namespace for the rest, this code is very much
146 # internal to ikiwiki even though it's separated into a plugin.
152 sub preprocess_inline (@) {
155 if (! exists $params{pages} && ! exists $params{pagenames}) {
156 error gettext("missing pages parameter
");
158 my $raw=yesno($params{raw});
159 my $archive=yesno($params{archive});
160 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
161 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
162 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
163 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick && ! $raw;
164 my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
165 my $feedonly=yesno($params{feedonly});
166 if (! exists $params{show} && ! $archive) {
169 if (! exists $params{feedshow} && exists $params{show}) {
170 $params{feedshow}=$params{show};
173 if (exists $params{description}) {
174 $desc = $params{description}
177 $desc = $config{wikiname};
179 my $actions=yesno($params{actions});
180 if (exists $params{template}) {
181 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
184 $params{template} = $archive ? "archivepage
" : "inlinepage
";
189 if (exists $params{pagenames}) {
190 foreach my $p (qw(sort pages)) {
191 if (exists $params{$p}) {
192 error
sprintf(gettext
("the %s and %s parameters cannot be used together"),
197 @list = map { bestlink
($params{page
}, $_) }
198 split ' ', $params{pagenames
};
200 if (yesno
($params{reverse})) {
201 @list=reverse(@list);
204 foreach my $p (@list) {
205 add_depends
($params{page
}, $p, deptype
($quick ?
"presence" : "content"));
213 if ($params{feedshow
} && $num < $params{feedshow
} && $num > 0) {
214 $num=$params{feedshow
};
216 if ($params{skip
} && $num) {
220 @list = pagespec_match_list
($params{page
}, $params{pages
},
221 deptype
=> deptype
($quick ?
"presence" : "content"),
222 filter
=> sub { $_[0] eq $params{page
} },
223 sort => exists $params{sort} ?
$params{sort} : "age",
224 reverse => yesno
($params{reverse}),
225 ($num ?
(num
=> $num) : ()),
229 if (exists $params{skip
}) {
230 @list=@list[$params{skip
} .. $#list];
235 if (exists $params{feedshow
} &&
236 $params{feedshow
} && @list > $params{feedshow
}) {
237 @feedlist=@list[0..$params{feedshow
} - 1];
244 if ($params{show
} && @list > $params{show
}) {
245 @list=@list[0..$params{show
} - 1];
248 if ($feeds && exists $params{feedpages
}) {
249 @feedlist = pagespec_match_list
(
250 $params{page
}, "($params{pages}) and ($params{feedpages})",
251 deptype
=> deptype
($quick ?
"presence" : "content"),
256 my ($feedbase, $feednum);
258 # Ensure that multiple feeds on a page go to unique files.
260 # Feedfile can lead to conflicts if usedirs is not enabled,
261 # so avoid supporting it in that case.
262 delete $params{feedfile
} if ! $config{usedirs
};
263 # Tight limits on legal feedfiles, to avoid security issues
265 if (defined $params{feedfile
}) {
266 if ($params{feedfile
} =~ /\// ||
267 $params{feedfile
} !~ /$config{wiki_file_regexp}/) {
268 error
("illegal feedfile");
270 $params{feedfile
}=possibly_foolish_untaint
($params{feedfile
});
272 $feedbase=targetpage
($params{page
}, "", $params{feedfile
});
274 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
275 if (exists $knownfeeds{$feedid}) {
276 $feednum=$knownfeeds{$feedid};
279 if (exists $page_numfeeds{$params{destpage
}}{$feedbase}) {
281 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage
}}{$feedbase};
285 $feednum=$knownfeeds{$feedid}="";
287 $page_numfeeds{$params{destpage
}}{$feedbase}=1;
293 my ($rssurl, $atomurl, $rssdesc, $atomdesc);
296 $rssurl=abs2rel
($feedbase."rss".$feednum, dirname
(htmlpage
($params{destpage
})));
297 $rssdesc = sprintf(gettext
("%s (RSS feed)"), $desc);
300 $atomurl=abs2rel
($feedbase."atom".$feednum, dirname
(htmlpage
($params{destpage
})));
301 $atomdesc = sprintf(gettext
("%s (Atom feed)"), $desc);
307 if (length $config{cgiurl
} && ! $params{preview
} && (exists $params{rootpage
} ||
308 (exists $params{postform
} && yesno
($params{postform
}))) &&
309 IkiWiki
->can("cgi_editpage")) {
310 # Add a blog post form, with feed buttons.
311 my $formtemplate=template_depends
("blogpost.tmpl", $params{page
}, blind_cache
=> 1);
312 $formtemplate->param(cgiurl
=> IkiWiki
::cgiurl
());
313 $formtemplate->param(rootpage
=> rootpage
(%params));
316 $formtemplate->param(rssurl
=> $rssurl);
317 $formtemplate->param(rssdesc
=> $rssdesc);
320 $formtemplate->param(atomurl
=> $atomurl);
321 $formtemplate->param(atomdesc
=> $atomdesc);
324 if (exists $params{postformtext
}) {
325 $formtemplate->param(postformtext
=>
326 $params{postformtext
});
329 $formtemplate->param(postformtext
=>
330 gettext
("Add a new post titled:"));
332 if (exists $params{id
}) {
333 $formtemplate->param(postformid
=>
336 $ret.=$formtemplate->output;
338 # The post form includes the feed buttons, so
339 # emptyfeeds cannot be hidden.
342 elsif ($feeds && !$params{preview
} && ($emptyfeeds || @feedlist)) {
344 my $linktemplate=template_depends
("feedlink.tmpl", $params{page
}, blind_cache
=> 1);
346 $linktemplate->param(rssurl
=> $rssurl);
347 $linktemplate->param(rssdesc
=> $rssdesc);
350 $linktemplate->param(atomurl
=> $atomurl);
351 $linktemplate->param(atomdesc
=> $atomdesc);
353 if (exists $params{id
}) {
354 $linktemplate->param(id
=> $params{id
});
356 $ret.=$linktemplate->output;
362 # cannot use wiki pages as templates; template not sanitized due to
365 $template=template_depends
($params{template
}.".tmpl", $params{page
},
369 error
sprintf(gettext
("failed to process template %s"), $params{template
}.".tmpl").": $@";
372 my $needcontent=$raw || (!($archive && $quick) && $template->query(name
=> 'content'));
374 foreach my $page (@list) {
375 my $file = $pagesources{$page};
376 my $type = pagetype
($file);
379 # Get the content before populating the
380 # template, since getting the content uses
381 # the same template if inlines are nested.
382 my $content=get_inline_content
($page, $params{destpage
});
383 $template->param(content
=> $content);
385 $template->param(pageurl
=> urlto
($page, $params{destpage
}));
386 $template->param(inlinepage
=> $page);
387 $template->param(title
=> pagetitle
(basename
($page)));
388 $template->param(ctime
=> displaytime
($pagectime{$page}, $params{timeformat
}, 1));
389 $template->param(mtime
=> displaytime
($pagemtime{$page}, $params{timeformat
}));
390 $template->param(first
=> 1) if $page eq $list[0];
391 $template->param(last => 1) if $page eq $list[$#list];
392 $template->param(html5
=> $config{html5
});
395 my $file = $pagesources{$page};
396 my $type = pagetype
($file);
397 if ($config{discussion
}) {
398 if ($page !~ /.*\/\Q
$config{discussionpage
}\E
$/i
&&
399 (length $config{cgiurl
} ||
400 exists $pagesources{$page."/".lc($config{discussionpage
})})) {
401 $template->param(have_actions
=> 1);
402 $template->param(discussionlink
=>
405 $config{discussionpage
},
410 if (length $config{cgiurl
} &&
412 IkiWiki
->can("cgi_editpage")) {
413 $template->param(have_actions
=> 1);
414 $template->param(editurl
=> cgiurl
(do => "edit", page
=> $page));
419 run_hooks
(pagetemplate
=> sub {
420 shift->(page
=> $page, destpage
=> $params{destpage
},
421 template
=> $template,);
424 $ret.=$template->output;
425 $template->clear_params;
430 linkify
($page, $params{destpage
},
431 preprocess
($page, $params{destpage
},
432 filter
($page, $params{destpage
},
433 readfile
(srcfile
($file)))));
437 readfile
(srcfile
($file));
443 if ($feeds && ($emptyfeeds || @feedlist)) {
445 my $rssp=$feedbase."rss".$feednum;
446 will_render
($params{destpage
}, $rssp);
447 if (! $params{preview
}) {
448 writefile
($rssp, $config{destdir
},
450 $config{url
}."/".$rssp, $desc, $params{guid
}, $params{page
}, @feedlist));
451 $toping{$params{destpage
}}=1 unless $config{rebuild
};
452 $feedlinks{$params{destpage
}}.=qq{<link rel
="alternate" type
="application/rss+xml" title
="$rssdesc" href
="$rssurl" />};
456 my $atomp=$feedbase."atom".$feednum;
457 will_render
($params{destpage
}, $atomp);
458 if (! $params{preview
}) {
459 writefile
($atomp, $config{destdir
},
460 genfeed
("atom", $config{url
}."/".$atomp, $desc, $params{guid
}, $params{page
}, @feedlist));
461 $toping{$params{destpage
}}=1 unless $config{rebuild
};
462 $feedlinks{$params{destpage
}}.=qq{<link rel
="alternate" type
="application/atom+xml" title
="$atomdesc" href
="$atomurl" />};
467 clear_inline_content_cache
();
469 return $ret if $raw || $nested;
471 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
474 sub pagetemplate_inline
(@
) {
476 my $page=$params{page
};
477 my $template=$params{template
};
479 $template->param(feedlinks
=> $feedlinks{$page})
480 if exists $feedlinks{$page} && $template->query(name
=> "feedlinks");
485 my $cached_destpage="";
487 sub get_inline_content
($$) {
491 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
492 return $inline_content{$page};
495 my $file=$pagesources{$page};
496 my $type=pagetype
($file);
500 $ret=htmlize
($page, $destpage, $type,
501 linkify
($page, $destpage,
502 preprocess
($page, $destpage,
503 filter
($page, $destpage,
504 readfile
(srcfile
($file))))));
506 if (isinternal
($page)) {
507 # make inlined text of internal pages searchable
508 run_hooks
(indexhtml
=> sub {
509 shift->(page
=> $page, destpage
=> $page,
515 if ($cached_destpage ne $destpage) {
516 clear_inline_content_cache
();
517 $cached_destpage=$destpage;
519 return $inline_content{$page}=$ret;
522 sub clear_inline_content_cache
() {
531 my $lc_time=POSIX
::setlocale
(&POSIX
::LC_TIME
);
532 POSIX
::setlocale
(&POSIX
::LC_TIME
, "C");
533 my $ret=POSIX
::strftime
("%a, %d %b %Y %H:%M:%S %z", localtime($time));
534 POSIX
::setlocale
(&POSIX
::LC_TIME
, $lc_time);
538 sub absolute_urls
($$) {
539 # needed because rss sucks
545 my $urltop; # calculated if needed
549 eval q{use HTML::Parser; use HTML::Tagset};
551 my $p = HTML
::Parser
->new(api_version
=> 3);
552 $p->handler(default => sub { $ret.=join("", @_) }, "text");
553 $p->handler(start
=> sub {
554 my ($tagname, $pos, $text) = @_;
555 if (ref $HTML::Tagset
::linkElements
{$tagname}) {
557 # use attribute sets from right to left
558 # to avoid invalidating the offsets
559 # when replacing the values
560 my ($k_offset, $k_len, $v_offset, $v_len) =
562 my $attrname = lc(substr($text, $k_offset, $k_len));
563 next unless grep { $_ eq $attrname } @
{$HTML::Tagset
::linkElements
{$tagname}};
564 next unless $v_offset; # 0 v_offset means no value
565 my $v = substr($text, $v_offset, $v_len);
566 $v =~ s/^([\'\"])(.*)\1$/$2/;
568 $v=$baseurl.$v; # anchor
570 elsif ($v=~/^(?!\w+:)[^\/]/) {
571 $v=$url.$v; # relative url
574 if (! defined $urltop) {
575 # what is the non path part of the url?
576 my $top_uri = URI
->new($url);
577 $top_uri->path_query(""); # reset the path
578 $urltop = $top_uri->as_string;
580 $v=$urltop.$v; # url relative to top of site
582 $v =~ s/\"/"/g; # since we quote with ""
583 substr($text, $v_offset, $v_len) = qq("$v");
587 }, "tagname, tokenpos, text");
594 sub genfeed
($$$$$@
) {
602 my $url=URI
->new(encode_utf8
(urlto
($page,"",1)));
604 my $itemtemplate=template_depends
($feedtype."item.tmpl", $page, blind_cache
=> 1);
607 foreach my $p (@pages) {
608 my $u=URI
->new(encode_utf8
(urlto
($p, "", 1)));
609 my $pcontent = absolute_urls
(get_inline_content
($p, $page), $url);
611 $itemtemplate->param(
612 title
=> pagetitle
(basename
($p)),
615 cdate_822
=> date_822
($pagectime{$p}),
616 mdate_822
=> date_822
($pagemtime{$p}),
617 cdate_3339
=> date_3339
($pagectime{$p}),
618 mdate_3339
=> date_3339
($pagemtime{$p}),
621 if (exists $pagestate{$p}) {
622 if (exists $pagestate{$p}{meta
}{guid
}) {
623 eval q{use HTML::Entities};
624 $itemtemplate->param(guid
=> HTML
::Entities
::encode_numeric
($pagestate{$p}{meta
}{guid
}));
627 if (exists $pagestate{$p}{meta
}{updated
}) {
628 $itemtemplate->param(mdate_822
=> date_822
($pagestate{$p}{meta
}{updated
}));
629 $itemtemplate->param(mdate_3339
=> date_3339
($pagestate{$p}{meta
}{updated
}));
633 if ($itemtemplate->query(name
=> "enclosure")) {
634 my $file=$pagesources{$p};
635 my $type=pagetype
($file);
637 $itemtemplate->param(content
=> $pcontent);
640 my $size=(srcfile_stat
($file))[8];
642 eval q{use File::MimeInfo};
644 $mime = mimetype
($file);
646 $itemtemplate->param(
654 $itemtemplate->param(content
=> $pcontent);
657 run_hooks
(pagetemplate
=> sub {
658 shift->(page
=> $p, destpage
=> $page,
659 template
=> $itemtemplate);
662 $content.=$itemtemplate->output;
663 $itemtemplate->clear_params;
665 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
668 my $template=template_depends
($feedtype."page.tmpl", $page, blind_cache
=> 1);
670 title
=> $page ne "index" ? pagetitle
($page) : $config{wikiname
},
671 wikiname
=> $config{wikiname
},
674 feeddesc
=> $feeddesc,
676 feeddate
=> date_3339
($lasttime),
678 version
=> $IkiWiki::version
,
680 run_hooks
(pagetemplate
=> sub {
681 shift->(page
=> $page, destpage
=> $page,
682 template
=> $template);
685 return $template->output;
689 return unless @
{$config{pingurl
}} && %toping;
691 eval q{require RPC::XML::Client};
693 debug
(gettext
("RPC::XML::Client not found, not pinging"));
697 # daemonize here so slow pings don't slow down wiki updates
698 defined(my $pid = fork) or error
("Can't fork: $!");
701 POSIX
::setsid
() or error
("Can't start a new session: $!");
702 open STDIN
, '/dev/null';
703 open STDOUT
, '>/dev/null';
704 open STDERR
, '>&STDOUT' or error
("Can't dup stdout: $!");
706 # Don't need to keep a lock on the wiki as a daemon.
707 IkiWiki
::unlockwiki
();
709 foreach my $page (keys %toping) {
710 my $title=pagetitle
(basename
($page), 0);
711 my $url=urlto
($page, "", 1);
712 foreach my $pingurl (@
{$config{pingurl
}}) {
713 debug
("Pinging $pingurl for $page");
715 my $client = RPC
::XML
::Client
->new($pingurl);
716 my $req = RPC
::XML
::request
->new('weblogUpdates.ping',
718 my $res = $client->send_request($req);
720 error
("Did not receive response to ping");
723 if (! exists $r->{flerror
} || $r->{flerror
}) {
724 error
("Ping rejected: ".(exists $r->{message
} ?
$r->{message
} : "[unknown reason]"));
728 error
"Ping failed: $@";
733 exit 0; # daemon done
741 if (exists $params{rootpage
}) {
742 $rootpage=bestlink
($params{page
}, $params{rootpage
});
743 if (!length $rootpage) {
744 $rootpage=$params{rootpage
};
748 $rootpage=$params{page
};