You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IkiWiki.pm 61KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Encode;
  6. use URI::Escape q{uri_escape_utf8};
  7. use POSIX ();
  8. use Storable;
  9. use open qw{:utf8 :std};
  10. use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
  11. %pagestate %wikistate %renderedfiles %oldrenderedfiles
  12. %pagesources %delpagesources %destsources %depends %depends_simple
  13. @mass_depends %hooks %forcerebuild %loaded_plugins %typedlinks
  14. %oldtypedlinks %autofiles};
  15. use Exporter q{import};
  16. our @EXPORT = qw(hook debug error htmlpage template template_depends
  17. deptype add_depends pagespec_match pagespec_match_list bestlink
  18. htmllink readfile writefile pagetype srcfile pagename
  19. displaytime will_render gettext ngettext urlto targetpage
  20. add_underlay pagetitle titlepage linkpage newpagefile
  21. inject add_link add_autofile
  22. %config %links %pagestate %wikistate %renderedfiles
  23. %pagesources %destsources %typedlinks);
  24. our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
  25. our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
  26. our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
  27. # Page dependency types.
  28. our $DEPEND_CONTENT=1;
  29. our $DEPEND_PRESENCE=2;
  30. our $DEPEND_LINKS=4;
  31. # Optimisation.
  32. use Memoize;
  33. memoize("abs2rel");
  34. memoize("sortspec_translate");
  35. memoize("pagespec_translate");
  36. memoize("template_file");
  37. sub getsetup () {
  38. wikiname => {
  39. type => "string",
  40. default => "wiki",
  41. description => "name of the wiki",
  42. safe => 1,
  43. rebuild => 1,
  44. },
  45. adminemail => {
  46. type => "string",
  47. default => undef,
  48. example => 'me@example.com',
  49. description => "contact email for wiki",
  50. safe => 1,
  51. rebuild => 0,
  52. },
  53. adminuser => {
  54. type => "string",
  55. default => [],
  56. description => "users who are wiki admins",
  57. safe => 1,
  58. rebuild => 0,
  59. },
  60. banned_users => {
  61. type => "string",
  62. default => [],
  63. description => "users who are banned from the wiki",
  64. safe => 1,
  65. rebuild => 0,
  66. },
  67. srcdir => {
  68. type => "string",
  69. default => undef,
  70. example => "$ENV{HOME}/wiki",
  71. description => "where the source of the wiki is located",
  72. safe => 0, # path
  73. rebuild => 1,
  74. },
  75. destdir => {
  76. type => "string",
  77. default => undef,
  78. example => "/var/www/wiki",
  79. description => "where to build the wiki",
  80. safe => 0, # path
  81. rebuild => 1,
  82. },
  83. url => {
  84. type => "string",
  85. default => '',
  86. example => "http://example.com/wiki",
  87. description => "base url to the wiki",
  88. safe => 1,
  89. rebuild => 1,
  90. },
  91. cgiurl => {
  92. type => "string",
  93. default => '',
  94. example => "http://example.com/wiki/ikiwiki.cgi",
  95. description => "url to the ikiwiki.cgi",
  96. safe => 1,
  97. rebuild => 1,
  98. },
  99. cgi_wrapper => {
  100. type => "string",
  101. default => '',
  102. example => "/var/www/wiki/ikiwiki.cgi",
  103. description => "filename of cgi wrapper to generate",
  104. safe => 0, # file
  105. rebuild => 0,
  106. },
  107. cgi_wrappermode => {
  108. type => "string",
  109. default => '06755',
  110. description => "mode for cgi_wrapper (can safely be made suid)",
  111. safe => 0,
  112. rebuild => 0,
  113. },
  114. rcs => {
  115. type => "string",
  116. default => '',
  117. description => "rcs backend to use",
  118. safe => 0, # don't allow overriding
  119. rebuild => 0,
  120. },
  121. default_plugins => {
  122. type => "internal",
  123. default => [qw{mdwn link inline meta htmlscrubber passwordauth
  124. openid signinedit lockedit conditional
  125. recentchanges parentlinks editpage}],
  126. description => "plugins to enable by default",
  127. safe => 0,
  128. rebuild => 1,
  129. },
  130. add_plugins => {
  131. type => "string",
  132. default => [],
  133. description => "plugins to add to the default configuration",
  134. safe => 1,
  135. rebuild => 1,
  136. },
  137. disable_plugins => {
  138. type => "string",
  139. default => [],
  140. description => "plugins to disable",
  141. safe => 1,
  142. rebuild => 1,
  143. },
  144. templatedir => {
  145. type => "string",
  146. default => "$installdir/share/ikiwiki/templates",
  147. description => "additional directory to search for template files",
  148. advanced => 1,
  149. safe => 0, # path
  150. rebuild => 1,
  151. },
  152. underlaydir => {
  153. type => "string",
  154. default => "$installdir/share/ikiwiki/basewiki",
  155. description => "base wiki source location",
  156. advanced => 1,
  157. safe => 0, # path
  158. rebuild => 0,
  159. },
  160. underlaydirbase => {
  161. type => "internal",
  162. default => "$installdir/share/ikiwiki",
  163. description => "parent directory containing additional underlays",
  164. safe => 0,
  165. rebuild => 0,
  166. },
  167. wrappers => {
  168. type => "internal",
  169. default => [],
  170. description => "wrappers to generate",
  171. safe => 0,
  172. rebuild => 0,
  173. },
  174. underlaydirs => {
  175. type => "internal",
  176. default => [],
  177. description => "additional underlays to use",
  178. safe => 0,
  179. rebuild => 0,
  180. },
  181. verbose => {
  182. type => "boolean",
  183. example => 1,
  184. description => "display verbose messages?",
  185. safe => 1,
  186. rebuild => 0,
  187. },
  188. syslog => {
  189. type => "boolean",
  190. example => 1,
  191. description => "log to syslog?",
  192. safe => 1,
  193. rebuild => 0,
  194. },
  195. usedirs => {
  196. type => "boolean",
  197. default => 1,
  198. description => "create output files named page/index.html?",
  199. safe => 0, # changing requires manual transition
  200. rebuild => 1,
  201. },
  202. prefix_directives => {
  203. type => "boolean",
  204. default => 1,
  205. description => "use '!'-prefixed preprocessor directives?",
  206. safe => 0, # changing requires manual transition
  207. rebuild => 1,
  208. },
  209. indexpages => {
  210. type => "boolean",
  211. default => 0,
  212. description => "use page/index.mdwn source files",
  213. safe => 1,
  214. rebuild => 1,
  215. },
  216. discussion => {
  217. type => "boolean",
  218. default => 1,
  219. description => "enable Discussion pages?",
  220. safe => 1,
  221. rebuild => 1,
  222. },
  223. discussionpage => {
  224. type => "string",
  225. default => gettext("Discussion"),
  226. description => "name of Discussion pages",
  227. safe => 1,
  228. rebuild => 1,
  229. },
  230. html5 => {
  231. type => "boolean",
  232. default => 0,
  233. description => "generate HTML5?",
  234. advanced => 0,
  235. safe => 1,
  236. rebuild => 1,
  237. },
  238. sslcookie => {
  239. type => "boolean",
  240. default => 0,
  241. description => "only send cookies over SSL connections?",
  242. advanced => 1,
  243. safe => 1,
  244. rebuild => 0,
  245. },
  246. default_pageext => {
  247. type => "string",
  248. default => "mdwn",
  249. description => "extension to use for new pages",
  250. safe => 0, # not sanitized
  251. rebuild => 0,
  252. },
  253. htmlext => {
  254. type => "string",
  255. default => "html",
  256. description => "extension to use for html files",
  257. safe => 0, # not sanitized
  258. rebuild => 1,
  259. },
  260. timeformat => {
  261. type => "string",
  262. default => '%c',
  263. description => "strftime format string to display date",
  264. advanced => 1,
  265. safe => 1,
  266. rebuild => 1,
  267. },
  268. locale => {
  269. type => "string",
  270. default => undef,
  271. example => "en_US.UTF-8",
  272. description => "UTF-8 locale to use",
  273. advanced => 1,
  274. safe => 0,
  275. rebuild => 1,
  276. },
  277. userdir => {
  278. type => "string",
  279. default => "",
  280. example => "users",
  281. description => "put user pages below specified page",
  282. safe => 1,
  283. rebuild => 1,
  284. },
  285. numbacklinks => {
  286. type => "integer",
  287. default => 10,
  288. description => "how many backlinks to show before hiding excess (0 to show all)",
  289. safe => 1,
  290. rebuild => 1,
  291. },
  292. hardlink => {
  293. type => "boolean",
  294. default => 0,
  295. description => "attempt to hardlink source files? (optimisation for large files)",
  296. advanced => 1,
  297. safe => 0, # paranoia
  298. rebuild => 0,
  299. },
  300. umask => {
  301. type => "integer",
  302. example => "022",
  303. description => "force ikiwiki to use a particular umask",
  304. advanced => 1,
  305. safe => 0, # paranoia
  306. rebuild => 0,
  307. },
  308. wrappergroup => {
  309. type => "string",
  310. example => "ikiwiki",
  311. description => "group for wrappers to run in",
  312. advanced => 1,
  313. safe => 0, # paranoia
  314. rebuild => 0,
  315. },
  316. libdir => {
  317. type => "string",
  318. default => "",
  319. example => "$ENV{HOME}/.ikiwiki/",
  320. description => "extra library and plugin directory",
  321. advanced => 1,
  322. safe => 0, # directory
  323. rebuild => 0,
  324. },
  325. ENV => {
  326. type => "string",
  327. default => {},
  328. description => "environment variables",
  329. safe => 0, # paranoia
  330. rebuild => 0,
  331. },
  332. timezone => {
  333. type => "string",
  334. default => "",
  335. example => "US/Eastern",
  336. description => "time zone name",
  337. safe => 1,
  338. rebuild => 1,
  339. },
  340. include => {
  341. type => "string",
  342. default => undef,
  343. example => '^\.htaccess$',
  344. description => "regexp of normally excluded files to include",
  345. advanced => 1,
  346. safe => 0, # regexp
  347. rebuild => 1,
  348. },
  349. exclude => {
  350. type => "string",
  351. default => undef,
  352. example => '^(*\.private|Makefile)$',
  353. description => "regexp of files that should be skipped",
  354. advanced => 1,
  355. safe => 0, # regexp
  356. rebuild => 1,
  357. },
  358. wiki_file_prune_regexps => {
  359. type => "internal",
  360. default => [qr/(^|\/)\.\.(\/|$)/, qr/^\//, qr/^\./, qr/\/\./,
  361. qr/\.x?html?$/, qr/\.ikiwiki-new$/,
  362. qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
  363. qr/(^|\/)_MTN\//, qr/(^|\/)_darcs\//,
  364. qr/(^|\/)CVS\//, qr/\.dpkg-tmp$/],
  365. description => "regexps of source files to ignore",
  366. safe => 0,
  367. rebuild => 1,
  368. },
  369. wiki_file_chars => {
  370. type => "string",
  371. description => "specifies the characters that are allowed in source filenames",
  372. default => "-[:alnum:]+/.:_",
  373. safe => 0,
  374. rebuild => 1,
  375. },
  376. wiki_file_regexp => {
  377. type => "internal",
  378. description => "regexp of legal source files",
  379. safe => 0,
  380. rebuild => 1,
  381. },
  382. web_commit_regexp => {
  383. type => "internal",
  384. default => qr/^web commit (by (.*?(?=: |$))|from ([0-9a-fA-F:.]+[0-9a-fA-F])):?(.*)/,
  385. description => "regexp to parse web commits from logs",
  386. safe => 0,
  387. rebuild => 0,
  388. },
  389. cgi => {
  390. type => "internal",
  391. default => 0,
  392. description => "run as a cgi",
  393. safe => 0,
  394. rebuild => 0,
  395. },
  396. cgi_disable_uploads => {
  397. type => "internal",
  398. default => 1,
  399. description => "whether CGI should accept file uploads",
  400. safe => 0,
  401. rebuild => 0,
  402. },
  403. post_commit => {
  404. type => "internal",
  405. default => 0,
  406. description => "run as a post-commit hook",
  407. safe => 0,
  408. rebuild => 0,
  409. },
  410. rebuild => {
  411. type => "internal",
  412. default => 0,
  413. description => "running in rebuild mode",
  414. safe => 0,
  415. rebuild => 0,
  416. },
  417. setup => {
  418. type => "internal",
  419. default => undef,
  420. description => "running in setup mode",
  421. safe => 0,
  422. rebuild => 0,
  423. },
  424. clean => {
  425. type => "internal",
  426. default => 0,
  427. description => "running in clean mode",
  428. safe => 0,
  429. rebuild => 0,
  430. },
  431. refresh => {
  432. type => "internal",
  433. default => 0,
  434. description => "running in refresh mode",
  435. safe => 0,
  436. rebuild => 0,
  437. },
  438. test_receive => {
  439. type => "internal",
  440. default => 0,
  441. description => "running in receive test mode",
  442. safe => 0,
  443. rebuild => 0,
  444. },
  445. wrapper_background_command => {
  446. type => "internal",
  447. default => '',
  448. description => "background shell command to run",
  449. safe => 0,
  450. rebuild => 0,
  451. },
  452. gettime => {
  453. type => "internal",
  454. description => "running in gettime mode",
  455. safe => 0,
  456. rebuild => 0,
  457. },
  458. w3mmode => {
  459. type => "internal",
  460. default => 0,
  461. description => "running in w3mmode",
  462. safe => 0,
  463. rebuild => 0,
  464. },
  465. wikistatedir => {
  466. type => "internal",
  467. default => undef,
  468. description => "path to the .ikiwiki directory holding ikiwiki state",
  469. safe => 0,
  470. rebuild => 0,
  471. },
  472. setupfile => {
  473. type => "internal",
  474. default => undef,
  475. description => "path to setup file",
  476. safe => 0,
  477. rebuild => 0,
  478. },
  479. setuptype => {
  480. type => "internal",
  481. default => "Yaml",
  482. description => "perl class to use to dump setup file",
  483. safe => 0,
  484. rebuild => 0,
  485. },
  486. allow_symlinks_before_srcdir => {
  487. type => "boolean",
  488. default => 0,
  489. description => "allow symlinks in the path leading to the srcdir (potentially insecure)",
  490. safe => 0,
  491. rebuild => 0,
  492. },
  493. }
  494. sub defaultconfig () {
  495. my %s=getsetup();
  496. my @ret;
  497. foreach my $key (keys %s) {
  498. push @ret, $key, $s{$key}->{default};
  499. }
  500. return @ret;
  501. }
  502. # URL to top of wiki as a path starting with /, valid from any wiki page or
  503. # the CGI; if that's not possible, an absolute URL. Either way, it ends with /
  504. my $local_url;
  505. # URL to CGI script, similar to $local_url
  506. my $local_cgiurl;
  507. sub checkconfig () {
  508. # locale stuff; avoid LC_ALL since it overrides everything
  509. if (defined $ENV{LC_ALL}) {
  510. $ENV{LANG} = $ENV{LC_ALL};
  511. delete $ENV{LC_ALL};
  512. }
  513. if (defined $config{locale}) {
  514. if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
  515. $ENV{LANG}=$config{locale};
  516. define_gettext();
  517. }
  518. }
  519. if (! defined $config{wiki_file_regexp}) {
  520. $config{wiki_file_regexp}=qr/(^[$config{wiki_file_chars}]+$)/;
  521. }
  522. if (ref $config{ENV} eq 'HASH') {
  523. foreach my $val (keys %{$config{ENV}}) {
  524. $ENV{$val}=$config{ENV}{$val};
  525. }
  526. }
  527. if (defined $config{timezone} && length $config{timezone}) {
  528. $ENV{TZ}=$config{timezone};
  529. }
  530. else {
  531. $config{timezone}=$ENV{TZ};
  532. }
  533. if ($config{w3mmode}) {
  534. eval q{use Cwd q{abs_path}};
  535. error($@) if $@;
  536. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  537. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  538. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  539. unless $config{cgiurl} =~ m!file:///!;
  540. $config{url}="file://".$config{destdir};
  541. }
  542. if ($config{cgi} && ! length $config{url}) {
  543. error(gettext("Must specify url to wiki with --url when using --cgi"));
  544. }
  545. if (defined $config{url} && length $config{url}) {
  546. eval q{use URI};
  547. my $baseurl = URI->new($config{url});
  548. $local_url = $baseurl->path . "/";
  549. $local_cgiurl = undef;
  550. if (length $config{cgiurl}) {
  551. my $cgiurl = URI->new($config{cgiurl});
  552. $local_cgiurl = $cgiurl->path;
  553. if ($cgiurl->scheme ne $baseurl->scheme or
  554. $cgiurl->authority ne $baseurl->authority) {
  555. # too far apart, fall back to absolute URLs
  556. $local_url = "$config{url}/";
  557. $local_cgiurl = $config{cgiurl};
  558. }
  559. }
  560. $local_url =~ s{//$}{/};
  561. }
  562. else {
  563. $local_cgiurl = $config{cgiurl};
  564. }
  565. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  566. unless exists $config{wikistatedir} && defined $config{wikistatedir};
  567. if (defined $config{umask}) {
  568. umask(possibly_foolish_untaint($config{umask}));
  569. }
  570. run_hooks(checkconfig => sub { shift->() });
  571. return 1;
  572. }
  573. sub listplugins () {
  574. my %ret;
  575. foreach my $dir (@INC, $config{libdir}) {
  576. next unless defined $dir && length $dir;
  577. foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
  578. my ($plugin)=$file=~/.*\/(.*)\.pm$/;
  579. $ret{$plugin}=1;
  580. }
  581. }
  582. foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
  583. next unless defined $dir && length $dir;
  584. foreach my $file (glob("$dir/plugins/*")) {
  585. $ret{basename($file)}=1 if -x $file;
  586. }
  587. }
  588. return keys %ret;
  589. }
  590. sub loadplugins () {
  591. if (defined $config{libdir} && length $config{libdir}) {
  592. unshift @INC, possibly_foolish_untaint($config{libdir});
  593. }
  594. foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
  595. loadplugin($plugin);
  596. }
  597. if ($config{rcs}) {
  598. if (exists $hooks{rcs}) {
  599. error(gettext("cannot use multiple rcs plugins"));
  600. }
  601. loadplugin($config{rcs});
  602. }
  603. if (! exists $hooks{rcs}) {
  604. loadplugin("norcs");
  605. }
  606. run_hooks(getopt => sub { shift->() });
  607. if (grep /^-/, @ARGV) {
  608. print STDERR "Unknown option (or missing parameter): $_\n"
  609. foreach grep /^-/, @ARGV;
  610. usage();
  611. }
  612. return 1;
  613. }
  614. sub loadplugin ($;$) {
  615. my $plugin=shift;
  616. my $force=shift;
  617. return if ! $force && grep { $_ eq $plugin} @{$config{disable_plugins}};
  618. foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
  619. "$installdir/lib/ikiwiki") {
  620. if (defined $dir && -x "$dir/plugins/$plugin") {
  621. eval { require IkiWiki::Plugin::external };
  622. if ($@) {
  623. my $reason=$@;
  624. error(sprintf(gettext("failed to load external plugin needed for %s plugin: %s"), $plugin, $reason));
  625. }
  626. import IkiWiki::Plugin::external "$dir/plugins/$plugin";
  627. $loaded_plugins{$plugin}=1;
  628. return 1;
  629. }
  630. }
  631. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  632. eval qq{use $mod};
  633. if ($@) {
  634. error("Failed to load plugin $mod: $@");
  635. }
  636. $loaded_plugins{$plugin}=1;
  637. return 1;
  638. }
  639. sub error ($;$) {
  640. my $message=shift;
  641. my $cleaner=shift;
  642. log_message('err' => $message) if $config{syslog};
  643. if (defined $cleaner) {
  644. $cleaner->();
  645. }
  646. die $message."\n";
  647. }
  648. sub debug ($) {
  649. return unless $config{verbose};
  650. return log_message(debug => @_);
  651. }
  652. my $log_open=0;
  653. sub log_message ($$) {
  654. my $type=shift;
  655. if ($config{syslog}) {
  656. require Sys::Syslog;
  657. if (! $log_open) {
  658. Sys::Syslog::setlogsock('unix');
  659. Sys::Syslog::openlog('ikiwiki', '', 'user');
  660. $log_open=1;
  661. }
  662. return eval {
  663. Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
  664. };
  665. }
  666. elsif (! $config{cgi}) {
  667. return print "@_\n";
  668. }
  669. else {
  670. return print STDERR "@_\n";
  671. }
  672. }
  673. sub possibly_foolish_untaint ($) {
  674. my $tainted=shift;
  675. my ($untainted)=$tainted=~/(.*)/s;
  676. return $untainted;
  677. }
  678. sub basename ($) {
  679. my $file=shift;
  680. $file=~s!.*/+!!;
  681. return $file;
  682. }
  683. sub dirname ($) {
  684. my $file=shift;
  685. $file=~s!/*[^/]+$!!;
  686. return $file;
  687. }
  688. sub isinternal ($) {
  689. my $page=shift;
  690. return exists $pagesources{$page} &&
  691. $pagesources{$page} =~ /\._([^.]+)$/;
  692. }
  693. sub pagetype ($) {
  694. my $file=shift;
  695. if ($file =~ /\.([^.]+)$/) {
  696. return $1 if exists $hooks{htmlize}{$1};
  697. }
  698. my $base=basename($file);
  699. if (exists $hooks{htmlize}{$base} &&
  700. $hooks{htmlize}{$base}{noextension}) {
  701. return $base;
  702. }
  703. return;
  704. }
  705. my %pagename_cache;
  706. sub pagename ($) {
  707. my $file=shift;
  708. if (exists $pagename_cache{$file}) {
  709. return $pagename_cache{$file};
  710. }
  711. my $type=pagetype($file);
  712. my $page=$file;
  713. $page=~s/\Q.$type\E*$//
  714. if defined $type && !$hooks{htmlize}{$type}{keepextension}
  715. && !$hooks{htmlize}{$type}{noextension};
  716. if ($config{indexpages} && $page=~/(.*)\/index$/) {
  717. $page=$1;
  718. }
  719. $pagename_cache{$file} = $page;
  720. return $page;
  721. }
  722. sub newpagefile ($$) {
  723. my $page=shift;
  724. my $type=shift;
  725. if (! $config{indexpages} || $page eq 'index') {
  726. return $page.".".$type;
  727. }
  728. else {
  729. return $page."/index.".$type;
  730. }
  731. }
  732. sub targetpage ($$;$) {
  733. my $page=shift;
  734. my $ext=shift;
  735. my $filename=shift;
  736. if (defined $filename) {
  737. return $page."/".$filename.".".$ext;
  738. }
  739. elsif (! $config{usedirs} || $page eq 'index') {
  740. return $page.".".$ext;
  741. }
  742. else {
  743. return $page."/index.".$ext;
  744. }
  745. }
  746. sub htmlpage ($) {
  747. my $page=shift;
  748. return targetpage($page, $config{htmlext});
  749. }
  750. sub srcfile_stat {
  751. my $file=shift;
  752. my $nothrow=shift;
  753. return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
  754. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  755. return "$dir/$file", stat(_) if -e "$dir/$file";
  756. }
  757. error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
  758. return;
  759. }
  760. sub srcfile ($;$) {
  761. return (srcfile_stat(@_))[0];
  762. }
  763. sub add_literal_underlay ($) {
  764. my $dir=shift;
  765. if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
  766. unshift @{$config{underlaydirs}}, $dir;
  767. }
  768. }
  769. sub add_underlay ($) {
  770. my $dir = shift;
  771. if ($dir !~ /^\//) {
  772. $dir="$config{underlaydirbase}/$dir";
  773. }
  774. add_literal_underlay($dir);
  775. # why does it return 1? we just don't know
  776. return 1;
  777. }
  778. sub readfile ($;$$) {
  779. my $file=shift;
  780. my $binary=shift;
  781. my $wantfd=shift;
  782. if (-l $file) {
  783. error("cannot read a symlink ($file)");
  784. }
  785. local $/=undef;
  786. open (my $in, "<", $file) || error("failed to read $file: $!");
  787. binmode($in) if ($binary);
  788. return \*$in if $wantfd;
  789. my $ret=<$in>;
  790. # check for invalid utf-8, and toss it back to avoid crashes
  791. if (! utf8::valid($ret)) {
  792. $ret=encode_utf8($ret);
  793. }
  794. close $in || error("failed to read $file: $!");
  795. return $ret;
  796. }
  797. sub prep_writefile ($$) {
  798. my $file=shift;
  799. my $destdir=shift;
  800. my $test=$file;
  801. while (length $test) {
  802. if (-l "$destdir/$test") {
  803. error("cannot write to a symlink ($test)");
  804. }
  805. if (-f _ && $test ne $file) {
  806. # Remove conflicting file.
  807. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  808. foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  809. if ($f eq $test) {
  810. unlink("$destdir/$test");
  811. last;
  812. }
  813. }
  814. }
  815. }
  816. $test=dirname($test);
  817. }
  818. my $dir=dirname("$destdir/$file");
  819. if (! -d $dir) {
  820. my $d="";
  821. foreach my $s (split(m!/+!, $dir)) {
  822. $d.="$s/";
  823. if (! -d $d) {
  824. mkdir($d) || error("failed to create directory $d: $!");
  825. }
  826. }
  827. }
  828. return 1;
  829. }
  830. sub writefile ($$$;$$) {
  831. my $file=shift; # can include subdirs
  832. my $destdir=shift; # directory to put file in
  833. my $content=shift;
  834. my $binary=shift;
  835. my $writer=shift;
  836. prep_writefile($file, $destdir);
  837. my $newfile="$destdir/$file.ikiwiki-new";
  838. if (-l $newfile) {
  839. error("cannot write to a symlink ($newfile)");
  840. }
  841. my $cleanup = sub { unlink($newfile) };
  842. open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
  843. binmode($out) if ($binary);
  844. if ($writer) {
  845. $writer->(\*$out, $cleanup);
  846. }
  847. else {
  848. print $out $content or error("failed writing to $newfile: $!", $cleanup);
  849. }
  850. close $out || error("failed saving $newfile: $!", $cleanup);
  851. rename($newfile, "$destdir/$file") ||
  852. error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
  853. return 1;
  854. }
  855. my %cleared;
  856. sub will_render ($$;$) {
  857. my $page=shift;
  858. my $dest=shift;
  859. my $clear=shift;
  860. # Important security check for independently created files.
  861. if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
  862. ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
  863. my $from_other_page=0;
  864. # Expensive, but rarely runs.
  865. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  866. if (grep {
  867. $_ eq $dest ||
  868. dirname($_) eq $dest
  869. } @{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  870. $from_other_page=1;
  871. last;
  872. }
  873. }
  874. error("$config{destdir}/$dest independently created, not overwriting with version from $page")
  875. unless $from_other_page;
  876. }
  877. # If $dest exists as a directory, remove conflicting files in it
  878. # rendered from other pages.
  879. if (-d _) {
  880. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  881. foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  882. if (dirname($f) eq $dest) {
  883. unlink("$config{destdir}/$f");
  884. rmdir(dirname("$config{destdir}/$f"));
  885. }
  886. }
  887. }
  888. }
  889. if (! $clear || $cleared{$page}) {
  890. $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
  891. }
  892. else {
  893. foreach my $old (@{$renderedfiles{$page}}) {
  894. delete $destsources{$old};
  895. }
  896. $renderedfiles{$page}=[$dest];
  897. $cleared{$page}=1;
  898. }
  899. $destsources{$dest}=$page;
  900. return 1;
  901. }
  902. sub bestlink ($$) {
  903. my $page=shift;
  904. my $link=shift;
  905. my $cwd=$page;
  906. if ($link=~s/^\/+//) {
  907. # absolute links
  908. $cwd="";
  909. }
  910. $link=~s/\/$//;
  911. do {
  912. my $l=$cwd;
  913. $l.="/" if length $l;
  914. $l.=$link;
  915. if (exists $pagesources{$l}) {
  916. return $l;
  917. }
  918. elsif (exists $pagecase{lc $l}) {
  919. return $pagecase{lc $l};
  920. }
  921. } while $cwd=~s{/?[^/]+$}{};
  922. if (length $config{userdir}) {
  923. my $l = "$config{userdir}/".lc($link);
  924. if (exists $pagesources{$l}) {
  925. return $l;
  926. }
  927. elsif (exists $pagecase{lc $l}) {
  928. return $pagecase{lc $l};
  929. }
  930. }
  931. #print STDERR "warning: page $page, broken link: $link\n";
  932. return "";
  933. }
  934. sub isinlinableimage ($) {
  935. my $file=shift;
  936. return $file =~ /\.(png|gif|jpg|jpeg|svg)$/i;
  937. }
  938. sub pagetitle ($;$) {
  939. my $page=shift;
  940. my $unescaped=shift;
  941. if ($unescaped) {
  942. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
  943. }
  944. else {
  945. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
  946. }
  947. return $page;
  948. }
  949. sub titlepage ($) {
  950. my $title=shift;
  951. # support use w/o %config set
  952. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  953. $title=~s/([^$chars]|_)/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  954. return $title;
  955. }
  956. sub linkpage ($) {
  957. my $link=shift;
  958. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  959. $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  960. return $link;
  961. }
  962. sub cgiurl (@) {
  963. my %params=@_;
  964. my $cgiurl=$local_cgiurl;
  965. if (exists $params{cgiurl}) {
  966. $cgiurl=$params{cgiurl};
  967. delete $params{cgiurl};
  968. }
  969. unless (%params) {
  970. return $cgiurl;
  971. }
  972. return $cgiurl."?".
  973. join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
  974. }
  975. sub baseurl (;$) {
  976. my $page=shift;
  977. return $local_url if ! defined $page;
  978. $page=htmlpage($page);
  979. $page=~s/[^\/]+$//;
  980. $page=~s/[^\/]+\//..\//g;
  981. return $page;
  982. }
  983. sub urlabs ($$) {
  984. my $url=shift;
  985. my $urlbase=shift;
  986. return $url unless defined $urlbase && length $urlbase;
  987. eval q{use URI};
  988. URI->new_abs($url, $urlbase)->as_string;
  989. }
  990. sub abs2rel ($$) {
  991. # Work around very innefficient behavior in File::Spec if abs2rel
  992. # is passed two relative paths. It's much faster if paths are
  993. # absolute! (Debian bug #376658; fixed in debian unstable now)
  994. my $path="/".shift;
  995. my $base="/".shift;
  996. require File::Spec;
  997. my $ret=File::Spec->abs2rel($path, $base);
  998. $ret=~s/^// if defined $ret;
  999. return $ret;
  1000. }
  1001. sub displaytime ($;$$) {
  1002. # Plugins can override this function to mark up the time to
  1003. # display.
  1004. my $time=formattime($_[0], $_[1]);
  1005. if ($config{html5}) {
  1006. return '<time datetime="'.date_3339($_[0]).'"'.
  1007. ($_[2] ? ' pubdate="pubdate"' : '').
  1008. '>'.$time.'</time>';
  1009. }
  1010. else {
  1011. return '<span class="date">'.$time.'</span>';
  1012. }
  1013. }
  1014. sub formattime ($;$) {
  1015. # Plugins can override this function to format the time.
  1016. my $time=shift;
  1017. my $format=shift;
  1018. if (! defined $format) {
  1019. $format=$config{timeformat};
  1020. }
  1021. # strftime doesn't know about encodings, so make sure
  1022. # its output is properly treated as utf8
  1023. return decode_utf8(POSIX::strftime($format, localtime($time)));
  1024. }
  1025. sub date_3339 ($) {
  1026. my $time=shift;
  1027. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  1028. POSIX::setlocale(&POSIX::LC_TIME, "C");
  1029. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
  1030. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  1031. return $ret;
  1032. }
  1033. sub beautify_urlpath ($) {
  1034. my $url=shift;
  1035. # Ensure url is not an empty link, and if necessary,
  1036. # add ./ to avoid colon confusion.
  1037. if ($url !~ /^\// && $url !~ /^\.\.?\//) {
  1038. $url="./$url";
  1039. }
  1040. if ($config{usedirs}) {
  1041. $url =~ s!/index.$config{htmlext}$!/!;
  1042. }
  1043. return $url;
  1044. }
  1045. sub urlto ($;$$) {
  1046. my $to=shift;
  1047. my $from=shift;
  1048. my $absolute=shift;
  1049. if (! length $to) {
  1050. $to = 'index';
  1051. }
  1052. if (! $destsources{$to}) {
  1053. $to=htmlpage($to);
  1054. }
  1055. if ($absolute) {
  1056. return $config{url}.beautify_urlpath("/".$to);
  1057. }
  1058. if (! defined $from) {
  1059. my $u = $local_url || '';
  1060. $u =~ s{/$}{};
  1061. return $u.beautify_urlpath("/".$to);
  1062. }
  1063. my $link = abs2rel($to, dirname(htmlpage($from)));
  1064. return beautify_urlpath($link);
  1065. }
  1066. sub isselflink ($$) {
  1067. # Plugins can override this function to support special types
  1068. # of selflinks.
  1069. my $page=shift;
  1070. my $link=shift;
  1071. return $page eq $link;
  1072. }
  1073. sub htmllink ($$$;@) {
  1074. my $lpage=shift; # the page doing the linking
  1075. my $page=shift; # the page that will contain the link (different for inline)
  1076. my $link=shift;
  1077. my %opts=@_;
  1078. $link=~s/\/$//;
  1079. my $bestlink;
  1080. if (! $opts{forcesubpage}) {
  1081. $bestlink=bestlink($lpage, $link);
  1082. }
  1083. else {
  1084. $bestlink="$lpage/".lc($link);
  1085. }
  1086. my $linktext;
  1087. if (defined $opts{linktext}) {
  1088. $linktext=$opts{linktext};
  1089. }
  1090. else {
  1091. $linktext=pagetitle(basename($link));
  1092. }
  1093. return "<span class=\"selflink\">$linktext</span>"
  1094. if length $bestlink && isselflink($page, $bestlink) &&
  1095. ! defined $opts{anchor};
  1096. if (! $destsources{$bestlink}) {
  1097. $bestlink=htmlpage($bestlink);
  1098. if (! $destsources{$bestlink}) {
  1099. my $cgilink = "";
  1100. if (length $config{cgiurl}) {
  1101. $cgilink = "<a href=\"".
  1102. cgiurl(
  1103. do => "create",
  1104. page => $link,
  1105. from => $lpage
  1106. )."\" rel=\"nofollow\">?</a>";
  1107. }
  1108. return "<span class=\"createlink\">$cgilink$linktext</span>"
  1109. }
  1110. }
  1111. $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
  1112. $bestlink=beautify_urlpath($bestlink);
  1113. if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
  1114. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  1115. }
  1116. if (defined $opts{anchor}) {
  1117. $bestlink.="#".$opts{anchor};
  1118. }
  1119. my @attrs;
  1120. foreach my $attr (qw{rel class title}) {
  1121. if (defined $opts{$attr}) {
  1122. push @attrs, " $attr=\"$opts{$attr}\"";
  1123. }
  1124. }
  1125. return "<a href=\"$bestlink\"@attrs>$linktext</a>";
  1126. }
  1127. sub userpage ($) {
  1128. my $user=shift;
  1129. return length $config{userdir} ? "$config{userdir}/$user" : $user;
  1130. }
  1131. sub openiduser ($) {
  1132. my $user=shift;
  1133. if (defined $user && $user =~ m!^https?://! &&
  1134. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  1135. my $display;
  1136. if (Net::OpenID::VerifiedIdentity->can("DisplayOfURL")) {
  1137. $display = Net::OpenID::VerifiedIdentity::DisplayOfURL($user);
  1138. }
  1139. else {
  1140. # backcompat with old version
  1141. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
  1142. $display=$oid->display;
  1143. }
  1144. # Convert "user.somehost.com" to "user [somehost.com]"
  1145. # (also "user.somehost.co.uk")
  1146. if ($display !~ /\[/) {
  1147. $display=~s/^([-a-zA-Z0-9]+?)\.([-.a-zA-Z0-9]+\.[a-z]+)$/$1 [$2]/;
  1148. }
  1149. # Convert "http://somehost.com/user" to "user [somehost.com]".
  1150. # (also "https://somehost.com/user/")
  1151. if ($display !~ /\[/) {
  1152. $display=~s/^https?:\/\/(.+)\/([^\/#?]+)\/?(?:[#?].*)?$/$2 [$1]/;
  1153. }
  1154. $display=~s!^https?://!!; # make sure this is removed
  1155. eval q{use CGI 'escapeHTML'};
  1156. error($@) if $@;
  1157. return escapeHTML($display);
  1158. }
  1159. return;
  1160. }
  1161. sub htmlize ($$$$) {
  1162. my $page=shift;
  1163. my $destpage=shift;
  1164. my $type=shift;
  1165. my $content=shift;
  1166. my $oneline = $content !~ /\n/;
  1167. if (exists $hooks{htmlize}{$type}) {
  1168. $content=$hooks{htmlize}{$type}{call}->(
  1169. page => $page,
  1170. content => $content,
  1171. );
  1172. }
  1173. else {
  1174. error("htmlization of $type not supported");
  1175. }
  1176. run_hooks(sanitize => sub {
  1177. $content=shift->(
  1178. page => $page,
  1179. destpage => $destpage,
  1180. content => $content,
  1181. );
  1182. });
  1183. if ($oneline) {
  1184. # hack to get rid of enclosing junk added by markdown
  1185. # and other htmlizers/sanitizers
  1186. $content=~s/^<p>//i;
  1187. $content=~s/<\/p>\n*$//i;
  1188. }
  1189. return $content;
  1190. }
  1191. sub linkify ($$$) {
  1192. my $page=shift;
  1193. my $destpage=shift;
  1194. my $content=shift;
  1195. run_hooks(linkify => sub {
  1196. $content=shift->(
  1197. page => $page,
  1198. destpage => $destpage,
  1199. content => $content,
  1200. );
  1201. });
  1202. return $content;
  1203. }
  1204. our %preprocessing;
  1205. our $preprocess_preview=0;
  1206. sub preprocess ($$$;$$) {
  1207. my $page=shift; # the page the data comes from
  1208. my $destpage=shift; # the page the data will appear in (different for inline)
  1209. my $content=shift;
  1210. my $scan=shift;
  1211. my $preview=shift;
  1212. # Using local because it needs to be set within any nested calls
  1213. # of this function.
  1214. local $preprocess_preview=$preview if defined $preview;
  1215. my $handle=sub {
  1216. my $escape=shift;
  1217. my $prefix=shift;
  1218. my $command=shift;
  1219. my $params=shift;
  1220. $params="" if ! defined $params;
  1221. if (length $escape) {
  1222. return "[[$prefix$command $params]]";
  1223. }
  1224. elsif (exists $hooks{preprocess}{$command}) {
  1225. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  1226. # Note: preserve order of params, some plugins may
  1227. # consider it significant.
  1228. my @params;
  1229. while ($params =~ m{
  1230. (?:([-\w]+)=)? # 1: named parameter key?
  1231. (?:
  1232. """(.*?)""" # 2: triple-quoted value
  1233. |
  1234. "([^"]*?)" # 3: single-quoted value
  1235. |
  1236. '''(.*?)''' # 4: triple-single-quote
  1237. |
  1238. <<(?<start>[a-zA-Z]+)\n(?<heredoc>.*?)\n\k<start> # 5, 6: heredoc'd value.
  1239. |
  1240. (\S+) # 7: unquoted value
  1241. )
  1242. (?:\s+|$) # delimiter to next param
  1243. }msgx) {
  1244. my $key=$1;
  1245. my $val;
  1246. if (defined $2) {
  1247. $val=$2;
  1248. $val=~s/\r\n/\n/mg;
  1249. $val=~s/^\n+//g;
  1250. $val=~s/\n+$//g;
  1251. }
  1252. elsif (defined $3) {
  1253. $val=$3;
  1254. }
  1255. elsif (defined $4) {
  1256. $val=$4;
  1257. }
  1258. elsif (defined $7) {
  1259. $val=$7;
  1260. }
  1261. elsif (defined $+{heredoc}) {
  1262. $val=$+{heredoc};
  1263. }
  1264. if (defined $key) {
  1265. push @params, $key, $val;
  1266. }
  1267. else {
  1268. push @params, $val, '';
  1269. }
  1270. }
  1271. if ($preprocessing{$page}++ > 3) {
  1272. # Avoid loops of preprocessed pages preprocessing
  1273. # other pages that preprocess them, etc.
  1274. return "[[!$command <span class=\"error\">".
  1275. sprintf(gettext("preprocessing loop detected on %s at depth %i"),
  1276. $page, $preprocessing{$page}).
  1277. "</span>]]";
  1278. }
  1279. my $ret;
  1280. if (! $scan) {
  1281. $ret=eval {
  1282. $hooks{preprocess}{$command}{call}->(
  1283. @params,
  1284. page => $page,
  1285. destpage => $destpage,
  1286. preview => $preprocess_preview,
  1287. );
  1288. };
  1289. if ($@) {
  1290. my $error=$@;
  1291. chomp $error;
  1292. $ret="[[!$command <span class=\"error\">".
  1293. gettext("Error").": $error"."</span>]]";
  1294. }
  1295. }
  1296. else {
  1297. # use void context during scan pass
  1298. eval {
  1299. $hooks{preprocess}{$command}{call}->(
  1300. @params,
  1301. page => $page,
  1302. destpage => $destpage,
  1303. preview => $preprocess_preview,
  1304. );
  1305. };
  1306. $ret="";
  1307. }
  1308. $preprocessing{$page}--;
  1309. return $ret;
  1310. }
  1311. else {
  1312. return "[[$prefix$command $params]]";
  1313. }
  1314. };
  1315. my $regex;
  1316. if ($config{prefix_directives}) {
  1317. $regex = qr{
  1318. (\\?) # 1: escape?
  1319. \[\[(!) # directive open; 2: prefix
  1320. ([-\w]+) # 3: command
  1321. ( # 4: the parameters..
  1322. \s+ # Must have space if parameters present
  1323. (?:
  1324. (?:[-\w]+=)? # named parameter key?
  1325. (?:
  1326. """.*?""" # triple-quoted value
  1327. |
  1328. "[^"]*?" # single-quoted value
  1329. |
  1330. <<(?<start>[a-zA-Z]+)\n(?<heredoc>.*?)\n\k<start> # heredoc'd value.
  1331. |
  1332. '''.*?''' # triple-single-quoted value
  1333. |
  1334. [^"\s\]]+ # unquoted value
  1335. )
  1336. \s* # whitespace or end
  1337. # of directive
  1338. )
  1339. *)? # 0 or more parameters
  1340. \]\] # directive closed
  1341. }sx;
  1342. }
  1343. else {
  1344. $regex = qr{
  1345. (\\?) # 1: escape?
  1346. \[\[(!?) # directive open; 2: optional prefix
  1347. ([-\w]+) # 3: command
  1348. \s+
  1349. ( # 4: the parameters..
  1350. (?:
  1351. (?:[-\w]+=)? # named parameter key?
  1352. (?:
  1353. """.*?""" # triple-quoted value
  1354. |
  1355. "[^"]*?" # single-quoted value
  1356. |
  1357. '''.*?''' # triple-single-quoted value
  1358. |
  1359. <<(?<start>[a-zA-Z]+)\n(?<heredoc>.*?)\n\k<start> # heredoc'd value.
  1360. |
  1361. [^"\s\]]+ # unquoted value
  1362. )
  1363. \s* # whitespace or end
  1364. # of directive
  1365. )
  1366. *) # 0 or more parameters
  1367. \]\] # directive closed
  1368. }sx;
  1369. }
  1370. $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
  1371. return $content;
  1372. }
  1373. sub filter ($$$) {
  1374. my $page=shift;
  1375. my $destpage=shift;
  1376. my $content=shift;
  1377. run_hooks(filter => sub {
  1378. $content=shift->(page => $page, destpage => $destpage,
  1379. content => $content);
  1380. });
  1381. return $content;
  1382. }
  1383. sub check_canedit ($$$;$) {
  1384. my $page=shift;
  1385. my $q=shift;
  1386. my $session=shift;
  1387. my $nonfatal=shift;
  1388. my $canedit;
  1389. run_hooks(canedit => sub {
  1390. return if defined $canedit;
  1391. my $ret=shift->($page, $q, $session);
  1392. if (defined $ret) {
  1393. if ($ret eq "") {
  1394. $canedit=1;
  1395. }
  1396. elsif (ref $ret eq 'CODE') {
  1397. $ret->() unless $nonfatal;
  1398. $canedit=0;
  1399. }
  1400. elsif (defined $ret) {
  1401. error($ret) unless $nonfatal;
  1402. $canedit=0;
  1403. }
  1404. }
  1405. });
  1406. return defined $canedit ? $canedit : 1;
  1407. }
  1408. sub check_content (@) {
  1409. my %params=@_;
  1410. return 1 if ! exists $hooks{checkcontent}; # optimisation
  1411. if (exists $pagesources{$params{page}}) {
  1412. my @diff;
  1413. my %old=map { $_ => 1 }
  1414. split("\n", readfile(srcfile($pagesources{$params{page}})));
  1415. foreach my $line (split("\n", $params{content})) {
  1416. push @diff, $line if ! exists $old{$line};
  1417. }
  1418. $params{diff}=join("\n", @diff);
  1419. }
  1420. my $ok;
  1421. run_hooks(checkcontent => sub {
  1422. return if defined $ok;
  1423. my $ret=shift->(%params);
  1424. if (defined $ret) {
  1425. if ($ret eq "") {
  1426. $ok=1;
  1427. }
  1428. elsif (ref $ret eq 'CODE') {
  1429. $ret->() unless $params{nonfatal};
  1430. $ok=0;
  1431. }
  1432. elsif (defined $ret) {
  1433. error($ret) unless $params{nonfatal};
  1434. $ok=0;
  1435. }
  1436. }
  1437. });
  1438. return defined $ok ? $ok : 1;
  1439. }
  1440. sub check_canchange (@) {
  1441. my %params = @_;
  1442. my $cgi = $params{cgi};
  1443. my $session = $params{session};
  1444. my @changes = @{$params{changes}};
  1445. my %newfiles;
  1446. foreach my $change (@changes) {
  1447. # This untaint is safe because we check file_pruned and
  1448. # wiki_file_regexp.
  1449. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  1450. $file=possibly_foolish_untaint($file);
  1451. if (! defined $file || ! length $file ||
  1452. file_pruned($file)) {
  1453. error(gettext("bad file name %s"), $file);
  1454. }
  1455. my $type=pagetype($file);
  1456. my $page=pagename($file) if defined $type;
  1457. if ($change->{action} eq 'add') {
  1458. $newfiles{$file}=1;
  1459. }
  1460. if ($change->{action} eq 'change' ||
  1461. $change->{action} eq 'add') {
  1462. if (defined $page) {
  1463. check_canedit($page, $cgi, $session);
  1464. next;
  1465. }
  1466. else {
  1467. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  1468. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  1469. check_canedit($file, $cgi, $session);
  1470. next;
  1471. }
  1472. }
  1473. }
  1474. elsif ($change->{action} eq 'remove') {
  1475. # check_canremove tests to see if the file is present
  1476. # on disk. This will fail when a single commit adds a
  1477. # file and then removes it again. Avoid the problem
  1478. # by not testing the removal in such pairs of changes.
  1479. # (The add is still tested, just to make sure that
  1480. # no data is added to the repo that a web edit
  1481. # could not add.)
  1482. next if $newfiles{$file};
  1483. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  1484. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  1485. check_canedit(defined $page ? $page : $file, $cgi, $session);
  1486. next;
  1487. }
  1488. }
  1489. else {
  1490. error "unknown action ".$change->{action};
  1491. }
  1492. error sprintf(gettext("you are not allowed to change %s"), $file);
  1493. }
  1494. }
  1495. my $wikilock;
  1496. sub lockwiki () {
  1497. # Take an exclusive lock on the wiki to prevent multiple concurrent
  1498. # run issues. The lock will be dropped on program exit.
  1499. if (! -d $config{wikistatedir}) {
  1500. mkdir($config{wikistatedir});
  1501. }
  1502. open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
  1503. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  1504. if (! flock($wikilock, 2)) { # LOCK_EX
  1505. error("failed to get lock");
  1506. }
  1507. return 1;
  1508. }
  1509. sub unlockwiki () {
  1510. POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
  1511. return close($wikilock) if $wikilock;
  1512. return;
  1513. }
  1514. my $commitlock;
  1515. sub commit_hook_enabled () {
  1516. open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
  1517. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1518. if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
  1519. close($commitlock) || error("failed closing commitlock: $!");
  1520. return 0;
  1521. }
  1522. close($commitlock) || error("failed closing commitlock: $!");
  1523. return 1;
  1524. }
  1525. sub disable_commit_hook () {
  1526. open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
  1527. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1528. if (! flock($commitlock, 2)) { # LOCK_EX
  1529. error("failed to get commit lock");
  1530. }
  1531. return 1;
  1532. }
  1533. sub enable_commit_hook () {
  1534. return close($commitlock) if $commitlock;
  1535. return;
  1536. }
  1537. sub loadindex () {
  1538. %oldrenderedfiles=%pagectime=();
  1539. if (! $config{rebuild}) {
  1540. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  1541. %destsources=%renderedfiles=%pagecase=%pagestate=
  1542. %depends_simple=%typedlinks=%oldtypedlinks=();
  1543. }
  1544. my $in;
  1545. if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
  1546. if (-e "$config{wikistatedir}/index") {
  1547. system("ikiwiki-transition", "indexdb", $config{srcdir});
  1548. open ($in, "<", "$config{wikistatedir}/indexdb") || return;
  1549. }
  1550. else {
  1551. $config{gettime}=1; # first build
  1552. return;
  1553. }
  1554. }
  1555. my $index=Storable::fd_retrieve($in);
  1556. if (! defined $index) {
  1557. return 0;
  1558. }
  1559. my $pages;
  1560. if (exists $index->{version} && ! ref $index->{version}) {
  1561. $pages=$index->{page};
  1562. %wikistate=%{$index->{state}};
  1563. # Handle plugins that got disabled by loading a new setup.
  1564. if (exists $config{setupfile}) {
  1565. require IkiWiki::Setup;
  1566. IkiWiki::Setup::disabled_plugins(
  1567. grep { ! $loaded_plugins{$_} } keys %wikistate);
  1568. }
  1569. }
  1570. else {
  1571. $pages=$index;
  1572. %wikistate=();
  1573. }
  1574. foreach my $src (keys %$pages) {
  1575. my $d=$pages->{$src};
  1576. my $page=pagename($src);
  1577. $pagectime{$page}=$d->{ctime};
  1578. $pagesources{$page}=$src;
  1579. if (! $config{rebuild}) {
  1580. $pagemtime{$page}=$d->{mtime};
  1581. $renderedfiles{$page}=$d->{dest};
  1582. if (exists $d->{links} && ref $d->{links}) {
  1583. $links{$page}=$d->{links};
  1584. $oldlinks{$page}=[@{$d->{links}}];
  1585. }
  1586. if (ref $d->{depends_simple} eq 'ARRAY') {
  1587. # old format
  1588. $depends_simple{$page}={
  1589. map { $_ => 1 } @{$d->{depends_simple}}
  1590. };
  1591. }
  1592. elsif (exists $d->{depends_simple}) {
  1593. $depends_simple{$page}=$d->{depends_simple};
  1594. }
  1595. if (exists $d->{dependslist}) {
  1596. # old format
  1597. $depends{$page}={
  1598. map { $_ => $DEPEND_CONTENT }
  1599. @{$d->{dependslist}}
  1600. };
  1601. }
  1602. elsif (exists $d->{depends} && ! ref $d->{depends}) {
  1603. # old format
  1604. $depends{$page}={$d->{depends} => $DEPEND_CONTENT };
  1605. }
  1606. elsif (exists $d->{depends}) {
  1607. $depends{$page}=$d->{depends};
  1608. }
  1609. if (exists $d->{state}) {
  1610. $pagestate{$page}=$d->{state};
  1611. }
  1612. if (exists $d->{typedlinks}) {
  1613. $typedlinks{$page}=$d->{typedlinks};
  1614. while (my ($type, $links) = each %{$typedlinks{$page}}) {
  1615. next unless %$links;
  1616. $oldtypedlinks{$page}{$type} = {%$links};
  1617. }
  1618. }
  1619. }
  1620. $oldrenderedfiles{$page}=[@{$d->{dest}}];
  1621. }
  1622. foreach my $page (keys %pagesources) {
  1623. $pagecase{lc $page}=$page;
  1624. }
  1625. foreach my $page (keys %renderedfiles) {
  1626. $destsources{$_}=$page foreach @{$renderedfiles{$page}};
  1627. }
  1628. return close($in);
  1629. }
  1630. sub saveindex () {
  1631. run_hooks(savestate => sub { shift->() });
  1632. my @plugins=keys %loaded_plugins;
  1633. if (! -d $config{wikistatedir}) {
  1634. mkdir($config{wikistatedir});
  1635. }
  1636. my $newfile="$config{wikistatedir}/indexdb.new";
  1637. my $cleanup = sub { unlink($newfile) };
  1638. open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
  1639. my %index;
  1640. foreach my $page (keys %pagemtime) {
  1641. next unless $pagemtime{$page};
  1642. my $src=$pagesources{$page};
  1643. $index{page}{$src}={
  1644. ctime => $pagectime{$page},
  1645. mtime => $pagemtime{$page},
  1646. dest => $renderedfiles{$page},
  1647. links => $links{$page},
  1648. };
  1649. if (exists $depends{$page}) {
  1650. $index{page}{$src}{depends} = $depends{$page};
  1651. }
  1652. if (exists $depends_simple{$page}) {
  1653. $index{page}{$src}{depends_simple} = $depends_simple{$page};
  1654. }
  1655. if (exists $typedlinks{$page} && %{$typedlinks{$page}}) {
  1656. $index{page}{$src}{typedlinks} = $typedlinks{$page};
  1657. }
  1658. if (exists $pagestate{$page}) {
  1659. foreach my $id (@plugins) {
  1660. foreach my $key (keys %{$pagestate{$page}{$id}}) {
  1661. $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
  1662. }
  1663. }
  1664. }
  1665. }
  1666. $index{state}={};
  1667. foreach my $id (@plugins) {
  1668. $index{state}{$id}={}; # used to detect disabled plugins
  1669. foreach my $key (keys %{$wikistate{$id}}) {
  1670. $index{state}{$id}{$key}=$wikistate{$id}{$key};
  1671. }
  1672. }
  1673. $index{version}="3";
  1674. my $ret=Storable::nstore_fd(\%index, $out);
  1675. return if ! defined $ret || ! $ret;
  1676. close $out || error("failed saving to $newfile: $!", $cleanup);
  1677. rename($newfile, "$config{wikistatedir}/indexdb") ||
  1678. error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
  1679. return 1;
  1680. }
  1681. sub template_file ($) {
  1682. my $name=shift;
  1683. my $tpage=($name =~ s/^\///) ? $name : "templates/$name";
  1684. my $template;
  1685. if ($name !~ /\.tmpl$/ && exists $pagesources{$tpage}) {
  1686. $template=srcfile($pagesources{$tpage}, 1);
  1687. $name.=".tmpl";
  1688. }
  1689. else {
  1690. $template=srcfile($tpage, 1);
  1691. }
  1692. if (defined $template) {
  1693. return $template, $tpage, 1 if wantarray;
  1694. return $template;
  1695. }
  1696. else {
  1697. $name=~s:/::; # avoid path traversal
  1698. foreach my $dir ($config{templatedir},
  1699. "$installdir/share/ikiwiki/templates") {
  1700. if (-e "$dir/$name") {
  1701. $template="$dir/$name";
  1702. last;
  1703. }
  1704. }
  1705. if (defined $template) {
  1706. return $template, $tpage if wantarray;
  1707. return $template;
  1708. }
  1709. }
  1710. return;
  1711. }
  1712. sub template_depends ($$;@) {
  1713. my $name=shift;
  1714. my $page=shift;
  1715. my ($filename, $tpage, $untrusted)=template_file($name);
  1716. if (! defined $filename) {
  1717. error(sprintf(gettext("template %s not found"), $name))
  1718. }
  1719. if (defined $page && defined $tpage) {
  1720. add_depends($page, $tpage);
  1721. }
  1722. my @opts=(
  1723. filter => sub {
  1724. my $text_ref = shift;
  1725. ${$text_ref} = decode_utf8(${$text_ref});
  1726. },
  1727. loop_context_vars => 1,
  1728. die_on_bad_params => 0,
  1729. parent_global_vars => 1,
  1730. filename => $filename,
  1731. @_,
  1732. ($untrusted ? (no_includes => 1) : ()),
  1733. );
  1734. return @opts if wantarray;
  1735. require HTML::Template;
  1736. return HTML::Template->new(@opts);
  1737. }
  1738. sub template ($;@) {
  1739. template_depends(shift, undef, @_);
  1740. }
  1741. sub templateactions ($$) {
  1742. my $template=shift;
  1743. my $page=shift;
  1744. my $have_actions=0;
  1745. my @actions;
  1746. run_hooks(pageactions => sub {
  1747. push @actions, map { { action => $_ } }
  1748. grep { defined } shift->(page => $page);
  1749. });
  1750. $template->param(actions => \@actions);
  1751. if ($config{cgiurl} && exists $hooks{auth}) {
  1752. $template->param(prefsurl => cgiurl(do => "prefs"));
  1753. $have_actions=1;
  1754. }
  1755. if ($have_actions || @actions) {
  1756. $template->param(have_actions => 1);
  1757. }
  1758. }
  1759. sub hook (@) {
  1760. my %param=@_;
  1761. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  1762. error 'hook requires type, call, and id parameters';
  1763. }
  1764. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  1765. $hooks{$param{type}}{$param{id}}=\%param;
  1766. return 1;
  1767. }
  1768. sub run_hooks ($$) {
  1769. # Calls the given sub for each hook of the given type,
  1770. # passing it the hook function to call.
  1771. my $type=shift;
  1772. my $sub=shift;
  1773. if (exists $hooks{$type}) {
  1774. my (@first, @middle, @last);
  1775. foreach my $id (keys %{$hooks{$type}}) {
  1776. if ($hooks{$type}{$id}{first}) {
  1777. push @first, $id;
  1778. }
  1779. elsif ($hooks{$type}{$id}{last}) {
  1780. push @last, $id;
  1781. }
  1782. else {
  1783. push @middle, $id;
  1784. }
  1785. }
  1786. foreach my $id (@first, @middle, @last) {
  1787. $sub->($hooks{$type}{$id}{call});
  1788. }
  1789. }
  1790. return 1;
  1791. }
  1792. sub rcs_update () {
  1793. $hooks{rcs}{rcs_update}{call}->(@_);
  1794. }
  1795. sub rcs_prepedit ($) {
  1796. $hooks{rcs}{rcs_prepedit}{call}->(@_);
  1797. }
  1798. sub rcs_commit (@) {
  1799. $hooks{rcs}{rcs_commit}{call}->(@_);
  1800. }
  1801. sub rcs_commit_staged (@) {
  1802. $hooks{rcs}{rcs_commit_staged}{call}->(@_);
  1803. }
  1804. sub rcs_add ($) {
  1805. $hooks{rcs}{rcs_add}{call}->(@_);
  1806. }
  1807. sub rcs_remove ($) {
  1808. $hooks{rcs}{rcs_remove}{call}->(@_);
  1809. }
  1810. sub rcs_rename ($$) {
  1811. $hooks{rcs}{rcs_rename}{call}->(@_);
  1812. }
  1813. sub rcs_recentchanges ($) {
  1814. $hooks{rcs}{rcs_recentchanges}{call}->(@_);
  1815. }
  1816. sub rcs_diff ($;$) {
  1817. $hooks{rcs}{rcs_diff}{call}->(@_);
  1818. }
  1819. sub rcs_getctime ($) {
  1820. $hooks{rcs}{rcs_getctime}{call}->(@_);
  1821. }
  1822. sub rcs_getmtime ($) {
  1823. $hooks{rcs}{rcs_getmtime}{call}->(@_);
  1824. }
  1825. sub rcs_receive () {
  1826. $hooks{rcs}{rcs_receive}{call}->();
  1827. }
  1828. sub add_depends ($$;$) {
  1829. my $page=shift;
  1830. my $pagespec=shift;
  1831. my $deptype=shift || $DEPEND_CONTENT;
  1832. # Is the pagespec a simple page name?
  1833. if ($pagespec =~ /$config{wiki_file_regexp}/ &&
  1834. $pagespec !~ /[\s*?()!]/) {
  1835. $depends_simple{$page}{lc $pagespec} |= $deptype;
  1836. return 1;
  1837. }
  1838. # Add explicit dependencies for influences.
  1839. my $sub=pagespec_translate($pagespec);
  1840. return unless defined $sub;
  1841. foreach my $p (keys %pagesources) {
  1842. my $r=$sub->($p, location => $page);
  1843. my $i=$r->influences;
  1844. my $static=$r->influences_static;
  1845. foreach my $k (keys %$i) {
  1846. next unless $r || $static || $k eq $page;
  1847. $depends_simple{$page}{lc $k} |= $i->{$k};
  1848. }
  1849. last if $static;
  1850. }
  1851. $depends{$page}{$pagespec} |= $deptype;
  1852. return 1;
  1853. }
  1854. sub deptype (@) {
  1855. my $deptype=0;
  1856. foreach my $type (@_) {
  1857. if ($type eq 'presence') {
  1858. $deptype |= $DEPEND_PRESENCE;
  1859. }
  1860. elsif ($type eq 'links') {
  1861. $deptype |= $DEPEND_LINKS;
  1862. }
  1863. elsif ($type eq 'content') {
  1864. $deptype |= $DEPEND_CONTENT;
  1865. }
  1866. }
  1867. return $deptype;
  1868. }
  1869. my $file_prune_regexp;
  1870. sub file_pruned ($) {
  1871. my $file=shift;
  1872. if (defined $config{include} && length $config{include}) {
  1873. return 0 if $file =~ m/$config{include}/;
  1874. }
  1875. if (! defined $file_prune_regexp) {
  1876. $file_prune_regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  1877. $file_prune_regexp=qr/$file_prune_regexp/;
  1878. }
  1879. return $file =~ m/$file_prune_regexp/;
  1880. }
  1881. sub define_gettext () {
  1882. # If translation is needed, redefine the gettext function to do it.
  1883. # Otherwise, it becomes a quick no-op.
  1884. my $gettext_obj;
  1885. my $getobj;
  1886. if ((exists $ENV{LANG} && length $ENV{LANG}) ||
  1887. (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
  1888. (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
  1889. $getobj=sub {
  1890. $gettext_obj=eval q{
  1891. use Locale::gettext q{textdomain};
  1892. Locale::gettext->domain('ikiwiki')
  1893. };
  1894. };
  1895. }
  1896. no warnings 'redefine';
  1897. *gettext=sub {
  1898. $getobj->() if $getobj;
  1899. if ($gettext_obj) {
  1900. $gettext_obj->get(shift);
  1901. }
  1902. else {
  1903. return shift;
  1904. }
  1905. };
  1906. *ngettext=sub {
  1907. $getobj->() if $getobj;
  1908. if ($gettext_obj) {
  1909. $gettext_obj->nget(@_);
  1910. }
  1911. else {
  1912. return ($_[2] == 1 ? $_[0] : $_[1])
  1913. }
  1914. };
  1915. }
  1916. sub gettext {
  1917. define_gettext();
  1918. gettext(@_);
  1919. }
  1920. sub ngettext {
  1921. define_gettext();
  1922. ngettext(@_);
  1923. }
  1924. sub yesno ($) {
  1925. my $val=shift;
  1926. return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
  1927. }
  1928. sub inject {
  1929. # Injects a new function into the symbol table to replace an
  1930. # exported function.
  1931. my %params=@_;
  1932. # This is deep ugly perl foo, beware.
  1933. no strict;
  1934. no warnings;
  1935. if (! defined $params{parent}) {
  1936. $params{parent}='::';
  1937. $params{old}=\&{$params{name}};
  1938. $params{name}=~s/.*:://;
  1939. }
  1940. my $parent=$params{parent};
  1941. foreach my $ns (grep /^\w+::/, keys %{$parent}) {
  1942. $ns = $params{parent} . $ns;
  1943. inject(%params, parent => $ns) unless $ns eq '::main::';
  1944. *{$ns . $params{name}} = $params{call}
  1945. if exists ${$ns}{$params{name}} &&
  1946. \&{${$ns}{$params{name}}} == $params{old};
  1947. }
  1948. use strict;
  1949. use warnings;
  1950. }
  1951. sub add_link ($$;$) {
  1952. my $page=shift;
  1953. my $link=shift;
  1954. my $type=shift;
  1955. push @{$links{$page}}, $link
  1956. unless grep { $_ eq $link } @{$links{$page}};
  1957. if (defined $type) {
  1958. $typedlinks{$page}{$type}{$link} = 1;
  1959. }
  1960. }
  1961. sub add_autofile ($$$) {
  1962. my $file=shift;
  1963. my $plugin=shift;
  1964. my $generator=shift;
  1965. $autofiles{$file}{plugin}=$plugin;
  1966. $autofiles{$file}{generator}=$generator;
  1967. }
  1968. sub sortspec_translate ($$) {
  1969. my $spec = shift;
  1970. my $reverse = shift;
  1971. my $code = "";
  1972. my @data;
  1973. while ($spec =~ m{
  1974. \s*
  1975. (-?) # group 1: perhaps negated
  1976. \s*
  1977. ( # group 2: a word
  1978. \w+\([^\)]*\) # command(params)
  1979. |
  1980. [^\s]+ # or anything else
  1981. )
  1982. \s*
  1983. }gx) {
  1984. my $negated = $1;
  1985. my $word = $2;
  1986. my $params = undef;
  1987. if ($word =~ m/^(\w+)\((.*)\)$/) {
  1988. # command with parameters
  1989. $params = $2;
  1990. $word = $1;
  1991. }
  1992. elsif ($word !~ m/^\w+$/) {
  1993. error(sprintf(gettext("invalid sort type %s"), $word));
  1994. }
  1995. if (length $code) {
  1996. $code .= " || ";
  1997. }
  1998. if ($negated) {
  1999. $code .= "-";
  2000. }
  2001. if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
  2002. if (defined $params) {
  2003. push @data, $params;
  2004. $code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
  2005. }
  2006. else {
  2007. $code .= "IkiWiki::SortSpec::cmp_$word(undef)";
  2008. }
  2009. }
  2010. else {
  2011. error(sprintf(gettext("unknown sort type %s"), $word));
  2012. }
  2013. }
  2014. if (! length $code) {
  2015. # undefined sorting method... sort arbitrarily
  2016. return sub { 0 };
  2017. }
  2018. if ($reverse) {
  2019. $code="-($code)";
  2020. }
  2021. no warnings;
  2022. return eval 'sub { '.$code.' }';
  2023. }
  2024. sub pagespec_translate ($) {
  2025. my $spec=shift;
  2026. # Convert spec to perl code.
  2027. my $code="";
  2028. my @data;
  2029. while ($spec=~m{
  2030. \s* # ignore whitespace
  2031. ( # 1: match a single word
  2032. \! # !
  2033. |
  2034. \( # (
  2035. |
  2036. \) # )
  2037. |
  2038. \w+\([^\)]*\) # command(params)
  2039. |
  2040. [^\s()]+ # any other text
  2041. )
  2042. \s* # ignore whitespace
  2043. }gx) {
  2044. my $word=$1;
  2045. if (lc $word eq 'and') {
  2046. $code.=' &';
  2047. }
  2048. elsif (lc $word eq 'or') {
  2049. $code.=' |';
  2050. }
  2051. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  2052. $code.=' '.$word;
  2053. }
  2054. elsif ($word =~ /^(\w+)\((.*)\)$/) {
  2055. if (exists $IkiWiki::PageSpec::{"match_$1"}) {
  2056. push @data, $2;
  2057. $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
  2058. }
  2059. else {
  2060. push @data, qq{unknown function in pagespec "$word"};
  2061. $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
  2062. }
  2063. }
  2064. else {
  2065. push @data, $word;
  2066. $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
  2067. }
  2068. }
  2069. if (! length $code) {
  2070. $code="IkiWiki::FailReason->new('empty pagespec')";
  2071. }
  2072. no warnings;
  2073. return eval 'sub { my $page=shift; '.$code.' }';
  2074. }
  2075. sub pagespec_match ($$;@) {
  2076. my $page=shift;
  2077. my $spec=shift;
  2078. my @params=@_;
  2079. # Backwards compatability with old calling convention.
  2080. if (@params == 1) {
  2081. unshift @params, 'location';
  2082. }
  2083. my $sub=pagespec_translate($spec);
  2084. return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
  2085. if ! defined $sub;
  2086. return $sub->($page, @params);
  2087. }
  2088. sub pagespec_match_list ($$;@) {
  2089. my $page=shift;
  2090. my $pagespec=shift;
  2091. my %params=@_;
  2092. # Backwards compatability with old calling convention.
  2093. if (ref $page) {
  2094. print STDERR "warning: a plugin (".caller().") is using pagespec_match_list in an obsolete way, and needs to be updated\n";
  2095. $params{list}=$page;
  2096. $page=$params{location}; # ugh!
  2097. }
  2098. my $sub=pagespec_translate($pagespec);
  2099. error "syntax error in pagespec \"$pagespec\""
  2100. if ! defined $sub;
  2101. my $sort=sortspec_translate($params{sort}, $params{reverse})
  2102. if defined $params{sort};
  2103. my @candidates;
  2104. if (exists $params{list}) {
  2105. @candidates=exists $params{filter}
  2106. ? grep { ! $params{filter}->($_) } @{$params{list}}
  2107. : @{$params{list}};
  2108. }
  2109. else {
  2110. @candidates=exists $params{filter}
  2111. ? grep { ! $params{filter}->($_) } keys %pagesources
  2112. : keys %pagesources;
  2113. }
  2114. # clear params, remainder is passed to pagespec
  2115. $depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
  2116. my $num=$params{num};
  2117. delete @params{qw{num deptype reverse sort filter list}};
  2118. # when only the top matches will be returned, it's efficient to
  2119. # sort before matching to pagespec,
  2120. if (defined $num && defined $sort) {
  2121. @candidates=IkiWiki::SortSpec::sort_pages(
  2122. $sort, @candidates);
  2123. }
  2124. my @matches;
  2125. my $firstfail;
  2126. my $count=0;
  2127. my $accum=IkiWiki::SuccessReason->new();
  2128. foreach my $p (@candidates) {
  2129. my $r=$sub->($p, %params, location => $page);
  2130. error(sprintf(gettext("cannot match pages: %s"), $r))
  2131. if $r->isa("IkiWiki::ErrorReason");
  2132. unless ($r || $r->influences_static) {
  2133. $r->remove_influence($p);
  2134. }
  2135. $accum |= $r;
  2136. if ($r) {
  2137. push @matches, $p;
  2138. last if defined $num && ++$count == $num;
  2139. }
  2140. }
  2141. # Add simple dependencies for accumulated influences.
  2142. my $i=$accum->influences;
  2143. foreach my $k (keys %$i) {
  2144. $depends_simple{$page}{lc $k} |= $i->{$k};
  2145. }
  2146. # when all matches will be returned, it's efficient to
  2147. # sort after matching
  2148. if (! defined $num && defined $sort) {
  2149. return IkiWiki::SortSpec::sort_pages(
  2150. $sort, @matches);
  2151. }
  2152. else {
  2153. return @matches;
  2154. }
  2155. }
  2156. sub pagespec_valid ($) {
  2157. my $spec=shift;
  2158. return defined pagespec_translate($spec);
  2159. }
  2160. sub glob2re ($) {
  2161. my $re=quotemeta(shift);
  2162. $re=~s/\\\*/.*/g;
  2163. $re=~s/\\\?/./g;
  2164. return qr/^$re$/i;
  2165. }
  2166. package IkiWiki::FailReason;
  2167. use overload (
  2168. '""' => sub { $_[0][0] },
  2169. '0+' => sub { 0 },
  2170. '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
  2171. '&' => sub { $_[0]->merge_influences($_[1], 1); $_[0] },
  2172. '|' => sub { $_[1]->merge_influences($_[0]); $_[1] },
  2173. fallback => 1,
  2174. );
  2175. our @ISA = 'IkiWiki::SuccessReason';
  2176. package IkiWiki::SuccessReason;
  2177. use overload (
  2178. '""' => sub { $_[0][0] },
  2179. '0+' => sub { 1 },
  2180. '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
  2181. '&' => sub { $_[1]->merge_influences($_[0], 1); $_[1] },
  2182. '|' => sub { $_[0]->merge_influences($_[1]); $_[0] },
  2183. fallback => 1,
  2184. );
  2185. sub new {
  2186. my $class = shift;
  2187. my $value = shift;
  2188. return bless [$value, {@_}], $class;
  2189. }
  2190. sub influences {
  2191. my $this=shift;
  2192. $this->[1]={@_} if @_;
  2193. my %i=%{$this->[1]};
  2194. delete $i{""};
  2195. return \%i;
  2196. }
  2197. sub influences_static {
  2198. return ! $_[0][1]->{""};
  2199. }
  2200. sub merge_influences {
  2201. my $this=shift;
  2202. my $other=shift;
  2203. my $anded=shift;
  2204. if (! $anded || (($this || %{$this->[1]}) &&
  2205. ($other || %{$other->[1]}))) {
  2206. foreach my $influence (keys %{$other->[1]}) {
  2207. $this->[1]{$influence} |= $other->[1]{$influence};
  2208. }
  2209. }
  2210. else {
  2211. # influence blocker
  2212. $this->[1]={};
  2213. }
  2214. }
  2215. sub remove_influence {
  2216. my $this=shift;
  2217. my $torm=shift;
  2218. delete $this->[1]{$torm};
  2219. }
  2220. package IkiWiki::ErrorReason;
  2221. our @ISA = 'IkiWiki::FailReason';
  2222. package IkiWiki::PageSpec;
  2223. sub derel ($$) {
  2224. my $path=shift;
  2225. my $from=shift;
  2226. if ($path =~ m!^\.(/|$)!) {
  2227. if ($1) {
  2228. $from=~s#/?[^/]+$## if defined $from;
  2229. $path=~s#^\./##;
  2230. $path="$from/$path" if defined $from && length $from;
  2231. }
  2232. else {
  2233. $path = $from;
  2234. $path = "" unless defined $path;
  2235. }
  2236. }
  2237. return $path;
  2238. }
  2239. my %glob_cache;
  2240. sub match_glob ($$;@) {
  2241. my $page=shift;
  2242. my $glob=shift;
  2243. my %params=@_;
  2244. $glob=derel($glob, $params{location});
  2245. # Instead of converting the glob to a regex every time,
  2246. # cache the compiled regex to save time.
  2247. my $re=$glob_cache{$glob};
  2248. unless (defined $re) {
  2249. $glob_cache{$glob} = $re = IkiWiki::glob2re($glob);
  2250. }
  2251. if ($page =~ $re) {
  2252. if (! IkiWiki::isinternal($page) || $params{internal}) {
  2253. return IkiWiki::SuccessReason->new("$glob matches $page");
  2254. }
  2255. else {
  2256. return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
  2257. }
  2258. }
  2259. else {
  2260. return IkiWiki::FailReason->new("$glob does not match $page");
  2261. }
  2262. }
  2263. sub match_internal ($$;@) {
  2264. return match_glob(shift, shift, @_, internal => 1)
  2265. }
  2266. sub match_page ($$;@) {
  2267. my $page=shift;
  2268. my $match=match_glob($page, shift, @_);
  2269. if ($match) {
  2270. my $source=exists $IkiWiki::pagesources{$page} ?
  2271. $IkiWiki::pagesources{$page} :
  2272. $IkiWiki::delpagesources{$page};
  2273. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  2274. if (! defined $type) {
  2275. return IkiWiki::FailReason->new("$page is not a page");
  2276. }
  2277. }
  2278. return $match;
  2279. }
  2280. sub match_link ($$;@) {
  2281. my $page=shift;
  2282. my $link=lc(shift);
  2283. my %params=@_;
  2284. $link=derel($link, $params{location});
  2285. my $from=exists $params{location} ? $params{location} : '';
  2286. my $linktype=$params{linktype};
  2287. my $qualifier='';
  2288. if (defined $linktype) {
  2289. $qualifier=" with type $linktype";
  2290. }
  2291. my $links = $IkiWiki::links{$page};
  2292. return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2293. unless $links && @{$links};
  2294. my $bestlink = IkiWiki::bestlink($from, $link);
  2295. foreach my $p (@{$links}) {
  2296. next unless (! defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p});
  2297. if (length $bestlink) {
  2298. if ($bestlink eq IkiWiki::bestlink($page, $p)) {
  2299. return IkiWiki::SuccessReason->new("$page links to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2300. }
  2301. }
  2302. else {
  2303. if (match_glob($p, $link, %params)) {
  2304. return IkiWiki::SuccessReason->new("$page links to page $p$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2305. }
  2306. my ($p_rel)=$p=~/^\/?(.*)/;
  2307. $link=~s/^\///;
  2308. if (match_glob($p_rel, $link, %params)) {
  2309. return IkiWiki::SuccessReason->new("$page links to page $p_rel$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2310. }
  2311. }
  2312. }
  2313. return IkiWiki::FailReason->new("$page does not link to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1);
  2314. }
  2315. sub match_backlink ($$;@) {
  2316. my $ret=match_link($_[1], $_[0], @_);
  2317. $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
  2318. return $ret;
  2319. }
  2320. sub match_created_before ($$;@) {
  2321. my $page=shift;
  2322. my $testpage=shift;
  2323. my %params=@_;
  2324. $testpage=derel($testpage, $params{location});
  2325. if (exists $IkiWiki::pagectime{$testpage}) {
  2326. if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
  2327. return IkiWiki::SuccessReason->new("$page created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2328. }
  2329. else {
  2330. return IkiWiki::FailReason->new("$page not created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2331. }
  2332. }
  2333. else {
  2334. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2335. }
  2336. }
  2337. sub match_created_after ($$;@) {
  2338. my $page=shift;
  2339. my $testpage=shift;
  2340. my %params=@_;
  2341. $testpage=derel($testpage, $params{location});
  2342. if (exists $IkiWiki::pagectime{$testpage}) {
  2343. if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
  2344. return IkiWiki::SuccessReason->new("$page created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2345. }
  2346. else {
  2347. return IkiWiki::FailReason->new("$page not created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2348. }
  2349. }
  2350. else {
  2351. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2352. }
  2353. }
  2354. sub match_creation_day ($$;@) {
  2355. my $page=shift;
  2356. my $d=shift;
  2357. if ($d !~ /^\d+$/) {
  2358. return IkiWiki::ErrorReason->new("invalid day $d");
  2359. }
  2360. if ((localtime($IkiWiki::pagectime{$page}))[3] == $d) {
  2361. return IkiWiki::SuccessReason->new('creation_day matched');
  2362. }
  2363. else {
  2364. return IkiWiki::FailReason->new('creation_day did not match');
  2365. }
  2366. }
  2367. sub match_creation_month ($$;@) {
  2368. my $page=shift;
  2369. my $m=shift;
  2370. if ($m !~ /^\d+$/) {
  2371. return IkiWiki::ErrorReason->new("invalid month $m");
  2372. }
  2373. if ((localtime($IkiWiki::pagectime{$page}))[4] + 1 == $m) {
  2374. return IkiWiki::SuccessReason->new('creation_month matched');
  2375. }
  2376. else {
  2377. return IkiWiki::FailReason->new('creation_month did not match');
  2378. }
  2379. }
  2380. sub match_creation_year ($$;@) {
  2381. my $page=shift;
  2382. my $y=shift;
  2383. if ($y !~ /^\d+$/) {
  2384. return IkiWiki::ErrorReason->new("invalid year $y");
  2385. }
  2386. if ((localtime($IkiWiki::pagectime{$page}))[5] + 1900 == $y) {
  2387. return IkiWiki::SuccessReason->new('creation_year matched');
  2388. }
  2389. else {
  2390. return IkiWiki::FailReason->new('creation_year did not match');
  2391. }
  2392. }
  2393. sub match_user ($$;@) {
  2394. shift;
  2395. my $user=shift;
  2396. my %params=@_;
  2397. my $regexp=IkiWiki::glob2re($user);
  2398. if (! exists $params{user}) {
  2399. return IkiWiki::ErrorReason->new("no user specified");
  2400. }
  2401. if (defined $params{user} && $params{user}=~$regexp) {
  2402. return IkiWiki::SuccessReason->new("user is $user");
  2403. }
  2404. elsif (! defined $params{user}) {
  2405. return IkiWiki::FailReason->new("not logged in");
  2406. }
  2407. else {
  2408. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  2409. }
  2410. }
  2411. sub match_admin ($$;@) {
  2412. shift;
  2413. shift;
  2414. my %params=@_;
  2415. if (! exists $params{user}) {
  2416. return IkiWiki::ErrorReason->new("no user specified");
  2417. }
  2418. if (defined $params{user} && IkiWiki::is_admin($params{user})) {
  2419. return IkiWiki::SuccessReason->new("user is an admin");
  2420. }
  2421. elsif (! defined $params{user}) {
  2422. return IkiWiki::FailReason->new("not logged in");
  2423. }
  2424. else {
  2425. return IkiWiki::FailReason->new("user is not an admin");
  2426. }
  2427. }
  2428. sub match_ip ($$;@) {
  2429. shift;
  2430. my $ip=shift;
  2431. my %params=@_;
  2432. if (! exists $params{ip}) {
  2433. return IkiWiki::ErrorReason->new("no IP specified");
  2434. }
  2435. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  2436. return IkiWiki::SuccessReason->new("IP is $ip");
  2437. }
  2438. else {
  2439. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  2440. }
  2441. }
  2442. package IkiWiki::SortSpec;
  2443. # This is in the SortSpec namespace so that the $a and $b that sort() uses
  2444. # are easily available in this namespace, for cmp functions to use them.
  2445. sub sort_pages {
  2446. my $f=shift;
  2447. sort $f @_
  2448. }
  2449. sub cmp_title {
  2450. IkiWiki::pagetitle(IkiWiki::basename($a))
  2451. cmp
  2452. IkiWiki::pagetitle(IkiWiki::basename($b))
  2453. }
  2454. sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
  2455. sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }
  2456. 1