refcount.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2018 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. Script maintained by Petko YOTOV www.pmwiki.org/petko
  17. */
  18. SDV($PageRefCountFmt,"<h2 class='wikiaction'>Reference Count Results</h2>");
  19. SDV($RefCountTimeFmt," <small>%Y-%b-%d %H:%M</small>");
  20. SDV($HandleActions['refcount'], 'HandleRefCount');
  21. function PrintRefCount($pagename) {
  22. global $GroupPattern,$NamePattern,$PageRefCountFmt,$RefCountTimeFmt, $ScriptUrl;
  23. $pagelist = ListPages();
  24. $grouplist = array();
  25. foreach($pagelist as $pname) {
  26. if (!preg_match("/^($GroupPattern)[\\/.]($NamePattern)$/",$pname,$m))
  27. continue;
  28. $grouplist[$m[1]]=$m[1];
  29. }
  30. asort($grouplist);
  31. $grouplist = array_merge(array('all' => 'all groups'),$grouplist);
  32. $wlist = array('all','missing','existing','orphaned');
  33. $tlist = isset($_REQUEST['tlist']) ? $_REQUEST['tlist'] : array('all');
  34. $flist = isset($_REQUEST['flist']) ? $_REQUEST['flist'] : array('all');
  35. $whichrefs = @$_REQUEST['whichrefs'];
  36. $showrefs = (@$_REQUEST['showrefs']=='checked')? "checked='checked'" : '';
  37. $submit = @$_REQUEST['submit'];
  38. echo FmtPageName($PageRefCountFmt,$pagename);
  39. echo FmtPageName("<form method='post' action='{\$PageUrl}'><input type='hidden' name='action' value='refcount'/>
  40. <table cellspacing='10'><tr><td valign='top'>Show
  41. <br/><select name='whichrefs'>",$pagename);
  42. foreach($wlist as $w)
  43. echo "<option ",($whichrefs==$w) ? 'selected="selected"' : ''," value='$w'>$w</option>\n";
  44. echo "</select></td><td valign='top'> page names in group<br/>
  45. <select name='tlist[]' multiple='multiple' size='4'>";
  46. foreach($grouplist as $g=>$t)
  47. echo "<option ",in_array($g,$tlist) ? 'selected="selected"' : ''," value='$g'>$t</option>\n";
  48. echo "</select></td><td valign='top'> referenced from pages in<br/>
  49. <select name='flist[]' multiple='multiple' size='4'>";
  50. foreach($grouplist as $g=>$t)
  51. echo "<option ",in_array($g,$flist) ? 'selected="selected"' : ''," value='$g'>$t</option>\n";
  52. echo "</select></td></tr></table>
  53. <p><input type='checkbox' name='showrefs' id='chk_showrefs' value='checked' $showrefs/>
  54. <label for='chk_showrefs'>Display referencing pages</label>
  55. </p><p><input type='submit' name='submit' value='Search'/></p></form><hr/>";
  56. if ($submit) {
  57. foreach($pagelist as $pname) {
  58. $ref = array();
  59. $page = ReadPage($pname, READPAGE_CURRENT);
  60. if (!$page) continue;
  61. $tref[$pname]['time'] = $page['time'];
  62. if (!in_array('all',$flist) &&
  63. !in_array(FmtPageName('$Group',$pname),$flist)) continue;
  64. $rc = preg_match('/RecentChanges$/',$pname);
  65. foreach(explode(',',@$page['targets']) as $r) {
  66. if ($r=='') continue;
  67. if ($rc) @$tref[$r]['rc']++;
  68. else { @$tref[$r]['page']++; @$pref[$r][$pname]++; }
  69. }
  70. }
  71. uasort($tref,'RefCountCmp');
  72. echo "<table class='refcount'>
  73. <tr><th></th><th colspan='2'>Referring pages</th></tr>
  74. <tr><th>Name / Time</th><th>All</th><th>R.C.</th></tr>";
  75. reset($tref);
  76. foreach($tref as $p=>$c) {
  77. if (!in_array('all',$tlist) &&
  78. !in_array(FmtPageName('$Group',$p),$tlist)) continue;
  79. if ($whichrefs=='missing' && PageExists($p)) continue;
  80. elseif ($whichrefs=='existing' && !PageExists($p)) continue;
  81. elseif ($whichrefs=='orphaned' &&
  82. (@$tref[$p]['page']>0 || !PageExists($p))) continue;
  83. echo "<tr><td valign='top'>",LinkPage($pagename, '', $p, '', $p);
  84. if (@$tref[$p]['time']) echo strftime($RefCountTimeFmt,$tref[$p]['time']);
  85. if ($showrefs && is_array(@$pref[$p])) {
  86. foreach($pref[$p] as $pr=>$pc)
  87. echo "<dd>", LinkPage($pagename, '', $pr, '', $pr);
  88. }
  89. echo "</td>";
  90. echo "<td align='center' valign='top'>",@$tref[$p]['page']+0,"</td>";
  91. echo "<td align='center' valign='top'>",@$tref[$p]['rc']+0,"</td>";
  92. echo "</tr>";
  93. }
  94. echo "</table>";
  95. }
  96. }
  97. function RefCountCmp($ua,$ub) {
  98. if (@($ua['page']!=$ub['page'])) return @($ub['page']-$ua['page']);
  99. if (@($ua['rc']!=$ub['rc'])) return @($ub['rc']-$ua['rc']);
  100. return @($ub['time']-$ua['time']);
  101. }
  102. function HandleRefCount($pagename, $auth='read') {
  103. global $HandleRefCountFmt,$PageStartFmt,$PageEndFmt;
  104. $page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT);
  105. if (!$page) Abort('?unauthorized');
  106. PCache($pagename, $page);
  107. SDV($HandleRefCountFmt,array(&$PageStartFmt,
  108. 'function:PrintRefCount',&$PageEndFmt));
  109. PrintFmt($pagename,$HandleRefCountFmt);
  110. }