[evolvis-commits] r17778: extend util_tasktracker_links (and by extension util_uri_grabber)↵ to handle [Tnnn], [forum:nnn] and [wiki:name] links, similar to↵ that stuff from utils_crossref.php except we use MediaWiki

mirabilos at evolvis.org mirabilos at evolvis.org
Tue Dec 6 20:26:19 CET 2011


Author: mirabilos
Date: 2011-12-06 20:26:19 +0100 (Tue, 06 Dec 2011)
New Revision: 17778

Modified:
   trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php
Log:
extend util_tasktracker_links (and by extension util_uri_grabber)
to handle [Tnnn], [forum:nnn] and [wiki:name] links, similar to
that stuff from utils_crossref.php except we use MediaWiki


Modified: trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php
===================================================================
--- trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php	2011-12-06 17:01:12 UTC (rev 17777)
+++ trunk/gforge_base/evolvisforge-5.1/src/common/include/utils.php	2011-12-06 19:26:19 UTC (rev 17778)
@@ -58,7 +58,7 @@
     "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
     return "$"."apr1"."$".$salt."$".$tmp;
 }
- 
+
 /**
  * is_utf8($string) - utf-8 detection
  *
@@ -91,11 +91,11 @@
 }
 
 /**
- * removeCRLF() - remove any Carriage Return-Line Feed from a string. 
+ * removeCRLF() - remove any Carriage Return-Line Feed from a string.
  * That function is useful to remove the possibility of a CRLF Injection when sending mail
  * All the data that we will send should be passed through that function
  *
- * @param	   string  The string that we want to empty from any CRLF 
+ * @param	   string  The string that we want to empty from any CRLF
  */
 function util_remove_CRLF($str) {
 	return strtr($str, "\015\012", '  ');
@@ -173,8 +173,8 @@
 	if (!$from) {
 		$from='noreply@'.forge_get_config('web_host');
 	}
-	
 
+
 	$charset = _('UTF-8');
 	if (!$charset) {
 		$charset = 'UTF-8';
@@ -197,7 +197,7 @@
 		"\nContent-type: text/$type; charset=$charset".
 		"\n\n".
 		util_convert_body($body, $charset);
-	
+
 	if (!forge_get_config('sendmail_path')){
 		$sys_sendmail_path="/usr/sbin/sendmail";
 	}
@@ -222,7 +222,7 @@
 				$name,$charset,"UTF-8")).
 			"?=";
 	}
-	
+
 	return $name." <".$email."> ";
 }
 
@@ -268,7 +268,7 @@
 	if (!function_exists('mb_convert_encoding') || $charset == 'UTF-8') {
 		return $str;
 	}
-	
+
 	return mb_convert_encoding($str,$charset,"UTF-8");
 }
 
@@ -449,7 +449,7 @@
 					if ($code <= 0x7F ||
 					    $code >= 0xC0) {
 						//Here is single byte character
-						//or head of multi byte character  
+						//or head of multi byte character
 						return $wrap;
 					}
 					//Do not break multi byte character
@@ -509,8 +509,8 @@
  *
  */
 function util_make_links ($data='') {
-	if(empty($data)) { 
-		return $data; 
+	if(empty($data)) {
+		return $data;
 	}
 	$lines = explode("\n",$data);
 	$newText = "";
@@ -752,14 +752,14 @@
 				$headersCellData[] = array($fieldName);
 			}
 		}
-		
+
 		/*  Create the title  */
 		if(strlen($title) > 0) {
 			$titleCellData = array();
 			$titleCellData[] = array($title, 'colspan="'.count($headersCellData).'"');
 			echo $HTML->multiTableRow('', $titleCellData, TRUE);
 		}
-		
+
 		/* Display the headers */
 		if($displayHeaders) {
 			echo $HTML->multiTableRow('', $headersCellData, TRUE);
@@ -834,10 +834,10 @@
 */
 function validate_emails ($addresses, $separator=',') {
 	if (strlen($addresses) == 0) return array();
-	
+
 	$emails = explode($separator, $addresses);
 	$ret 	= array();
-	
+
 	if (is_array($emails)) {
 		foreach ($emails as $email) {
 			$email = trim($email);		// This is done so we can validate lists like "a at b.com, c at d.com"
@@ -913,7 +913,7 @@
  * @version        1.0
  * @param int       bytes   is the size
  * @param bool     base10  enable base 10 representation, otherwise
- *                 default base 2  is used  
+ *                 default base 2  is used
  * @param int       round   number of fractional digits
  * @param array     labels  strings associated to each 2^10 or
  *                  10^3(base10==true) multiple of base units
@@ -976,12 +976,12 @@
     $chunksize = 1*(1024*1024); // 1MB chunks
     $buffer = '';
     $byteCounter = 0;
-    
+
     $handle = fopen($filename, 'rb');
     if ($handle === false) {
         return false;
     }
-    
+
     ob_start () ;
     while (!feof($handle)) {
 	    $buffer = fread($handle, $chunksize);
@@ -1028,7 +1028,7 @@
     if (util_is_dot_or_dotdot($sub_dir))
       return true;
   }
-  
+
   return false;
 }
 
@@ -1057,7 +1057,7 @@
 
 /**
  * Constructs the forge's URL prefix out of forge_get_config('url_prefix')
- * 
+ *
  * @return string
  */
 function normalized_urlprefix () {
@@ -1065,7 +1065,7 @@
 	$prefix = preg_replace ("/^\//", "", $prefix) ;
 	$prefix = preg_replace ("/\/$/", "", $prefix) ;
 	$prefix = "/$prefix/" ;
-	if ($prefix == '//') 
+	if ($prefix == '//')
 		$prefix = '/' ;
 	return $prefix ;
 }
@@ -1085,7 +1085,7 @@
 
 /**
  * Construct full URL from a relative path
- * 
+ *
  * @param string $path
  * @return string URL
  */
@@ -1107,7 +1107,7 @@
 
 /**
  * Construct proper (relative) URI (prepending prefix)
- * 
+ *
  * @param string $path
  * @return string URI
  */
@@ -1134,7 +1134,7 @@
 
 /**
  * Create an HTML link to a user's profile page
- * 
+ *
  * @param string $username
  * @param int $user_id
  * @param string $text
@@ -1147,7 +1147,7 @@
 /**
  * Display username with link to a user's profile page
  * and icon face if possible.
- * 
+ *
  * @param string $username
  * @param int $user_id
  * @param string $text
@@ -1175,7 +1175,7 @@
 
 /**
  * Create URL for users' profile pages
- * 
+ *
  * @param string $username
  * @param int $user_id
  * @return string URL
@@ -1201,7 +1201,7 @@
 
 /**
  * Create URL for a project's page
- * 
+ *
  * @param string $groupame
  * @param int $group_id
  * @return string
@@ -1241,7 +1241,7 @@
 			return false;
 		}
 	}
-		
+
 	// Check if a forum with same name already exists
 	$ff = new ForumFactory($group);
 	if (!$ff || !is_object($ff) || $ff->isError()) {
@@ -1258,7 +1258,7 @@
 			}
 		}
 	}
-	
+
 	// Email is available
 	return true;
 }
@@ -1285,7 +1285,7 @@
 			    !isset ($result[$k]) || !is_array ($result[$k])) {
 				$result[$k] = $v ;
 			}
-			
+
 			$result[$k] = array_replace_recursive ($result[$k],
 							       $v) ;
 		}
@@ -1578,9 +1578,19 @@
 	$s = str_replace("\x01", "\x02X\x02", str_replace("\x02", "\x02Y\x02",
 	    $unencoded_string));
 	/* replace all URIs with ^AURI^A */
-	$s = preg_replace(
-	    '|([a-zA-Z][a-zA-Z0-9+.-]*:[#0-9a-zA-Z;/?:@&=+$,_.!~*\'()%-]+)|',
-	    "\x01\$1\x01", $s);
+	$s = preg_replace_callback(
+	    '|(.?)([a-zA-Z][a-zA-Z0-9+.-]*):([#0-9a-zA-Z;/?:@&=+$,_.!~*\'()%-]+)(.?)|',
+	    create_function('$matches', '
+		if ($matches[1] == "[" && $matches[4] == "]") {
+			/* but not forum/wiki internal links */
+			if ($matches[2] == "forum" ||
+			    $matches[2] == "wiki") {
+				return $matches[0];
+			}
+		}
+		return $matches[1] . "\x01" . $matches[2] . ":" . $matches[3] .
+		    "\x01" . $matches[4];
+	    '), $s);
 	if (!$s)
 		return htmlspecialchars($unencoded_string, ENT_QUOTES, "UTF-8");
 	/* encode the string */
@@ -1639,25 +1649,90 @@
 }
 
 function util_tasktracker_links($s) {
-	global $gfcommon;
+	global $gfcommon, $group_id;
 	require_once $gfcommon.'pm/ProjectTaskSqlQueries.php';
-	$t = preg_replace_callback('|\[#([0-9]+)]|', create_function(
-	    '$matches', '
-		$r = tasktracker_gettype($matches[1]);
-		if (!$r)
-			return $matches[0];
+
+	if (isset($group_id) && ($grp = group_get_object($group_id)) &&
+	    ($prj = $grp->getUnixName())) {
+		$t = preg_replace_callback('/\[wiki:([ -"$-;=?-Z\\^-z~]+)\]/',
+		    create_function('$matches', '
+			$p = ' . "'$prj'" . ';
+			$w = $matches[1];
+			$w = html_entity_decode($w, ENT_QUOTES, "UTF-8");
+			$l = util_make_url("/plugins/mediawiki/wiki/" .
+			    $p . "/index.php/" . urlencode($w));
+			return "[" . html_e("a", array(
+				"href" => util_html_secure($l),
+			    ), "wiki:" . htmlspecialchars($w, ENT_QUOTES,
+			    "UTF-8")) . "]";
+		    '), $s);
+		if ($t) {
+			$s = $t;
+		}
+	}
+
+	$t = preg_replace_callback('/\[(#|T|forum:)([0-9]+)]/',
+	    create_function('$matches', '
+		$original = $matches[0];
+		$mid = (int)$matches[2];
+		if (!$mid) {
+			return $original;
+		}
+		$doselect = true;
+		if ($matches[1] == "T") {
+			$sname = "summary";
+			$tname = "project_task";
+			$iname = "project_task_id";
+			$r = array("is_a" => "tid");
+		} else if ($matches[1] == "forum:") {
+			$sname = "subject";
+			$tname = "forum";
+			$iname = "msg_id";
+			$r = array("is_a" => "msgid");
+		} else if ($mid < tasktracker_getminid()) {
+			$sname = "summary";
+			$tname = "artifact";
+			$iname = "artifact_id";
+			$r = array("is_a" => "aid");
+		} else {
+			$doselect = false;
+			$r = tasktracker_gettype($matches[2]);
+			if (!$r) {
+				return $original;
+			}
+		}
+		if ($doselect) {
+			/*
+			 * I think we cannot avoid the string concatenation
+			 * here because substitution only affects arguments
+			 */
+			$res = db_query_params("SELECT " . $sname . " FROM " .
+			    $tname . " WHERE " . $iname . "=$1;", array(
+				$mid,
+			    ));
+			if (db_numrows($res) != 1) {
+				return $original;
+			}
+			$row = db_fetch_array($res);
+			$r["summary"] = $row[$sname];
+		}
+
 		if ($r["is_a"] == "aid") {
-			$p = "/tracker";
-			$q = " (" . _("Bug/FR") . ")";
+			$linkf = "/tracker/t_follow.php/%d";
+			$textf = _("%s (Task)");
 		} else if ($r["is_a"] == "tid") {
-			$p = "/pm";
-			$q = " (" . _("Task") . ")";
-		} else
-			return $matches[0];
-		return "[<a href=\"" . util_make_url($p .
-		    "/t_follow.php/" . $matches[1]) .
-		    "\" title=\"" . util_html_secure($r["summary"]) . $q .
-		    "\">#" . $matches[1] . "</a>]";
+			$linkf = "/pm/t_follow.php/%d";
+			$textf = _("%s (Bug/FR)");
+		} else if ($r["is_a"] == "msgid") {
+			$linkf = "/forum/message.php?msg_id=%d";
+			$textf = _("%s (Forum Message)");
+		} else {
+			return $original;
+		}
+		return "[" . html_e("a", array(
+			"href" => util_make_url(sprintf($linkf, $mid)),
+			"title" => util_html_secure($r["summary"]),
+		    ), $matches[1] . $mid) . "]";
 	    '), $s);
 	if ($t)
 		return $t;



More information about the evolvis-commits mailing list