wiki.pl 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112
  1. #! /usr/bin/perl
  2. # Copyright (C) 2001-2018
  3. # Alex Schroeder <alex@gnu.org>
  4. # Copyright (C) 2014-2015
  5. # Alex Jakimenko <alex.jakimenko@gmail.com>
  6. # Copyleft 2008 Brian Curry <http://www.raiazome.com>
  7. # ... including lots of patches from the UseModWiki site
  8. # Copyright (C) 2001, 2002 various authors
  9. # ... which was based on UseModWiki version 0.92 (April 21, 2001)
  10. # Copyright (C) 2000, 2001 Clifford A. Adams
  11. # <caadams@frontiernet.net> or <usemod@usemod.com>
  12. # ... which was based on the GPLed AtisWiki 0.3
  13. # Copyright (C) 1998 Markus Denker <marcus@ira.uka.de>
  14. # ... which was based on the LGPLed CVWiki CVS-patches
  15. # Copyright (C) 1997 Peter Merel
  16. # ... and The Original WikiWikiWeb
  17. # Copyright (C) 1996, 1997 Ward Cunningham <ward@c2.com>
  18. # (code reused with permission)
  19. # This program is free software: you can redistribute it and/or modify it under
  20. # the terms of the GNU General Public License as published by the Free Software
  21. # Foundation, either version 3 of the License, or (at your option) any later
  22. # version.
  23. #
  24. # This program is distributed in the hope that it will be useful, but WITHOUT
  25. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  26. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License along with
  29. # this program. If not, see <http://www.gnu.org/licenses/>.
  30. package OddMuse;
  31. use strict;
  32. use warnings;
  33. no warnings 'numeric';
  34. no warnings 'uninitialized';
  35. use utf8; # in case anybody ever adds UTF8 characters to the source
  36. use B;
  37. use CGI qw/-utf8/;
  38. use CGI::Carp qw(fatalsToBrowser);
  39. use File::Glob ':glob';
  40. use Encode qw(encode_utf8 decode_utf8);
  41. use sigtrap 'handler' => \&HandleSignals, 'normal-signals', 'error-signals';
  42. local $| = 1; # Do not buffer output (localized for mod_perl)
  43. # Options:
  44. our ($ScriptName, $FullUrl, $PageDir, $TempDir, $LockDir, $KeepDir, $RssDir,
  45. $RcFile, $RcOldFile, $IndexFile, $NoEditFile, $VisitorFile, $DeleteFile, $RssLicense,
  46. $FreeLinkPattern, $LinkPattern, $FreeInterLinkPattern, $InterLinkPattern,
  47. $UrlPattern, $FullUrlPattern, $InterSitePattern,
  48. $UrlProtocols, $ImageExtensions, $LastUpdate,
  49. %LockOnCreation, %PlainTextPages, %AdminPages,
  50. @MyAdminCode, @MyFormChanges, @MyInitVariables, @MyMacros, @MyMaintenance,
  51. $DocumentHeader, %HtmlEnvironmentContainers, $FS, $Counter, @Debugging);
  52. # Internal variables:
  53. our ($q, $bol, $OpenPageName, %Page, %Translate, %IndexHash, @IndexList,
  54. @HtmlStack, @HtmlAttrStack, @Blocks, @Flags,
  55. %Includes, $FootnoteNumber, $CollectingJournal, $HeaderIsPrinted,
  56. %Locks, $Fragment, $Today, $ModulesDescription, %RssInterwikiTranslate,
  57. $Message, $Now, %RecentVisitors, %MyInc, $WikiDescription, %InterSite, %OldCookie);
  58. # Can be set outside the script: $DataDir, $UseConfig, $ConfigFile, $ModuleDir,
  59. # $ConfigPage, $AdminPass, $EditPass, $ScriptName, $FullUrl, $RunCGI.
  60. # 1 = load config file in the data directory
  61. our $UseConfig //= 1;
  62. # Main wiki directory
  63. our $DataDir;
  64. $DataDir ||= decode_utf8($ENV{WikiDataDir}) if $UseConfig;
  65. $DataDir ||= '/tmp/oddmuse'; # FIXME: /var/opt/oddmuse/wiki ?
  66. $DataDir = "./$DataDir" unless $DataDir =~ m!^(/|\./)!;
  67. our $ConfigFile;
  68. $ConfigFile ||= $ENV{WikiConfigFile} if $UseConfig;
  69. our $ModuleDir;
  70. $ModuleDir ||= $ENV{WikiModuleDir} if $UseConfig;
  71. our $ConfigPage ||= '';
  72. # 1 = Run script as CGI instead of loading as module
  73. our $RunCGI //= 1;
  74. # 1 = allow page views using wiki.pl/PageName
  75. our $UsePathInfo = 1;
  76. # -1 = disabled, 0 = 10s; 1 = partial HTML cache; 2 = HTTP/1.1 caching
  77. our $UseCache = 2;
  78. our $SiteName = 'Wiki'; # Name of site (used for titles)
  79. our $HomePage = 'HomePage'; # Home page
  80. our $CookieName = 'Wiki'; # Name for this wiki (for multi-wiki sites)
  81. our $MaxPost = 1024 * 210; # Maximum 210K posts (about 200K for pages)
  82. our $StyleSheet = ''; # URL for CSS stylesheet (like '/wiki.css')
  83. our $StyleSheetPage = ''; # Page for CSS sheet
  84. our $LogoUrl = ''; # URL for site logo ('' for no logo)
  85. our $NotFoundPg = ''; # Page for not-found links ('' for blank pg)
  86. our $EditAllowed = 1; # 0 = no, 1 = yes, 2 = comments pages only, 3 = comments only
  87. our $AdminPass //= ''; # Whitespace separated passwords.
  88. our $EditPass //= ''; # Whitespace separated passwords.
  89. our $PassHashFunction //= ''; # Name of the function to create hashes
  90. our $PassSalt //= ''; # Salt will be added to any password before hashing
  91. our $BannedHosts = 'BannedHosts'; # Page for banned hosts
  92. our $BannedCanRead = 1; # 1 = banned cannot edit, 0 = banned cannot read
  93. our $BannedContent = 'BannedContent'; # Page for banned content (usually for link-ban)
  94. our $WikiLinks = ''; # 1 = LinkPattern is a link
  95. our $FreeLinks = 1; # 1 = [[some text]] is a link
  96. our $UseQuestionmark = 1; # 1 = append questionmark to links to nonexisting pages
  97. our $BracketText = 1; # 1 = [URL desc] uses a description for the URL
  98. our $BracketWiki = 1; # 1 = [WikiLink desc] uses a desc for the local link
  99. our $NetworkFile = 1; # 1 = file: is a valid protocol for URLs
  100. our $AllNetworkFiles = 0; # 1 = file:///foo is allowed -- the default allows only file://foo
  101. our $InterMap = 'InterMap'; # name of the intermap page, '' = disable
  102. our $RssInterwikiTranslate = 'RssInterwikiTranslate'; # name of RSS interwiki translation page, '' = disable
  103. $ENV{PATH} = '/bin:/usr/bin'; # Path used to find 'diff' and 'grep'
  104. our $UseDiff = 1; # 1 = use diff
  105. our $SurgeProtection = 1; # 1 = protect against leeches
  106. our $SurgeProtectionTime = 20; # Size of the protected window in seconds
  107. our $SurgeProtectionViews = 20; # How many page views to allow in this window
  108. our $DeletedPage = 'DeletedPage'; # Pages starting with this can be deleted
  109. our $RCName = 'RecentChanges'; # Name of changes page
  110. our @RcDays = qw(1 3 7 30 90); # Days for links on RecentChanges
  111. our $RcDefault = 30; # Default number of RecentChanges days
  112. our $KeepHostDays = 4; # Days to keep IP numbers for
  113. our $KeepDays = 0; # Days to keep old revisions (0 means keep forever)
  114. our $KeepMajor = 1; # 1 = keep at least one major rev when expiring pages
  115. our $SummaryHours = 4; # Hours to offer the old subject when editing a page
  116. our $SummaryDefaultLength = 150; # Length of default text for summary (0 to disable)
  117. our $ShowEdits = 0; # 1 = major and show minor edits in recent changes
  118. our $ShowAll = 0; # 1 = show multiple edits per page in recent changes
  119. our $ShowRollbacks = 0; # 1 = show rollbacks in recent changes
  120. our $RecentLink = 1; # 1 = link to usernames
  121. our $PageCluster = ''; # name of cluster page, eg. 'Cluster' to enable
  122. our $InterWikiMoniker = ''; # InterWiki prefix for this wiki for RSS
  123. our $SiteDescription = ''; # RSS Description of this wiki
  124. our $RssStrip = '^\d\d\d\d-\d\d-\d\d_'; # Regexp to strip from feed item titles
  125. our $RssImageUrl = $LogoUrl; # URL to image to associate with your RSS feed
  126. our $RssRights = ''; # Copyright notice for RSS, usually an URL to the appropriate text
  127. our $RssExclude = 'RssExclude'; # name of the page that lists pages to be excluded from the feed
  128. our $RssCacheHours = 1; # How many hours to cache remote RSS files
  129. our $RssStyleSheet = ''; # External style sheet for RSS files
  130. our $UploadAllowed = 0; # 1 = yes, 0 = administrators only
  131. our @UploadTypes = ('image/jpeg', 'image/png'); # MIME types allowed, all allowed if empty list
  132. our $EmbedWiki = 0; # 1 = no headers/footers
  133. our $FooterNote = ''; # HTML for bottom of every page
  134. our $EditNote = ''; # HTML notice above buttons on edit page
  135. our $TopLinkBar = 1; # 0 = goto bar both at the top and bottom; 1 = top, 2 = bottom
  136. our $TopSearchForm = 1; # 0 = search form both at the top and bottom; 1 = top, 2 = bottom
  137. our $MatchingPages = 0; # 1 = search page content and page titles
  138. our @UserGotoBarPages = (); # List of pagenames
  139. our $UserGotoBar = ''; # HTML added to end of goto bar
  140. our $CommentsPrefix = ''; # prefix for comment pages, eg. 'Comments_on_' to enable
  141. our $CommentsPattern = undef; # regex used to match comment pages
  142. our $HtmlHeaders = ''; # Additional stuff to put in the HTML <head> section
  143. our $IndentLimit = 20; # Maximum depth of nested lists
  144. our $LanguageLimit = 3; # Number of matches req. for each language
  145. our $JournalLimit = 200; # how many pages can be collected in one go?
  146. our $PageNameLimit = 120; # max length of page name in bytes
  147. $DocumentHeader = qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN")
  148. . qq( "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n)
  149. . qq(<html xmlns="http://www.w3.org/1999/xhtml">);
  150. our @MyFooters = (\&GetCommentForm, \&WrapperEnd, \&DefaultFooter);
  151. # Checkboxes at the end of the index.
  152. our @IndexOptions = ();
  153. # Display short comments below the GotoBar for special days
  154. # Example: %SpecialDays = ('1-1' => 'New Year', '1-2' => 'Next Day');
  155. our %SpecialDays = ();
  156. # Replace regular expressions with inlined images
  157. # Example: %Smilies = (":-?D(?=\\W)" => '/pics/grin.png');
  158. our %Smilies = ();
  159. # Detect page languages when saving edits
  160. # Example: %Languages = ('de' => '\b(der|die|das|und|oder)\b');
  161. our %Languages = ();
  162. our @KnownLocks = qw(main diff index merge visitors); # locks to remove
  163. our $LockExpiration = 60; # How long before expirable locks are expired
  164. our %LockExpires = (diff=>1, index=>1, merge=>1, visitors=>1); # locks to expire after some time
  165. our %LockCleaners = (); # What to do if a job under a lock gets a signal like SIGINT. e.g. 'diff' => \&CleanDiff
  166. our %CookieParameters = (username=>'', pwd=>'', homepage=>'', theme=>'', css=>'', msg=>'', lang=>'', embed=>$EmbedWiki,
  167. toplinkbar=>$TopLinkBar, topsearchform=>$TopSearchForm, matchingpages=>$MatchingPages, );
  168. our %Action = (rc => \&BrowseRc, rollback => \&DoRollback,
  169. browse => \&BrowseResolvedPage, maintain => \&DoMaintain,
  170. random => \&DoRandom, pagelock => \&DoPageLock,
  171. history => \&DoHistory, editlock => \&DoEditLock,
  172. edit => \&DoEdit, version => \&DoShowVersion,
  173. download => \&DoDownload, rss => \&DoRss,
  174. unlock => \&DoUnlock, password => \&DoPassword,
  175. index => \&DoIndex, admin => \&DoAdminPage,
  176. clear => \&DoClearCache, debug => \&DoDebug,
  177. contrib => \&DoContributors, more => \&DoJournal);
  178. our @MyRules = (\&LinkRules, \&ListRule); # don't set this variable, add to it!
  179. our %RuleOrder = (\&LinkRules => 0, \&ListRule => 0);
  180. # The 'main' program, called at the end of this script file (aka. as handler)
  181. sub DoWikiRequest {
  182. Init();
  183. DoSurgeProtection();
  184. if (not $BannedCanRead and UserIsBanned() and not UserIsEditor()) {
  185. ReportError(T('Reading not allowed: user, ip, or network is blocked.'), '403 FORBIDDEN',
  186. 0, $q->p(ScriptLink('action=password', T('Login'), 'password')));
  187. }
  188. DoBrowseRequest();
  189. }
  190. sub ReportError { # fatal!
  191. my ($errmsg, $status, $log, @html) = @_;
  192. InitRequest(); # make sure we can report errors before InitRequest
  193. print GetHttpHeader('text/html', 'nocache', $status), GetHtmlHeader(T('Error')),
  194. $q->start_div({class=>"error"}), $q->h1(QuoteHtml($errmsg)), @html, $q->end_div,
  195. $q->end_html, "\n\n"; # newlines for FCGI because of exit()
  196. WriteStringToFile("$TempDir/error", '<body>' . $q->h1("$status $errmsg") . $q->Dump) if $log;
  197. map { ReleaseLockDir($_); } keys %Locks;
  198. exit 2;
  199. }
  200. sub Init {
  201. binmode(STDOUT, ':encoding(UTF-8)'); # this is where the HTML gets printed
  202. binmode(STDERR, ':encoding(UTF-8)'); # just in case somebody prints debug info to stderr
  203. InitDirConfig();
  204. $FS = "\x1e"; # The FS character is the RECORD SEPARATOR control char in ASCII
  205. $Message = ''; # Warnings and non-fatal errors.
  206. InitLinkPatterns(); # Link pattern can be changed in config files
  207. InitModules(); # Modules come first so that users can change module variables in config
  208. InitConfig(); # Config comes as early as possible; remember $q is not available here
  209. InitRequest(); # get $q with $MaxPost; set these in the config file
  210. InitCookie(); # After InitRequest, because $q is used
  211. InitVariables(); # After config, to change variables, after InitCookie for GetParam
  212. }
  213. sub InitModules {
  214. if ($UseConfig and $ModuleDir and IsDir($ModuleDir)) {
  215. foreach my $lib (Glob("$ModuleDir/*.p[ml]")) {
  216. if (not $MyInc{$lib}) {
  217. $MyInc{$lib} = 1; # Cannot use %INC in mod_perl settings
  218. my $file = encode_utf8($lib);
  219. do $file;
  220. $Message .= CGI::p("$lib: $@") if $@; # no $q exists, yet
  221. }
  222. }
  223. }
  224. }
  225. sub InitConfig {
  226. if ($UseConfig and $ConfigFile and not $INC{$ConfigFile} and IsFile($ConfigFile)) {
  227. do $ConfigFile; # these options must be set in a wrapper script or via the environment
  228. $Message .= CGI::p("$ConfigFile: $@") if $@; # remember, no $q exists, yet
  229. }
  230. if ($ConfigPage) { # $FS and $MaxPost must be set in config file!
  231. my ($status, $data) = ReadFile(GetPageFile(FreeToNormal($ConfigPage)));
  232. my $page = ParseData($data); # before InitVariables so GetPageContent won't work
  233. eval $page->{text} if $page->{text}; # perlcritic dislikes the use of eval here but we really mean it
  234. $Message .= CGI::p("$ConfigPage: $@") if $@;
  235. }
  236. }
  237. sub InitDirConfig {
  238. $PageDir = "$DataDir/page"; # Stores page data
  239. $KeepDir = "$DataDir/keep"; # Stores kept (old) page data
  240. $TempDir = "$DataDir/temp"; # Temporary files and locks
  241. $LockDir = "$TempDir/lock"; # DB is locked if this exists
  242. $NoEditFile = "$DataDir/noedit"; # Indicates that the site is read-only
  243. $RcFile = "$DataDir/rc.log"; # New RecentChanges logfile
  244. $RcOldFile = "$DataDir/oldrc.log"; # Old RecentChanges logfile
  245. $IndexFile = "$DataDir/pageidx"; # List of all pages
  246. $VisitorFile = "$DataDir/visitors.log"; # List of recent visitors
  247. $DeleteFile = "$DataDir/delete.log"; # Deletion logfile
  248. $RssDir = "$DataDir/rss"; # For rss feed cache
  249. $ConfigFile ||= "$DataDir/config"; # Config file with Perl code to execute
  250. $ModuleDir ||= "$DataDir/modules"; # For extensions (ending in .pm or .pl)
  251. }
  252. sub InitRequest { # set up $q
  253. $CGI::POST_MAX = $MaxPost;
  254. $q ||= new CGI;
  255. }
  256. sub InitVariables { # Init global session variables for mod_perl!
  257. $WikiDescription = $q->p($q->a({-href=>'https://www.oddmuse.org/'}, 'Oddmuse'),
  258. $Counter++ > 0 ? Ts('%s calls', $Counter) : '');
  259. $WikiDescription .= $ModulesDescription if $ModulesDescription;
  260. $HeaderIsPrinted = 0; # print HTTP headers only once
  261. $ScriptName //= $q->url(); # URL used in links
  262. $FullUrl ||= $ScriptName; # URL used in forms
  263. %Locks = ();
  264. @Blocks = ();
  265. @Flags = ();
  266. $Fragment = '';
  267. %RecentVisitors = ();
  268. $OpenPageName = ''; # Currently open page
  269. my $add_space = $CommentsPrefix =~ /[ \t_]$/;
  270. $$_ = FreeToNormal($$_) for # convert spaces to underscores on all configurable pagenames
  271. (\$HomePage, \$RCName, \$BannedHosts, \$InterMap, \$StyleSheetPage, \$CommentsPrefix,
  272. \$ConfigPage, \$NotFoundPg, \$RssInterwikiTranslate, \$BannedContent, \$RssExclude, );
  273. $CommentsPrefix .= '_' if $add_space;
  274. $CommentsPattern = "^$CommentsPrefix(.*)" unless defined $CommentsPattern or not $CommentsPrefix;
  275. @UserGotoBarPages = ($HomePage, $RCName) unless @UserGotoBarPages;
  276. my @pages = sort($BannedHosts, $StyleSheetPage, $ConfigPage, $InterMap,
  277. $RssInterwikiTranslate, $BannedContent);
  278. %AdminPages = map { $_ => 1} @pages, $RssExclude unless %AdminPages;
  279. %LockOnCreation = map { $_ => 1} @pages unless %LockOnCreation;
  280. %PlainTextPages = ($BannedHosts => 1, $BannedContent => 1,
  281. $StyleSheetPage => 1, $ConfigPage => 1) unless %PlainTextPages;
  282. delete $PlainTextPages{''}; # $ConfigPage and others might be empty.
  283. CreateDir($DataDir); # Create directory if it doesn't exist
  284. $Now = time; # Reset in case script is persistent
  285. my $ts = Modified($IndexFile); # always stat for multiple server processes
  286. ReInit() if not $ts or $LastUpdate != $ts; # reinit if another process changed files (requires $DataDir)
  287. $LastUpdate = $ts;
  288. unshift(@MyRules, \&MyRules) if defined(&MyRules) && (not @MyRules or $MyRules[0] != \&MyRules);
  289. @MyRules = sort {$RuleOrder{$a} <=> $RuleOrder{$b}} @MyRules; # default is 0
  290. ReportError(Ts('Cannot create %s', $DataDir) . ": $!", '500 INTERNAL SERVER ERROR') unless IsDir($DataDir);
  291. @IndexOptions = (['pages', T('Include normal pages'), 1, \&AllPagesList]);
  292. foreach my $sub (@MyInitVariables) {
  293. my $result = $sub->();
  294. $Message .= $q->p($@) if $@;
  295. }
  296. }
  297. sub ReInit { # init everything we need if we want to link to stuff
  298. my $id = shift; # when saving a page, what to do depends on the page being saved
  299. AllPagesList() unless $id;
  300. InterInit() if $InterMap and (not $id or $id eq $InterMap);
  301. %RssInterwikiTranslate = () if not $id or $id eq $RssInterwikiTranslate; # special since rarely used
  302. }
  303. sub InitCookie {
  304. undef $q->{'.cookies'}; # Clear cache if it exists (for SpeedyCGI)
  305. my $cookie = $q->cookie($CookieName);
  306. %OldCookie = split(/$FS/, UrlDecode($cookie));
  307. my %provided = map { $_ => 1 } $q->param;
  308. for my $key (keys %OldCookie) {
  309. SetParam($key, $OldCookie{$key}) unless $provided{$key};
  310. }
  311. CookieUsernameFix();
  312. CookieRollbackFix();
  313. }
  314. sub CookieUsernameFix {
  315. # Only valid usernames get stored in the new cookie.
  316. my $name = GetParam('username', '');
  317. $q->delete('username');
  318. if (not $name) {
  319. # do nothing
  320. } elsif ($WikiLinks and not $FreeLinks and $name !~ /^$LinkPattern$/) {
  321. $Message .= $q->p(Ts('Invalid UserName %s: not saved.', $name));
  322. } elsif ($FreeLinks and $name !~ /^$FreeLinkPattern$/) {
  323. $Message .= $q->p(Ts('Invalid UserName %s: not saved.', $name));
  324. } elsif (length($name) > 50) { # Too long
  325. $Message .= $q->p(T('UserName must be 50 characters or less: not saved'));
  326. } else {
  327. SetParam('username', $name);
  328. }
  329. }
  330. sub CookieRollbackFix {
  331. my @rollback = grep(/rollback-(\d+)/, $q->param);
  332. if (@rollback and $rollback[0] =~ /(\d+)/) {
  333. SetParam('to', $1);
  334. $q->delete('action');
  335. SetParam('action', 'rollback');
  336. }
  337. }
  338. sub GetParam {
  339. my ($name, $default) = @_;
  340. my $result = $q->param(encode_utf8($name));
  341. $result //= $default;
  342. return QuoteHtml($result); # you need to unquote anything that can have <tags>
  343. }
  344. sub SetParam {
  345. my ($name, $val) = @_;
  346. $q->param($name, $val);
  347. }
  348. sub InitLinkPatterns {
  349. my ($WikiWord, $QDelim);
  350. $QDelim = '(?:"")?'; # Optional quote delimiter (removed from the output)
  351. $WikiWord = '\p{Uppercase}+\p{Lowercase}+\p{Uppercase}\p{Alphabetic}*';
  352. $LinkPattern = "($WikiWord)$QDelim";
  353. $FreeLinkPattern = "([-,.()'%&!?;<> _1-9A-Za-z\x{0080}-\x{fffd}]|[-,.()'%&!?;<> _0-9A-Za-z\x{0080}-\x{fffd}][-,.()'%&!?;<> _0-9A-Za-z\x{0080}-\x{fffd}]+)"; # disallow "0" and must match HTML and plain text (ie. > and &gt;)
  354. # Intersites must start with uppercase letter to avoid confusion with URLs.
  355. $InterSitePattern = '[A-Z\x{0080}-\x{fffd}]+[A-Za-z\x{0080}-\x{fffd}]+';
  356. $InterLinkPattern = "($InterSitePattern:[-a-zA-Z0-9\x{0080}-\x{fffd}_=!?#\$\@~`\%&*+\\/:;.,]*[-a-zA-Z0-9\x{0080}-\x{fffd}_=#\$\@~`\%&*+\\/])$QDelim";
  357. $FreeInterLinkPattern = "($InterSitePattern:[-a-zA-Z0-9\x{0080}-\x{fffd}_=!?#\$\@~`\%&*+\\/:;.,()' ]+)"; # plus space and other characters, and no restrictions on the end of the pattern
  358. $UrlProtocols = 'http|https|ftp|afs|news|nntp|mid|cid|mailto|wais|prospero|telnet|gopher|irc|feed';
  359. $UrlProtocols .= '|file' if $NetworkFile;
  360. my $UrlChars = '[-a-zA-Z0-9/@=+$_~*.,;:?!\'"()&#%]'; # see RFC 2396
  361. my $EndChars = '[-a-zA-Z0-9/@=+$_~*]'; # no punctuation at the end of the url.
  362. $UrlPattern = "((?:$UrlProtocols):$UrlChars+$EndChars)";
  363. $FullUrlPattern="((?:$UrlProtocols):$UrlChars+)"; # when used in square brackets
  364. $ImageExtensions = '(gif|jpg|jpeg|png|bmp|svg)';
  365. }
  366. sub Clean {
  367. my $block = shift;
  368. return 0 unless defined($block); # "0" must print
  369. return 1 if $block eq ''; # '' is the result of a dirty rule
  370. $Fragment .= $block;
  371. return 1;
  372. }
  373. sub Dirty { # arg 1 is the raw text; the real output must be printed instead
  374. if ($Fragment ne '') {
  375. $Fragment =~ s|<p>\s*</p>||g; # clean up extra paragraphs (see end of ApplyRules)
  376. print $Fragment;
  377. push(@Blocks, $Fragment);
  378. push(@Flags, 0);
  379. }
  380. push(@Blocks, shift);
  381. push(@Flags, 1);
  382. $Fragment = '';
  383. }
  384. sub ApplyRules {
  385. # locallinks: apply rules that create links depending on local config (incl. interlink!)
  386. my ($text, $locallinks, $withanchors, $revision, @tags) = @_; # $revision is used for images
  387. $text =~ s/\r\n/\n/g; # DOS to Unix
  388. $text =~ s/\n+$//g; # No trailing paragraphs
  389. return if $text eq ''; # allow the text '0'
  390. local $Fragment = ''; # the clean HTML fragment not yet on @Blocks
  391. local @Blocks = (); # the list of cached HTML blocks
  392. local @Flags = (); # a list for each block, 1 = dirty, 0 = clean
  393. Clean(join('', map { AddHtmlEnvironment($_) } @tags));
  394. if ($OpenPageName and $PlainTextPages{$OpenPageName}) { # there should be no $PlainTextPages{''}
  395. Clean(CloseHtmlEnvironments() . $q->pre($text));
  396. } elsif (my ($type) = TextIsFile($text)) { # TODO? $type defined here??
  397. Clean(CloseHtmlEnvironments() . $q->p(T('This page contains an uploaded file:'))
  398. . $q->p(GetDownloadLink($OpenPageName, (substr($type, 0, 6) eq 'image/'), $revision))
  399. . (length $Page{summary} > 0 ? $q->blockquote(QuoteHtml($Page{summary})) : $q->p(T('No summary was provided for this file.'))));
  400. } else {
  401. my $smileyregex = join "|", keys %Smilies;
  402. $smileyregex = qr/(?=$smileyregex)/;
  403. local $_ = $text;
  404. local $bol = 1;
  405. while (1) {
  406. # Block level elements should eat trailing empty lines to prevent empty p elements.
  407. if ($bol and m/\G(\s*\n)+/cg) {
  408. Clean(CloseHtmlEnvironments() . AddHtmlEnvironment('p'));
  409. } elsif ($bol and m/\G(\&lt;include(\s+(text|with-anchors))?\s+"(.*)"\&gt;[ \t]*\n?)/cgi) {
  410. # <include "uri..."> includes the text of the given URI verbatim
  411. Clean(CloseHtmlEnvironments());
  412. Dirty($1);
  413. my ($oldpos, $old_, $type, $uri) = ((pos), $_, $3, UnquoteHtml($4)); # remember, page content is quoted!
  414. if ($uri =~ /^($UrlProtocols):/) {
  415. if ($type eq 'text') {
  416. print $q->pre({class=>"include $uri"}, QuoteHtml(GetRaw($uri)));
  417. } else { # never use local links for remote pages, with a starting tag
  418. print $q->start_div({class=>"include"});
  419. ApplyRules(QuoteHtml(GetRaw($uri)), 0, ($type eq 'with-anchors'), undef, 'p');
  420. print $q->end_div();
  421. }
  422. } else {
  423. $Includes{$OpenPageName} = 1;
  424. local $OpenPageName = FreeToNormal($uri);
  425. if ($type eq 'text') {
  426. print $q->pre({class=>"include $OpenPageName"}, QuoteHtml(GetPageContent($OpenPageName)));
  427. } elsif (not $Includes{$OpenPageName}) { # with a starting tag, watch out for recursion
  428. print $q->start_div({class=>"include $OpenPageName"});
  429. ApplyRules(QuoteHtml(GetPageContent($OpenPageName)), $locallinks, $withanchors, undef, 'p');
  430. print $q->end_div();
  431. delete $Includes{$OpenPageName};
  432. } else {
  433. print $q->p({-class=>'error'}, $q->strong(Ts('Recursive include of %s!', $OpenPageName)));
  434. }
  435. }
  436. Clean(AddHtmlEnvironment('p')); # if dirty block is looked at later, this will disappear
  437. ($_, pos) = ($old_, $oldpos); # restore \G (assignment order matters!)
  438. } elsif ($bol and m/\G(\&lt;(journal|titles):?(\d*)((\s+|:)(\d*),?(\d*))?(\s+"(.*?)")?(\s+(reverse|past|future))?(\s+search\s+(.*))?\&gt;[ \t]*\n?)/cgi) {
  439. # <journal 10 "regexp"> includes 10 pages matching regexp
  440. Clean(CloseHtmlEnvironments());
  441. Dirty($1);
  442. my ($oldpos, $old_) = (pos, $_); # remember these because of the call to PrintJournal()
  443. PrintJournal($6, $7, $9, $11, $3, $13, $2);
  444. Clean(AddHtmlEnvironment('p')); # if dirty block is looked at later, this will disappear
  445. ($_, pos) = ($old_, $oldpos); # restore \G (assignment order matters!)
  446. } elsif ($bol and m/\G(\&lt;rss(\s+(\d*))?\s+(.*?)\&gt;[ \t]*\n?)/cgis) {
  447. # <rss "uri..."> stores the parsed RSS of the given URI
  448. Clean(CloseHtmlEnvironments());
  449. Dirty($1);
  450. my ($oldpos, $old_) = (pos, $_); # remember these because of the call to RSS()
  451. print RSS($3 || 15, split(/\s+/, UnquoteHtml($4)));
  452. Clean(AddHtmlEnvironment('p')); # if dirty block is looked at later, this will disappear
  453. ($_, pos) = ($old_, $oldpos); # restore \G (assignment order matters!)
  454. } elsif (/\G(&lt;search (.*?)&gt;)/cgis) {
  455. # <search regexp>
  456. Clean(CloseHtmlEnvironments());
  457. Dirty($1);
  458. my ($oldpos, $old_) = (pos, $_);
  459. print $q->start_div({-class=>'search'});
  460. SearchTitleAndBody($2, \&PrintSearchResult, SearchRegexp($2));
  461. print $q->end_div;
  462. Clean(AddHtmlEnvironment('p')); # if dirty block is looked at later, this will disappear
  463. ($_, pos) = ($old_, $oldpos); # restore \G (assignment order matters!)
  464. } elsif ($bol and m/\G(&lt;&lt;&lt;&lt;&lt;&lt;&lt; )/cg) {
  465. my ($str, $count, $limit, $oldpos) = ($1, 0, 100, pos);
  466. while (m/\G(.*\n)/cg and $count++ < $limit) {
  467. $str .= $1;
  468. last if (substr($1, 0, 29) eq '&gt;&gt;&gt;&gt;&gt;&gt;&gt; ');
  469. }
  470. if ($count >= $limit) {
  471. pos = $oldpos; # reset because we did not find a match
  472. Clean('&lt;&lt;&lt;&lt;&lt;&lt;&lt; ');
  473. } else {
  474. Clean(CloseHtmlEnvironments() . $q->pre({-class=>'conflict'}, $str) . AddHtmlEnvironment('p'));
  475. }
  476. } elsif ($bol and m/\G#REDIRECT/cg) {
  477. Clean('#REDIRECT');
  478. } elsif (%Smilies and m/\G$smileyregex/cg and Clean(SmileyReplace())) {
  479. } elsif (Clean(RunMyRules($locallinks, $withanchors))) {
  480. } elsif (m/\G\s*\n(\s*\n)+/cg) { # paragraphs: at least two newlines
  481. Clean(CloseHtmlEnvironments() . AddHtmlEnvironment('p')); # another one like this further up
  482. } elsif (m/\G&amp;([A-Za-z]+|#[0-9]+|#x[A-Za-f0-9]+);/cg) { # entity references
  483. Clean("&$1;");
  484. } elsif (m/\G\s+/cg) {
  485. Clean(' ');
  486. } elsif (m/\G([A-Za-z\x{0080}-\x{fffd}]+([ \t]+[a-z\x{0080}-\x{fffd}]+)*[ \t]+)/cg
  487. or m/\G([A-Za-z\x{0080}-\x{fffd}]+)/cg or m/\G(\S)/cg) {
  488. Clean($1); # multiple words but do not match http://foo
  489. } else {
  490. last;
  491. }
  492. $bol = (substr($_, pos() - 1, 1) eq "\n");
  493. }
  494. }
  495. pos = length $_; # notify module functions we've completed rule handling
  496. Clean(CloseHtmlEnvironments()); # last block -- close it, cache it
  497. if ($Fragment ne '') {
  498. $Fragment =~ s|<p>\s*</p>||g; # clean up extra paragraphs (see end Dirty())
  499. print $Fragment;
  500. push(@Blocks, $Fragment);
  501. push(@Flags, 0);
  502. }
  503. # this can be stored in the page cache -- see PrintCache
  504. return (join($FS, @Blocks), join($FS, @Flags));
  505. }
  506. sub ListRule {
  507. if ($bol && m/\G(\s*\n)*(\*+)[ \t]+/cg
  508. or InElement('li') && m/\G(\s*\n)+(\*+)[ \t]+/cg) {
  509. return CloseHtmlEnvironmentUntil('li')
  510. . OpenHtmlEnvironment('ul', length($2)) . AddHtmlEnvironment('li');
  511. }
  512. return;
  513. }
  514. sub LinkRules {
  515. my ($locallinks, $withanchors) = @_;
  516. if ($locallinks
  517. and ($BracketText && m/\G(\[$InterLinkPattern\s+([^\]]+?)\])/cg
  518. or $BracketText && m/\G(\[\[$FreeInterLinkPattern\|([^\]]+?)\]\])/cg
  519. or m/\G(\[$InterLinkPattern\])/cg or m/\G(\[\[\[$FreeInterLinkPattern\]\]\])/cg
  520. or m/\G($InterLinkPattern)/cg or m/\G(\[\[$FreeInterLinkPattern\]\])/cg)) {
  521. # [InterWiki:FooBar text] or [InterWiki:FooBar] or
  522. # InterWiki:FooBar or [[InterWiki:foo bar|text]] or
  523. # [[InterWiki:foo bar]] or [[[InterWiki:foo bar]]]-- Interlinks
  524. # can change when the intermap changes (local config, therefore
  525. # depend on $locallinks). The intermap is only read if
  526. # necessary, so if this not an interlink, we have to backtrack a
  527. # bit.
  528. my $bracket = (substr($1, 0, 1) eq '[') # but \[\[$FreeInterLinkPattern\]\] it not bracket!
  529. && !((substr($1, 0, 2) eq '[[') && (substr($1, 2, 1) ne '[') && index($1, '|') < 0);
  530. my $quote = (substr($1, 0, 2) eq '[[');
  531. my ($oldmatch, $output) = ($1, GetInterLink($2, $3, $bracket, $quote)); # $3 may be empty
  532. if ($oldmatch eq $output) { # no interlink
  533. my ($site, $rest) = split(/:/, $oldmatch, 2);
  534. Clean($site);
  535. pos = (pos) - length($rest) - 1; # skip site, but reparse rest
  536. } else {
  537. Dirty($oldmatch);
  538. print $output; # this is an interlink
  539. }
  540. } elsif ($BracketText && m/\G(\[$FullUrlPattern[|[:space:]]([^\]]+?)\])/cg
  541. or $BracketText && m/\G(\[\[$FullUrlPattern[|[:space:]]([^\]]+?)\]\])/cg
  542. or m/\G(\[$FullUrlPattern\])/cg or m/\G($UrlPattern)/cg) {
  543. # [URL text] makes [text] link to URL, [URL] makes footnotes [1]
  544. my ($str, $url, $text, $bracket, $rest) = ($1, $2, $3, (substr($1, 0, 1) eq '['), '');
  545. if ($url =~ /(&lt|&gt|&amp)$/) { # remove trailing partial named entitites and add them as
  546. $rest = $1; # back again at the end as trailing text.
  547. $url =~ s/&(lt|gt|amp)$//;
  548. }
  549. if ($bracket and not defined $text) { # [URL] is dirty because the number may change
  550. Dirty($str);
  551. print GetUrl($url, $text, $bracket), $rest;
  552. } else {
  553. Clean(GetUrl($url, $text, $bracket, not $bracket) . $rest); # $text may be empty, no images in brackets
  554. }
  555. } elsif ($WikiLinks && m/\G!$LinkPattern/cg) {
  556. Clean($1); # ! gets eaten
  557. } elsif ($WikiLinks && $locallinks
  558. && ($BracketWiki && m/\G(\[$LinkPattern\s+([^\]]+?)\])/cg
  559. or m/\G(\[$LinkPattern\])/cg or m/\G($LinkPattern)/cg)) {
  560. # [LocalPage text], [LocalPage], LocalPage
  561. Dirty($1);
  562. my $bracket = (substr($1, 0, 1) eq '[' and not $3);
  563. print GetPageOrEditLink($2, $3, $bracket);
  564. } elsif ($locallinks && $FreeLinks && (m/\G(\[\[image:$FreeLinkPattern\]\])/cg
  565. or m/\G(\[\[image:$FreeLinkPattern\|([^]|]+)\]\])/cg)) {
  566. # [[image:Free Link]], [[image:Free Link|alt text]]
  567. Dirty($1);
  568. print GetDownloadLink(FreeToNormal($2), 1, undef, UnquoteHtml($3));
  569. } elsif ($FreeLinks && $locallinks
  570. && ($BracketWiki && m/\G(\[\[$FreeLinkPattern\|([^\]]+)\]\])/cg
  571. or m/\G(\[\[\[$FreeLinkPattern\]\]\])/cg
  572. or m/\G(\[\[$FreeLinkPattern\]\])/cg)) {
  573. # [[Free Link|text]], [[[Free Link]]], [[Free Link]]
  574. Dirty($1);
  575. my $bracket = (substr($1, 0, 3) eq '[[[');
  576. print GetPageOrEditLink($2, $3, $bracket, 1); # $3 may be empty
  577. } else {
  578. return; # nothing matched
  579. }
  580. return ''; # one of the dirty rules matched (and they all are)
  581. }
  582. sub SetHtmlEnvironmentContainer {
  583. my ($html_tag, $html_tag_attr) = @_;
  584. $HtmlEnvironmentContainers{$html_tag} = defined $html_tag_attr ? (
  585. $HtmlEnvironmentContainers{$html_tag} ? '|' . $HtmlEnvironmentContainers{$html_tag} : '')
  586. . $html_tag_attr : '';
  587. }
  588. sub InElement { # is $html_tag in @HtmlStack?
  589. my ($html_tag, $html_tag_attr) = @_;
  590. my $i = 0;
  591. foreach my $html_tag_current (@HtmlStack) {
  592. return 1 if $html_tag_current eq $html_tag and
  593. ($html_tag_attr ? $HtmlAttrStack[$i] =~ m/$html_tag_attr/ : 1);
  594. $i++;
  595. } return '';
  596. }
  597. sub AddOrCloseHtmlEnvironment { # add $html_tag, if not already added; close, otherwise
  598. my ($html_tag, $html_tag_attr) = @_;
  599. return InElement ($html_tag, '^' . $html_tag_attr . '$')
  600. ? CloseHtmlEnvironment($html_tag, '^' . $html_tag_attr . '$')
  601. : AddHtmlEnvironment ($html_tag, $html_tag_attr);
  602. }
  603. sub AddHtmlEnvironment { # add a new $html_tag
  604. my ($html_tag, $html_tag_attr) = @_;
  605. $html_tag_attr //= '';
  606. if ($html_tag and not (@HtmlStack and $HtmlStack[0] eq $html_tag and
  607. ($html_tag_attr ? $HtmlAttrStack[0] =~ m/$html_tag_attr/ : 1))) {
  608. unshift(@HtmlStack, $html_tag);
  609. unshift(@HtmlAttrStack, $html_tag_attr);
  610. return '<' . $html_tag . ($html_tag_attr ? ' ' . $html_tag_attr : '') . '>';
  611. } return ''; # always return something
  612. }
  613. sub OpenHtmlEnvironment { # close the previous $html_tag and open a new one
  614. my ($html_tag, $depth, $html_tag_attr, $tag_regex) = @_;
  615. my ($html, $found, @stack) = ('', 0); # always return something
  616. while (@HtmlStack and $found < $depth) { # determine new stack
  617. my $tag = pop(@HtmlStack);
  618. $found++ if ($tag_regex ? $tag =~ $tag_regex : $tag eq $html_tag);
  619. unshift(@stack, $tag);
  620. }
  621. unshift(@stack, pop(@HtmlStack)) if @HtmlStack and $found < $depth; # nested sublist coming up, keep list item
  622. @HtmlStack = @stack unless $found; # if starting a new list
  623. $html .= CloseHtmlEnvironments(); # close remaining elements (or all elements if a new list)
  624. @HtmlStack = @stack if $found; # if not starting a new list
  625. $depth = $IndentLimit if $depth > $IndentLimit; # requested depth 0 makes no sense
  626. $html_tag_attr = qq/class="$html_tag_attr"/ # backwards-compatibility hack: classically, the third argument to this function was a single CSS class, rather than string of HTML tag attributes as in the second argument to the "AddHtmlEnvironment" function. To allow both sorts, we conditionally change this string to 'class="$html_tag_attr"' when this string is a single CSS class.
  627. if $html_tag_attr and $html_tag_attr !~ m/^\s*[[:alpha:]]@@+\s*=\s*('|").+\1/;
  628. splice(@HtmlAttrStack, 0, @HtmlAttrStack - @HtmlStack); # truncate to size of @HtmlStack
  629. foreach ($found .. $depth - 1) {
  630. unshift(@HtmlStack, $html_tag);
  631. unshift(@HtmlAttrStack, $html_tag_attr);
  632. $html .= $html_tag_attr ? "<$html_tag $html_tag_attr>" : "<$html_tag>";
  633. }
  634. return $html;
  635. }
  636. sub CloseHtmlEnvironments { # close all -- remember to use AddHtmlEnvironment('p') if required!
  637. return CloseHtmlEnvironmentUntil() if pos($_) == length($_); # close all HTML environments if we're are at the end of this page
  638. my $html = '';
  639. while (@HtmlStack) {
  640. defined $HtmlEnvironmentContainers{$HtmlStack[0]} and # avoid closing block level elements
  641. ($HtmlEnvironmentContainers{$HtmlStack[0]} ? $HtmlAttrStack[0] =~
  642. m/$HtmlEnvironmentContainers{$HtmlStack[0]}/ : 1) and return $html;
  643. shift(@HtmlAttrStack);
  644. $html .= '</' . shift(@HtmlStack) . '>';
  645. }
  646. return $html;
  647. }
  648. sub CloseHtmlEnvironment { # close environments up to and including $html_tag
  649. my $html = (@_ and InElement(@_)) ? CloseHtmlEnvironmentUntil(@_) : undef;
  650. if (@HtmlStack and (not(@_) or defined $html)) {
  651. shift(@HtmlAttrStack);
  652. $html .= '</' . shift(@HtmlStack) . '>';
  653. }
  654. return $html || ''; # avoid returning undefined
  655. }
  656. sub CloseHtmlEnvironmentUntil { # close environments up to but not including $html_tag
  657. my ($html_tag, $html_tag_attr) = @_;
  658. my $html = '';
  659. while (@HtmlStack && (pos($_) == length($_) || # while there is an HTML tag-stack and we are at the end of this page or...
  660. !($html_tag ? $HtmlStack[0] eq $html_tag && # the top tag is not the desired tag and...
  661. ($html_tag_attr ? $HtmlAttrStack[0] =~ # its attributes do not match,
  662. m/$html_tag_attr/ : 1) : ''))) { # then...
  663. shift(@HtmlAttrStack); # shift off the top tag and
  664. $html .= '</' . shift(@HtmlStack) . '>'; # append it to our HTML string.
  665. }
  666. return $html;
  667. }
  668. sub SmileyReplace {
  669. foreach my $regexp (keys %Smilies) {
  670. if (m/\G($regexp)/cg) {
  671. return $q->img({-src=>$Smilies{$regexp}, -alt=>UnquoteHtml($1), -class=>'smiley'});
  672. }
  673. }
  674. }
  675. sub RunMyRules {
  676. my ($locallinks, $withanchors) = @_;
  677. foreach my $sub (@MyRules) {
  678. my $result = $sub->($locallinks, $withanchors);
  679. SetParam('msg', $@) if $@;
  680. return $result if defined($result);
  681. }
  682. return;
  683. }
  684. sub RunMyMacros {
  685. $_ = shift;
  686. foreach my $macro (@MyMacros) { $macro->() };
  687. return $_;
  688. }
  689. sub PrintWikiToHTML {
  690. my ($markup, $is_saving_cache, $revision, $is_locked) = @_;
  691. my ($blocks, $flags);
  692. $FootnoteNumber = 0;
  693. $markup =~ s/$FS//g if $markup; # Remove separators (paranoia)
  694. $markup = QuoteHtml($markup);
  695. ($blocks, $flags) = ApplyRules($markup, 1, $is_saving_cache, $revision, 'p');
  696. if ($is_saving_cache and not $revision and $Page{revision} # don't save revision 0 pages
  697. and $Page{blocks} ne $blocks and $Page{flags} ne $flags) {
  698. $Page{blocks} = $blocks;
  699. $Page{flags} = $flags;
  700. if ($is_locked or RequestLockDir('main')) { # not fatal!
  701. SavePage();
  702. ReleaseLock() unless $is_locked;
  703. }
  704. }
  705. }
  706. sub DoClearCache {
  707. return unless UserIsAdminOrError();
  708. RequestLockOrError();
  709. print GetHeader('', T('Clear Cache')), $q->start_div({-class=>'content clear'}),
  710. $q->p(T('Main lock obtained.')), '<p>';
  711. foreach my $id (AllPagesList()) {
  712. OpenPage($id);
  713. delete @Page{qw(blocks flags languages)};
  714. $Page{languages} = GetLanguages($Page{blocks}) unless TextIsFile($Page{blocks});
  715. SavePage();
  716. print $q->br(), GetPageLink($id);
  717. }
  718. print '</p>', $q->p(T('Main lock released.')), $q->end_div();
  719. utime time, time, $IndexFile; # touch index file
  720. ReleaseLock();
  721. PrintFooter();
  722. }
  723. sub QuoteHtml {
  724. my $html = shift;
  725. $html =~ s/&/&amp;/g;
  726. $html =~ s/</&lt;/g;
  727. $html =~ s/>/&gt;/g;
  728. $html =~ s/[\x00-\x08\x0b\x0c\x0e-\x1f]/ /g; # legal xml: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
  729. return $html;
  730. }
  731. sub UnquoteHtml {
  732. my $html = shift;
  733. $html =~ s/&lt;/</g;
  734. $html =~ s/&gt;/>/g;
  735. $html =~ s/&amp;/&/g;
  736. $html =~ s/%26/&/g;
  737. return $html;
  738. }
  739. sub UrlEncode {
  740. my $str = shift;
  741. return '' unless $str;
  742. my @letters = split(//, encode_utf8($str));
  743. my %safe = map {$_ => 1} ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')', '#');
  744. foreach my $letter (@letters) {
  745. $letter = sprintf("%%%02x", ord($letter)) unless $safe{$letter};
  746. }
  747. return join('', @letters);
  748. }
  749. sub UrlDecode {
  750. my $str = shift;
  751. return decode_utf8($str) if $str =~ s/%([0-9a-f][0-9a-f])/chr(hex($1))/eg;
  752. return $str;
  753. }
  754. sub QuoteRegexp {
  755. my $re = shift;
  756. $re =~ s/([\\\[\]\$()^.])/\\$1/g;
  757. return $re;
  758. }
  759. sub GetRaw {
  760. my $uri = shift;
  761. return unless eval { require LWP::UserAgent; };
  762. my $ua = LWP::UserAgent->new;
  763. my $response = $ua->get($uri);
  764. return $response->decoded_content if $response->is_success;
  765. }
  766. sub DoJournal {
  767. print GetHeader(undef, T('Journal'));
  768. print $q->start_div({-class=>'content journal'});
  769. PrintJournal(map { GetParam($_, ''); } qw(num num regexp mode offset search variation));
  770. print $q->end_div();
  771. PrintFooter();
  772. }
  773. sub JournalSort { $b cmp $a }
  774. sub PrintJournal {
  775. return if $CollectingJournal; # avoid infinite loops
  776. local $CollectingJournal = 1;
  777. my ($num, $numMore, $regexp, $mode, $offset, $search, $variation) = @_;
  778. $variation ||= 'journal';
  779. $regexp ||= '^\d\d\d\d-\d\d-\d\d';
  780. $num ||= 10;
  781. $numMore = $num unless $numMore ne '';
  782. $offset ||= 0;
  783. # FIXME: Should pass filtered list of pages to SearchTitleAndBody to save time?
  784. my @pages = sort JournalSort (grep(/$regexp/, $search ? SearchTitleAndBody($search) : AllPagesList()));
  785. @pages = reverse @pages if $mode eq 'reverse' or $mode eq 'future';
  786. $b = $Today // CalcDay($Now);
  787. if ($mode eq 'future' || $mode eq 'past') {
  788. my $compare = $mode eq 'future' ? -1 : 1;
  789. for (my $i = 0; $i < @pages; $i++) {
  790. $a = $pages[$i];
  791. if (JournalSort() == $compare) {
  792. @pages = @pages[$i .. $#pages];
  793. last;
  794. }
  795. }
  796. }
  797. return unless $pages[$offset];
  798. print $q->start_div({-class=>'journal'});
  799. my $next = $offset + PrintAllPages(1, 1, $num, $variation, @pages[$offset .. $#pages]);
  800. print $q->end_div();
  801. $regexp = UrlEncode($regexp);
  802. $search = UrlEncode($search);
  803. if ($pages[$next] and $numMore != 0) {
  804. print $q->p({-class=>'more'}, ScriptLink("action=more;num=$numMore;regexp=$regexp;search=$search;mode=$mode;offset=$next;variation=$variation", T('More...'), 'more'));
  805. }
  806. }
  807. sub PrintAllPages {
  808. my ($links, $comments, $num, $variation, @pages) = @_;
  809. my $lang = GetParam('lang', 0);
  810. my ($i, $n) = 0;
  811. for my $id (@pages) {
  812. last if $n >= $JournalLimit and not UserIsAdmin() or $num and $n >= $num;
  813. $i++; # pages looked at
  814. local ($OpenPageName, %Page); # this is local!
  815. OpenPage($id);
  816. my @languages = split(/,/, $Page{languages});
  817. next if $lang and @languages and not grep(/$lang/, @languages);
  818. next if PageMarkedForDeletion();
  819. next if substr($Page{text}, 0, 10) eq '#REDIRECT ';
  820. print $q->start_div({-class=>'page'}),
  821. $q->h1($links ? GetPageLink($id)
  822. : $q->a({-name=>$id}, UrlEncode(FreeToNormal($id))));
  823. if ($variation ne 'titles') {
  824. PrintPageHtml();
  825. PrintPageCommentsLink($id, $comments);
  826. }
  827. print $q->end_div();
  828. $n++; # pages actually printed
  829. }
  830. return $i;
  831. }
  832. sub PrintPageCommentsLink {
  833. my ($id, $comments) = @_;
  834. if ($comments and $CommentsPattern and $id !~ /$CommentsPattern/) {
  835. print $q->p({-class=>'comment'},
  836. GetPageLink($CommentsPrefix . $id, T('Comments on this page')));
  837. }
  838. }
  839. sub RSS {
  840. return if $CollectingJournal; # avoid infinite loops when using full=1
  841. local $CollectingJournal = 1;
  842. my $maxitems = shift;
  843. my @uris = @_;
  844. my %lines;
  845. if (not eval { require XML::RSS; }) {
  846. my $err = $@;
  847. return $q->div({-class=>'rss'}, $q->p({-class=>'error'}, $q->strong(T('XML::RSS is not available on this system.')), $err));
  848. }
  849. # All strings that are concatenated with strings returned by the RSS
  850. # feed must be decoded. Without this decoding, 'diff' and 'history'
  851. # translations will be double encoded when printing the result.
  852. my $tDiff = T('diff');
  853. my $tHistory = T('history');
  854. my $wikins = 'http://purl.org/rss/1.0/modules/wiki/';
  855. my $rdfns = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  856. @uris = map { my $x = $_; $x =~ s/^"?(.*?)"?$/$1/; $x; } @uris; # strip quotes of uris
  857. my ($str, %data) = GetRss(@uris);
  858. foreach my $uri (keys %data) {
  859. my $data = $data{$uri};
  860. if (not $data) {
  861. $str .= $q->p({-class=>'error'}, $q->strong(Ts('%s returned no data, or LWP::UserAgent is not available.',
  862. $q->a({-href=>$uri}, $uri))));
  863. } else {
  864. my $rss = new XML::RSS;
  865. eval { local $SIG{__DIE__}; $rss->parse($data); };
  866. if ($@) {
  867. $str .= $q->p({-class=>'error'}, $q->strong(Ts('RSS parsing failed for %s', $q->a({-href=>$uri}, $uri)) . ': ' . $@));
  868. } else {
  869. my $interwiki;
  870. if (@uris > 1) {
  871. RssInterwikiTranslateInit(); # not needed anywhere else thus init only now and not in ReInit
  872. $interwiki = $rss->{channel}->{$wikins}->{interwiki};
  873. $interwiki =~ s/^\s+//; # when RDF is used, sometimes whitespace remains,
  874. $interwiki =~ s/\s+$//; # which breaks the test for an existing $interwiki below
  875. $interwiki ||= $rss->{channel}->{$rdfns}->{value};
  876. $interwiki = $RssInterwikiTranslate{$interwiki} if $RssInterwikiTranslate{$interwiki};
  877. $interwiki ||= $RssInterwikiTranslate{$uri};
  878. }
  879. my $num = 999;
  880. $str .= $q->p({-class=>'error'}, $q->strong(Ts('No items found in %s.', $q->a({-href=>$uri}, $uri))))
  881. unless @{$rss->{items}};
  882. foreach my $i (@{$rss->{items}}) {
  883. my $line;
  884. my $date = $i->{dc}->{date};
  885. if (not $date and $i->{pubDate}) {
  886. $date = $i->{pubDate};
  887. my %mon = (Jan=>1, Feb=>2, Mar=>3, Apr=>4, May=>5, Jun=>6,
  888. Jul=>7, Aug=>8, Sep=>9, Oct=>10, Nov=>11, Dec=>12);
  889. $date =~ s/^(?:[A-Z][a-z][a-z], )?(\d\d?) ([A-Z][a-z][a-z]) (\d\d(?:\d\d)?)/ # pubDate uses RFC 822
  890. sprintf('%04d-%02d-%02d', ($3 < 100 ? 1900 + $3 : $3), $mon{$2}, $1)/e;
  891. }
  892. $date ||= sprintf("%03d", $num--); # for RSS 0.91 feeds without date, descending
  893. my $title = $i->{title};
  894. my $description = $i->{description};
  895. if (not $title and $description) { # title may be missing in RSS 2.00
  896. $title = $description;
  897. $description = '';
  898. }
  899. $title = $i->{link} if not $title and $i->{link}; # if description and title are missing
  900. $line .= ' (' . $q->a({-href=>$i->{$wikins}->{diff}}, $tDiff) . ')' if $i->{$wikins}->{diff};
  901. $line .= ' (' . $q->a({-href=>$i->{$wikins}->{history}}, $tHistory) . ')' if $i->{$wikins}->{history};
  902. if ($title) {
  903. if ($i->{link}) {
  904. $line .= ' ' . $q->a({-href=>$i->{link}, -title=>$date},
  905. ($interwiki ? $interwiki . ':' : '') . $title);
  906. } else {
  907. $line .= ' ' . $title;
  908. }
  909. }
  910. my $contributor = $i->{dc}->{contributor};
  911. $contributor ||= $i->{$wikins}->{username};
  912. $contributor =~ s/^\s+//;
  913. $contributor =~ s/\s+$//;
  914. $contributor ||= $i->{$rdfns}->{value};
  915. $line .= $q->span({-class=>'contributor'}, $q->span(T(' . . . .') . ' ') . $contributor) if $contributor;
  916. if ($description) {
  917. if ($description =~ /</) {
  918. $line .= $q->div({-class=>'description'}, $description);
  919. } else {
  920. $line .= $q->span({class=>'dash'}, ' &#8211; ') . $q->strong({-class=>'description'}, $description);
  921. }
  922. }
  923. $date .= ' ' while ($lines{$date}); # make sure this is unique
  924. $lines{$date} = $line;
  925. }
  926. }
  927. }
  928. }
  929. my @lines = sort { $b cmp $a } keys %lines;
  930. @lines = @lines[0 .. $maxitems-1] if $maxitems and $#lines > $maxitems;
  931. my $date = '';
  932. foreach my $key (@lines) {
  933. my $line = $lines{$key};
  934. if ($key =~ /(\d\d\d\d(?:-\d?\d)?(?:-\d?\d)?)(?:[T ](\d?\d:\d\d))?/) {
  935. my ($day, $time) = ($1, $2);
  936. if ($day ne $date) {
  937. $str .= '</ul>' if $date; # close ul except for the first time where no open ul exists
  938. $date = $day;
  939. $str .= $q->p($q->strong($day)) . '<ul>';
  940. }
  941. $line = $q->span({-class=>'time'}, $time . ' UTC ') . $line if $time;
  942. } elsif (not $date) {
  943. $str .= '<ul>'; # if the feed doesn't have any dates we need to start the list anyhow
  944. $date = $Now; # to ensure the list starts only once
  945. }
  946. $str .= $q->li($line);
  947. }
  948. $str .= '</ul>' if $date;
  949. return $q->div({-class=>'rss'}, $str);
  950. }
  951. sub GetRss {
  952. my %todo = map {$_, GetRssFile($_)} @_;
  953. my %data = ();
  954. my $str = '';
  955. if (GetParam('cache', $UseCache) > 0) {
  956. foreach my $uri (keys %todo) { # read cached rss files if possible
  957. if ($Now - Modified($todo{$uri}) < $RssCacheHours * 3600) {
  958. $data{$uri} = ReadFile($todo{$uri});
  959. delete($todo{$uri}); # no need to fetch them below
  960. }
  961. }
  962. }
  963. my @need_cache = keys %todo;
  964. if (keys %todo > 1) { # try parallel access if available
  965. eval { # see code example in LWP::Parallel, not LWP::Parallel::UserAgent (no callbacks here)
  966. require LWP::Parallel::UserAgent;
  967. my $pua = LWP::Parallel::UserAgent->new();
  968. foreach my $uri (keys %todo) {
  969. if (my $res = $pua->register(HTTP::Request->new('GET', $uri))) {
  970. $str .= $res->error_as_HTML;
  971. }
  972. }
  973. %todo = (); # because the uris in the response may have changed due to redirects
  974. my $entries = $pua->wait();
  975. foreach (keys %$entries) {
  976. my $uri = $entries->{$_}->request->uri;
  977. $data{$uri} = $entries->{$_}->response->decoded_content;
  978. }
  979. }
  980. }
  981. foreach my $uri (keys %todo) { # default operation: synchronous fetching
  982. $data{$uri} = GetRaw($uri);
  983. }
  984. if (GetParam('cache', $UseCache) > 0) {
  985. CreateDir($RssDir);
  986. foreach my $uri (@need_cache) {
  987. my $data = $data{$uri};
  988. # possibly a Latin-1 file without encoding attribute will cause a problem?
  989. $data =~ s/encoding="[^"]*"/encoding="UTF-8"/; # content was converted
  990. WriteStringToFile(GetRssFile($uri), $data) if $data;
  991. }
  992. }
  993. return $str, %data;
  994. }
  995. sub GetRssFile {
  996. return $RssDir . '/' . UrlEncode(shift);
  997. }
  998. sub RssInterwikiTranslateInit {
  999. return unless $RssInterwikiTranslate;
  1000. %RssInterwikiTranslate = ();
  1001. foreach (split(/\n/, GetPageContent($RssInterwikiTranslate))) {
  1002. if (/^ ([^ ]+)[ \t]+([^ ]+)$/) {
  1003. $RssInterwikiTranslate{$1} = $2;
  1004. }
  1005. }
  1006. }
  1007. sub GetInterSiteUrl {
  1008. my ($site, $page, $quote) = @_;
  1009. return unless $page;
  1010. $page = join('/', map { UrlEncode($_) } split(/\//, $page)) if $quote; # Foo:bar+baz is not quoted, [[Foo:bar baz]] is.
  1011. my $url = $InterSite{$site} or return;
  1012. $url =~ s/\%s/$page/g or $url .= $page;
  1013. return $url;
  1014. }
  1015. sub BracketLink { # brackets can be removed via CSS
  1016. return $q->span($q->span({class=>'bracket'}, '[') . (shift) . $q->span({class=>'bracket'}, ']'));
  1017. }
  1018. sub GetInterLink {
  1019. my ($id, $text, $bracket, $quote) = @_;
  1020. my ($site, $page) = split(/:/, $id, 2);
  1021. $page =~ s/&amp;/&/g; # Unquote common URL HTML
  1022. my $url = GetInterSiteUrl($site, $page, $quote);
  1023. my $class = 'inter ' . $site;
  1024. return "[$id $text]" if $text and $bracket and not $url;
  1025. return "[$id]" if $bracket and not $url;
  1026. return $id if not $url;
  1027. if ($bracket and not $text) {
  1028. $text = BracketLink(++$FootnoteNumber);
  1029. $class .= ' number';
  1030. } elsif (not $text) {
  1031. $text = $q->span({-class=>'site'}, $site)
  1032. . $q->span({-class=>'separator'}, ':')
  1033. . $q->span({-class=>'interpage'}, $page);
  1034. } elsif ($bracket) { # and $text is set
  1035. $class .= ' outside';
  1036. }
  1037. return $q->a({-href=>$url, -class=>$class}, $text);
  1038. }
  1039. sub InterInit {
  1040. %InterSite = ();
  1041. foreach (split(/\n/, GetPageContent($InterMap))) {
  1042. if (/^ ($InterSitePattern)[ \t]+([^ ]+)$/) {
  1043. $InterSite{$1} = $2;
  1044. }
  1045. }
  1046. }
  1047. sub GetUrl {
  1048. my ($url, $text, $bracket, $images) = @_;
  1049. $url =~ /^($UrlProtocols)/;
  1050. my $class = "url $1";
  1051. if ($NetworkFile && $url =~ m|^file:///| && !$AllNetworkFiles
  1052. or !$NetworkFile && $url =~ m|^file:|) {
  1053. # Only do remote file:// links. No file:///c|/windows.
  1054. return $url;
  1055. } elsif ($bracket and not defined $text) {
  1056. $text = BracketLink(++$FootnoteNumber);
  1057. $class .= ' number';
  1058. } elsif (not defined $text) {
  1059. $text = $url;
  1060. } elsif ($bracket) { # and $text is set
  1061. $class .= ' outside';
  1062. }
  1063. $url = UnquoteHtml($url); # links should be unquoted again
  1064. if ($images and $url =~ /^(http:|https:|ftp:).+\.$ImageExtensions$/i) {
  1065. return $q->img({-src=>$url, -alt=>$url, -class=>$class});
  1066. } else {
  1067. return $q->a({-href=>$url, -class=>$class}, $text);
  1068. }
  1069. }
  1070. sub GetPageOrEditLink { # use GetPageLink and GetEditLink if you know the result!
  1071. my ($id, $text, $bracket, $free) = @_;
  1072. $id = FreeToNormal($id);
  1073. my ($class, $resolved, $title, $exists) = ResolveId($id);
  1074. if (not $text and $resolved and $bracket) {
  1075. $text = BracketLink(++$FootnoteNumber);
  1076. $class .= ' number';
  1077. $title = NormalToFree($id);
  1078. }
  1079. my $link = $text || NormalToFree($id);
  1080. if ($resolved) { # anchors don't exist as pages, therefore do not use $exists
  1081. return ScriptLink(UrlEncode($resolved), $link, $class, undef, $title);
  1082. } else { # reproduce markup if $UseQuestionmark
  1083. return GetEditLink($id, UnquoteHtml($bracket ? "[$link]" : $link)) unless $UseQuestionmark;
  1084. $link = QuoteHtml($id) . GetEditLink($id, '?');
  1085. $link .= ($free ? '|' : ' ') . $text if $text and FreeToNormal($text) ne $id;
  1086. $link = "[[$link]]" if $free;
  1087. $link = "[$link]" if $bracket or not $free and $text;
  1088. return $link;
  1089. }
  1090. }
  1091. sub GetPageLink { # use if you want to force a link to local pages, whether it exists or not
  1092. my ($id, $name, $class, $accesskey) = @_;
  1093. $id = FreeToNormal($id);
  1094. $name ||= $id;
  1095. $class .= ' ' if $class;
  1096. return ScriptLink(UrlEncode($id), NormalToFree($name), $class . 'local',
  1097. undef, undef, $accesskey);
  1098. }
  1099. sub GetEditLink { # shortcut
  1100. my ($id, $name, $upload, $accesskey) = @_;
  1101. $id = FreeToNormal($id);
  1102. my $action = 'action=edit;id=' . UrlEncode($id);
  1103. $action .= ';upload=1' if $upload;
  1104. return ScriptLink($action, NormalToFree($name), 'edit', undef, T('Click to edit this page'), $accesskey);
  1105. }
  1106. sub ScriptUrl {
  1107. my $action = shift;
  1108. if ($action =~ /^($UrlProtocols)\%3a/ or $action =~ /^\%2f/) { # nearlinks and other URLs
  1109. $action =~ s/%([0-9a-f][0-9a-f])/chr(hex($1))/eg; # undo urlencode
  1110. # do nothing
  1111. } else {
  1112. $action = $ScriptName . (($UsePathInfo and index($action, '=') == -1) ? '/' : '?') . $action;
  1113. }
  1114. return $action unless wantarray;
  1115. return ($action, index($action, '=') != -1);
  1116. }
  1117. sub ScriptLink {
  1118. my ($action, $text, $class, $name, $title, $accesskey) = @_;
  1119. my ($url, $nofollow) = ScriptUrl($action);
  1120. my %params;
  1121. $params{-href} = $url;
  1122. $params{'-rel'} = 'nofollow' if $nofollow;
  1123. $params{'-class'} = $class if $class;
  1124. $params{'-name'} = $name if $name;
  1125. $params{'-title'} = $title if $title;
  1126. $params{'-accesskey'} = $accesskey if $accesskey;
  1127. return $q->a(\%params, $text);
  1128. }
  1129. sub GetDownloadLink {
  1130. my ($id, $image, $revision, $alt) = @_;
  1131. $alt ||= NormalToFree($id);
  1132. # if the page does not exist
  1133. return '[[' . ($image ? 'image' : 'download') . ':'
  1134. . ($UseQuestionmark ? QuoteHtml($id) . GetEditLink($id, '?', 1)
  1135. : GetEditLink($id, $id, 1)) . ']]'
  1136. unless $IndexHash{$id};
  1137. my $action;
  1138. if ($revision) {
  1139. $action = "action=download;id=" . UrlEncode($id) . ";revision=$revision";
  1140. } elsif ($UsePathInfo) {
  1141. $action = "download/" . UrlEncode($id);
  1142. } else {
  1143. $action = "action=download;id=" . UrlEncode($id);
  1144. }
  1145. if ($image) {
  1146. $action = $ScriptName . (($UsePathInfo and not $revision) ? '/' : '?') . $action;
  1147. return $action if $image == 2;
  1148. my $result = $q->img({-src=>$action, -alt=>UnquoteHtml($alt), -title=>UnquoteHtml($alt), -class=>'upload'});
  1149. $result = ScriptLink(UrlEncode($id), $result, 'image') unless $id eq $OpenPageName;
  1150. return $result;
  1151. } else {
  1152. return ScriptLink($action, $alt, 'upload');
  1153. }
  1154. }
  1155. sub PrintCache { # Use after OpenPage!
  1156. my @blocks = split($FS, $Page{blocks});
  1157. my @flags = split($FS, $Page{flags});
  1158. $FootnoteNumber = 0;
  1159. foreach my $block (@blocks) {
  1160. if (shift(@flags)) {
  1161. ApplyRules($block, 1, 1); # local links, anchors, current revision, no start tag
  1162. } else {
  1163. print $block;
  1164. }
  1165. }
  1166. }
  1167. sub PrintPageHtml { # print an open page
  1168. return unless GetParam('page', 1);
  1169. if ($Page{blocks} and defined $Page{flags} and GetParam('cache', $UseCache) > 0) {
  1170. PrintCache();
  1171. } else {
  1172. PrintWikiToHTML($Page{text}, 1); # save cache, current revision, no main lock
  1173. }
  1174. }
  1175. sub PrintPageDiff { # print diff for open page
  1176. my $diff = GetParam('diff', 0);
  1177. if ($UseDiff and $diff) {
  1178. PrintHtmlDiff($diff);
  1179. print $q->hr() if GetParam('page', 1);
  1180. }
  1181. }
  1182. sub ToString {
  1183. my $sub_ref = shift;
  1184. my $output;
  1185. open(my $outputFH, '>:encoding(UTF-8)', \$output) or die "Can't open memory file: $!";
  1186. my $oldFH = select $outputFH;
  1187. $sub_ref->(@_);
  1188. select $oldFH;
  1189. close $outputFH;
  1190. return decode_utf8($output);
  1191. }
  1192. sub PageHtml {
  1193. my ($id, $limit, $error) = @_;
  1194. OpenPage($id);
  1195. my $diff = ToString \&PrintPageDiff;
  1196. return $error if $limit and length($diff) > $limit;
  1197. my $page = ToString \&PrintPageHtml;
  1198. return $diff . $q->p($error) if $limit and length($diff . $page) > $limit;
  1199. return $diff . $page;
  1200. }
  1201. sub T {
  1202. my $text = shift;
  1203. return $Translate{$text} || $text;
  1204. }
  1205. sub Ts {
  1206. my ($text, $string) = @_;
  1207. $text = T($text);
  1208. $text =~ s/\%s/$string/ if defined($string);
  1209. return $text;
  1210. }
  1211. sub Tss {
  1212. my $text = $_[0];
  1213. $text = T($text);
  1214. $text =~ s/\%([1-9])/$_[$1]/eg;
  1215. return $text;
  1216. }
  1217. sub GetId {
  1218. my $id = UnquoteHtml(GetParam('id', GetParam('title', ''))); # id=x or title=x -> x
  1219. if (not $id and $q->keywords) {
  1220. $id = decode_utf8(join('_', $q->keywords)); # script?p+q -> p_q
  1221. }
  1222. if ($UsePathInfo and $q->path_info) {
  1223. my @path = map { decode_utf8($_) } split(/\//, $q->path_info);
  1224. $id ||= pop(@path); # script/p/q -> q
  1225. foreach my $p (@path) {
  1226. SetParam($p, 1); # script/p/q -> p=1
  1227. }
  1228. }
  1229. return $id;
  1230. }
  1231. sub DoBrowseRequest {
  1232. # We can use the error message as the HTTP error code
  1233. ReportError(Ts('CGI Internal error: %s', $q->cgi_error), $q->cgi_error) if $q->cgi_error;
  1234. print $q->header(-status=>'304 NOT MODIFIED') and return if PageFresh(); # return value is ignored
  1235. my $id = GetId();
  1236. my $action = lc(GetParam('action', '')); # script?action=foo;id=bar
  1237. $action = 'download' if GetParam('download', '') and not $action; # script/download/id
  1238. if ($Action{$action}) {
  1239. &{$Action{$action}}($id);
  1240. } elsif ($action and defined &MyActions) {
  1241. eval { local $SIG{__DIE__}; MyActions(); };
  1242. } elsif ($action) {
  1243. ReportError(Ts('Invalid action parameter %s', $action), '501 NOT IMPLEMENTED');
  1244. } elsif (GetParam('search', '') ne '') { # allow search for "0"
  1245. SetParam('action', 'search'); # make sure this gets a NOINDEX
  1246. DoSearch();
  1247. } elsif (GetParam('match', '') ne '') {
  1248. SetParam('action', 'index'); # make sure this gets a NOINDEX
  1249. DoIndex();
  1250. } elsif (GetParam('title', '') and not GetParam('Cancel', '')) {
  1251. DoPost(GetParam('title', ''));
  1252. } else {
  1253. BrowseResolvedPage($id || $HomePage); # default action!
  1254. }
  1255. }
  1256. sub ValidId { # hack alert: returns error message if invalid, and unfortunately the empty string if valid!
  1257. my $id = FreeToNormal(shift);
  1258. return T('Page name is missing') unless $id;
  1259. require bytes;
  1260. return Ts('Page name is too long: %s', $id) if bytes::length($id) > $PageNameLimit;
  1261. return Ts('Invalid Page %s (must not end with .db)', $id) if $id =~ m|\.db$|;
  1262. return Ts('Invalid Page %s (must not end with .lck)', $id) if $id =~ m|\.lck$|;
  1263. return Ts('Invalid Page %s', $id) if $FreeLinks ? $id !~ m|^$FreeLinkPattern$| : $id !~ m|^$LinkPattern$|;
  1264. }
  1265. sub ValidIdOrDie {
  1266. my $id = shift;
  1267. my $error = ValidId($id);
  1268. ReportError($error, '400 BAD REQUEST') if $error;
  1269. return 1;
  1270. }
  1271. sub ResolveId { # return css class, resolved id, title (eg. for popups), exist-or-not
  1272. my $id = shift;
  1273. return ('local', $id, '', 1) if $IndexHash{$id};
  1274. return ('', '', '', '');
  1275. }
  1276. sub BrowseResolvedPage {
  1277. my $id = FreeToNormal(shift);
  1278. my ($class, $resolved, $title, $exists) = ResolveId($id);
  1279. if ($class and $class eq 'near' and not GetParam('rcclusteronly', 0)) { # nearlink (is url)
  1280. print $q->redirect({-uri=>$resolved});
  1281. } elsif ($class and $class eq 'alias') { # an anchor was found instead of a page
  1282. ReBrowsePage($resolved);
  1283. } elsif (not $resolved and $NotFoundPg and $id !~ /$CommentsPattern/) { # custom page-not-found message
  1284. BrowsePage($NotFoundPg);
  1285. } elsif ($resolved or $id =~ /$CommentsPattern/ and $1 and $IndexHash{$1}) { # an existing page
  1286. BrowsePage(($resolved or $id), GetParam('raw', 0));
  1287. } else { # new page!
  1288. BrowsePage($id, GetParam('raw', 0), undef, '404 NOT FOUND') if ValidIdOrDie($id);
  1289. }
  1290. }
  1291. sub NewText { # only if no revision is available
  1292. my $id = shift;
  1293. if ($CommentsPrefix and $id =~ /^($CommentsPrefix)/) {
  1294. return T('There are no comments, yet. Be the first to leave a comment!');
  1295. } elsif ($id eq $HomePage) {
  1296. return T('Welcome!');
  1297. } else {
  1298. return Ts('This page does not exist, but you can %s.',
  1299. '[' . ScriptUrl('action=edit;id=' . UrlEncode($id)) . ' '
  1300. . T('create it now') . ']');
  1301. }
  1302. }
  1303. sub BrowsePage {
  1304. my ($id, $raw, $comment, $status) = @_;
  1305. OpenPage($id);
  1306. my ($revisionPage, $revision) = GetTextRevision(GetParam('revision', ''));
  1307. my $text = $revisionPage->{text};
  1308. $text = NewText($id) unless $revision or $Page{revision} or $comment; # new text for new pages
  1309. # handle a single-level redirect
  1310. my $oldId = GetParam('oldid', '');
  1311. if ((substr($text, 0, 10) eq '#REDIRECT ')) {
  1312. if ($oldId) {
  1313. $Message .= $q->p(T('Too many redirections'));
  1314. } elsif ($revision) {
  1315. $Message .= $q->p(T('No redirection for old revisions'));
  1316. } elsif (($FreeLinks and $text =~ /^\#REDIRECT\s+\[\[$FreeLinkPattern\]\]/)
  1317. or ($WikiLinks and $text =~ /^\#REDIRECT\s+$LinkPattern/)) {
  1318. return ReBrowsePage(FreeToNormal($1), $id);
  1319. } else {
  1320. $Message .= $q->p(T('Invalid link pattern for #REDIRECT'));
  1321. }
  1322. }
  1323. # shortcut if we only need the raw text: no caching, no diffs, no html.
  1324. if ($raw) {
  1325. print GetHttpHeader('text/plain', $Page{ts}, $IndexHash{$id} ? undef : '404 NOT FOUND');
  1326. print $Page{ts} . " # Do not delete this line when editing!\n" if $raw == 2;
  1327. print $text;
  1328. return;
  1329. }
  1330. # normal page view
  1331. my $msg = GetParam('msg', '');
  1332. $Message .= $q->p($msg) if $msg; # show message if the page is shown
  1333. SetParam('msg', '');
  1334. print GetHeader($id, NormalToFree($id), $oldId, undef, $status);
  1335. my $showDiff = GetParam('diff', 0);
  1336. if ($UseDiff and $showDiff) {
  1337. PrintHtmlDiff($showDiff, GetParam('diffrevision'), $revisionPage, $Page{revision});
  1338. print $q->hr();
  1339. }
  1340. PrintPageContent($text, $revision, $comment);
  1341. SetParam('rcclusteronly', $id) if FreeToNormal(GetCluster($text)) eq $id; # automatically filter by cluster
  1342. PrintRcHtml($id);
  1343. PrintFooter($id, $revision, $comment, $revisionPage);
  1344. }
  1345. sub ReBrowsePage {
  1346. my ($id, $oldId) = map { UrlEncode($_); } @_; # encode before printing URL
  1347. if ($oldId) { # Target of #REDIRECT (loop breaking)
  1348. print GetRedirectPage("action=browse;oldid=$oldId;id=$id", $id);
  1349. } else {
  1350. print GetRedirectPage($id, $id);
  1351. }
  1352. }
  1353. sub GetRedirectPage {
  1354. my ($action, $name) = @_;
  1355. my ($url, $html);
  1356. if (GetParam('raw', 0)) {
  1357. $html = GetHttpHeader('text/plain');
  1358. $html .= Ts('Please go on to %s.', $action); # no redirect
  1359. return $html;
  1360. }
  1361. $url = $ScriptName . (($UsePathInfo and $action !~ /=/) ? '/' : '?') . $action;
  1362. my $nameLink = $q->a({-href=>$url}, $name);
  1363. my %headers = (-uri=>$url);
  1364. my $cookie = Cookie();
  1365. $headers{-cookie} = $cookie if $cookie;
  1366. return $q->redirect(%headers);
  1367. }
  1368. sub DoRandom {
  1369. my @pages = AllPagesList();
  1370. ReBrowsePage($pages[int(rand($#pages + 1))]);
  1371. }
  1372. sub PageFresh { # pages can depend on other pages (ie. last update), admin status, and css
  1373. return 1 if $q->http('HTTP_IF_NONE_MATCH') and GetParam('cache', $UseCache) >= 2
  1374. and $q->http('HTTP_IF_NONE_MATCH') eq PageEtag();
  1375. }
  1376. sub PageEtag {
  1377. my ($changed, %params) = CookieData();
  1378. return UrlEncode(join($FS, $LastUpdate||$Now, sort(values %params))); # no CTL in field values
  1379. }
  1380. sub FileFresh { # old files are never stale, current files are stale when the page was modified
  1381. return 1 if $q->http('HTTP_IF_NONE_MATCH') and GetParam('cache', $UseCache) >= 2
  1382. and (GetParam('revision', 0) or $q->http('HTTP_IF_NONE_MATCH') eq $Page{ts});
  1383. }
  1384. sub BrowseRc {
  1385. my $id = shift;
  1386. if (GetParam('raw', 0)) {
  1387. print GetHttpHeader('text/plain');
  1388. PrintRcText();
  1389. } else {
  1390. PrintRcHtml($id || $RCName, 1);
  1391. }
  1392. }
  1393. sub GetRcLines { # starttime, hash of seen pages to use as a second return value
  1394. my $starttime = shift || GetParam('from', 0) ||
  1395. $Now - GetParam('days', $RcDefault) * 86400; # 24*60*60
  1396. my $filterOnly = GetParam('rcfilteronly', '');
  1397. # these variables apply accross logfiles
  1398. my %match = $filterOnly ? map { $_ => 1 } SearchTitleAndBody($filterOnly) : ();
  1399. my %following = ();
  1400. my @result = ();
  1401. my $ts;
  1402. # check the first timestamp in the default file, maybe read old log file
  1403. if (open(my $F, '<:encoding(UTF-8)', encode_utf8($RcFile))) {
  1404. my $line = <$F>;
  1405. ($ts) = split(/$FS/, $line); # the first timestamp in the regular rc file
  1406. }
  1407. if (not $ts or $ts > $starttime) { # we need to read the old rc file, too
  1408. push(@result, GetRcLinesFor($RcOldFile, $starttime, \%match, \%following));
  1409. }
  1410. push(@result, GetRcLinesFor($RcFile, $starttime, \%match, \%following));
  1411. # GetRcLinesFor is trying to save memory space, but some operations
  1412. # can only happen once we have all the data.
  1413. return LatestChanges(StripRollbacks(@result));
  1414. }
  1415. sub LatestChanges {
  1416. my $all = GetParam('all', $ShowAll);
  1417. my @result = @_;
  1418. my %seen = ();
  1419. for (my $i = $#result; $i >= 0; $i--) {
  1420. my $id = $result[$i][1];
  1421. if ($all) {
  1422. $result[$i][9] = 1 unless $seen{$id}; # mark latest edit
  1423. } else {
  1424. splice(@result, $i, 1) if $seen{$id}; # remove older edits
  1425. }
  1426. $seen{$id} = 1;
  1427. }
  1428. my $to = GetParam('upto', 0);
  1429. if ($to) {
  1430. for (my $i = 0; $i < $#result; $i++) {
  1431. if ($result[$i][0] > $to) {
  1432. splice(@result, $i);
  1433. last;
  1434. }
  1435. }
  1436. }
  1437. return reverse @result;
  1438. }
  1439. sub StripRollbacks {
  1440. my @result = @_;
  1441. if (not (GetParam('all', $ShowAll) or GetParam('rollback', $ShowRollbacks))) { # strip rollbacks
  1442. my (%rollback);
  1443. for (my $i = $#result; $i >= 0; $i--) {
  1444. # some fields have a different meaning if looking at rollbacks
  1445. my ($ts, $id, $target_ts, $target_id) = @{$result[$i]};
  1446. if ($id eq '[[rollback]]') {
  1447. if ($target_id) {
  1448. $rollback{$target_id} = $target_ts; # single page rollback
  1449. splice(@result, $i, 1); # strip marker
  1450. } else {
  1451. my $end = $i;
  1452. while ($ts > $target_ts and $i > 0) {
  1453. $i--; # quickly skip all these lines
  1454. $ts = $result[$i][0];
  1455. }
  1456. splice(@result, $i + 1, $end - $i);
  1457. $i++; # compensate $i-- in for loop
  1458. }
  1459. } elsif ($rollback{$id} and $ts > $rollback{$id}) {
  1460. splice(@result, $i, 1); # strip rolled back single pages
  1461. }
  1462. }
  1463. } else { # just strip the marker left by DoRollback()
  1464. for (my $i = $#result; $i >= 0; $i--) {
  1465. splice(@result, $i, 1) if $result[$i][1] eq '[[rollback]]'; # id
  1466. }
  1467. }
  1468. return @result;
  1469. }
  1470. sub GetRcLinesFor {
  1471. my $file = shift;
  1472. my $starttime = shift;
  1473. my %match = %{$_[0]}; # deref
  1474. my %following = %{$_[1]}; # deref
  1475. # parameters
  1476. my $showminoredit = GetParam('showedit', $ShowEdits); # show minor edits
  1477. my $all = GetParam('all', $ShowAll);
  1478. my ($idOnly, $userOnly, $hostOnly, $clusterOnly, $filterOnly, $match, $lang,
  1479. $followup) = map { UnquoteHtml(GetParam($_, '')); }
  1480. qw(rcidonly rcuseronly rchostonly
  1481. rcclusteronly rcfilteronly match lang followup);
  1482. # parsing and filtering
  1483. my @result = ();
  1484. open(my $F, '<:encoding(UTF-8)', encode_utf8($file)) or return ();
  1485. while (my $line = <$F>) {
  1486. chomp($line);
  1487. my ($ts, $id, $minor, $summary, $host, $username, $revision,
  1488. $languages, $cluster) = split(/$FS/, $line);
  1489. next if $ts < $starttime;
  1490. $following{$id} = $ts if $followup and $followup eq $username;
  1491. next if $followup and (not $following{$id} or $ts <= $following{$id});
  1492. next if $idOnly and $idOnly ne $id;
  1493. next if $filterOnly and not $match{$id};
  1494. next if ($userOnly and $userOnly ne $username);
  1495. next if $minor == 1 and not $showminoredit; # skip minor edits (if [[rollback]] this is bogus)
  1496. next if not $minor and $showminoredit == 2; # skip major edits
  1497. next if $match and $id !~ /$match/i;
  1498. next if $hostOnly and $host !~ /$hostOnly/i;
  1499. my @languages = split(/,/, $languages);
  1500. next if $lang and @languages and not grep(/$lang/, @languages);
  1501. if ($PageCluster) {
  1502. ($cluster, $summary) = ($1, $2) if $summary =~ /^\[\[$FreeLinkPattern\]\] ?: *(.*)/
  1503. or $summary =~ /^$LinkPattern ?: *(.*)/;
  1504. next if ($clusterOnly and $clusterOnly ne $cluster);
  1505. $cluster = '' if $clusterOnly; # don't show cluster if $clusterOnly eq $cluster
  1506. if ($all < 2 and not $clusterOnly and $cluster) {
  1507. $summary = "$id: $summary"; # print the cluster instead of the page
  1508. $id = $cluster;
  1509. $revision = '';
  1510. }
  1511. } else {
  1512. $cluster = '';
  1513. }
  1514. $following{$id} = $ts if $followup and $followup eq $username;
  1515. push(@result, [$ts, $id, $minor, $summary, $host, $username, $revision,
  1516. \@languages, $cluster]);
  1517. }
  1518. return @result;
  1519. }
  1520. sub ProcessRcLines {
  1521. my ($printDailyTear, $printRCLine) = @_; # code references
  1522. # needed for output
  1523. my $date = '';
  1524. for my $line (GetRcLines()) {
  1525. my ($ts, $id, $minor, $summary, $host, $username, $revision, $languageref,
  1526. $cluster, $last) = @$line;
  1527. if ($date ne CalcDay($ts)) {
  1528. $date = CalcDay($ts);
  1529. $printDailyTear->($date);
  1530. }
  1531. $printRCLine->($id, $ts, $host, $username, $summary, $minor, $revision,
  1532. $languageref, $cluster, $last);
  1533. }
  1534. }
  1535. sub RcHeader {
  1536. my ($from, $upto, $html) = (GetParam('from', 0), GetParam('upto', 0), '');
  1537. my $days = GetParam('days') + 0 || $RcDefault; # force numeric $days
  1538. my $all = GetParam('all', $ShowAll);
  1539. my $edits = GetParam('showedit', $ShowEdits);
  1540. my $rollback = GetParam('rollback', $ShowRollbacks);
  1541. if ($from) {
  1542. $html .= $q->h2(Ts('Updates since %s', TimeToText(GetParam('from', 0))) . ' '
  1543. . ($upto ? Ts('up to %s', TimeToText($upto)) : ''));
  1544. } else {
  1545. $html .= $q->h2((GetParam('days', $RcDefault) != 1)
  1546. ? Ts('Updates in the last %s days', $days)
  1547. : Ts('Updates in the last day'));
  1548. }
  1549. my $action = '';
  1550. my ($idOnly, $userOnly, $hostOnly, $clusterOnly, $filterOnly,
  1551. $match, $lang, $followup) =
  1552. map {
  1553. my $val = GetParam($_, '');
  1554. $html .= $q->p($q->b('(' . Ts('for %s only', $val) . ')')) if $val;
  1555. $action .= ";$_=$val" if $val; # remember these parameters later!
  1556. $val;
  1557. } qw(rcidonly rcuseronly rchostonly rcclusteronly rcfilteronly
  1558. match lang followup);
  1559. my $rss = "action=rss$action;days=$days;all=$all;showedit=$edits";
  1560. if ($clusterOnly) {
  1561. $action = GetPageParameters('browse', $clusterOnly) . $action;
  1562. } else {
  1563. $action = "action=rc$action";
  1564. }
  1565. my @menu;
  1566. if ($all) {
  1567. push(@menu, ScriptLink("$action;days=$days;all=0;showedit=$edits",
  1568. T('List latest change per page only')));
  1569. } else {
  1570. push(@menu, ScriptLink("$action;days=$days;all=1;showedit=$edits",
  1571. T('List all changes')));
  1572. if ($rollback) {
  1573. push(@menu, ScriptLink("$action;days=$days;all=0;rollback=0;"
  1574. . "showedit=$edits", T('Skip rollbacks')));
  1575. } else {
  1576. push(@menu, ScriptLink("$action;days=$days;all=0;rollback=1;"
  1577. . "showedit=$edits", T('Include rollbacks')));
  1578. }
  1579. }
  1580. if ($edits) {
  1581. push(@menu, ScriptLink("$action;days=$days;all=$all;showedit=0",
  1582. T('List only major changes')));
  1583. } else {
  1584. push(@menu, ScriptLink("$action;days=$days;all=$all;showedit=1",
  1585. T('Include minor changes')));
  1586. }
  1587. $html .= $q->p(join(' | ', (map { ScriptLink("$action;days=$_;all=$all;showedit=$edits", $_); } @RcDays)),
  1588. T('days'), $q->br(), @menu, $q->br(),
  1589. ScriptLink($action . ';from=' . ($LastUpdate + 1)
  1590. . ";all=$all;showedit=$edits", T('List later changes')),
  1591. ScriptLink($rss, T('RSS'), 'rss nopages nodiff'),
  1592. ScriptLink("$rss;full=1", T('RSS with pages'), 'rss pages nodiff'),
  1593. ScriptLink("$rss;full=1;diff=1", T('RSS with pages and diff'),
  1594. 'rss pages diff'));
  1595. $html .= $q->p({-class => 'documentation'}, T('Using the 「rollback」 button on this page will reset the wiki to that particular point in time, undoing any later changes to all of the pages.')) if UserIsAdmin() and GetParam('all', $ShowAll);
  1596. return $html;
  1597. }
  1598. sub GetScriptUrlWithRcParameters {
  1599. my $url = "$ScriptName?action=rss";
  1600. foreach my $param (qw(from upto days all showedit rollback rcidonly rcuseronly
  1601. rchostonly rcclusteronly rcfilteronly match lang
  1602. followup page diff full)) {
  1603. my $val = GetParam($param, undef);
  1604. $url .= ";$param=$val" if defined $val;
  1605. }
  1606. return $url;
  1607. }
  1608. sub GetFilterForm {
  1609. my $form = $q->strong(T('Filters'));
  1610. $form .= $q->input({-type=>'hidden', -name=>'action', -value=>'rc'});
  1611. $form .= $q->input({-type=>'hidden', -name=>'all', -value=>1}) if (GetParam('all', $ShowAll));
  1612. $form .= $q->input({-type=>'hidden', -name=>'showedit', -value=>1}) if (GetParam('showedit', $ShowEdits));
  1613. if (GetParam('days', $RcDefault) != $RcDefault) {
  1614. $form .= $q->input({-type=>'hidden', -name=>'days', -value=>GetParam('days', $RcDefault)});
  1615. }
  1616. my $table = '';
  1617. foreach my $h (['match' => T('Title:')],
  1618. ['rcfilteronly' => T('Title and Body:')],
  1619. ['rcuseronly' => T('Username:')], ['rchostonly' => T('Host:')],
  1620. ['followup' => T('Follow up to:')]) {
  1621. $table .= $q->Tr($q->td($q->label({-for=>$h->[0]}, $h->[1])),
  1622. $q->td($q->textfield(-name=>$h->[0], -id=>$h->[0], -size=>20)));
  1623. }
  1624. if (%Languages) {
  1625. $table .= $q->Tr($q->td($q->label({-for=>'rclang'}, T('Language:')))
  1626. . $q->td($q->textfield(-name=>'lang', -id=>'rclang', -size=>10,
  1627. -default=>GetParam('lang', ''))));
  1628. }
  1629. return GetFormStart(undef, 'get', 'filter') . $q->p($form) . $q->table($table)
  1630. . $q->p($q->submit('dofilter', T('Go!'))) . $q->end_form;
  1631. }
  1632. sub RcHtml {
  1633. my ($html, $inlist) = ('', 0);
  1634. # Optimize param fetches and translations out of main loop
  1635. my $all = GetParam('all', $ShowAll);
  1636. my $admin = UserIsAdmin();
  1637. my $rollback_was_possible = 0;
  1638. my $printDailyTear = sub {
  1639. my $date = shift;
  1640. if ($inlist) {
  1641. $html .= '</ul>';
  1642. $inlist = 0;
  1643. }
  1644. $html .= $q->p($q->strong($date));
  1645. if (not $inlist) {
  1646. $html .= '<ul>';
  1647. $inlist = 1;
  1648. }
  1649. };
  1650. my $printRCLine = sub {
  1651. my($id, $ts, $host, $username, $summary, $minor, $revision,
  1652. $languages, $cluster, $last) = @_;
  1653. my $all_revision = $last ? undef : $revision; # no revision for the last one
  1654. $host = QuoteHtml($host);
  1655. my $author = GetAuthorLink($username, $host);
  1656. my $sum = $summary ? $q->span({class=>'dash'}, ' &#8211; ')
  1657. . $q->strong(QuoteHtml($summary)) : '';
  1658. my $edit = $minor ? $q->em({class=>'type'}, T('(minor)')) : '';
  1659. my $lang = @{$languages}
  1660. ? $q->span({class=>'lang'}, '[' . join(', ', @{$languages}) . ']') : '';
  1661. my ($pagelink, $history, $diff, $rollback) = ('', '', '', '');
  1662. if ($all) {
  1663. $pagelink = GetOldPageLink('browse', $id, $all_revision, $id, $cluster);
  1664. my $rollback_is_possible = RollbackPossible($ts);
  1665. if ($admin and ($rollback_is_possible or $rollback_was_possible)) {
  1666. $rollback = $q->submit("rollback-$ts", T('rollback'));
  1667. $rollback_was_possible = $rollback_is_possible;
  1668. } else {
  1669. $rollback_was_possible = 0;
  1670. }
  1671. } elsif ($cluster) {
  1672. $pagelink = GetOldPageLink('browse', $id, $revision, $id, $cluster);
  1673. } else {
  1674. $pagelink = GetPageLink($id, $cluster);
  1675. $history = '(' . GetHistoryLink($id, T('history')) . ')';
  1676. }
  1677. if ($cluster and $PageCluster) {
  1678. $diff .= GetPageLink($PageCluster) . ':';
  1679. } elsif ($UseDiff and GetParam('diffrclink', 1)) {
  1680. if ($revision == 1) {
  1681. $diff .= '(' . $q->span({-class=>'new'}, T('new')) . ')';
  1682. } elsif ($all) {
  1683. $diff .= '(' . ScriptLinkDiff(2, $id, T('diff'), $all_revision) .')';
  1684. } else {
  1685. $diff .= '(' . ScriptLinkDiff($minor ? 2 : 1, $id, T('diff')) . ')';
  1686. }
  1687. }
  1688. $html .= $q->li($q->span({-class=>'time'}, CalcTime($ts)), $diff, $history,
  1689. $rollback, $pagelink, T(' . . . .'), $author, $sum, $lang,
  1690. $edit);
  1691. };
  1692. ProcessRcLines($printDailyTear, $printRCLine);
  1693. $html .= '</ul>' if $inlist;
  1694. # use delta between from and upto, or use days, whichever is available
  1695. my $to = GetParam('from', GetParam('upto', $Now - GetParam('days', $RcDefault) * 86400));
  1696. my $from = $to - (GetParam('upto') ? GetParam('upto') - GetParam('from') : GetParam('days', $RcDefault) * 86400);
  1697. my $more = "action=rc;from=$from;upto=$to";
  1698. foreach (qw(all showedit rollback rcidonly rcuseronly rchostonly
  1699. rcclusteronly rcfilteronly match lang followup)) {
  1700. my $val = GetParam($_, '');
  1701. $more .= ";$_=$val" if $val;
  1702. }
  1703. $html .= $q->p({-class=>'more'}, ScriptLink($more, T('More...'), 'more'));
  1704. return GetFormStart(undef, 'get', 'rc') . $html . $q->end_form;
  1705. }
  1706. sub PrintRcHtml { # to append RC to existing page, or action=rc directly
  1707. my ($id, $standalone) = @_;
  1708. my $rc = ($id eq $RCName or $id eq T($RCName) or T($id) eq $RCName);
  1709. if ($standalone) {
  1710. print GetHeader('', $rc ? NormalToFree($id) : Ts('All changes for %s', NormalToFree($id)));
  1711. }
  1712. if ($standalone or $rc or GetParam('rcclusteronly', '')) {
  1713. print $q->start_div({-class=>'rc'});
  1714. print $q->hr() unless $standalone or GetParam('embed', $EmbedWiki);
  1715. print RcHeader() . RcHtml() . GetFilterForm() . $q->end_div();
  1716. }
  1717. PrintFooter($id) if $standalone;
  1718. }
  1719. sub RcTextItem {
  1720. my ($name, $value) = @_;
  1721. $value = UnquoteHtml($value);
  1722. $value =~ s/\n+$//;
  1723. $value =~ s/\n+/\n /;
  1724. return $value ? $name . ': ' . $value . "\n" : '';
  1725. }
  1726. sub RcTextRevision {
  1727. my($id, $ts, $host, $username, $summary, $minor, $revision,
  1728. $languages, $cluster, $last) = @_;
  1729. my $link = $ScriptName
  1730. . (GetParam('all', $ShowAll) && ! $last
  1731. ? '?' . GetPageParameters('browse', $id, $revision, $cluster, $last)
  1732. : ($UsePathInfo ? '/' : '?') . UrlEncode($id));
  1733. print "\n", RcTextItem('title', NormalToFree($id)),
  1734. RcTextItem('description', $summary),
  1735. RcTextItem('generator', GetAuthor($username)),
  1736. RcTextItem('language', join(', ', @{$languages})), RcTextItem('link', $link),
  1737. RcTextItem('last-modified', TimeToW3($ts)),
  1738. RcTextItem('revision', $revision),
  1739. RcTextItem('minor', $minor);
  1740. }
  1741. sub PrintRcText { # print text rss header and call ProcessRcLines
  1742. local $RecentLink = 0;
  1743. print RcTextItem('title', $SiteName),
  1744. RcTextItem('description', $SiteDescription), RcTextItem('link', $ScriptName),
  1745. RcTextItem('generator', 'Oddmuse'), RcTextItem('rights', $RssRights);
  1746. ProcessRcLines(sub {}, \&RcTextRevision);
  1747. }
  1748. sub GetRcRss {
  1749. my $date = TimeToRFC822($LastUpdate);
  1750. my %excluded = ();
  1751. if (GetParam("exclude", 1)) {
  1752. foreach (split(/\n/, GetPageContent($RssExclude))) {
  1753. if (/^ ([^ ]+)[ \t]*$/) { # only read lines with one word after one space
  1754. $excluded{$1} = 1;
  1755. }
  1756. }
  1757. }
  1758. my $rss = qq{<?xml version="1.0" encoding="UTF-8"?>\n};
  1759. if ($RssStyleSheet =~ /\.(xslt?|xml)$/) {
  1760. $rss .= qq{<?xml-stylesheet type="text/xml" href="$RssStyleSheet" ?>\n};
  1761. } elsif ($RssStyleSheet) {
  1762. $rss .= qq{<?xml-stylesheet type="text/css" href="$RssStyleSheet" ?>\n};
  1763. }
  1764. $rss .= qq{<rss version="2.0"
  1765. xmlns:wiki="http://purl.org/rss/1.0/modules/wiki/"
  1766. xmlns:dc="http://purl.org/dc/elements/1.1/"
  1767. xmlns:cc="http://web.resource.org/cc/"
  1768. xmlns:atom="http://www.w3.org/2005/Atom">
  1769. <channel>
  1770. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  1771. };
  1772. my $title = QuoteHtml($SiteName) . ': ' . GetParam('title', QuoteHtml(NormalToFree($HomePage)));
  1773. $rss .= "<title>$title</title>\n";
  1774. $rss .= "<link>" . ScriptUrl($HomePage) . "</link>\n";
  1775. $rss .= qq{<atom:link href="} . GetScriptUrlWithRcParameters()
  1776. . qq{" rel="self" type="application/rss+xml" />\n};
  1777. if ($SiteDescription) {
  1778. $rss .= "<description>" . QuoteHtml($SiteDescription) . "</description>\n"
  1779. }
  1780. $rss .= "<pubDate>$date</pubDate>\n";
  1781. $rss .= "<lastBuildDate>$date</lastBuildDate>\n";
  1782. $rss .= "<generator>Oddmuse</generator>\n";
  1783. $rss .= "<copyright>$RssRights</copyright>\n" if $RssRights;
  1784. if ($RssLicense) {
  1785. $rss .= join('', map {"<cc:license>" . QuoteHtml($_) . "</cc:license>\n"}
  1786. (ref $RssLicense eq 'ARRAY' ? @$RssLicense : $RssLicense))
  1787. }
  1788. $rss .= "<wiki:interwiki>$InterWikiMoniker</wiki:interwiki>\n" if $InterWikiMoniker;
  1789. if ($RssImageUrl) {
  1790. $rss .= "<image>\n";
  1791. $rss .= "<url>$RssImageUrl</url>\n";
  1792. $rss .= "<title>$title</title>\n"; # the same as the channel
  1793. $rss .= "<link>$ScriptName</link>\n"; # the same as the channel
  1794. $rss .= "</image>\n";
  1795. }
  1796. my $limit = GetParam("rsslimit", 15); # Only take the first 15 entries
  1797. my $count = 0;
  1798. ProcessRcLines(sub {}, sub {
  1799. my $id = shift;
  1800. return if $excluded{$id} or ($limit ne 'all' and $count++ >= $limit);
  1801. $rss .= "\n" . RssItem($id, @_);
  1802. });
  1803. $rss .= "</channel>\n</rss>\n";
  1804. return $rss;
  1805. }
  1806. sub RssItem {
  1807. my ($id, $ts, $host, $username, $summary, $minor, $revision,
  1808. $languages, $cluster, $last) = @_;
  1809. my $name = ItemName($id);
  1810. if (GetParam('full', 0)) { # full page means summary is not shown
  1811. $summary = PageHtml($id, 50 * 1024, T('This page is too big to send over RSS.'));
  1812. } else {
  1813. $summary = QuoteHtml($summary); # page summary must be quoted
  1814. }
  1815. my $date = TimeToRFC822($ts);
  1816. $username = QuoteHtml($username);
  1817. my $rss = "<item>\n";
  1818. $rss .= "<title>$name</title>\n";
  1819. my $link = ScriptUrl(GetParam('all', $cluster)
  1820. ? GetPageParameters('browse', $id, $revision, $cluster, $last)
  1821. : UrlEncode($id));
  1822. $rss .= "<link>$link</link>\n<guid>$link</guid>\n";
  1823. $rss .= "<description>" . QuoteHtml($summary) . "</description>\n" if $summary;
  1824. $rss .= "<pubDate>" . $date . "</pubDate>\n";
  1825. $rss .= "<comments>" . ScriptUrl($CommentsPrefix . UrlEncode($id))
  1826. . "</comments>\n" if $CommentsPattern and $id !~ /$CommentsPattern/;
  1827. $rss .= "<dc:contributor>" . $username . "</dc:contributor>\n" if $username;
  1828. $rss .= "<wiki:status>" . (1 == $revision ? 'new' : 'updated') . "</wiki:status>\n";
  1829. $rss .= "<wiki:importance>" . ($minor ? 'minor' : 'major') . "</wiki:importance>\n";
  1830. $rss .= "<wiki:version>" . $revision . "</wiki:version>\n";
  1831. $rss .= "<wiki:history>" . ScriptUrl("action=history;id=" . UrlEncode($id))
  1832. . "</wiki:history>\n";
  1833. $rss .= "<wiki:diff>" . ScriptUrl("action=browse;diff=1;id=" . UrlEncode($id))
  1834. . "</wiki:diff>\n" if $UseDiff and GetParam('diffrclink', 1);
  1835. return $rss . "</item>\n";
  1836. }
  1837. sub DoRss {
  1838. print GetHttpHeader('application/xml');
  1839. print GetRcRss();
  1840. }
  1841. sub DoHistory {
  1842. my $id = shift;
  1843. ValidIdOrDie($id);
  1844. OpenPage($id);
  1845. if (GetParam('raw', 0)) {
  1846. DoRawHistory($id);
  1847. } else {
  1848. DoHtmlHistory($id);
  1849. }
  1850. }
  1851. sub DoRawHistory {
  1852. my ($id) = @_;
  1853. print GetHttpHeader('text/plain'),
  1854. RcTextItem('title', Ts('History of %s', NormalToFree($OpenPageName))),
  1855. RcTextItem('date', TimeToText($Now)),
  1856. RcTextItem('link', ScriptUrl("action=history;id=$OpenPageName;raw=1")),
  1857. RcTextItem('generator', 'Oddmuse');
  1858. SetParam('all', 1);
  1859. my @languages = split(/,/, $Page{languages});
  1860. RcTextRevision($id, $Page{ts}, $Page{host}, $Page{username}, $Page{summary},
  1861. $Page{minor}, $Page{revision}, \@languages, undef, 1);
  1862. foreach my $revision (GetKeepRevisions($OpenPageName)) {
  1863. my $keep = GetKeptRevision($revision);
  1864. @languages = split(/,/, $keep->{languages});
  1865. RcTextRevision($id, $keep->{ts}, $keep->{host}, $keep->{username},
  1866. $keep->{summary}, $keep->{minor}, $keep->{revision}, \@languages);
  1867. }
  1868. }
  1869. sub DoHtmlHistory {
  1870. my ($id) = @_;
  1871. print GetHeader('', Ts('History of %s', NormalToFree($id)));
  1872. my $row = 0;
  1873. my $rollback = UserCanEdit($id, 0) && (GetParam('username', '') or UserIsEditor());
  1874. my $date = CalcDay($Page{ts});
  1875. my @html = (GetFormStart(undef, 'get', 'history'));
  1876. push(@html, $q->p({-class => 'documentation'}, T('Using the 「rollback」 button on this page will reset the page to that particular point in time, undoing any later changes to this page.'))) if $rollback;
  1877. push(@html, $q->p(# don't use $q->hidden here!
  1878. $q->input({-type=>'hidden', -name=>'action', -value=>'browse'}),
  1879. $q->input({-type=>'hidden', -name=>'diff', -value=>'1'}),
  1880. $q->input({-type=>'hidden', -name=>'id', -value=>$id})));
  1881. # list of rows with revisions, starting with current revision
  1882. push(@html, $q->p($q->submit({-name=>T('Compare')}))) if $UseDiff;
  1883. my @rows = (GetHistoryLine($id, \%Page, $row++, $rollback, $date, 1));
  1884. foreach my $revision (GetKeepRevisions($OpenPageName)) {
  1885. my $keep = GetKeptRevision($revision);
  1886. my $new = CalcDay($keep->{ts});
  1887. push(@rows, GetHistoryLine($id, $keep, $row++, $rollback, $new, $new ne $date));
  1888. $date = $new;
  1889. }
  1890. # if we can use diff, add radio-buttons and compare buttons if $UseDiff
  1891. if ($UseDiff) {
  1892. push(@html, $q->table({-class=>'history'}, @rows),
  1893. $q->p($q->submit({-name=>T('Compare')})), $q->end_form());
  1894. } else {
  1895. push(@html, @rows);
  1896. }
  1897. if ($KeepDays and $rollback and $Page{revision}) {
  1898. push(@html, $q->p(ScriptLink('title=' . UrlEncode($id) . ';text='
  1899. . UrlEncode($DeletedPage) . ';summary='
  1900. . UrlEncode(T('Deleted')),
  1901. T('Mark this page for deletion'))));
  1902. }
  1903. print $q->div({-class=>'content history'}, @html);
  1904. PrintFooter($id, 'history');
  1905. }
  1906. sub GetHistoryLine {
  1907. my ($id, $dataref, $row, $rollback, $date, $newday) = @_;
  1908. my %data = %$dataref;
  1909. my $revision = $data{revision};
  1910. return $q->p(T('No other revisions available')) unless $revision;
  1911. my $html = CalcTime($data{ts});
  1912. if ($row == 0) { # current revision
  1913. $html .= ' (' . T('current') . ')' if $rollback;
  1914. $html .= ' ' . GetPageLink($id, Ts('Revision %s', $revision));
  1915. } else {
  1916. $html .= ' ' . $q->submit("rollback-$data{ts}", T('rollback')) if $rollback;
  1917. $html .= ' ' . GetOldPageLink('browse', $id, $revision,
  1918. Ts('Revision %s', $revision));
  1919. }
  1920. $html .= T(' . . . .') . ' ' . GetAuthorLink($data{username});
  1921. $html .= $q->span({class=>'dash'}, ' &#8211; ')
  1922. . $q->strong(QuoteHtml($data{summary})) if $data{summary};
  1923. $html .= ' ' . $q->em({class=>'type'}, T('(minor)')) . ' ' if $data{minor};
  1924. if ($UseDiff) {
  1925. my %attr1 = (-type=>'radio', -name=>'diffrevision', -value=>$revision);
  1926. $attr1{-checked} = 'checked' if $row == 1;
  1927. my %attr2 = (-type=>'radio', -name=>'revision', -value=> $row ? $revision : '');
  1928. $attr2{-checked} = 'checked' if $row == 0; # first row is special
  1929. $html = $q->Tr($q->td($q->input(\%attr1)), $q->td($q->input(\%attr2)), $q->td($html));
  1930. $html = $q->Tr($q->td({-colspan=>3}, $q->strong($date))) . $html if $newday;
  1931. } else {
  1932. $html .= $q->br();
  1933. $html = $q->strong($date) . $q->br() . $html if $newday;
  1934. }
  1935. return $html;
  1936. }
  1937. sub DoContributors {
  1938. my $id = shift;
  1939. SetParam('rcidonly', $id);
  1940. SetParam('all', 1);
  1941. print GetHeader('', Ts('Contributors to %s', NormalToFree($id || $SiteName)));
  1942. my %contrib = ();
  1943. for my $line (GetRcLines(1)) {
  1944. my ($ts, $pagename, $minor, $summary, $host, $username) = @$line;
  1945. $contrib{$username}++ if $username;
  1946. }
  1947. print $q->div({-class=>'content contrib'},
  1948. $q->p(map { GetPageLink($_) } sort(keys %contrib)));
  1949. PrintFooter();
  1950. }
  1951. sub RollbackPossible {
  1952. my $ts = shift; # there can be no rollback to the most recent change(s) made (1s resolution!)
  1953. return $ts != $LastUpdate && (!$KeepDays || ($Now - $ts) < $KeepDays * 86400); # 24*60*60
  1954. }
  1955. sub DoRollback {
  1956. my $page = shift;
  1957. my $to = GetParam('to', 0);
  1958. ReportError(T('Missing target for rollback.'), '400 BAD REQUEST') unless $to;
  1959. ReportError(T('Target for rollback is too far back.'), '400 BAD REQUEST') unless $page or RollbackPossible($to);
  1960. ReportError(T('A username is required for ordinary users.'), '403 FORBIDDEN') unless GetParam('username', '') or UserIsEditor();
  1961. my @ids = ();
  1962. if (not $page) { # cannot just use list length because of ('')
  1963. return unless UserIsAdminOrError(); # only admins can do mass changes
  1964. SetParam('showedit', 1); # make GetRcLines return minor edits as well
  1965. SetParam('all', 1); # prevent LatestChanges from interfering
  1966. SetParam('rollback', 1); # prevent StripRollbacks from interfering
  1967. my %ids = map { my ($ts, $id) = @$_; $id => 1; } # make unique via hash
  1968. GetRcLines($to); # list all the pages edited since $to
  1969. @ids = keys %ids;
  1970. } else {
  1971. @ids = ($page);
  1972. }
  1973. RequestLockOrError();
  1974. print GetHeader('', T('Rolling back changes')),
  1975. $q->start_div({-class=>'content rollback'}), $q->start_p();
  1976. foreach my $id (@ids) {
  1977. OpenPage($id);
  1978. my ($text, $minor, $ts) = GetTextAtTime($to);
  1979. if ($Page{text} eq $text) {
  1980. print T("The two revisions are the same."), $q->br() if $page; # no message when doing mass revert
  1981. } elsif (not UserCanEdit($id, 1)) {
  1982. print Ts('Editing not allowed: %s is read-only.', $id), $q->br();
  1983. } elsif (not UserIsEditor() and my $rule = BannedContent($text)) {
  1984. print Ts('Rollback of %s would restore banned content.', $id), $rule, $q->br();
  1985. } else {
  1986. Save($id, $text, Ts('Rollback to %s', TimeToText($to)), $minor, ($Page{host} ne $q->remote_addr()));
  1987. print Ts('%s rolled back', GetPageLink($id)), ($ts ? ' ' . Ts('to %s', TimeToText($to)) : ''), $q->br();
  1988. }
  1989. }
  1990. WriteRcLog('[[rollback]]', $page, $to); # leave marker
  1991. print $q->end_p() . $q->end_div();
  1992. ReleaseLock();
  1993. PrintFooter($page, 'edit');
  1994. }
  1995. sub DoAdminPage {
  1996. my ($id, @rest) = @_;
  1997. my @menu = ();
  1998. push(@menu, ScriptLink('action=index', T('Index of all pages'), 'index')) if $Action{index};
  1999. push(@menu, ScriptLink('action=version', T('Wiki Version'), 'version')) if $Action{version};
  2000. push(@menu, ScriptLink('action=password', T('Password'), 'password')) if $Action{password};
  2001. push(@menu, ScriptLink('action=maintain', T('Run maintenance'), 'maintain')) if $Action{maintain};
  2002. my @locks;
  2003. for my $pattern (@KnownLocks) {
  2004. for my $name (Glob($pattern)) {
  2005. if (IsDir($LockDir . $name)) {
  2006. push(@locks, $name);
  2007. }
  2008. }
  2009. }
  2010. if (@locks and $Action{unlock}) {
  2011. push(@menu, ScriptLink('action=unlock', T('Unlock Wiki'), 'unlock') . ' (' . join(', ', @locks) . ')');
  2012. };
  2013. if (UserIsAdmin()) {
  2014. if ($Action{editlock}) {
  2015. if (IsFile("$DataDir/noedit")) {
  2016. push(@menu, ScriptLink('action=editlock;set=0', T('Unlock site'), 'editlock 0'));
  2017. } else {
  2018. push(@menu, ScriptLink('action=editlock;set=1', T('Lock site'), 'editlock 1'));
  2019. }
  2020. }
  2021. if ($id and $Action{pagelock}) {
  2022. my $title = NormalToFree($id);
  2023. if (IsFile(GetLockedPageFile($id))) {
  2024. push(@menu, ScriptLink('action=pagelock;set=0;id=' . UrlEncode($id),
  2025. Ts('Unlock %s', $title), 'pagelock 0'));
  2026. } else {
  2027. push(@menu, ScriptLink('action=pagelock;set=1;id=' . UrlEncode($id),
  2028. Ts('Lock %s', $title), 'pagelock 1'));
  2029. }
  2030. }
  2031. push(@menu, ScriptLink('action=clear', T('Clear Cache'), 'clear')) if $Action{clear};
  2032. }
  2033. foreach my $sub (@MyAdminCode) {
  2034. $sub->($id, \@menu, \@rest);
  2035. $Message .= $q->p($@) if $@; # since this happens before GetHeader is called, the message will be shown
  2036. }
  2037. print GetHeader('', T('Administration')),
  2038. $q->div({-class=>'content admin'}, $q->p(T('Actions:')), $q->ul($q->li(\@menu)),
  2039. $q->p(T('Important pages:')) . $q->ul(map { $q->li(GetPageOrEditLink($_, NormalToFree($_))) if $_;
  2040. } sort keys %AdminPages),
  2041. $q->p(Ts('To mark a page for deletion, put <strong>%s</strong> on the first line.',
  2042. $DeletedPage)), @rest);
  2043. PrintFooter();
  2044. }
  2045. sub GetPageParameters {
  2046. my ($action, $id, $revision, $cluster, $last) = @_;
  2047. $id = FreeToNormal($id);
  2048. my $link = "action=$action;id=" . UrlEncode($id);
  2049. $link .= ";revision=$revision" if $revision and not $last;
  2050. $link .= ';rcclusteronly=' . UrlEncode($cluster) if $cluster;
  2051. return $link;
  2052. }
  2053. sub GetOldPageLink {
  2054. my ($action, $id, $revision, $name, $cluster, $last) = @_;
  2055. return ScriptLink(GetPageParameters($action, $id, $revision, $cluster, $last),
  2056. NormalToFree($name), 'revision');
  2057. }
  2058. sub GetSearchLink {
  2059. my ($text, $class, $name, $title) = @_;
  2060. my $id = UrlEncode(QuoteRegexp('"' . $text . '"'));
  2061. $name = UrlEncode($name);
  2062. $text = NormalToFree($text);
  2063. $id =~ s/_/+/g; # Search for url-escaped spaces
  2064. return ScriptLink('search=' . $id, $text, $class, $name, $title);
  2065. }
  2066. sub ScriptLinkDiff {
  2067. my ($diff, $id, $text, $new, $old) = @_;
  2068. my $action = 'action=browse;diff=' . $diff . ';id=' . UrlEncode($id);
  2069. $action .= ";diffrevision=$old" if $old;
  2070. $action .= ";revision=$new" if $new;
  2071. return ScriptLink($action, $text, 'diff');
  2072. }
  2073. sub ColorCode {
  2074. my ($str) = @_;
  2075. my $num = unpack("L",B::hash($str)); # 32-bit integer
  2076. my $code = sprintf("%o", $num); # octal is 0-7
  2077. my @indexes = split(//, substr($code, 0, 4)); # four numbers
  2078. my @colors = qw/red orange yellow green blue indigo violet white/;
  2079. return $q->span({-class => 'ip-code', -title => T('Anonymous')},
  2080. join('', map { $q->span({-class => $colors[$_]}, $_) }
  2081. @indexes));
  2082. }
  2083. sub GetAuthor {
  2084. my ($username) = @_;
  2085. return $username if $username;
  2086. return T('Anonymous');
  2087. }
  2088. sub GetAuthorLink {
  2089. my ($username, $host) = @_;
  2090. $username = FreeToNormal($username);
  2091. my $name = NormalToFree($username);
  2092. if (ValidId($username) ne '') { # ValidId() returns error string
  2093. $username = ''; # Just pretend it isn't there.
  2094. }
  2095. if ($username and $RecentLink) {
  2096. return ScriptLink(UrlEncode($username), $name, 'author');
  2097. } elsif ($username) {
  2098. return $q->span({-class=>'author'}, $name);
  2099. }
  2100. return T('Anonymous') if $host eq 'Anonymous';
  2101. return ColorCode($host);
  2102. }
  2103. sub GetHistoryLink {
  2104. my ($id, $text) = @_;
  2105. my $action = 'action=history;id=' . UrlEncode(FreeToNormal($id));
  2106. return ScriptLink($action, $text, 'history');
  2107. }
  2108. sub GetRCLink {
  2109. my ($id, $text) = @_;
  2110. return ScriptLink('action=rc;all=1;from=1;showedit=1;rcidonly='
  2111. . UrlEncode(FreeToNormal($id)), $text, 'rc');
  2112. }
  2113. sub GetHeader {
  2114. my ($id, $title, $oldId, $nocache, $status) = @_;
  2115. my $embed = GetParam('embed', $EmbedWiki);
  2116. my $result = GetHttpHeader('text/html', $nocache, $status);
  2117. if ($oldId) {
  2118. $Message .= $q->p('(' . Ts('redirected from %s', GetEditLink($oldId, $oldId)) . ')');
  2119. }
  2120. $result .= GetHtmlHeader(Ts('%s:', $SiteName) . ' ' . UnWiki($title), $id);
  2121. if ($embed) {
  2122. $result .= $q->div({-class=>'header'}, $q->div({-class=>'message'}, $Message)) if $Message;
  2123. return $result;
  2124. }
  2125. $result .= GetHeaderDiv($id, $title, $oldId, $embed);
  2126. return $result . $q->start_div({-class=>'wrapper'});
  2127. }
  2128. sub GetHeaderDiv {
  2129. my ($id, $title, $oldId, $embed) = @_;
  2130. my $result .= $q->start_div({-class=>'header'});
  2131. if (not $embed and $LogoUrl) {
  2132. my $url = $IndexHash{$LogoUrl} ? GetDownloadLink($LogoUrl, 2) : $LogoUrl;
  2133. $result .= ScriptLink(UrlEncode($HomePage), $q->img({-src=>$url, -alt=>T('[Home]'), -class=>'logo'}), 'logo');
  2134. }
  2135. $result .= $q->start_div({-class=>'menu'});
  2136. if (GetParam('toplinkbar', $TopLinkBar) != 2) {
  2137. $result .= GetGotoBar($id);
  2138. if (%SpecialDays) {
  2139. my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($Now);
  2140. if ($SpecialDays{($mon + 1) . '-' . $mday}) {
  2141. $result .= $q->br() . $q->span({-class=>'specialdays'},
  2142. $SpecialDays{($mon + 1) . '-' . $mday});
  2143. }
  2144. }
  2145. }
  2146. $result .= GetSearchForm() if GetParam('topsearchform', $TopSearchForm) != 2;
  2147. $result .= $q->end_div();
  2148. $result .= $q->div({-class=>'message'}, $Message) if $Message;
  2149. $result .= GetHeaderTitle($id, $title, $oldId);
  2150. $result .= $q->end_div();
  2151. return $result;
  2152. }
  2153. sub GetHeaderTitle {
  2154. my ($id, $title, $oldId) = @_;
  2155. return $q->h1($title) if $id eq '';
  2156. return $q->h1(GetSearchLink($id, '', '', T('Click to search for references to this page')));
  2157. }
  2158. sub GetHttpHeader {
  2159. return if $HeaderIsPrinted; # When calling ReportError, we don't know whether HTTP headers have
  2160. $HeaderIsPrinted = 1; # already been printed. We want them printed just once.
  2161. my ($type, $ts, $status, $encoding) = @_;
  2162. $q->charset($type =~ m!^(text/|application/xml)! ? 'utf-8' : ''); # text/plain, text/html, application/xml: UTF-8
  2163. my %headers = (-cache_control=>($UseCache < 0 ? 'no-cache' : 'max-age=10'));
  2164. # Set $ts when serving raw content that cannot be modified by cookie
  2165. # parameters; or 'nocache'; or undef. If you provide a $ts, the last-modified
  2166. # header generated will by used by HTTP/1.0 clients. If you provide no $ts,
  2167. # the etag header generated will be used by HTTP/1.1 clients. In this
  2168. # situation, cookie parameters can influence the look of the page and we
  2169. # cannot rely on $LastUpdate. HTTP/1.0 clients will ignore etags. See RFC 2616
  2170. # section 13.3.4.
  2171. if (GetParam('cache', $UseCache) >= 2 and $ts ne 'nocache') {
  2172. $headers{'-last-modified'} = TimeToRFC822($ts) if $ts;
  2173. $headers{-etag} = PageEtag();
  2174. }
  2175. $headers{-type} = GetParam('mime-type', $type);
  2176. $headers{-status} = $status if $status;
  2177. $headers{-Content_Encoding} = $encoding if $encoding;
  2178. my $cookie = Cookie();
  2179. $headers{-cookie} = $cookie if $cookie;
  2180. if ($q->request_method() eq 'HEAD') {
  2181. print $q->header(%headers), "\n\n"; # add newlines for FCGI because of exit()
  2182. exit; # total shortcut -- HEAD never expects anything other than the header!
  2183. }
  2184. return $q->header(%headers);
  2185. }
  2186. sub CookieData {
  2187. my ($changed, %params);
  2188. foreach my $key (keys %CookieParameters) {
  2189. my $default = $CookieParameters{$key};
  2190. my $value = GetParam($key, $default);
  2191. $params{$key} = $value if $value ne $default;
  2192. # The cookie is considered to have changed under the following
  2193. # condition: If the value was already set, and the new value is
  2194. # not the same as the old value, or if there was no old value, and
  2195. # the new value is not the default.
  2196. my $change = (defined $OldCookie{$key} ? ($value ne $OldCookie{$key}) : ($value ne $default));
  2197. $changed = 1 if $change; # note if any parameter changed and needs storing
  2198. }
  2199. return $changed, %params;
  2200. }
  2201. sub Cookie {
  2202. my ($changed, %params) = CookieData(); # params are URL encoded
  2203. if ($changed) {
  2204. my $cookie = join(UrlEncode($FS), %params); # no CTL in field values
  2205. return $q->cookie(-name=>$CookieName, -value=>$cookie, -expires=>'+2y', secure=>$ENV{'HTTPS'}, httponly=>1);
  2206. }
  2207. return '';
  2208. }
  2209. sub GetHtmlHeader { # always HTML!
  2210. my ($title, $id) = @_;
  2211. my $edit_link = '';
  2212. $edit_link = '<link rel="alternate" type="application/wiki" title="'
  2213. . T('Edit this page') . '" href="'
  2214. . ScriptUrl('action=edit;id=' . UrlEncode($id)) . '" />' if $id;
  2215. return $DocumentHeader
  2216. . $q->head($q->title($title) . $edit_link
  2217. . GetCss() . GetRobots() . GetFeeds() . $HtmlHeaders
  2218. . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />')
  2219. . '<body class="' . GetParam('theme', 'default') . '">';
  2220. }
  2221. sub GetRobots { # NOINDEX for non-browse pages.
  2222. if (GetParam('action', 'browse') eq 'browse' and not GetParam('revision', '')) {
  2223. return '<meta name="robots" content="INDEX,FOLLOW" />';
  2224. } else {
  2225. return '<meta name="robots" content="NOINDEX,FOLLOW" />';
  2226. }
  2227. }
  2228. sub GetFeeds { # default for $HtmlHeaders
  2229. my $html = '<link rel="alternate" type="application/rss+xml" title="'
  2230. . QuoteHtml($SiteName) . '" href="' . $ScriptName . '?action=rss" />';
  2231. my $id = GetId(); # runs during Init, not during DoBrowseRequest
  2232. $html .= '<link rel="alternate" type="application/rss+xml" title="'
  2233. . QuoteHtml("$SiteName: $id") . '" href="' . $ScriptName
  2234. . '?action=rss;rcidonly=' . UrlEncode(FreeToNormal($id)) . '" />' if $id;
  2235. my $username = GetParam('username', '');
  2236. $html .= '<link rel="alternate" type="application/rss+xml" '
  2237. . 'title="Follow-ups for ' . NormalToFree($username) . '" '
  2238. . 'href="' . ScriptUrl('action=rss;followup=' . UrlEncode($username))
  2239. . '" />' if $username;
  2240. return $html;
  2241. }
  2242. sub GetCss { # prevent javascript injection
  2243. my @css = map { my $x = $_; $x =~ s/\".*//; $x; } split(/\s+/, GetParam('css', ''));
  2244. push (@css, ref $StyleSheet ? @$StyleSheet : $StyleSheet) if $StyleSheet and not @css;
  2245. if ($IndexHash{$StyleSheetPage} and not @css) {
  2246. push (@css, "$ScriptName?action=browse;id=" . UrlEncode($StyleSheetPage) . ";raw=1;mime-type=text/css")
  2247. }
  2248. push (@css, 'https://oddmuse.org/default.css') unless @css;
  2249. return join('', map { qq(<link type="text/css" rel="stylesheet" href="$_" />) } @css);
  2250. }
  2251. sub PrintPageContent {
  2252. my ($text, $revision, $comment) = @_;
  2253. print $q->start_div({-class=>'content browse'});
  2254. if ($revision eq '' and $Page{blocks} and GetParam('cache', $UseCache) > 0) {
  2255. PrintCache();
  2256. } else {
  2257. my $savecache = ($Page{revision} > 0 and $revision eq ''); # new page not cached
  2258. PrintWikiToHTML($text, $savecache, $revision); # unlocked, with anchors, unlocked
  2259. }
  2260. if ($comment) {
  2261. print $q->start_div({-class=>'preview'}), $q->hr();
  2262. print $q->h2(T('Preview:'));
  2263. # no caching, current revision, unlocked
  2264. PrintWikiToHTML(AddComment('', $comment));
  2265. print $q->hr(), $q->h2(T('Preview only, not yet saved')), $q->end_div();
  2266. }
  2267. print $q->end_div();
  2268. }
  2269. sub PrintFooter {
  2270. my ($id, $rev, $comment, $page) = @_;
  2271. if (GetParam('embed', $EmbedWiki)) {
  2272. print $q->end_html, "\n";
  2273. return;
  2274. }
  2275. PrintMyContent($id) if defined(&PrintMyContent);
  2276. foreach my $sub (@MyFooters) {
  2277. print $sub->(@_);
  2278. }
  2279. print $q->end_html, "\n";
  2280. }
  2281. sub WrapperEnd { # called via @MyFooters
  2282. return $q->start_div({-class=>'wrapper close'}) . $q->end_div() . $q->end_div(); # closes content
  2283. }
  2284. sub DefaultFooter { # called via @MyFooters
  2285. my ($id, $rev, $comment, $page) = @_;
  2286. my $html = $q->start_div({-class=>'footer'}) . $q->hr();
  2287. $html .= GetGotoBar($id) if GetParam('toplinkbar', $TopLinkBar) != 1;
  2288. $html .= GetFooterLinks($id, $rev);
  2289. $html .= GetFooterTimestamp($id, $rev, $page);
  2290. $html .= GetSearchForm() if GetParam('topsearchform', $TopSearchForm) != 1;
  2291. if ($DataDir =~ m|/tmp/|) {
  2292. $html .= $q->p($q->strong(T('Warning') . ': ')
  2293. . Ts('Database is stored in temporary directory %s', $DataDir));
  2294. }
  2295. $html .= T($FooterNote) if $FooterNote;
  2296. $html .= $q->p(Ts('%s seconds', (time - $Now))) if GetParam('timing', 0);
  2297. $html .= $q->end_div();
  2298. return $html;
  2299. }
  2300. sub GetFooterTimestamp {
  2301. my ($id, $rev, $page) = @_;
  2302. $page //= \%Page;
  2303. if ($id and $rev ne 'history' and $rev ne 'edit' and $page->{revision}) {
  2304. my @elements = (($rev eq '' ? T('Last edited') : T('Edited')), TimeToText($page->{ts}),
  2305. Ts('by %s', GetAuthorLink($page->{username})));
  2306. push(@elements, ScriptLinkDiff(2, $id, T('(diff)'), $rev)) if $UseDiff and $page->{revision} > 1;
  2307. return $q->div({-class=>'time'}, @elements);
  2308. }
  2309. return '';
  2310. }
  2311. sub GetFooterLinks {
  2312. my ($id, $rev) = @_;
  2313. my @elements;
  2314. if ($id and $rev ne 'history' and $rev ne 'edit') {
  2315. if ($CommentsPattern) {
  2316. if ($id =~ /$CommentsPattern/) {
  2317. push(@elements, GetPageLink($1, undef, 'original', T('a'))) if $1;
  2318. } else {
  2319. push(@elements, GetPageLink($CommentsPrefix . $id, undef, 'comment', T('c')));
  2320. }
  2321. }
  2322. if (UserCanEdit($id, 0)) {
  2323. if ($rev) { # showing old revision
  2324. push(@elements, GetOldPageLink('edit', $id, $rev, Ts('Edit revision %s of this page', $rev)));
  2325. } else { # showing current revision
  2326. push(@elements, GetEditLink($id, T('Edit this page'), undef, T('e')));
  2327. }
  2328. } else { # no permission or generated page
  2329. push(@elements, ScriptLink('action=password', T('This page is read-only'), 'password'));
  2330. }
  2331. }
  2332. push(@elements, GetHistoryLink($id, T('View other revisions'))) if $Action{history} and $id and $rev ne 'history';
  2333. push(@elements, GetPageLink($id, T('View current revision')),
  2334. GetRCLink($id, T('View all changes'))) if $Action{history} and $rev ne '';
  2335. if ($Action{contrib} and $id and $rev eq 'history') {
  2336. push(@elements, ScriptLink("action=contrib;id=" . UrlEncode($id), T('View contributors'), 'contrib'));
  2337. }
  2338. if ($Action{admin} and GetParam('action', '') ne 'admin') {
  2339. my $action = 'action=admin';
  2340. $action .= ';id=' . UrlEncode($id) if $id;
  2341. push(@elements, ScriptLink($action, T('Administration'), 'admin'));
  2342. }
  2343. return @elements ? $q->div({-class=>'edit bar'}, @elements) : '';
  2344. }
  2345. sub GetCommentForm {
  2346. my ($id, $rev, $comment) = @_;
  2347. if ($CommentsPattern ne '' and $id and $rev ne 'history' and $rev ne 'edit'
  2348. and $id =~ /$CommentsPattern/ and UserCanEdit($id, 0, 1)) {
  2349. my $html = $q->div({-class=>'comment'},
  2350. GetFormStart(undef, undef, 'comment'),
  2351. $q->p(GetHiddenValue('title', $id),
  2352. $q->label({-for=>'aftertext', -accesskey=>T('c')},
  2353. T('Add your comment here:')), $q->br(),
  2354. GetTextArea('aftertext', $comment, 10)),
  2355. $EditNote,
  2356. $q->p($q->span({-class=>'username'},
  2357. $q->label({-for=>'username'}, T('Username:')), ' ',
  2358. $q->textfield(-name=>'username', -id=>'username',
  2359. -default=>GetParam('username', ''),
  2360. -override=>1, -size=>20, -maxlength=>50)),
  2361. $q->span({-class=>'homepage'},
  2362. $q->label({-for=>'homepage'}, T('Homepage URL:')), ' ',
  2363. $q->textfield(-name=>'homepage', -id=>'homepage',
  2364. -default=>GetParam('homepage', ''),
  2365. -override=>1, -size=>40, -maxlength=>100))),
  2366. $q->p($q->submit(-name=>'Save', -accesskey=>T('s'), -value=>T('Save')), ' ',
  2367. $q->submit(-name=>'Preview', -accesskey=>T('p'), -value=>T('Preview'))),
  2368. $q->end_form());
  2369. foreach my $sub (@MyFormChanges) {
  2370. $html = $sub->($html, 'comment');
  2371. }
  2372. return $html;
  2373. }
  2374. return '';
  2375. }
  2376. sub GetFormStart {
  2377. my ($ignore, $method, $class) = @_;
  2378. $method ||= 'post';
  2379. $class ||= 'form';
  2380. return $q->start_multipart_form(-method=>$method, -action=>$FullUrl,
  2381. -accept_charset=>'utf-8', -class=>$class);
  2382. }
  2383. sub GetSearchForm {
  2384. my $html = GetFormStart(undef, 'get', 'search') . $q->start_p;
  2385. $html .= $q->label({-for=>'search'}, T('Search:')) . ' '
  2386. . $q->textfield(-name=>'search', -id=>'search', -size=>15, -accesskey=>T('f')) . ' ';
  2387. if (GetParam('search') ne '' and UserIsAdmin()) { # see DoBrowseRequest
  2388. $html .= $q->label({-for=>'replace'}, T('Replace:')) . ' '
  2389. . $q->textfield(-name=>'replace', -id=>'replace', -size=>20) . ' '
  2390. . $q->label({-for=>'delete', -title=>'If you want to replace matches with the empty string'}, T('Delete')) . ' '
  2391. . $q->input({-type=>'checkbox', -name=>'delete'})
  2392. . $q->submit('preview', T('Preview'));
  2393. }
  2394. if (GetParam('matchingpages', $MatchingPages)) {
  2395. $html .= $q->label({-for=>'matchingpage'}, T('Filter:')) . ' '
  2396. . $q->textfield(-name=>'match', -id=>'matchingpage', -size=>15) . ' ';
  2397. }
  2398. if (%Languages) {
  2399. $html .= $q->label({-for=>'searchlang'}, T('Language:')) . ' '
  2400. . $q->textfield(-name=>'lang', -id=>'searchlang', -size=>5, -default=>GetParam('lang', '')) . ' ';
  2401. }
  2402. $html .= $q->submit('dosearch', T('Go!')) . $q->end_p . $q->end_form;
  2403. return $html;
  2404. }
  2405. sub GetGotoBar { # ignore $id parameter
  2406. return $q->span({-class=>'gotobar bar'}, (map { GetPageLink($_) }
  2407. @UserGotoBarPages), $UserGotoBar);
  2408. }
  2409. sub PrintHtmlDiff {
  2410. my ($type, $old, $page, $current) = @_;
  2411. $page //= \%Page;
  2412. $current //= $page->{revision};
  2413. $type = 2 if $old or $page->{revision} != $current; # explicit revisions means minor diffs!
  2414. my $summary = $page->{$type == 1 ? 'lastmajorsummary' : 'summary'};
  2415. my $intro = T('Last edit');
  2416. my $diff;
  2417. # use the cached diff if possible
  2418. if (not $old or $old == $page->{$type == 1 ? 'lastmajor' : 'revision'} - 1) {
  2419. $diff = GetCacheDiff($type == 1 ? 'major' : 'minor', $page);
  2420. $old = $page->{$type == 1 ? 'lastmajor' : 'revision'} - 1 if not $old;
  2421. }
  2422. # if there was no cached diff: compute it, and new intro
  2423. if (not $diff and $old > 0) {
  2424. ($diff, my $keptPage) = GetKeptDiff($page->{text}, $old);
  2425. my $to = $page->{revision} != $current ? Ts('revision %s', $page->{revision}) : T('current revision');
  2426. $intro = Tss('Difference between revision %1 and %2', $old, $to);
  2427. }
  2428. # if this is the last major diff and there are minor diffs to look at, and we
  2429. # didn't request a particular old revision
  2430. if ($type == 1 and $page->{lastmajor} and $page->{lastmajor} != $current) {
  2431. $intro = Ts('Last major edit (%s)', ScriptLinkDiff(2, $OpenPageName, T('later minor edits'),
  2432. undef, $page->{lastmajor} || 1));
  2433. }
  2434. $diff =~ s!<p><strong>(.*?)</strong></p>!'<p><strong>' . T($1) . '</strong></p>'!eg;
  2435. $diff ||= T('No diff available.');
  2436. print $q->div({-class=>'diff'}, $q->p($q->b($intro)),
  2437. $summary ? $q->p({-class=>'summary'}, T('Summary:') . ' ' . QuoteHtml($summary)) : '',
  2438. $diff);
  2439. }
  2440. sub GetCacheDiff {
  2441. my ($type, $page) = @_;
  2442. my $diff = $page->{"diff-$type"};
  2443. $diff = $page->{"diff-minor"} if $diff eq '1'; # if major eq minor diff
  2444. return $diff;
  2445. }
  2446. sub GetKeptDiff {
  2447. my ($new, $revision) = @_;
  2448. $revision ||= 1;
  2449. my ($revisionPage, $rev) = GetTextRevision($revision, 1);
  2450. return '', $revisionPage unless $rev;
  2451. return T("The two revisions are the same."), $revisionPage if $revisionPage->{text} eq $new;
  2452. return GetDiff($revisionPage->{text}, $new, $rev), $revisionPage;
  2453. }
  2454. sub DoDiff { # Actualy call the diff program
  2455. CreateDir($TempDir);
  2456. my $oldName = "$TempDir/old";
  2457. my $newName = "$TempDir/new";
  2458. RequestLockDir('diff') or return '';
  2459. WriteStringToFile($oldName, $_[0]);
  2460. WriteStringToFile($newName, $_[1]);
  2461. my $command = encode_utf8("diff -- \Q$oldName\E \Q$newName\E");
  2462. my $diff_out = decode_utf8(qx($command));
  2463. ReleaseLockDir('diff');
  2464. $diff_out =~ s/\n\K\\ No newline.*\n//g; # Get rid of common complaint.
  2465. # No need to unlink temp files--next diff will just overwrite.
  2466. return $diff_out;
  2467. }
  2468. sub GetDiff {
  2469. my ($old, $new, $revision) = @_;
  2470. my $old_is_file = (TextIsFile($old))[0] || '';
  2471. my $old_is_image = ($old_is_file =~ /^image\//);
  2472. my $new_is_file = TextIsFile($new);
  2473. if ($old_is_file or $new_is_file) {
  2474. return $q->p($q->strong(T('Old revision:')))
  2475. . $q->div({-class=>'old'}, # don't pring new revision, because that's the one that gets shown!
  2476. $q->p($old_is_file ? GetDownloadLink($OpenPageName, $old_is_image, $revision) : $old))
  2477. }
  2478. $old =~ s/[\r\n]+/\n/g;
  2479. $new =~ s/[\r\n]+/\n/g;
  2480. return ImproveDiff(DoDiff($old, $new));
  2481. }
  2482. sub ImproveDiff { # NO NEED TO BE called within a diff lock
  2483. my $diff = QuoteHtml(shift);
  2484. $diff =~ tr/\r//d;
  2485. my @hunks = split (/^(\d+,?\d*[adc]\d+,?\d*\n)/m, $diff);
  2486. my $result = shift (@hunks); # intro
  2487. while ($#hunks > 0) { # at least one header and a real hunk
  2488. my $header = shift (@hunks);
  2489. $header =~ s|^(\d+.*c.*)|<p><strong>Changed:</strong></p>| # T('Changed:')
  2490. or $header =~ s|^(\d+.*d.*)|<p><strong>Deleted:</strong></p>| # T('Deleted:')
  2491. or $header =~ s|^(\d+.*a.*)|<p><strong>Added:</strong></p>|; # T('Added:')
  2492. $result .= $header;
  2493. my $chunk = shift (@hunks);
  2494. my ($old, $new) = split (/\n---\n/, $chunk, 2);
  2495. if ($old and $new) {
  2496. ($old, $new) = DiffMarkWords($old, $new);
  2497. $result .= "$old<p><strong>to</strong></p>\n$new"; # T('to')
  2498. } else {
  2499. if (substr($chunk, 0, 2) eq '&g') {
  2500. $result .= DiffAddPrefix(DiffStripPrefix($chunk), '&gt; ', 'new');
  2501. } else {
  2502. $result .= DiffAddPrefix(DiffStripPrefix($chunk), '&lt; ', 'old');
  2503. }
  2504. }
  2505. }
  2506. return $result;
  2507. }
  2508. sub DiffMarkWords {
  2509. my ($old, $new) = map { DiffStripPrefix($_) } @_;
  2510. my @diffs = grep(/^\d/, split(/\n/, DoDiff(join("\n", split(/\s+|\b/, $old)) . "\n",
  2511. join("\n", split(/\s+|\b/, $new)) . "\n")));
  2512. foreach my $diff (reverse @diffs) { # so that new html tags don't confuse word counts
  2513. my ($start1, $end1, $type, $start2, $end2) = $diff =~ /^(\d+),?(\d*)([adc])(\d+),?(\d*)$/gm;
  2514. if ($type eq 'd' or $type eq 'c') {
  2515. $end1 ||= $start1;
  2516. $old = DiffHtmlMarkWords($old, $start1, $end1);
  2517. }
  2518. if ($type eq 'a' or $type eq 'c') {
  2519. $end2 ||= $start2;
  2520. $new = DiffHtmlMarkWords($new, $start2, $end2);
  2521. }
  2522. }
  2523. return (DiffAddPrefix($old, '&lt; ', 'old'),
  2524. DiffAddPrefix($new, '&gt; ', 'new'));
  2525. }
  2526. sub DiffHtmlMarkWords {
  2527. my ($text, $start, $end) = @_;
  2528. my @fragments = split(/(\s+|\b)/, $text);
  2529. splice(@fragments, 2 * ($start - 1), 0, '<strong class="changes">');
  2530. splice(@fragments, 2 * $end, 0, '</strong>');
  2531. my $result = join('', @fragments);
  2532. $result =~ s!&<(/?)strong([^>]*)>(amp|[gl]t);!<$1strong$2>&$3;!g;
  2533. $result =~ s!&(amp|[gl]t)<(/?)strong([^>]*)>;!&$1;<$2strong$3>!g;
  2534. return $result;
  2535. }
  2536. sub DiffStripPrefix {
  2537. my $str = shift;
  2538. $str =~ s/^&[lg]t; //gm;
  2539. return $str;
  2540. }
  2541. sub DiffAddPrefix {
  2542. my ($str, $prefix, $class) = @_;
  2543. my @lines = split(/\n/, $str);
  2544. for my $line (@lines) {
  2545. $line = $prefix . $line;
  2546. }
  2547. return $q->div({-class=>$class}, $q->p(join($q->br(), @lines)));
  2548. }
  2549. sub ParseData {
  2550. my $data = shift;
  2551. my %result;
  2552. while ($data =~ /(\S+?): (.*?)(?=\n[^ \t]|\Z)/gs) {
  2553. my ($key, $value) = ($1, $2);
  2554. $value =~ s/\n\t/\n/g;
  2555. $result{$key} = $value;
  2556. }
  2557. # return unless %result; # undef instead of empty hash # TODO should we do that?
  2558. return wantarray ? %result : \%result; # return list sometimes for compatibility
  2559. }
  2560. sub OpenPage { # Sets global variables
  2561. my $id = shift;
  2562. return if $OpenPageName eq $id;
  2563. if ($IndexHash{$id}) {
  2564. %Page = %{ParseData(ReadFileOrDie(GetPageFile($id)))};
  2565. } else {
  2566. %Page = ();
  2567. $Page{ts} = $Now;
  2568. $Page{revision} = 0;
  2569. }
  2570. $OpenPageName = $id;
  2571. }
  2572. sub GetTextAtTime { # call with opened page, return $minor if all pages between now and $ts are minor!
  2573. my $ts = shift;
  2574. my $minor = $Page{minor};
  2575. return ($Page{text}, $minor, 0) if $Page{ts} <= $ts; # current page is old enough
  2576. return ($DeletedPage, $minor, 0) if $Page{revision} == 1 and $Page{ts} > $ts; # created after $ts
  2577. my $keep = {}; # info may be needed after the loop
  2578. foreach my $revision (GetKeepRevisions($OpenPageName)) {
  2579. $keep = GetKeptRevision($revision);
  2580. # $minor = 0 unless defined $keep; # TODO?
  2581. $minor = 0 if not $keep->{minor} and $keep->{ts} >= $ts; # ignore keep{minor} if keep{ts} is too old
  2582. return ($keep->{text}, $minor, 0) if $keep->{ts} <= $ts;
  2583. }
  2584. return ($DeletedPage, $minor, 0) if $keep->{revision} == 1; # then the page was created after $ts!
  2585. return ($keep->{text}, $minor, $keep->{ts}); # the oldest revision available is not old enough
  2586. }
  2587. sub GetTextRevision {
  2588. my ($revision, $quiet) = @_;
  2589. $revision =~ s/\D//g; # Remove non-numeric chars
  2590. return wantarray ? (\%Page, $revision) : \%Page unless $revision and $revision ne $Page{revision};
  2591. my $keep = GetKeptRevision($revision);
  2592. if (not defined $keep) {
  2593. $Message .= $q->p(Ts('Revision %s not available', $revision)
  2594. . ' (' . T('showing current revision instead') . ')') unless $quiet;
  2595. return wantarray ? (\%Page, '') : \%Page;
  2596. }
  2597. $Message .= $q->p(Ts('Showing revision %s', $revision)) unless $quiet;
  2598. return wantarray ? ($keep, $revision) : $keep;
  2599. }
  2600. sub GetPageContent {
  2601. my $id = shift;
  2602. return ParseData(ReadFileOrDie(GetPageFile($id)))->{text} if $IndexHash{$id};
  2603. return '';
  2604. }
  2605. sub GetKeptRevision { # Call after OpenPage
  2606. my ($status, $data) = ReadFile(GetKeepFile($OpenPageName, (shift)));
  2607. return unless $status;
  2608. return ParseData($data);
  2609. }
  2610. sub GetPageFile {
  2611. my ($id) = @_;
  2612. return "$PageDir/$id.pg";
  2613. }
  2614. sub GetKeepFile {
  2615. my ($id, $revision) = @_; die "No revision for $id" unless $revision; #FIXME
  2616. return GetKeepDir($id) . "/$revision.kp";
  2617. }
  2618. sub GetKeepDir {
  2619. my $id = shift; die 'No id' unless $id; #FIXME
  2620. return "$KeepDir/$id";
  2621. }
  2622. sub GetKeepFiles {
  2623. return Glob(GetKeepDir(shift) . '/*.kp'); # files such as 1.kp, 2.kp, etc.
  2624. }
  2625. sub GetKeepRevisions {
  2626. my @result = sort {$b <=> $a} map { m/([0-9]+)\.kp$/; $1; } GetKeepFiles(shift);
  2627. return @result;
  2628. }
  2629. # Always call SavePage within a lock.
  2630. sub SavePage { # updating the cache will not change timestamp and revision!
  2631. ReportError(T('Cannot save a nameless page.'), '400 BAD REQUEST', 1) unless $OpenPageName;
  2632. ReportError(T('Cannot save a page without revision.'), '400 BAD REQUEST', 1) unless $Page{revision};
  2633. CreateDir($PageDir);
  2634. WriteStringToFile(GetPageFile($OpenPageName), EncodePage(%Page));
  2635. }
  2636. sub SaveKeepFile {
  2637. return if ($Page{revision} < 1); # Don't keep 'empty' revision
  2638. delete $Page{blocks}; # delete some info from the page
  2639. delete $Page{flags};
  2640. delete $Page{'diff-major'};
  2641. delete $Page{'diff-minor'};
  2642. $Page{'keep-ts'} = $Now; # expire only $KeepDays from $Now!
  2643. CreateDir($KeepDir);
  2644. CreateDir(GetKeepDir($OpenPageName));
  2645. WriteStringToFile(GetKeepFile($OpenPageName, $Page{revision}), EncodePage(%Page));
  2646. }
  2647. sub EncodePage {
  2648. my @data = @_;
  2649. my $result = '';
  2650. $result .= (shift @data) . ': ' . EscapeNewlines(shift @data) . "\n" while (@data);
  2651. return $result;
  2652. }
  2653. sub EscapeNewlines {
  2654. $_[0] =~ s/\n/\n\t/g; # modify original instead of copying
  2655. return $_[0];
  2656. }
  2657. sub ExpireAllKeepFiles {
  2658. foreach my $name (AllPagesList()) {
  2659. print $q->br(), GetPageLink($name);
  2660. OpenPage($name);
  2661. my $delete = PageDeletable();
  2662. if ($delete) {
  2663. my $status = DeletePage($OpenPageName);
  2664. print ' ', ($status ? T('not deleted:') . ' ' . $status : T('deleted'));
  2665. } else {
  2666. ExpireKeepFiles();
  2667. }
  2668. }
  2669. }
  2670. sub ExpireKeepFiles { # call with opened page
  2671. return unless $KeepDays;
  2672. my $expirets = $Now - ($KeepDays * 86400); # 24*60*60
  2673. foreach my $revision (GetKeepRevisions($OpenPageName)) {
  2674. my $keep = GetKeptRevision($revision);
  2675. next if $keep->{'keep-ts'} >= $expirets;
  2676. next if $KeepMajor and $keep->{revision} == $Page{lastmajor};
  2677. Unlink(GetKeepFile($OpenPageName, $revision));
  2678. }
  2679. }
  2680. sub ReadFile {
  2681. if (open(my $IN, '<:encoding(UTF-8)', encode_utf8(shift))) {
  2682. local $/ = undef; # Read complete files
  2683. my $data=<$IN>;
  2684. close $IN;
  2685. return (1, $data);
  2686. }
  2687. return (0, '');
  2688. }
  2689. sub ReadFileOrDie {
  2690. my ($file) = @_;
  2691. my ($status, $data);
  2692. ($status, $data) = ReadFile($file);
  2693. if (not $status) {
  2694. ReportError(Ts('Cannot open %s', $file) . ": $!", '500 INTERNAL SERVER ERROR');
  2695. }
  2696. return $data;
  2697. }
  2698. sub WriteStringToFile {
  2699. my ($file, $string) = @_;
  2700. open(my $OUT, '>:encoding(UTF-8)', encode_utf8($file))
  2701. or ReportError(Ts('Cannot write %s', $file) . ": $!", '500 INTERNAL SERVER ERROR');
  2702. print $OUT $string;
  2703. close($OUT);
  2704. }
  2705. sub AppendStringToFile {
  2706. my ($file, $string) = @_;
  2707. open(my $OUT, '>>:encoding(UTF-8)', encode_utf8($file))
  2708. or ReportError(Ts('Cannot write %s', $file) . ": $!", '500 INTERNAL SERVER ERROR');
  2709. print $OUT $string;
  2710. close($OUT);
  2711. }
  2712. sub IsFile { return -f encode_utf8(shift); }
  2713. sub IsDir { return -d encode_utf8(shift); }
  2714. sub ZeroSize { return -z encode_utf8(shift); }
  2715. sub Unlink { return unlink(map { encode_utf8($_) } @_); }
  2716. sub Modified { return (stat(encode_utf8(shift)))[9]; }
  2717. sub Glob { return map { decode_utf8($_) } bsd_glob(encode_utf8(shift)); }
  2718. sub ChangeMod { return chmod(shift, map { encode_utf8($_) } @_); }
  2719. sub Rename { return rename(encode_utf8($_[0]), encode_utf8($_[1])); }
  2720. sub RemoveDir { return rmdir(encode_utf8(shift)); }
  2721. sub ChangeDir { return chdir(encode_utf8(shift)); }
  2722. sub CreateDir {
  2723. my ($newdir) = @_;
  2724. return if IsDir($newdir);
  2725. mkdir(encode_utf8($newdir), 0775)
  2726. or ReportError(Ts('Cannot create %s', $newdir) . ": $!", '500 INTERNAL SERVER ERROR');
  2727. }
  2728. sub GetLockedPageFile {
  2729. my $id = shift;
  2730. return "$PageDir/$id.lck";
  2731. }
  2732. sub RequestLockDir {
  2733. my ($name, $tries, $wait, $error, $retried) = @_;
  2734. $tries ||= 4;
  2735. $wait ||= 2;
  2736. CreateDir($TempDir);
  2737. my $lock = $LockDir . $name;
  2738. my $n = 0;
  2739. # Cannot use CreateDir because we don't want to skip mkdir if the directory
  2740. # already exists.
  2741. while (mkdir(encode_utf8($lock), 0555) == 0) {
  2742. if ($n++ >= $tries) {
  2743. my $ts = Modified($lock);
  2744. if ($Now - $ts > $LockExpiration and $LockExpires{$name} and not $retried) { # XXX should we remove this now?
  2745. ReleaseLockDir($name); # try to expire lock (no checking)
  2746. return 1 if RequestLockDir($name, undef, undef, undef, 1);
  2747. }
  2748. return 0 unless $error;
  2749. ReportError(Ts('Could not get %s lock', $name) . ": $!. ",
  2750. '503 SERVICE UNAVAILABLE', undef,
  2751. Ts('The lock was created %s.', CalcTimeSince($Now - $ts))
  2752. . ($retried && ' ' . T('Maybe the user running this script is no longer allowed to remove the lock directory?'))
  2753. . ' ' . T('Sometimes locks are left behind if a job crashes.') . ' '
  2754. . ($Now - $ts < 600 ? T('After ten minutes, you could try to unlock the wiki.')
  2755. : ScriptLink('action=unlock', T('Unlock Wiki'), 'unlock')));
  2756. }
  2757. sleep($wait);
  2758. }
  2759. $Locks{$name} = 1;
  2760. return 1;
  2761. }
  2762. sub HandleSignals {
  2763. my ($signal) = @_; # TODO should we pass it to CleanLock?
  2764. CleanLock($_) foreach keys %Locks;
  2765. exit; # let's count it as graceful exit
  2766. }
  2767. sub CleanLock {
  2768. my ($name) = @_;
  2769. $LockCleaners{$name}->() if exists $LockCleaners{$name};
  2770. ReleaseLockDir($name); # TODO should we log this?
  2771. }
  2772. sub ReleaseLockDir {
  2773. my $name = shift; # We don't check whether we succeeded.
  2774. RemoveDir($LockDir . $name); # Before fixing, make sure we only call this
  2775. delete $Locks{$name}; # when we know the lock exists.
  2776. }
  2777. sub RequestLockOrError {
  2778. return RequestLockDir('main', 10, 3, 1); # 10 tries, 3 second wait, die on error
  2779. }
  2780. sub ReleaseLock {
  2781. ReleaseLockDir('main');
  2782. }
  2783. sub ForceReleaseLock {
  2784. my $pattern = shift;
  2785. my $forced;
  2786. foreach my $name (Glob($pattern)) {
  2787. # First try to obtain lock (in case of normal edit lock)
  2788. $forced = 1 unless RequestLockDir($name, 5, 3, 0);
  2789. ReleaseLockDir($name); # Release the lock, even if we didn't get it. This should not happen.
  2790. }
  2791. return $forced;
  2792. }
  2793. sub DoUnlock {
  2794. my $message = '';
  2795. print GetHeader('', T('Unlock Wiki'), undef, 'nocache');
  2796. print $q->p(T('This operation may take several seconds...'));
  2797. for my $lock (@KnownLocks) {
  2798. if (ForceReleaseLock($lock)) {
  2799. $message .= $q->p(Ts('Forced unlock of %s lock.', $lock));
  2800. }
  2801. }
  2802. print $message || $q->p(T('No unlock required.'));
  2803. PrintFooter();
  2804. }
  2805. sub CalcDay {
  2806. my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
  2807. return sprintf('%4d-%02d-%02d', $year + 1900, $mon + 1, $mday);
  2808. }
  2809. sub CalcTime {
  2810. my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
  2811. return sprintf('%02d:%02d UTC', $hour, $min);
  2812. }
  2813. sub CalcTimeSince {
  2814. my $total = shift;
  2815. return Ts('%s hours ago', int($total/3600)) if ($total >= 7200);
  2816. return T('1 hour ago') if ($total >= 3600);
  2817. return Ts('%s minutes ago', int($total/60)) if ($total >= 120);
  2818. return T('1 minute ago') if ($total >= 60);
  2819. return Ts('%s seconds ago', int($total)) if ($total >= 2);
  2820. return T('1 second ago') if ($total == 1);
  2821. return T('just now');
  2822. }
  2823. sub TimeToText {
  2824. my $t = shift;
  2825. return CalcDay($t) . ' ' . CalcTime($t);
  2826. }
  2827. sub TimeToW3 { # Complete date plus hours and minutes: YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
  2828. my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift); # use special UTC designator ("Z")
  2829. return sprintf('%4d-%02d-%02dT%02d:%02dZ', $year + 1900, $mon + 1, $mday, $hour, $min);
  2830. }
  2831. sub TimeToRFC822 {
  2832. my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime(shift); # Sat, 07 Sep 2002 00:00:01 GMT
  2833. return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", qw(Sun Mon Tue Wed Thu Fri Sat)[$wday], $mday,
  2834. qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$mon], $year + 1900, $hour, $min, $sec);
  2835. }
  2836. sub GetHiddenValue {
  2837. my ($name, $value) = @_;
  2838. return $q->input({-type=>"hidden", -name=>$name, -value=>$value});
  2839. }
  2840. sub FreeToNormal { # trim all spaces and convert them to underlines
  2841. my $id = shift;
  2842. return '' unless $id;
  2843. $id =~ s/ /_/g;
  2844. $id =~ s/__+/_/g;
  2845. $id =~ s/^_//;
  2846. $id =~ s/_$//;
  2847. return UnquoteHtml($id);
  2848. }
  2849. sub ItemName {
  2850. my $id = shift; # id
  2851. return NormalToFree($id) unless GetParam('short', 1) and $RssStrip;
  2852. my $comment = $id =~ s/^($CommentsPrefix)//; # strip first so that ^ works
  2853. $id =~ s/^$RssStrip//;
  2854. $id = $CommentsPrefix . $id if $comment;
  2855. return NormalToFree($id);
  2856. }
  2857. sub NormalToFree { # returns HTML quoted title with spaces
  2858. my $title = shift;
  2859. $title =~ s/_/ /g;
  2860. return QuoteHtml($title);
  2861. }
  2862. sub UnWiki {
  2863. my $str = shift;
  2864. return $str unless $WikiLinks and $str =~ /^$LinkPattern$/;
  2865. $str =~ s/([[:lower:]])([[:upper:]])/$1 $2/g;
  2866. return $str;
  2867. }
  2868. sub DoEdit {
  2869. my ($id, $newText, $preview) = @_;
  2870. UserCanEditOrDie($id);
  2871. my $upload = GetParam('upload', undef);
  2872. if ($upload and not $UploadAllowed and not UserIsAdmin()) {
  2873. ReportError(T('Only administrators can upload files.'), '403 FORBIDDEN');
  2874. }
  2875. OpenPage($id);
  2876. my ($revisionPage, $revision) = GetTextRevision(GetParam('revision', ''), 1); # maybe revision reset!
  2877. my $oldText = $preview ? $newText : $revisionPage->{text};
  2878. my $isFile = TextIsFile($oldText);
  2879. $upload //= $isFile;
  2880. if ($upload and not $UploadAllowed and not UserIsAdmin()) {
  2881. ReportError(T('Only administrators can upload files.'), '403 FORBIDDEN');
  2882. }
  2883. if ($upload) { # shortcut lots of code
  2884. $revision = '';
  2885. $preview = 0;
  2886. } elsif ($isFile) {
  2887. $oldText = '';
  2888. }
  2889. my $header;
  2890. if ($revision and not $upload) {
  2891. $header = Ts('Editing revision %s of', $revision) . ' ' . NormalToFree($id);
  2892. } else {
  2893. $header = Ts('Editing %s', NormalToFree($id));
  2894. }
  2895. print GetHeader('', $header), $q->start_div({-class=>'content edit'});
  2896. if ($preview and not $upload) {
  2897. print $q->start_div({-class=>'preview'});
  2898. print $q->h2(T('Preview:'));
  2899. PrintWikiToHTML($oldText); # no caching, current revision, unlocked
  2900. print $q->hr(), $q->h2(T('Preview only, not yet saved')), $q->end_div();
  2901. }
  2902. if ($revision) {
  2903. print $q->strong(Ts('Editing old revision %s.', $revision) . ' '
  2904. . T('Saving this page will replace the latest revision with this text.'))
  2905. }
  2906. print GetEditForm($id, $upload, $oldText, $revision), $q->end_div();
  2907. PrintFooter($id, 'edit');
  2908. }
  2909. sub GetEditForm {
  2910. my ($page_name, $upload, $oldText, $revision) = @_;
  2911. my $html = GetFormStart(undef, undef, $upload ? 'edit upload' : 'edit text') # protected by questionasker
  2912. .$q->p(GetHiddenValue("title", $page_name),
  2913. ($revision ? GetHiddenValue('revision', $revision) : ''),
  2914. GetHiddenValue('oldtime', GetParam('oldtime', $Page{ts})), # prefer parameter over actual timestamp
  2915. ($upload ? GetUpload() : GetTextArea('text', $oldText)));
  2916. my $summary = UnquoteHtml(GetParam('summary', ''))
  2917. || ($Now - $Page{ts} < ($SummaryHours * 3600) ? $Page{summary} : '');
  2918. $html .= $q->p(T('Summary:').$q->br().GetTextArea('summary', $summary, 2))
  2919. .$q->p($q->checkbox(-name=>'recent_edit', -checked=>(GetParam('recent_edit', '') eq 'on'),
  2920. -label=>T('This change is a minor edit.')));
  2921. $html .= T($EditNote) if $EditNote; # Allow translation
  2922. my $username = GetParam('username', '');
  2923. $html .= $q->p($q->label({-for=>'username'}, T('Username:')).' '
  2924. .$q->textfield(-name=>'username', -id=>'username', -default=>$username,
  2925. -override=>1, -size=>20, -maxlength=>50))
  2926. .$q->p($q->submit(-name=>'Save', -accesskey=>T('s'), -value=>T('Save')),
  2927. ($upload ? '' : ' ' . $q->submit(-name=>'Preview', -accesskey=>T('p'), -value=>T('Preview'))).
  2928. ' '.$q->submit(-name=>'Cancel', -value=>T('Cancel')));
  2929. if ($upload) {
  2930. $html .= $q->p(ScriptLink('action=edit;upload=0;id=' . UrlEncode($page_name), T('Replace this file with text'), 'upload'));
  2931. } elsif ($UploadAllowed or UserIsAdmin()) {
  2932. $html .= $q->p(ScriptLink('action=edit;upload=1;id=' . UrlEncode($page_name), T('Replace this text with a file'), 'upload'));
  2933. }
  2934. $html .= $q->end_form();
  2935. foreach my $sub (@MyFormChanges) {
  2936. $html = $sub->($html, 'edit', $upload);
  2937. }
  2938. return $html;
  2939. }
  2940. sub GetTextArea {
  2941. my ($name, $text, $rows) = @_;
  2942. return $q->textarea(-id=>$name, -name=>$name, -default=>$text, -rows=>$rows || 25, -columns=>78, -override=>1);
  2943. }
  2944. sub GetUpload {
  2945. return T('File to upload:') . ' ' . $q->filefield(-name=>'file', -size=>50, -maxlength=>100);
  2946. }
  2947. sub DoDownload {
  2948. my $id = shift;
  2949. OpenPage($id) if ValidIdOrDie($id);
  2950. print $q->header(-status=>'304 NOT MODIFIED') and return if FileFresh(); # FileFresh needs an OpenPage!
  2951. my ($revisionPage, $revision) = GetTextRevision(GetParam('revision', '')); # maybe revision reset!
  2952. my $text = $revisionPage->{text};
  2953. if (my ($type, $encoding) = TextIsFile($text)) {
  2954. my ($data) = $text =~ /^[^\n]*\n(.*)/s;
  2955. my %allowed = map {$_ => 1} @UploadTypes;
  2956. if (@UploadTypes and not $allowed{$type}) {
  2957. ReportError(Ts('Files of type %s are not allowed.', $type), '415 UNSUPPORTED MEDIA TYPE');
  2958. }
  2959. print GetHttpHeader($type, $Page{ts}, undef, $encoding);
  2960. require MIME::Base64;
  2961. binmode(STDOUT, ":pop:raw"); # need to pop utf8 for Windows users!?
  2962. print MIME::Base64::decode($data);
  2963. } else {
  2964. print GetHttpHeader('text/plain', $Page{ts});
  2965. print $text;
  2966. }
  2967. }
  2968. sub DoPassword {
  2969. my $id = shift;
  2970. print GetHeader('', T('Password')), $q->start_div({-class=>'content password'});
  2971. print $q->p(T('Your password is saved in a cookie, if you have cookies enabled. Cookies may get lost if you connect from another machine, from another account, or using another software.'));
  2972. if (not $AdminPass and not $EditPass) {
  2973. print $q->p(T('This site does not use admin or editor passwords.'));
  2974. } else {
  2975. if (UserIsAdmin()) {
  2976. print $q->p(T('You are currently an administrator on this site.'));
  2977. } elsif (UserIsEditor()) {
  2978. print $q->p(T('You are currently an editor on this site.'));
  2979. } else {
  2980. print $q->p(T('You are a normal user on this site.'));
  2981. if (not GetParam('pwd')) {
  2982. print $q->p(T('You do not have a password set.'));
  2983. } else {
  2984. print $q->p(T('Your password does not match any of the administrator or editor passwords.'));
  2985. }
  2986. }
  2987. print GetFormStart(undef, undef, 'password'),
  2988. $q->p(GetHiddenValue('action', 'password'), T('Password:'), ' ',
  2989. $q->password_field(-name=>'pwd', -size=>20, -maxlength=>64),
  2990. $q->hidden(-name=>'id', -value=>$id),
  2991. $q->submit(-name=>'Save', -accesskey=>T('s'), -value=>T('Save'))),
  2992. $q->end_form;
  2993. }
  2994. if ($id) {
  2995. print $q->p(ScriptLink('action=browse;id=' . UrlEncode($id) . ';time=' . time,
  2996. Ts('Return to %s', NormalToFree($id))));
  2997. }
  2998. print $q->end_div();
  2999. PrintFooter();
  3000. }
  3001. sub UserIsEditorOrError {
  3002. UserIsEditor()
  3003. or ReportError(T('This operation is restricted to site editors only...'), '403 FORBIDDEN');
  3004. return 1;
  3005. }
  3006. sub UserIsAdminOrError {
  3007. UserIsAdmin()
  3008. or ReportError(T('This operation is restricted to administrators only...'), '403 FORBIDDEN');
  3009. return 1;
  3010. }
  3011. sub UserCanEditOrDie {
  3012. my $id = shift;
  3013. ValidIdOrDie($id);
  3014. if (not UserCanEdit($id, 1)) {
  3015. my $rule = UserIsBanned();
  3016. if ($rule) {
  3017. ReportError(T('Edit Denied'), '403 FORBIDDEN', undef,
  3018. $q->p(T('Editing not allowed: user, ip, or network is blocked.')),
  3019. $q->p(T('Contact the wiki administrator for more information.')),
  3020. $q->p(Ts('The rule %s matched for you.', $rule) . ' '
  3021. . Ts('See %s for more information.', GetPageLink($BannedHosts))));
  3022. } else {
  3023. ReportError(T('Edit Denied'), '403 FORBIDDEN', undef,
  3024. $q->p(Ts('Editing not allowed: %s is read-only.', NormalToFree($id))));
  3025. }
  3026. }
  3027. }
  3028. sub UserCanEdit {
  3029. my ($id, $editing, $comment) = @_;
  3030. return 0 if $id eq 'SampleUndefinedPage' or $id eq T('SampleUndefinedPage')
  3031. or $id eq 'Sample_Undefined_Page' or $id eq T('Sample_Undefined_Page');
  3032. return 1 if UserIsAdmin();
  3033. return 0 if $id ne '' and IsFile(GetLockedPageFile($id));
  3034. return 0 if $LockOnCreation{$id} and not IsFile(GetPageFile($id)); # new page
  3035. return 1 if UserIsEditor();
  3036. return 0 if not $EditAllowed or IsFile($NoEditFile);
  3037. return 0 if $editing and UserIsBanned(); # this call is more expensive
  3038. return 0 if $EditAllowed >= 2 and (not $CommentsPattern or $id !~ /$CommentsPattern/);
  3039. return 1 if $EditAllowed >= 3 and GetParam('recent_edit', '') ne 'on' # disallow minor comments
  3040. and ($comment or (GetParam('aftertext', '') and not GetParam('text', '')));
  3041. return 0 if $EditAllowed >= 3;
  3042. return 1;
  3043. }
  3044. sub UserIsBanned {
  3045. return 0 if GetParam('action', '') eq 'password'; # login is always ok
  3046. my $host = $q->remote_addr();
  3047. foreach (split(/\n/, GetPageContent($BannedHosts))) {
  3048. if (/^\s*([^#]\S+)/) { # all lines except empty lines and comments, trim whitespace
  3049. my $regexp = $1;
  3050. return $regexp if ($host =~ /$regexp/i);
  3051. }
  3052. }
  3053. return 0;
  3054. }
  3055. sub UserIsAdmin {
  3056. return UserHasPassword(GetParam('pwd', ''), $AdminPass);
  3057. }
  3058. sub UserIsEditor {
  3059. return 1 if UserIsAdmin(); # Admin includes editor
  3060. return UserHasPassword(GetParam('pwd', ''), $EditPass);
  3061. }
  3062. sub UserHasPassword {
  3063. my ($pwd, $pass) = @_;
  3064. return 0 unless $pass;
  3065. if ($PassHashFunction ne '') {
  3066. no strict 'refs'; # TODO this is kept for compatibility. Feel free to remove it later (comment written on 2015-07-14)
  3067. $pwd = $PassHashFunction->($pwd . $PassSalt);
  3068. }
  3069. foreach (split(/\s+/, $pass)) {
  3070. return 1 if $pwd eq $_;
  3071. }
  3072. return 0;
  3073. }
  3074. sub BannedContent {
  3075. my $str = shift;
  3076. my @urls = $str =~ /$FullUrlPattern/g;
  3077. foreach (split(/\n/, GetPageContent($BannedContent))) {
  3078. next unless m/^\s*([^#]+?)\s*(#\s*(\d\d\d\d-\d\d-\d\d\s*)?(.*))?$/;
  3079. my ($regexp, $comment, $re) = ($1, $4, undef);
  3080. foreach my $url (@urls) {
  3081. eval { $re = qr/$regexp/i; };
  3082. if (defined($re) and $url =~ $re) {
  3083. return Tss('Rule "%1" matched "%2" on this page.', $regexp, $url) . ' '
  3084. . ($comment ? Ts('Reason: %s.', $comment) : T('Reason unknown.')) . ' '
  3085. . Ts('See %s for more information.', GetPageLink($BannedContent));
  3086. }
  3087. }
  3088. }
  3089. return 0;
  3090. }
  3091. sub SortIndex {
  3092. my ($A, $B) = ($a, $b);
  3093. my $aIsComment = $A =~ s/^$CommentsPrefix//;
  3094. $B =~ s/^$CommentsPrefix//;
  3095. return $aIsComment ? 1 : -1 if $A eq $B;
  3096. $A cmp $B;
  3097. }
  3098. sub DoIndex {
  3099. my $raw = GetParam('raw', 0);
  3100. my $match = GetParam('match', '');
  3101. my @pages = ();
  3102. my @menu = ($q->label({-for=>'indexmatch'}, T('Filter:')) . ' '
  3103. . $q->textfield(-name=>'match', -id=>'indexmatch', -size=>20));
  3104. foreach my $data (@IndexOptions) {
  3105. my ($option, $text, $default, $sub) = @$data;
  3106. my $value = GetParam($option, $default); # HTML checkbox warning!
  3107. $value = 0 if GetParam('manual', 0) and $value ne 'on';
  3108. push(@pages, $sub->()) if $value;
  3109. push(@menu, $q->checkbox(-name=>$option, -checked=>$value, -label=>$text));
  3110. }
  3111. @pages = grep /$match/i, @pages if $match;
  3112. @pages = sort SortIndex @pages;
  3113. if ($raw) {
  3114. print GetHttpHeader('text/plain'); # and ignore @menu
  3115. } else {
  3116. print GetHeader('', T('Index of all pages'));
  3117. push(@menu, GetHiddenValue('manual', 1) . $q->submit(-value=>T('Go!')));
  3118. push(@menu, $q->b(Ts('(for %s)', GetParam('lang', '')))) if GetParam('lang', '');
  3119. print $q->start_div({-class=>'content index'}),
  3120. GetFormStart(undef, 'get', 'index'), GetHiddenValue('action', 'index'),
  3121. $q->p(join($q->br(), @menu)), $q->end_form(),
  3122. $q->h2(Ts('%s pages found.', ($#pages + 1))), $q->start_p();
  3123. }
  3124. PrintPage($_) foreach (@pages);
  3125. print $q->end_p(), $q->end_div() unless $raw;
  3126. PrintFooter() unless $raw;
  3127. }
  3128. sub PrintPage {
  3129. my $id = shift;
  3130. my $lang = GetParam('lang', 0);
  3131. if ($lang) {
  3132. OpenPage($id);
  3133. my @languages = split(/,/, $Page{languages});
  3134. next if (@languages and not grep(/$lang/, @languages));
  3135. }
  3136. if (GetParam('raw', 0)) {
  3137. if (GetParam('search', '') and GetParam('context', 1)) {
  3138. print "title: $id\n\n"; # for near links without full search
  3139. } else {
  3140. print $id, "\n";
  3141. }
  3142. } else {
  3143. print GetPageOrEditLink($id, NormalToFree($id)), $q->br();
  3144. }
  3145. }
  3146. sub AllPagesList {
  3147. my $refresh = GetParam('refresh', 0);
  3148. return @IndexList if @IndexList and not $refresh;
  3149. SetParam('refresh', 0) if $refresh;
  3150. return @IndexList if not $refresh and IsFile($IndexFile) and ReadIndex();
  3151. # If open fails just refresh the index
  3152. RefreshIndex();
  3153. return @IndexList;
  3154. }
  3155. sub ReadIndex {
  3156. my ($status, $rawIndex) = ReadFile($IndexFile); # not fatal
  3157. if ($status) {
  3158. @IndexList = split(/ /, $rawIndex);
  3159. %IndexHash = map {$_ => 1} @IndexList;
  3160. return @IndexList;
  3161. }
  3162. return;
  3163. }
  3164. sub WriteIndex {
  3165. WriteStringToFile($IndexFile, join(' ', @IndexList));
  3166. }
  3167. sub RefreshIndex {
  3168. @IndexList = ();
  3169. %IndexHash = ();
  3170. # If file exists and cannot be changed, error!
  3171. my $locked = RequestLockDir('index', undef, undef, IsFile($IndexFile));
  3172. foreach (Glob("$PageDir/*.pg"), Glob("$PageDir/.*.pg")) {
  3173. next unless m|/.*/(.+)\.pg$|;
  3174. my $id = $1;
  3175. push(@IndexList, $id);
  3176. $IndexHash{$id} = 1;
  3177. }
  3178. WriteIndex() if $locked;
  3179. ReleaseLockDir('index') if $locked;
  3180. }
  3181. sub AddToIndex {
  3182. my ($id) = @_;
  3183. $IndexHash{$id} = 1;
  3184. @IndexList = sort(keys %IndexHash);
  3185. WriteIndex();
  3186. }
  3187. sub DoSearch {
  3188. my $string = shift || GetParam('search', '');
  3189. my $re = UnquoteHtml($string);
  3190. return DoIndex() if $string eq '';
  3191. eval { qr/$re/ } or $re = quotemeta($re);
  3192. my $replacement = GetParam('replace', undef);
  3193. my $raw = GetParam('raw', '');
  3194. my @results;
  3195. if ($replacement or GetParam('delete', 0)) {
  3196. return unless UserIsAdminOrError();
  3197. if (GetParam('preview', '')) { # Preview button was used
  3198. print GetHeader('', Ts('Preview: %s', $string . " &#x2192; " . $replacement));
  3199. print $q->start_div({-class=>'content replacement'});
  3200. print GetFormStart(undef, 'post', 'replace');
  3201. print GetHiddenValue('search', $string);
  3202. print GetHiddenValue('replace', $replacement);
  3203. print GetHiddenValue('delete', GetParam('delete', 0));
  3204. print $q->submit(-value=>T('Go!')) . $q->end_form();
  3205. @results = ReplaceAndDiff($re, UnquoteHtml($replacement));
  3206. } else {
  3207. print GetHeader('', Ts('Replaced: %s', $string . " &#x2192; " . $replacement));
  3208. print $q->start_div({-class=>'content replacement'});
  3209. @results = ReplaceAndSave($re, UnquoteHtml($replacement));
  3210. foreach (@results) {
  3211. PrintSearchResult($_, quotemeta($replacement || $re)); # the replacement is not a valid regex
  3212. }
  3213. }
  3214. } else {
  3215. if ($raw) {
  3216. print GetHttpHeader('text/plain');
  3217. print RcTextItem('title', Ts('Search for: %s', $string)), RcTextItem('date', TimeToText($Now)),
  3218. RcTextItem('link', $q->url(-path_info=>1, -query=>1)), "\n" if GetParam('context', 1);
  3219. } else {
  3220. print GetHeader('', Ts('Search for: %s', $string)), $q->start_div({-class=>'content search'});
  3221. print $q->p({-class=>'links'}, SearchMenu($string));
  3222. }
  3223. @results = SearchTitleAndBody($re, \&PrintSearchResult, SearchRegexp($re));
  3224. }
  3225. print SearchResultCount($#results + 1), $q->end_div() unless $raw;
  3226. PrintFooter() unless $raw;
  3227. }
  3228. sub SearchMenu {
  3229. return ScriptLink('action=rc;rcfilteronly=' . UrlEncode(shift),
  3230. T('View changes for these pages'));
  3231. }
  3232. sub SearchResultCount { $q->p({-class=>'result'}, Ts('%s pages found.', (shift))); }
  3233. sub PageIsUploadedFile {
  3234. my $id = shift;
  3235. return if $OpenPageName eq $id;
  3236. if ($IndexHash{$id}) {
  3237. my $file = GetPageFile($id);
  3238. open(my $FILE, '<:encoding(UTF-8)', encode_utf8($file))
  3239. or ReportError(Ts('Cannot open %s', GetPageFile($id))
  3240. . ": $!", '500 INTERNAL SERVER ERROR');
  3241. while (defined($_ = <$FILE>) and $_ !~ /^text: /) {
  3242. } # read lines until we get to the text key
  3243. close $FILE;
  3244. return unless length($_) > 6;
  3245. return TextIsFile(substr($_, 6)); # pass "#FILE image/png\n" to the test
  3246. }
  3247. }
  3248. sub SearchTitleAndBody {
  3249. my ($regex, $func, @args) = @_;
  3250. my @found;
  3251. my $lang = GetParam('lang', '');
  3252. foreach my $id (Filtered($regex, AllPagesList())) {
  3253. my $name = NormalToFree($id);
  3254. my ($text) = PageIsUploadedFile($id); # set to mime-type if this is an uploaded file
  3255. local ($OpenPageName, %Page); # this is local!
  3256. if (not $text) { # not uploaded file, therefore allow searching of page body
  3257. OpenPage($id); # this opens a page twice if it is not uploaded, but that's ok
  3258. if ($lang) {
  3259. my @languages = split(/,/, $Page{languages});
  3260. next if (@languages and not grep(/$lang/, @languages));
  3261. }
  3262. $text = $Page{text};
  3263. }
  3264. if (SearchString($regex, $name . "\n" . $text)) { # the real search code
  3265. push(@found, $id);
  3266. $func->($id, @args) if $func;
  3267. }
  3268. }
  3269. return @found;
  3270. }
  3271. sub Filtered { # this is overwriten in extensions such as tags.pl
  3272. my ($string, @pages) = @_;
  3273. my $match = GetParam('match', '');
  3274. @pages = grep /$match/i, @pages if $match;
  3275. return @pages;
  3276. }
  3277. sub SearchString {
  3278. my ($string, $data) = @_;
  3279. my @strings = grep /./, $string =~ /\"([^\"]+)\"|(\S+)/g; # skip null entries
  3280. foreach my $str (@strings) {
  3281. return 0 unless ($data =~ /$str/i);
  3282. }
  3283. return 1;
  3284. }
  3285. sub SearchRegexp {
  3286. my $regexp = join '|', map { index($_, '|') == -1 ? $_ : "($_)" }
  3287. grep /./, shift =~ /\"([^\"]+)\"|(\S+)/g; # this acts as OR
  3288. $regexp =~ s/\\s/[[:space:]]/g;
  3289. return $regexp;
  3290. }
  3291. sub PrintSearchResult {
  3292. my ($name, $regex) = @_;
  3293. return PrintPage($name) if not GetParam('context', 1);
  3294. OpenPage($name); # should be open already, just making sure!
  3295. my $text = $Page{text};
  3296. my ($type) = TextIsFile($text); # MIME type if an uploaded file
  3297. my %entry;
  3298. # get the page, filter it, remove all tags
  3299. $text =~ s/$FS//g; # Remove separators (paranoia)
  3300. $text =~ s/[\s]+/ /g; # Shrink whitespace
  3301. $text =~ s/([-_=\\*\\.]){10,}/$1$1$1$1$1/g ; # e.g. shrink "----------"
  3302. $entry{title} = $name;
  3303. $entry{description} = $type || SearchHighlight(QuoteHtml(SearchExtract($text, $regex)), QuoteHtml($regex));
  3304. $entry{size} = int((length($text) / 1024) + 1) . 'K';
  3305. $entry{'last-modified'} = TimeToText($Page{ts});
  3306. $entry{username} = $Page{username};
  3307. PrintSearchResultEntry(\%entry);
  3308. }
  3309. sub PrintSearchResultEntry {
  3310. my %entry = %{(shift)}; # get value from reference
  3311. if (GetParam('raw', 0)) {
  3312. $entry{generator} = GetAuthor($entry{username});
  3313. foreach my $key (qw(title description size last-modified generator username)) {
  3314. print RcTextItem($key, $entry{$key});
  3315. }
  3316. print RcTextItem('link', "$ScriptName?$entry{title}"), "\n";
  3317. } else {
  3318. my $author = GetAuthorLink($entry{username});
  3319. $author ||= $entry{generator};
  3320. my $id = $entry{title};
  3321. my ($class, $resolved, $title, $exists) = ResolveId($id);
  3322. my $text = NormalToFree($id);
  3323. my $result = $q->span({-class=>'result'}, ScriptLink(UrlEncode($resolved), $text, $class, undef, $title));
  3324. my $description = $entry{description};
  3325. $description = $q->br() . $description if $description;
  3326. my $info = $entry{size};
  3327. $info .= ' - ' if $info;
  3328. $info .= T('last updated') . ' ' . $entry{'last-modified'} if $entry{'last-modified'};
  3329. $info .= ' ' . T('by') . ' ' . $author if $author;
  3330. $info = $q->br() . $q->span({-class=>'info'}, $info) if $info;
  3331. print $q->p($result, $description, $info);
  3332. }
  3333. }
  3334. sub SearchHighlight {
  3335. my ($data, $regex) = @_;
  3336. $data =~ s/($regex)/<strong>$1<\/strong>/gi unless GetParam('raw');
  3337. return $data;
  3338. }
  3339. sub SearchExtract {
  3340. my ($data, $regex) = @_;
  3341. my ($snippetlen, $maxsnippets) = (100, 4); # these seem nice.
  3342. # show a snippet from the beginning of the document
  3343. my $j = index($data, ' ', $snippetlen); # end on word boundary
  3344. my $t = substr($data, 0, $j);
  3345. my $result = $t . ' . . .';
  3346. $data = substr($data, $j); # to avoid rematching
  3347. my $jsnippet = 0 ;
  3348. while ($jsnippet < $maxsnippets and $data =~ m/($regex)/i) {
  3349. $jsnippet++;
  3350. if (($j = index($data, $1)) > -1 ) {
  3351. # get substr containing (start of) match, ending on word boundaries
  3352. my $start = index($data, ' ', $j - $snippetlen / 2);
  3353. $start = 0 if $start == -1;
  3354. my $end = index($data, ' ', $j + $snippetlen / 2);
  3355. $end = length($data) if $end == -1;
  3356. $t = substr($data, $start, $end - $start);
  3357. $result .= $t . ' . . .';
  3358. # truncate text to avoid rematching the same string.
  3359. $data = substr($data, $end);
  3360. }
  3361. }
  3362. return $result;
  3363. }
  3364. sub ReplaceAndSave {
  3365. my ($from, $to) = @_;
  3366. RequestLockOrError(); # fatal
  3367. my @result = Replace($from, $to, 1, sub {
  3368. my ($id, $new) = @_;
  3369. Save($id, $new, $from . ' → ' . $to, 1);
  3370. });
  3371. ReleaseLock();
  3372. return @result;
  3373. }
  3374. sub ReplaceAndDiff {
  3375. my ($from, $to) = @_;
  3376. my @found = Replace($from, $to, 0, sub {
  3377. my ($id, $new) = @_;
  3378. print $q->h2(GetPageLink($id)), $q->div({-class=>'diff'}, ImproveDiff(DoDiff($Page{text}, $new)));
  3379. });
  3380. if (@found > GetParam('offset', 0) + GetParam('num', 10)) {
  3381. my $more = "search=" . UrlEncode($from) . ";preview=1"
  3382. . ";offset=" . (GetParam('num', 10) + GetParam('offset', 0))
  3383. . ";num=" . GetParam('num', 10);
  3384. $more .= ";replace=" . UrlEncode($to) if $to;
  3385. $more .= ";delete=1" unless $to;
  3386. print $q->p({-class=>'more'}, ScriptLink($more, T('More...'), 'more'));
  3387. }
  3388. return @found;
  3389. }
  3390. sub Replace {
  3391. my ($from, $to, $all, $func) = @_; # $func takes $id and $new text
  3392. my $lang = GetParam('lang', '');
  3393. my $num = GetParam('num', 10);
  3394. my $offset = GetParam('offset', 0);
  3395. my @result;
  3396. foreach my $id (AllPagesList()) {
  3397. OpenPage($id);
  3398. if ($lang) {
  3399. my @languages = split(/,/, $Page{languages});
  3400. next if (@languages and not grep(/$lang/, @languages));
  3401. }
  3402. $_ = $Page{text};
  3403. my $replacement = sub {
  3404. my ($o1, $o2, $o3, $o4, $o5, $o6, $o7, $o8, $o9) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
  3405. my $str = $to;
  3406. $str =~ s/\$([1-9])/'$o' . $1/eeg;
  3407. $str
  3408. };
  3409. if (s/$from/$replacement->()/egi) { # allows use of backreferences
  3410. push (@result, $id);
  3411. $func->($id, $_) if $all or @result > $offset and @result <= $offset + $num;
  3412. }
  3413. }
  3414. return @result;
  3415. }
  3416. sub DoPost {
  3417. my $id = FreeToNormal(shift);
  3418. UserCanEditOrDie($id);
  3419. # Lock before getting old page to prevent races
  3420. RequestLockOrError(); # fatal
  3421. OpenPage($id);
  3422. my $old = $Page{text};
  3423. my $string = UnquoteHtml(GetParam('text', undef));
  3424. $string =~ s/(\r|$FS)//g;
  3425. my ($type) = TextIsFile($string); # MIME type if an uploaded file
  3426. my $filename = GetParam('file', undef);
  3427. if (($filename or $type) and not $UploadAllowed and not UserIsAdmin()) {
  3428. ReportError(T('Only administrators can upload files.'), '403 FORBIDDEN');
  3429. }
  3430. my $comment = UnquoteHtml(GetParam('aftertext', undef));
  3431. $comment =~ s/(\r|$FS)//g;
  3432. if (defined $comment and $comment eq '') {
  3433. ReleaseLock();
  3434. return ReBrowsePage($id);
  3435. }
  3436. if ($filename) { # upload file
  3437. my $file = $q->upload('file');
  3438. if (not $file and $q->cgi_error) {
  3439. ReportError(Ts('Transfer Error: %s', $q->cgi_error), '500 INTERNAL SERVER ERROR');
  3440. }
  3441. ReportError(T('Browser reports no file info.'), '500 INTERNAL SERVER ERROR') unless $q->uploadInfo($filename);
  3442. $type = $q->uploadInfo($filename)->{'Content-Type'};
  3443. ReportError(T('Browser reports no file type.'), '415 UNSUPPORTED MEDIA TYPE') unless $type;
  3444. local $/ = undef; # Read complete files
  3445. my $content = <$file>; # Apparently we cannot count on <$file> to always work within the eval!?
  3446. my $encoding = substr($content, 0, 2) eq "\x1f\x8b" ? 'gzip' : '';
  3447. eval { require MIME::Base64; $_ = MIME::Base64::encode($content) };
  3448. $string = "#FILE $type $encoding\n" . $_;
  3449. } else { # ordinary text edit
  3450. $string = AddComment($old, $comment) if defined $comment;
  3451. if ($comment and substr($string, 0, length($DeletedPage)) eq $DeletedPage) { # look ma, no regexp!
  3452. $string = substr($string, length($DeletedPage)); # undelete pages when adding a comment
  3453. }
  3454. $string .= "\n" if ($string !~ /\n$/); # add trailing newline
  3455. $string = RunMyMacros($string); # run macros on text pages only
  3456. }
  3457. my %allowed = map {$_ => 1} @UploadTypes;
  3458. if (@UploadTypes and $type and not $allowed{$type}) {
  3459. ReportError(Ts('Files of type %s are not allowed.', $type), '415 UNSUPPORTED MEDIA TYPE');
  3460. }
  3461. # Banned Content
  3462. my $summary = GetSummary();
  3463. if (not UserIsEditor()) {
  3464. my $rule = BannedContent(NormalToFree($id)) || BannedContent($string) || BannedContent($summary);
  3465. ReportError(T('Edit Denied'), '403 FORBIDDEN', undef, $q->p(T('The page contains banned text.')),
  3466. $q->p(T('Contact the wiki administrator for more information.')), $q->p($rule)) if $rule;
  3467. }
  3468. # rebrowse if no changes
  3469. my $oldrev = $Page{revision};
  3470. if (GetParam('Preview', '')) { # Preview button was used
  3471. ReleaseLock();
  3472. if (defined $comment) {
  3473. BrowsePage($id, 0, RunMyMacros($comment)); # show macros in preview
  3474. } else {
  3475. DoEdit($id, $string, 1);
  3476. }
  3477. return;
  3478. } elsif ($old eq $string) {
  3479. ReleaseLock(); # No changes -- just show the same page again
  3480. return ReBrowsePage($id);
  3481. } elsif ($oldrev == 0 and $string eq "\n") {
  3482. ReportError(T('No changes to be saved.'), '400 BAD REQUEST'); # don't fake page creation because of webdav
  3483. }
  3484. my $newAuthor = 0;
  3485. if ($oldrev) { # the first author (no old revision) is not considered to be "new"
  3486. $newAuthor = 1 if not $Page{username} or $Page{username} ne GetParam('username', '');
  3487. }
  3488. my $oldtime = $Page{ts};
  3489. my $myoldtime = GetParam('oldtime', ''); # maybe empty!
  3490. # Handle raw edits with the meta info on the first line
  3491. if (GetParam('raw', 0) == 2 and $string =~ /^([0-9]+).*\n((.*\n)*.*)/) {
  3492. $myoldtime = $1;
  3493. $string = $2;
  3494. }
  3495. my $generalwarning = 0;
  3496. if ($newAuthor and $oldtime ne $myoldtime and not defined $comment) {
  3497. if ($myoldtime) {
  3498. my ($ancestor) = GetTextAtTime($myoldtime);
  3499. if ($ancestor and $old ne $ancestor) {
  3500. my $new = MergeRevisions($string, $ancestor, $old);
  3501. if ($new) {
  3502. $string = $new;
  3503. if ($new =~ /^<<<<<<</m and $new =~ /^>>>>>>>/m) {
  3504. SetParam('msg', Ts('This page was changed by somebody else %s.',
  3505. CalcTimeSince($Now - $Page{ts}))
  3506. . ' ' . T('The changes conflict. Please check the page again.'));
  3507. } # else no conflict
  3508. } else {
  3509. $generalwarning = 1;
  3510. } # else merge revision didn't work
  3511. } # else nobody changed the page in the mean time (same text)
  3512. } else {
  3513. $generalwarning = 1;
  3514. } # no way to be sure since myoldtime is missing
  3515. } # same author or nobody changed the page in the mean time (same timestamp)
  3516. if ($generalwarning and ($Now - $Page{ts}) < 600) {
  3517. SetParam('msg', Ts('This page was changed by somebody else %s.',
  3518. CalcTimeSince($Now - $Page{ts}))
  3519. . ' ' . T('Please check whether you overwrote those changes.'));
  3520. }
  3521. Save($id, $string, $summary, (GetParam('recent_edit', '') eq 'on'), $filename);
  3522. ReleaseLock();
  3523. ReBrowsePage($id);
  3524. }
  3525. sub GetSummary {
  3526. my $text = GetParam('aftertext', '') || ($Page{revision} > 0 ? '' : GetParam('text', ''));
  3527. return '' if $text =~ /^#FILE /;
  3528. if ($SummaryDefaultLength and length($text) > $SummaryDefaultLength) {
  3529. $text = substr($text, 0, $SummaryDefaultLength);
  3530. $text =~ s/\s*\S*$/ . . ./;
  3531. }
  3532. my $summary = GetParam('summary', '') || $text; # not GetParam('summary', $text) work because '' is defined
  3533. $summary =~ s/$FS|[\r\n]+/ /g; # remove linebreaks and separator characters
  3534. $summary =~ s/\[$FullUrlPattern\s+(.*?)\]/$2/g; # fix common annoyance when copying text to summary
  3535. $summary =~ s/\[$FullUrlPattern\]//g;
  3536. $summary =~ s/\[\[$FreeLinkPattern\]\]/$1/g;
  3537. return UnquoteHtml($summary);
  3538. }
  3539. sub AddComment {
  3540. my ($string, $comment) = @_;
  3541. $comment =~ s/\r//g; # Remove "\r"-s (0x0d) from the string
  3542. $comment =~ s/\s+$//g; # Remove whitespace at the end
  3543. if ($comment ne '') {
  3544. my $author = GetParam('username', T('Anonymous'));
  3545. my $homepage = GetParam('homepage', '');
  3546. $homepage = 'http://' . $homepage if $homepage and $homepage !~ /^($UrlProtocols):/;
  3547. $author = "[$homepage $author]" if $homepage;
  3548. $string .= "\n----\n\n" if $string and $string ne "\n";
  3549. $string .= $comment . "\n\n"
  3550. . '-- ' . $author . ' ' . TimeToText($Now) . "\n\n";
  3551. }
  3552. return $string;
  3553. }
  3554. sub Save { # call within lock, with opened page
  3555. my ($id, $new, $summary, $minor, $upload) = @_;
  3556. my $user = GetParam('username', '');
  3557. my $revision = $Page{revision} + 1;
  3558. my $old = $Page{text};
  3559. my $olddiff = $Page{'diff-major'} == '1' ? $Page{'diff-minor'} : $Page{'diff-major'};
  3560. if ($revision == 1 and IsFile($IndexFile) and not Unlink($IndexFile)) { # regenerate index on next request
  3561. SetParam('msg', Ts('Cannot delete the index file %s.', $IndexFile)
  3562. . ' ' . T('Please check the directory permissions.')
  3563. . ' ' . T('Your changes were not saved.'));
  3564. return 0;
  3565. }
  3566. ReInit($id);
  3567. TouchIndexFile();
  3568. SaveKeepFile(); # deletes blocks, flags, diff-major, and diff-minor, and sets keep-ts
  3569. ExpireKeepFiles();
  3570. $Page{lastmajor} = $revision unless $minor;
  3571. $Page{lastmajorsummary} = $summary unless $minor;
  3572. @Page{qw(ts revision summary username minor text)} =
  3573. ($Now, $revision, $summary, $user, $minor, $new);
  3574. if ($UseDiff and $UseCache > 1 and $revision > 1 and not $upload and not TextIsFile($old)) {
  3575. UpdateDiffs($old, $new, $olddiff); # sets diff-major and diff-minor
  3576. }
  3577. my $languages;
  3578. $languages = GetLanguages($new) unless $upload;
  3579. $Page{languages} = $languages;
  3580. SavePage();
  3581. if ($revision == 1 and $LockOnCreation{$id}) {
  3582. WriteStringToFile(GetLockedPageFile($id), 'LockOnCreation');
  3583. }
  3584. my $host = $q->remote_addr();
  3585. WriteRcLog($id, $summary, $minor, $revision, $user, $host, $languages, GetCluster($new));
  3586. AddToIndex($id) if ($revision == 1);
  3587. }
  3588. sub TouchIndexFile {
  3589. my $ts = time;
  3590. utime $ts, $ts, $IndexFile;
  3591. $LastUpdate = $Now = $ts;
  3592. }
  3593. sub GetLanguages {
  3594. my $text = shift;
  3595. my @result;
  3596. for my $lang (sort keys %Languages) {
  3597. my @matches = $text =~ /$Languages{$lang}/gi;
  3598. push(@result, $lang) if $#matches >= $LanguageLimit;
  3599. }
  3600. return join(',', @result);
  3601. }
  3602. sub GetCluster {
  3603. $_ = shift;
  3604. return '' unless $PageCluster;
  3605. return $1 if ($WikiLinks && /^$LinkPattern\n/)
  3606. or ($FreeLinks && /^\[\[$FreeLinkPattern\]\]\n/);
  3607. }
  3608. sub MergeRevisions { # merge change from file2 to file3 into file1
  3609. my ($file1, $file2, $file3) = @_;
  3610. my ($name1, $name2, $name3) = ("$TempDir/file1", "$TempDir/file2", "$TempDir/file3");
  3611. CreateDir($TempDir);
  3612. RequestLockDir('merge') or return T('Could not get a lock to merge!');
  3613. WriteStringToFile($name1, $file1);
  3614. WriteStringToFile($name2, $file2);
  3615. WriteStringToFile($name3, $file3);
  3616. my ($you, $ancestor, $other) = (T('you'), T('ancestor'), T('other'));
  3617. my $output = decode_utf8(`diff3 -m -L \Q$you\E -L \Q$ancestor\E -L \Q$other\E -- \Q$name1\E \Q$name2\E \Q$name3\E`);
  3618. ReleaseLockDir('merge'); # don't unlink temp files--next merge will just overwrite.
  3619. return $output;
  3620. }
  3621. # Note: all diff and recent-list operations should be done within locks.
  3622. sub WriteRcLog {
  3623. my ($id, $summary, $minor, $revision, $username, $host, $languages, $cluster) = @_;
  3624. my $line = join($FS, $Now, $id, $minor, $summary, $host,
  3625. $username, $revision, $languages, $cluster);
  3626. AppendStringToFile($RcFile, $line . "\n");
  3627. }
  3628. sub UpdateDiffs { # this could be optimized, but isn't frequent enough
  3629. my ($old, $new, $olddiff) = @_;
  3630. $Page{'diff-minor'} = GetDiff($old, $new); # create new diff-minor
  3631. # 1 is a special value for GetCacheDiff telling it to use diff-minor
  3632. $Page{'diff-major'} = $Page{lastmajor} == $Page{revision} ? 1 : $olddiff;
  3633. }
  3634. sub DoMaintain {
  3635. print GetHeader('', T('Run Maintenance')), $q->start_div({-class=>'content maintain'});
  3636. my $fname = "$DataDir/maintain";
  3637. if (not UserIsAdmin()) {
  3638. if (IsFile($fname) and $Now - Modified($fname) < 0.5) {
  3639. print $q->p(T('Maintenance not done.') . ' ' . T('(Maintenance can only be done once every 12 hours.)')
  3640. . ' ', T('Remove the "maintain" file or wait.')), $q->end_div();
  3641. PrintFooter();
  3642. return;
  3643. }
  3644. }
  3645. print '<p>', T('Expiring keep files and deleting pages marked for deletion');
  3646. ExpireAllKeepFiles();
  3647. print '</p>';
  3648. RequestLockOrError();
  3649. print $q->p(T('Main lock obtained.'));
  3650. print $q->p(Ts('Moving part of the %s log file.', $RCName));
  3651. # Determine the number of days to go back, default is largest of @RcDays
  3652. my $days = 0;
  3653. foreach (@RcDays) {
  3654. $days = $_ if $_ > $days;
  3655. }
  3656. my $starttime = $Now - $days * 86400; # 24*60*60
  3657. # Read the current file
  3658. my ($status, $data) = ReadFile($RcFile);
  3659. if (not $status) {
  3660. print $q->p($q->strong(Ts('Could not open %s log file', $RCName) . ':') . ' ' . $RcFile),
  3661. $q->p(T('Error was') . ':'), $q->pre($!), $q->p(T('Note: This error is normal if no changes have been made.'));
  3662. } else {
  3663. WriteStringToFile($RcFile . '.old', $data);
  3664. }
  3665. # Move the old stuff from rc to temp
  3666. my @rc = split(/\n/, $data);
  3667. my @tmp = ();
  3668. my $line;
  3669. my $changed = 0;
  3670. while ($line = shift(@rc)) {
  3671. my ($ts, $id, $minor, $summary, $host, @rest) = split(/$FS/, $line);
  3672. last if $ts >= $starttime;
  3673. push(@tmp, join($FS, $ts, $id, $minor, $summary, 'Anonymous', @rest));
  3674. $changed = 1;
  3675. }
  3676. unshift(@rc, $line) if $line; # this one ended the loop
  3677. print $q->p(Ts('Moving %s log entries.', scalar(@tmp)));
  3678. AppendStringToFile($RcOldFile, join("\n", @tmp) . "\n") if @tmp;
  3679. # remove IP numbers from all but the last few days
  3680. $starttime = $Now - $KeepHostDays * 86400; # 24*60*60
  3681. @tmp = ();
  3682. while ($line = shift(@rc)) {
  3683. my ($ts, $id, $minor, $summary, $host, @rest) = split(/$FS/, $line);
  3684. last if $ts >= $starttime;
  3685. push(@tmp, join($FS, $ts, $id, $minor, $summary, T('Anonymous'), @rest));
  3686. $changed = 1;
  3687. }
  3688. unshift(@rc, $line) if $line; # this one ended the loop
  3689. unshift(@rc, @tmp) if @tmp;
  3690. print $q->p(Ts('Removing IP numbers from %s log entries.', scalar(@tmp)));
  3691. WriteStringToFile($RcFile, @rc ? join("\n", @rc) . "\n" : '') if $changed;
  3692. if (opendir(DIR, $RssDir)) { # cleanup if they should expire anyway
  3693. foreach (readdir(DIR)) {
  3694. Unlink("$RssDir/$_") if $Now - Modified($_) > $RssCacheHours * 3600;
  3695. }
  3696. closedir DIR;
  3697. }
  3698. foreach my $func (@MyMaintenance) {
  3699. $func->();
  3700. }
  3701. WriteStringToFile($fname, 'Maintenance done at ' . TimeToText($Now));
  3702. ReleaseLock();
  3703. print $q->p(T('Main lock released.')), $q->end_div();
  3704. PrintFooter();
  3705. }
  3706. sub PageDeletable {
  3707. return unless $KeepDays;
  3708. my $expirets = $Now - ($KeepDays * 86400); # 24*60*60
  3709. return 0 if $Page{ts} >= $expirets;
  3710. return PageMarkedForDeletion();
  3711. }
  3712. sub PageMarkedForDeletion {
  3713. # Only pages explicitly marked for deletion or whitespace-only pages
  3714. # are deleted; taking into account the very rare possiblity of a
  3715. # read error and the page text being undefined.
  3716. return 1 if defined $Page{text} and $Page{text} =~ /^\s*$/;
  3717. return $DeletedPage && substr($Page{text}, 0, length($DeletedPage)) eq $DeletedPage;
  3718. }
  3719. sub DeletePage { # Delete must be done inside locks.
  3720. my $id = shift;
  3721. ValidIdOrDie($id);
  3722. AppendStringToFile($DeleteFile, "$id\n");
  3723. foreach my $name (GetPageFile($id), GetKeepFiles($id), GetKeepDir($id), GetLockedPageFile($id), $IndexFile) {
  3724. Unlink($name) if IsFile($name);
  3725. RemoveDir($name) if IsDir($name);
  3726. }
  3727. ReInit($id);
  3728. delete $IndexHash{$id};
  3729. @IndexList = sort(keys %IndexHash);
  3730. return ''; # no error
  3731. }
  3732. sub DoEditLock {
  3733. return unless UserIsAdminOrError();
  3734. print GetHeader('', T('Set or Remove global edit lock'));
  3735. my $fname = "$NoEditFile";
  3736. if (GetParam("set", 1)) {
  3737. WriteStringToFile($fname, 'editing locked.');
  3738. } else {
  3739. Unlink($fname);
  3740. }
  3741. utime time, time, $IndexFile; # touch index file
  3742. print $q->p(IsFile($fname) ? T('Edit lock created.') : T('Edit lock removed.'));
  3743. PrintFooter();
  3744. }
  3745. sub DoPageLock {
  3746. return unless UserIsAdminOrError();
  3747. print GetHeader('', T('Set or Remove page edit lock'));
  3748. my $id = GetParam('id', '');
  3749. ValidIdOrDie($id);
  3750. my $fname = GetLockedPageFile($id);
  3751. if (GetParam('set', 1)) {
  3752. WriteStringToFile($fname, 'editing locked.');
  3753. } else {
  3754. Unlink($fname);
  3755. }
  3756. utime time, time, $IndexFile; # touch index file
  3757. print $q->p(IsFile($fname) ? Ts('Lock for %s created.', GetPageLink($id))
  3758. : Ts('Lock for %s removed.', GetPageLink($id)));
  3759. PrintFooter();
  3760. }
  3761. sub DoShowVersion {
  3762. print GetHeader('', T('Displaying Wiki Version')), $q->start_div({-class=>'content version'});
  3763. print $WikiDescription, $q->p($q->server_software()),
  3764. $q->p(sprintf('Perl v%vd', $^V)),
  3765. $q->p($ENV{MOD_PERL} ? $ENV{MOD_PERL} : "no mod_perl"), $q->p('CGI: ', $CGI::VERSION),
  3766. $q->p('LWP::UserAgent ', eval { local $SIG{__DIE__}; require LWP::UserAgent; $LWP::UserAgent::VERSION; }),
  3767. $q->p('XML::RSS: ', eval { local $SIG{__DIE__}; require XML::RSS; $XML::RSS::VERSION; }),
  3768. $q->p('XML::Parser: ', eval { local $SIG{__DIE__}; $XML::Parser::VERSION; });
  3769. print $q->p('diff: ' . (`diff --version` || $!)), $q->p('diff3: ' . (`diff3 --version` || $!)) if $UseDiff;
  3770. print $q->end_div();
  3771. PrintFooter();
  3772. }
  3773. sub DoDebug {
  3774. print GetHeader('', T('Debugging Information')),
  3775. $q->start_div({-class=>'content debug'});
  3776. foreach my $func (@Debugging) { $func->() }
  3777. print $q->end_div();
  3778. PrintFooter();
  3779. }
  3780. sub DoSurgeProtection {
  3781. return unless $SurgeProtection;
  3782. my $name = GetParam('username', $q->remote_addr());
  3783. return unless $name;
  3784. ReadRecentVisitors();
  3785. AddRecentVisitor($name);
  3786. if (RequestLockDir('visitors')) { # not fatal
  3787. WriteRecentVisitors();
  3788. ReleaseLockDir('visitors');
  3789. if (DelayRequired($name)) {
  3790. ReportError(Ts('Too many connections by %s', $name)
  3791. . ': ' . Tss('Please do not fetch more than %1 pages in %2 seconds.',
  3792. $SurgeProtectionViews, $SurgeProtectionTime),
  3793. '503 SERVICE UNAVAILABLE');
  3794. }
  3795. } elsif (GetParam('action', '') ne 'unlock') {
  3796. ReportError(Ts('Could not get %s lock', 'visitors') . ': ' . Ts('Check whether the web server can create the directory %s and whether it can create files in it.', $TempDir), '503 SERVICE UNAVAILABLE');
  3797. }
  3798. }
  3799. sub DelayRequired {
  3800. my $name = shift;
  3801. my @entries = @{$RecentVisitors{$name}};
  3802. my $ts = $entries[$SurgeProtectionViews];
  3803. return ($Now - $ts) < $SurgeProtectionTime;
  3804. }
  3805. sub AddRecentVisitor {
  3806. my $name = shift;
  3807. my $value = $RecentVisitors{$name};
  3808. my @entries = ($Now);
  3809. push(@entries, @{$value}) if $value;
  3810. $RecentVisitors{$name} = \@entries;
  3811. }
  3812. sub ReadRecentVisitors {
  3813. my ($status, $data) = ReadFile($VisitorFile);
  3814. %RecentVisitors = ();
  3815. return unless $status;
  3816. foreach (split(/\n/, $data)) {
  3817. my @entries = split /$FS/;
  3818. my $name = shift(@entries);
  3819. $RecentVisitors{$name} = \@entries if $name;
  3820. }
  3821. }
  3822. sub WriteRecentVisitors {
  3823. my $data = '';
  3824. my $limit = $Now - $SurgeProtectionTime;
  3825. foreach my $name (keys %RecentVisitors) {
  3826. my @entries = @{$RecentVisitors{$name}};
  3827. if ($entries[0] >= $limit) { # if the most recent one is too old, do not keep
  3828. $data .= join($FS, $name, @entries[0 .. $SurgeProtectionViews - 1]) . "\n";
  3829. }
  3830. }
  3831. WriteStringToFile($VisitorFile, $data);
  3832. }
  3833. sub TextIsFile { $_[0] =~ /^#FILE (\S+) ?(\S+)?\n/; }
  3834. sub AddModuleDescription { # cannot use $q here because this is module init time
  3835. my ($filename, $page, $dir, $tag) = @_;
  3836. my $src = "http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/$dir" . UrlEncode($filename) . ($tag ? '?' . $tag : '');
  3837. my $doc = 'https://www.oddmuse.org/cgi-bin/oddmuse/' . UrlEncode(FreeToNormal($page));
  3838. $ModulesDescription .= "<p><a href=\"$src\">" . QuoteHtml($filename) . "</a>" . ($tag ? " ($tag)" : '');
  3839. $ModulesDescription .= T(', see') . " <a href=\"$doc\">" . QuoteHtml($page) . "</a>" if $page;
  3840. $ModulesDescription .= "</p>";
  3841. }
  3842. DoWikiRequest() if $RunCGI and not exists $ENV{MOD_PERL}; # Do everything.
  3843. 1; # In case we are loaded from elsewhere