refcount.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2004 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This file adds the capability to perform reference counts on
  8. pages in the PmWiki database. Simply use "?action=refcount" to
  9. bring up the reference count form. The output is a table
  10. where each row of the table contains a page name or link reference,
  11. the number of (non-RecentChanges) pages that contain links to the
  12. page, the number of RecentChanges pages with links to the page, and
  13. the total number of references in all pages.
  14. This script can be activated by placing
  15. include_once("scripts/refcount.php");
  16. in the config.php file.
  17. */
  18. SDV($PageRefCountFmt,"<h1>Reference Count Results</h1><p>");
  19. SDV($RefCountTimeFmt," <small>%Y-%b-%d %H:%M</small>");
  20. SDV($HandleActions['refcount'],'HandleRefCount');
  21. function PrintRefCount() {
  22. global $WikiLibDirs,$WikiDir,$GroupNamePattern,$PageTitlePattern,
  23. $PageRefCountFmt,$WikiWordPattern,$FreeLinkPattern,$UrlPathPattern,
  24. $WikiPageExistsFmt, $WikiPageCreateFmt, $WikiPageCreateSpaceFmt,
  25. $RefCountTimeFmt,$InterMapUrls,$LinkPatterns;
  26. if (!isset($WikiLibDirs)) $WikiLibDirs = array($WikiDir,"wikilib.d");
  27. $grouplist = array('all' => ' all groups');
  28. foreach($WikiLibDirs as $d) {
  29. $dp = opendir($d);
  30. if (!$dp) continue;
  31. while (($pagename = readdir($dp))!==false) {
  32. if (!preg_match("/^($GroupNamePattern)\.$PageTitlePattern\$/",$pagename,
  33. $match)) continue;
  34. $pagelist[$pagename] = $pagename;
  35. $grouplist[$match[1]] = $match[1];
  36. }
  37. closedir($dp);
  38. }
  39. asort($grouplist);
  40. $wlist = array('all','missing','existing','orphaned');
  41. $tlist = isset($_REQUEST['tlist']) ? $_REQUEST['tlist'] : array('all');
  42. $flist = isset($_REQUEST['flist']) ? $_REQUEST['flist'] : array('all');
  43. $whichrefs = @$_REQUEST['whichrefs'];
  44. $showrefs = @$_REQUEST['showrefs'];
  45. $submit = @$_REQUEST['submit'];
  46. $WikiPageExistsFmt = "<a target='_blank' href='\$PageUrl'>\$LinkText</a>";
  47. $WikiPageCreateFmt =
  48. "\$LinkText<a target='_blank' href='\$PageUrl?action=edit'>?</a>";
  49. $WikiPageCreateSpaceFmt = $WikiPageCreateFmt;
  50. echo FmtPageName($PageRefCountFmt,NULL);
  51. echo "<form method='post'><input type='hidden' action='refcount'>
  52. <table cellspacing='10'><tr><td valign='top'>Show
  53. <br><select name='whichrefs'>";
  54. foreach($wlist as $w)
  55. echo "<option ",($whichrefs==$w) ? 'selected' : ''," value='$w'>$w\n";
  56. echo "</select></td><td valign='top'> page names in group<br>
  57. <select name='tlist[]' multiple size='4'>";
  58. foreach($grouplist as $g=>$t)
  59. echo "<option ",in_array($g,$tlist) ? 'selected' : ''," value='$g'>$t\n";
  60. echo "</select></td><td valign='top'> referenced from pages in<br>
  61. <select name='flist[]' multiple size='4'>";
  62. foreach($grouplist as $g=>$t)
  63. echo "<option ",in_array($g,$flist) ? 'selected' : ''," value='$g'>$t\n";
  64. echo "</select></td></tr></table>
  65. <p><input type='checkbox' name='showrefs' value='checked' $showrefs>
  66. Display referencing pages
  67. <p><input type='submit' name='submit' value='Search'></form><p><hr>";
  68. if ($submit) {
  69. ksort($LinkPatterns);
  70. foreach($LinkPatterns as $n=>$a)
  71. foreach($a as $p=>$r) $linkpats[$p]=$r;
  72. foreach($pagelist as $pagename) {
  73. $ref=array();
  74. $page = ReadPage($pagename); Lock(0);
  75. if (!$page) continue;
  76. $tref[$pagename]['time'] = $page['time'];
  77. if (!in_array('all',$flist) &&
  78. !in_array(FmtPageName('$Group',$pagename),$flist)) continue;
  79. $text = preg_replace("/\\[\\=.*?\\=\\]/s",' ',$page['text']);
  80. foreach($linkpats as $p=>$r)
  81. $text = preg_replace("/\\[\\[($p)(\\s*.*?)?\\]\\]/",'$1',$text);
  82. $text = preg_replace("/\\w+:($UrlPathPattern)/",' ',$text);
  83. $text = preg_replace("/\\$$WikiWordPattern/",' ',$text);
  84. $text = preg_replace("/\\[\\[#[A-Za-z][-.:\\w]*?\\]\\]/",' ',$text);
  85. foreach($InterMapUrls as $mapid=>$mapurl) {
  86. $text = preg_replace("/\\b$mapid:($UrlPathPattern)/",' ',$text);
  87. }
  88. if (!preg_match_all("/(($GroupNamePattern)[\\/.])?(($WikiWordPattern)|($FreeLinkPattern))/",$text,$match)) continue;
  89. for($i=0;$i<count($match[0]);$i++) {
  90. @$ref[FmtWikiLink('',$match[0][$i],NULL,'PageName',$pagename)]++;
  91. }
  92. $rc = preg_match('/RecentChanges$/',$pagename);
  93. foreach($ref as $r=>$c) {
  94. @$tref[$r]['tot'] += $c;
  95. if ($rc) @$tref[$r]['rc']++;
  96. else { @$tref[$r]['page']++; @$pref[$r][$pagename]++; }
  97. }
  98. }
  99. uasort($tref,'RefCountCmp');
  100. echo "<table >
  101. <tr><th></th><th colspan='2'>Referring pages</th><th>Total</th></tr>
  102. <tr><th>Name / Time</th><th>All</th><th>R.C.</th><th>Refs</th></tr>";
  103. reset($tref);
  104. foreach($tref as $p=>$c) {
  105. if (!in_array('all',$tlist) &&
  106. !in_array(FmtPageName('$Group',$p),$tlist)) continue;
  107. if ($whichrefs=='missing' && PageExists($p)) continue;
  108. elseif ($whichrefs=='existing' && !PageExists($p)) continue;
  109. elseif ($whichrefs=='orphaned' &&
  110. (@$tref[$p]['page']>0 || !PageExists($p))) continue;
  111. echo "<tr><td valign='top'>",FmtWikiLink('',$p,NULL);
  112. if (@$tref[$p]['time']) echo strftime($RefCountTimeFmt,$tref[$p]['time']);
  113. if ($showrefs && is_array(@$pref[$p])) {
  114. foreach($pref[$p] as $pr=>$pc) echo "<dd>",FmtWikiLink('',$pr,NULL);
  115. }
  116. echo "</td>";
  117. echo "<td align='center' valign='top'>",
  118. FmtPageName("<a href='\$PageUrl?action=search&amp;text=\$Title_'
  119. target='_blank'>",$p),@$tref[$p]['page']+0,"</a></td>";
  120. echo "<td align='center' valign='top'>",@$tref[$p]['rc'],"</td>";
  121. echo "<td align='center' valign='top'>",@$tref[$p]['tot'],"</td>";
  122. echo "</tr>";
  123. }
  124. echo "</table>";
  125. }
  126. EndHTML();
  127. }
  128. function RefCountCmp($ua,$ub) {
  129. if (@($ua['page']!=$ub['page'])) return @($ub['page']-$ua['page']);
  130. if (@($ua['rc']!=$ub['rc'])) return @($ub['rc']-$ua['rc']);
  131. if (@($ua['tot']!=$ub['tot'])) return @($ub['tot']-$ua['tot']);
  132. return @($ub['time']-$ua['time']);
  133. }
  134. function HandleRefCount($pagename) {
  135. global $HandleRefCountFmt,$PageStartFmt,$PageEndFmt;
  136. $page = array('timefmt'=>@$GLOBALS['CurrentTime'],
  137. 'author'=>@$GLOBALS['Author']);
  138. SetPageVars($pagename,$page,"Reference Counts");
  139. SDV($HandleRefCountFmt,array(&$PageStartFmt,
  140. 'function:PrintRefCount',&$PageEndFmt));
  141. PrintFmt($pagename,$HandleRefCountFmt);
  142. }
  143. ?>