home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / wiki / parse / macros.php < prev    next >
PHP Script  |  2004-03-08  |  13KB  |  451 lines

  1. <?php
  2. // $Id: macros.php,v 1.1 2004/01/12 22:14:05 comsubvie Exp $
  3.  
  4. // Prepare a category list.
  5. function view_macro_category($args)
  6. {
  7.   global $pagestore, $MinEntries, $DayLimit, $full, $page, $Entity;
  8.   global $FlgChr;
  9.  
  10.   $text = '';
  11.   if(strstr($args, '*'))                // Category containing all pages.
  12.   {
  13.     $list = $pagestore->allpages();
  14.   }
  15.   else if(strstr($args, '?'))           // New pages.
  16.   {
  17.     $list = $pagestore->newpages();
  18.   }
  19.   else if(strstr($args, '~'))           // Zero-length (deleted) pages.
  20.   {
  21.     $list = $pagestore->emptypages();
  22.   }
  23.   else                                  // Ordinary list of pages.
  24.   {
  25.     $parsed = parseText($args, array('parse_freelink', 'parse_wikiname'), '');
  26.     $pagenames = array();
  27.     preg_replace('/' . $FlgChr . '!?(\\d+)' . $FlgChr . '/e', '$pagenames[]=$Entity[\\1][1]', $parsed);
  28.     $list = $pagestore->givenpages($pagenames);
  29.   }
  30.  
  31.   if(count($list) == 0)
  32.     { return ''; }
  33.  
  34.   usort($list, 'catSort');
  35.  
  36.   $now = time();
  37.  
  38.   for($i = 0; $i < count($list); $i++)
  39.   {
  40.     $editTime = mktime(substr($list[$i][0], 8, 2),  substr($list[$i][0], 10, 2),
  41.                        substr($list[$i][0], 12, 2), substr($list[$i][0], 4, 2),
  42.                        substr($list[$i][0], 6, 2),  substr($list[$i][0], 0, 4));
  43.     if($DayLimit && $i >= $MinEntries
  44.        && !$full && ($now - $editTime) > $DayLimit * 24 * 60 * 60)
  45.       { break; }
  46.  
  47.     $text = $text . html_category($list[$i][0], $list[$i][1],
  48.                                   $list[$i][2], $list[$i][3],
  49.                                   $list[$i][5]);
  50.     if($i < count($list) - 1)           // Don't put a newline on the last one.
  51.       { $text = $text . html_newline(); }
  52.   }
  53.  
  54.   if($i < count($list))
  55.     { $text = $text . html_fulllist($page, count($list)); }
  56.  
  57.   return $text;
  58. }
  59.  
  60. function catSort($p1, $p2)
  61.   { return strcmp($p2[0], $p1[0]); }
  62.  
  63. function sizeSort($p1, $p2)
  64.   { return $p2[4] - $p1[4]; }
  65.  
  66. function nameSort($p1, $p2)
  67.   { return strcmp($p1[1], $p2[1]); }
  68.  
  69. // Prepare a list of pages sorted by size.
  70. function view_macro_pagesize()
  71. {
  72.   global $pagestore;
  73.  
  74.   $first = 1;
  75.   $list = $pagestore->allpages();
  76.  
  77.   usort($list, 'sizeSort');
  78.  
  79.   $text = '';
  80.  
  81.   foreach($list as $page)
  82.   {
  83.     if(!$first)                         // Don't prepend newline to first one.
  84.       { $text = $text . "\n"; }
  85.     else
  86.       { $first = 0; }
  87.  
  88.     $text = $text .
  89.             $page[4] . ' ' . html_ref($page[1], $page[1]);
  90.   }
  91.  
  92.   return html_code($text);
  93. }
  94.  
  95. // Prepare a list of pages and those pages they link to.
  96. function view_macro_linktab()
  97. {
  98.   global $pagestore, $LkTbl;
  99.  
  100.   $lastpage = '';
  101.   $text = '';
  102.  
  103.   $q1 = $pagestore->dbh->query("SELECT page, link FROM $LkTbl ORDER BY page");
  104.   while(($result = $pagestore->dbh->result($q1)))
  105.   {
  106.     if($lastpage != $result[0])
  107.     {
  108.       if($lastpage != '')
  109.         { $text = $text . "\n"; }
  110.  
  111.       $text = $text . html_ref($result[0], $result[0]) . ' |';
  112.       $lastpage = $result[0];
  113.     }
  114.  
  115.     $text = $text . ' ' . html_ref($result[1], $result[1]);
  116.   }
  117.  
  118.   return html_code($text);
  119. }
  120.  
  121. // Prepare a list of pages with no incoming links.
  122. function view_macro_orphans()
  123. {
  124.   global $pagestore, $LkTbl;
  125.  
  126.   $text = '';
  127.   $first = 1;
  128.  
  129.   $pages = $pagestore->allpages();
  130.   usort($pages, 'nameSort');
  131.  
  132.   foreach($pages as $page)
  133.   {
  134.     $esc_page = addslashes($page[1]);
  135.     $q2 = $pagestore->dbh->query("SELECT page FROM $LkTbl " .
  136.                                  "WHERE link='$esc_page' AND page!='$esc_page'");
  137.     if(!($r2 = $pagestore->dbh->result($q2)) || empty($r2[0]))
  138.     {
  139.       if(!$first)                       // Don't prepend newline to first one.
  140.         { $text = $text . "\n"; }
  141.       else
  142.         { $first = 0; }
  143.  
  144.       $text = $text . html_ref($page[1], $page[1]);
  145.       if ($page[4] == 0 ) {
  146.         $text .= PARSE_EmptyToBeDeleted;
  147.       }
  148.     }
  149.   }
  150.  
  151.   return html_code($text);
  152. }
  153.  
  154. // Prepare a list of pages linked to that do not exist.
  155. function view_macro_wanted($args)
  156. {
  157.   global $pagestore, $LkTbl, $PgTbl;
  158.  
  159.   // Check for CurlyOptions, and split them
  160.   preg_match("/^(?:\s*{([^]]*)})?\s*(.*)$/", $args, $arg);
  161.   $options = split(',', $arg[1]);
  162.   $search = $arg[2];
  163.  
  164.   // Defaults
  165.   $displayOrigin = false;
  166.  
  167.   // Parse options
  168.   foreach ($options as $opt) {
  169.     list($name, $value) = split('=', $opt);
  170.     $name=strtolower($name);
  171.     if (preg_match("/^or/i", $name)) {  // ORigin  - Displays origin of wanted pages
  172.       $displayOrigin = !($value == 'false');
  173.     }
  174.   }
  175.  
  176.   $text = '';
  177.   $first = 1;
  178.  
  179.   $q1 = $pagestore->dbh->query("SELECT l.link, SUM(l.count) AS ct, l.page, p.title " .
  180.                                "FROM $LkTbl AS l LEFT JOIN $PgTbl AS p " .
  181.                                "ON l.link = p.title " .
  182.                                "WHERE p.title IS NULL " .
  183.                                "GROUP BY l.link " .
  184.                                "ORDER BY ct DESC, l.link");
  185.  
  186.  
  187.   while(($result = $pagestore->dbh->result($q1)))
  188.   {
  189.     if(!$first)                         // Don't prepend newline to first one.
  190.       { $text = $text . "\n"; }
  191.     else
  192.       { $first = 0; }
  193.  
  194.     if ($displayOrigin) {
  195.       if ($result[1] > 1) {
  196.          $q2 = $pagestore->dbh->query("SELECT l.page, l.link from $LkTbl as l ".
  197.                                       "WHERE l.link = '". $result[0] ."'");
  198.  
  199.          $deref = ' ' . PARSE_From . ' ';
  200.          while (($res2 = $pagestore->dbh->result($q2))) {
  201.            $deref .= html_url(editUrl($res2[0]), '?'.$res2[0]) . ', ';
  202.          }
  203.  
  204.          $deref = preg_replace("/, $/", "", $deref);
  205.       } else {
  206.          $deref = ' ' . PARSE_From . ' ' . 
  207.                   html_url(editURL($result[2]), '?'.$result[2]);
  208.       }
  209.     } else {
  210.       $deref = '';
  211.     }
  212.     $text = $text . '(' .
  213.             html_url(findURL($result[0]), $result[1]) .
  214.             ') ' . html_ref($result[0], $result[0]) . $deref;
  215.   }
  216.  
  217.   return html_code($text);
  218. }
  219.  
  220. // Do a textual search in list of page titles
  221. function view_macro_titlesearch($args)
  222. {
  223.   // Description of TitleSearch macro:
  224.   //   [[TitleSearch {options} search-pattern]]
  225.   // This macro searches for page-titles matching the searchpattern,
  226.   // and presents it according to options. The pattern may include
  227.   // ^ or $ to lock it against start or end, and otherwise it must 
  228.   // only contain alpha-characters (or '/'). The special pattern '*'
  229.   // matches every title.
  230.   //
  231.   // Legal options, capitalized unique prefix:
  232.   //   Class      : Sets the class of the list used for results
  233.   //   STyle      : Sets the style attribute
  234.   //   Delimiter  : Choose delimiter between text entries
  235.   //   Index      : Divides the list according to first character,
  236.   //                or first character after value of option
  237.   //   Oneline/List : Indicates to use line/list-markup
  238.   //
  239.   // Examples:  [[TitleSearch Pages$]]
  240.   //            [[TitleSearch *]]
  241.   //            [[TitleSearch {c=prelist} ^Tavi]]
  242.   //            [[TitleSearch {i=5} ^Tavi]]
  243.  
  244.   global $pagestore, $AlphaPtn;
  245.  
  246.   // Check for CurlyOptions, and split them
  247.   preg_match("/^(?:\s*{([^]]*)})?\s*(.*)$/", $args, $arg);
  248.   $options = split(',', $arg[1]);
  249.   $search = $arg[2];
  250.  
  251.   // Some defaults
  252.   $useDelim = ''; // Empty delimiter at the start, changed by options
  253.  
  254.   // Parse options
  255.   foreach ($options as $opt) {
  256.     list($name, $value) = split('=', $opt);
  257.     $name=strtolower($name);
  258.     if (preg_match("/^st/", $name)) { // STyle - Adds a style-attribute
  259.       $style = $value;
  260.       $listAttr = "style=\"$value\" ";
  261.     } else if ($name[0]=='c') {   // Class - Adds a class-attribute
  262.       $listAttr = "class=\"$value\" ";
  263.       if ($value == "prelist") { $useDelim = ''; }
  264.     } else if ($name[0]=='d') {   // Delimiter - Changes the delimiter used
  265.       $useDelim = $value;
  266.     } else if ($name[0]=='o') {   // Oneline - use line-markup
  267.       $useList = false;
  268.     } else if ($name[0]=='l') {   // List - use list-markup
  269.       $useList = true;
  270.     } else if ($name[0]=='i') {  // Index - Use heading to divide index
  271.       $showIndex = true; $level=2;
  272.       if (is_numeric($value)) {
  273.         $indexCharNo = $value -1;
  274.       } else {
  275.         $indexCharNo = 0;
  276.       }
  277.     }
  278.   }
  279.  
  280.   // Check for illegal characters to make search pattern safer against exploits
  281.   if ($search == '*') {  // Match every title 
  282.     $pattern = "."; 
  283.   } else if ( !preg_match("/^\^?(\/|$AlphaPtn)+\\$?$/", $search)) {
  284.      // Search can be locked at ^start and/or end$, and elsewise only
  285.      // contain Alpha-characters, digits not included
  286.      return "[[TitleSearch $args]]";
  287.   } else {
  288.     // $search validates, so use as is
  289.     $pattern=$search;
  290.   }
  291.  
  292.   if (!isset($useList) or !$useList) {
  293.     $useList = false;
  294.     $useDelim = ($useDelim) ? $useDelim : ', ';
  295.   }
  296.  
  297.   if ($showIndex) {
  298.     $lastIndexChar ='';
  299.   } else {
  300.     if ($useList) { $text = entity_list("*", 'start', $listAttr); };
  301.   }
  302.  
  303.   // Loop through all pagetitles
  304.   $list = $pagestore->allpages();
  305.   foreach($list as $page)
  306.   {
  307.     if (preg_match("|$pattern|", $page[1])) {
  308.       if ($showIndex && ($lastIndexChar != $page[1][$indexCharNo])) {
  309.         if ($lastIndexChar != '') {  // End previous list
  310.           if ($useDelim) {
  311.            $text = preg_replace("/" . preg_quote($useDelim) . "$/",
  312.                                 "\n", $text);
  313.           }
  314.           if ($useList) { $text .= entity_list("*", "end"); };
  315.         }
  316.  
  317.         // Add index-header
  318.         $text .= new_entity(array('head_start', $level)) .
  319.            substr($page[1], 0, $indexCharNo+1) .
  320.            new_entity(array('head_end', $level));
  321.  
  322.         if ($useList) {
  323.           // Start list again
  324.           $text .= entity_list("*", 'start', $listAttr);
  325.         }
  326.         $lastIndexChar = $page[1][$indexCharNo];
  327.       }
  328.  
  329.       if ($useList) { $text .= entity_listitem("*", "start"); };
  330.       $text .= sprintf("%s".$useDelim, html_ref($page[1], $page[1]));
  331.     }
  332.   }
  333.  
  334.   if ($useDelim) {
  335.     $text = preg_replace("/" . preg_quote($useDelim) . "$/", "\n", $text);
  336.   }
  337.   if ($useList) { $text .= entity_list("*", "end"); };
  338.  
  339.   return parse_elements($text);
  340. }
  341.  
  342. // Prepare a list of pages sorted by how many links they contain.
  343. function view_macro_outlinks()
  344. {
  345.   global $pagestore, $LkTbl;
  346.  
  347.   $text = '';
  348.   $first = 1;
  349.  
  350.   $q1 = $pagestore->dbh->query("SELECT page, SUM(count) AS ct FROM $LkTbl " .
  351.                                "GROUP BY page ORDER BY ct DESC, page");
  352.   while(($result = $pagestore->dbh->result($q1)))
  353.   {
  354.     if(!$first)                         // Don't prepend newline to first one.
  355.       { $text = $text . "\n"; }
  356.     else
  357.       { $first = 0; }
  358.  
  359.     $text = $text .
  360.             '(' . $result[1] . ') ' . html_ref($result[0], $result[0]);
  361.   }
  362.  
  363.   return html_code($text);
  364. }
  365.  
  366. // Prepare a list of pages sorted by how many links to them exist.
  367. function view_macro_refs()
  368. {
  369.   global $pagestore, $LkTbl, $PgTbl;
  370.  
  371.   $text = '';
  372.   $first = 1;
  373.  
  374. // It's not quite as straightforward as one would imagine to turn the
  375. // following code into a JOIN, since we want to avoid multiplying the
  376. // number of links to a page by the number of versions of that page that
  377. // exist.  If anyone has some efficient suggestions, I'd be welcome to
  378. // entertain them.  -- ScottMoonen
  379.  
  380.   $q1 = $pagestore->dbh->query("SELECT link, SUM(count) AS ct FROM $LkTbl " .
  381.                                "GROUP BY link ORDER BY ct DESC, link");
  382.   while(($result = $pagestore->dbh->result($q1)))
  383.   {
  384.     $esc_page = addslashes($result[0]);
  385.     $q2 = $pagestore->dbh->query("SELECT MAX(version) FROM $PgTbl " .
  386.                                  "WHERE title='$esc_page'");
  387.     if(($r2 = $pagestore->dbh->result($q2)) && !empty($r2[0]))
  388.     {
  389.       if(!$first)                       // Don't prepend newline to first one.
  390.         { $text = $text . "\n"; }
  391.       else
  392.         { $first = 0; }
  393.  
  394.       $text = $text . '(' .
  395.               html_url(findURL($result[0]), $result[1]) . ') ' .
  396.               html_ref($result[0], $result[0]);
  397.     }
  398.   }
  399.  
  400.   return html_code($text);
  401. }
  402.  
  403. // This macro inserts an HTML anchor into the text.
  404. function view_macro_anchor($args)
  405. {
  406.   preg_match('/^([A-Za-z][-A-Za-z0-9_:.]*)$/', $args, $result);
  407.  
  408.   if($result[1] != '')
  409.     { return html_anchor($result[1]); }
  410.   else
  411.     { return ''; }
  412. }
  413.  
  414. // This macro transcludes another page into a wiki page.
  415. function view_macro_transclude($args)
  416. {
  417.   global $pagestore, $ParseEngine, $ParseObject;
  418.   static $visited_array = array();
  419.   static $visited_count = 0;
  420.  
  421.   if(!validate_page($args))
  422.     { return '[[Transclude ' . $args . ']]'; }
  423.  
  424.   $visited_array[$visited_count++] = $ParseObject;
  425.   for($i = 0; $i < $visited_count; $i++)
  426.   {
  427.     if($visited_array[$i] == $args)
  428.     {
  429.       $visited_count--;
  430.       return '[[Transclude ' . $args . ']]';
  431.     }
  432.   }
  433.  
  434.   $pg = $pagestore->page($args);
  435.   $pg->read();
  436.   if(!$pg->exists)
  437.   {
  438.     $visited_count--;
  439.     return '[[Transclude ' . $args . ']]';
  440.   }
  441.  
  442.   $result = parseText($pg->text, $ParseEngine, $args);
  443.   $visited_count--;
  444.   return $result;
  445. }
  446. function view_macro_reflist($args) 
  447. {
  448.   return parse_elements(new_entity(array("reflist", $args)));
  449. }
  450. ?>
  451.