refcount.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2006 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 does simple reference counting on pages in a PmWiki site.
  8. Simply activate this script using
  9. include_once('scripts/refcount.php');
  10. in the config.php file and then use ?action=refcount to bring up
  11. the reference count form. The output is a table where each row
  12. of the table contains a page name or link reference, the number
  13. of (non-RecentChanges) pages that contain links to the page,
  14. the number of RecentChanges pages with links to the page, and the
  15. total number of references in all pages.
  16. */
  17. SDV($PageRefCountFmt,"<h2 class='wikiaction'>Reference Count Results</h2>");
  18. SDV($RefCountTimeFmt," <small>%Y-%b-%d %H:%M</small>");
  19. SDV($HandleActions['refcount'], 'HandleRefCount');
  20. function PrintRefCount($pagename) {
  21. global $GroupPattern,$NamePattern,$PageRefCountFmt,$RefCountTimeFmt;
  22. $pagelist = ListPages();
  23. $grouplist = array();
  24. foreach($pagelist as $pname) {
  25. if (!preg_match("/^($GroupPattern)[\\/.]($NamePattern)$/",$pname,$m))
  26. continue;
  27. $grouplist[$m[1]]=$m[1];
  28. }
  29. asort($grouplist);
  30. $grouplist = array_merge(array('all' => 'all groups'),$grouplist);
  31. $wlist = array('all','missing','existing','orphaned');
  32. $tlist = isset($_REQUEST['tlist']) ? $_REQUEST['tlist'] : array('all');
  33. $flist = isset($_REQUEST['flist']) ? $_REQUEST['flist'] : array('all');
  34. $whichrefs = @$_REQUEST['whichrefs'];
  35. $showrefs = @$_REQUEST['showrefs'];
  36. $submit = @$_REQUEST['submit'];
  37. echo FmtPageName($PageRefCountFmt,$pagename);
  38. echo "<form method='post'><input type='hidden' action='refcount'>
  39. <table cellspacing='10'><tr><td valign='top'>Show
  40. <br><select name='whichrefs'>";
  41. foreach($wlist as $w)
  42. echo "<option ",($whichrefs==$w) ? 'selected' : ''," value='$w'>$w\n";
  43. echo "</select></td><td valign='top'> page names in group<br>
  44. <select name='tlist[]' multiple size='4'>";
  45. foreach($grouplist as $g=>$t)
  46. echo "<option ",in_array($g,$tlist) ? 'selected' : ''," value='$g'>$t\n";
  47. echo "</select></td><td valign='top'> referenced from pages in<br>
  48. <select name='flist[]' multiple size='4'>";
  49. foreach($grouplist as $g=>$t)
  50. echo "<option ",in_array($g,$flist) ? 'selected' : ''," value='$g'>$t\n";
  51. echo "</select></td></tr></table>
  52. <p><input type='checkbox' name='showrefs' value='checked' $showrefs>
  53. Display referencing pages
  54. <p><input type='submit' name='submit' value='Search'></form><p><hr>";
  55. if ($submit) {
  56. foreach($pagelist as $pname) {
  57. $ref = array();
  58. $page = ReadPage($pname, READPAGE_CURRENT);
  59. if (!$page) continue;
  60. $tref[$pname]['time'] = $page['time'];
  61. if (!in_array('all',$flist) &&
  62. !in_array(FmtPageName('$Group',$pname),$flist)) continue;
  63. $rc = preg_match('/RecentChanges$/',$pname);
  64. foreach(explode(',',@$page['targets']) as $r) {
  65. if ($r=='') continue;
  66. if ($rc) @$tref[$r]['rc']++;
  67. else { @$tref[$r]['page']++; @$pref[$r][$pname]++; }
  68. }
  69. }
  70. uasort($tref,'RefCountCmp');
  71. echo "<table >
  72. <tr><th></th><th colspan='2'>Referring pages</th></tr>
  73. <tr><th>Name / Time</th><th>All</th><th>R.C.</th></tr>";
  74. reset($tref);
  75. foreach($tref as $p=>$c) {
  76. if (!in_array('all',$tlist) &&
  77. !in_array(FmtPageName('$Group',$p),$tlist)) continue;
  78. if ($whichrefs=='missing' && PageExists($p)) continue;
  79. elseif ($whichrefs=='existing' && !PageExists($p)) continue;
  80. elseif ($whichrefs=='orphaned' &&
  81. (@$tref[$p]['page']>0 || !PageExists($p))) continue;
  82. echo "<tr><td valign='top'>",LinkPage($pagename, '', $p, '', $p);
  83. if (@$tref[$p]['time']) echo strftime($RefCountTimeFmt,$tref[$p]['time']);
  84. if ($showrefs && is_array(@$pref[$p])) {
  85. foreach($pref[$p] as $pr=>$pc)
  86. echo "<dd>", LinkPage($pagename, '', $pr, '', $pr);
  87. }
  88. echo "</td>";
  89. echo "<td align='center' valign='top'>",@$tref[$p]['page']+0,"</td>";
  90. echo "<td align='center' valign='top'>",@$tref[$p]['rc']+0,"</td>";
  91. echo "</tr>";
  92. }
  93. echo "</table>";
  94. }
  95. }
  96. function RefCountCmp($ua,$ub) {
  97. if (@($ua['page']!=$ub['page'])) return @($ub['page']-$ua['page']);
  98. if (@($ua['rc']!=$ub['rc'])) return @($ub['rc']-$ua['rc']);
  99. return @($ub['time']-$ua['time']);
  100. }
  101. function HandleRefCount($pagename, $auth='read') {
  102. global $HandleRefCountFmt,$PageStartFmt,$PageEndFmt;
  103. $page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT);
  104. if (!$page) Abort('?unauthorized');
  105. PCache($pagename, $page);
  106. SDV($HandleRefCountFmt,array(&$PageStartFmt,
  107. 'function:PrintRefCount',&$PageEndFmt));
  108. PrintFmt($pagename,$HandleRefCountFmt);
  109. }