From 5096a8eada392847234c4291e05f05423b3639ed Mon Sep 17 00:00:00 2001
From: julmis <julmis>
Date: Sun, 30 Jan 2005 14:21:52 +0000
Subject: [PATCH] Adding <span class="nolink"> excluding.

---
 filter/activitynames/filter.php |  19 +++-
 mod/resource/filter.php         |  25 ++++--
 mod/wiki/filter.php             | 151 +++++++++++++++++++++++++++++++-
 3 files changed, 187 insertions(+), 8 deletions(-)

diff --git a/filter/activitynames/filter.php b/filter/activitynames/filter.php
index cc85b4720ee..edd7d3f40ba 100644
--- a/filter/activitynames/filter.php
+++ b/filter/activitynames/filter.php
@@ -16,7 +16,7 @@
         $modinfo = unserialize($course->modinfo);
 
         //Sort modinfo by name length
-        usort($modinfo,'comparemodulenamesbylength'); 
+        usort($modinfo,'comparemodulenamesbylength');
 
         if (!empty($modinfo)) {
             $cm = '';
@@ -39,7 +39,7 @@
         }
         return $text;
     }
-    
+
     function activity_link_names($text,$name,$href_tag_begin,$href_tag_end = "</a>") {
 
         $list_of_words_cp = strip_tags($name);
@@ -77,6 +77,18 @@
             $text = str_replace($excludes,array_keys($excludes),$text);
         }
 
+        //Now avoid searching inside the <span class="nolink">tag
+        // This style doesn't break in editor. See bug #2428
+        $nolinkspan = array();
+        preg_match_all('/<span class=\"nolink\">(.+?)<\/span>/is',$text,$list_of_span);
+        foreach (array_unique($list_of_span[0]) as $key=>$value) {
+            $nolinkspan['<%'.$key.'%>'] = $value;
+        }
+
+        if (!empty($nolinkspan)) {
+            $text = str_replace($nolinkspan,array_keys($nolinkspan),$text);
+        }
+
         //Now avoid searching inside links
         $links = array();
         preg_match_all('/<A[\s](.+?)>(.+?)<\/A>/is',$text,$list_of_links);
@@ -109,6 +121,9 @@
         if (!empty($excludes)) {
             $text = str_replace(array_keys($excludes),$excludes,$text);
         }
+        if (!empty( $nolinkspan)) {
+            $text = str_replace(array_keys($nolinkspan),$nolinkspan,$text);
+        }
         if (!empty($words)) {
             $text = str_replace(array_keys($words),$words,$text);
         }
diff --git a/mod/resource/filter.php b/mod/resource/filter.php
index 9f30c03ea5e..ccfc10e101e 100644
--- a/mod/resource/filter.php
+++ b/mod/resource/filter.php
@@ -19,9 +19,9 @@
             case 'mysql':
                 $as = '';
                 break;
-        }    
+        }
 
-        ///sorting by the lenght of the title in order to assure that large resources 
+        ///sorting by the lenght of the title in order to assure that large resources
         ///could be linked first, if they exist in the text to parse
 
         switch ($CFG->dbtype) {
@@ -33,7 +33,7 @@
                 $rbylenght = "";
             break;
         }
-            
+
         $resources = get_records_select("resource", "course = $courseid", "$rbylenght");
 
         if (!empty($resources)) {
@@ -44,7 +44,7 @@
                 }
                 $title = strip_tags($resource->name);
                 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/resource/view.php?id=$cm->id\">";
-                $currentname = $resource->name;                    
+                $currentname = $resource->name;
                 if ($currentname = trim($currentname)) {
                     //Avoid integers < 1000 to be linked. See bug 1441.
                     $intcurrent = intval($currentname);
@@ -56,7 +56,7 @@
         }
         return $text;
     }
-    
+
     function resource_link_names($text,$name,$href_tag_begin,$href_tag_end = "</a>") {
 
         $list_of_words_cp = strip_tags($name);
@@ -92,6 +92,18 @@
             $text = str_replace($excludes,array_keys($excludes),$text);
         }
 
+        //Now avoid searching inside the <span class="nolink">tag
+        // This style doesn't break in editor. See bug #2428
+        $nolinkspan = array();
+        preg_match_all('/<span class=\"nolink\">(.+?)<\/span>/is',$text,$list_of_span);
+        foreach (array_unique($list_of_span[0]) as $key=>$value) {
+            $nolinkspan['<%'.$key.'%>'] = $value;
+        }
+
+        if (!empty($nolinkspan)) {
+            $text = str_replace($nolinkspan,array_keys($nolinkspan),$text);
+        }
+
         //Now avoid searching inside links
         $links = array();
         preg_match_all('/<a[\s](.+?)>(.+?)<\/A>/is',$text,$list_of_links);
@@ -124,6 +136,9 @@
         if (!empty($excludes)) {
             $text = str_replace(array_keys($excludes),$excludes,$text);
         }
+        if (!empty( $nolinkspan)) {
+            $text = str_replace(array_keys($nolinkspan),$nolinkspan,$text);
+        }
         if (!empty($words)) {
             $text = str_replace(array_keys($words),$words,$text);
         }
diff --git a/mod/wiki/filter.php b/mod/wiki/filter.php
index ab1ace4bd5f..db93ca7a5eb 100755
--- a/mod/wiki/filter.php
+++ b/mod/wiki/filter.php
@@ -1 +1,150 @@
-<?PHP // $Id$
    //This function provides automatic linking to
    //wiki pages when its page title is found inside every Moodle text
    //It's based in the glosssary filter by Williams Castillo
    //Modifications by mchurch. Enjoy! :-)

    require_once($CFG->dirroot.'/mod/wiki/lib.php');

    function wiki_filter($courseid, $text) {

        global $CFG;

        if (empty($courseid)) {
            if ($site = get_site()) {
                $courseid = $site->id;
            }
        }

        if (!($course = get_record('course', 'id', $courseid))) {
            return $text;
        }

//      Get all wikis for this course.
        $wikis = wiki_get_course_wikis($courseid);
        if (empty($wikis)) {
            return $text;
        }

//      Walk through each wiki, and get entries.
        foreach ($wikis as $wiki) {
            if ($wiki_entries = wiki_get_entries($wiki)) {

//              Walk through each entry and get the pages.
                foreach ($wiki_entries as $wiki_entry) {
                    if ($wiki_pages = get_records('wiki_pages', 'wiki', $wiki_entry->id)) {

//                      Walk through each page and filter.
                        foreach ($wiki_pages as $wiki_page) {
                            $startlink = '<a class="autolink" title="Wiki" href="'
                                        .$CFG->wwwroot.'/mod/wiki/view.php?wid='.$wiki->id
                                        .'&amp;userid='.$wiki_entry->userid
                                        .'&amp;groupid='.$wiki_entry->groupid
                                        .'&amp;page='.$wiki_page->pagename.'">';
                            $text = wiki_link_names($text, $wiki_page->pagename, $startlink, '</a>');
                        }
                    }
                }
            }
        }

        return $text;
    }

    function wiki_link_names($text,$name,$href_tag_begin,$href_tag_end = "</a>") {

        $list_of_words_cp = strip_tags($name);

        $list_of_words_cp = trim($list_of_words_cp,'|');

        $list_of_words_cp = trim($list_of_words_cp);

        $list_of_words_cp = preg_quote($list_of_words_cp,'/');

        $invalidprefixs = "([a-zA-Z0-9])";
        $invalidsufixs  = "([a-zA-Z0-9])";

        //Avoid seaching in the string if it's inside invalidprefixs and invalidsufixs
        $words = array();
        $regexp = '/'.$invalidprefixs.'('.$list_of_words_cp.')|('.$list_of_words_cp.')'.$invalidsufixs.'/is';
        preg_match_all($regexp,$text,$list_of_words);

        foreach (array_unique($list_of_words[0]) as $key=>$value) {
            $words['<*'.$key.'*>'] = $value;
        }

        if (!empty($words)) {
            $text = str_replace($words,array_keys($words),$text);
        }

        //Now avoid searching inside the <nolink>tag
        $excludes = array();
        preg_match_all('/<nolink>(.+?)<\/nolink>/is',$text,$list_of_excludes);
        foreach (array_unique($list_of_excludes[0]) as $key=>$value) {
            $excludes['<+'.$key.'+>'] = $value;
        }

        if (!empty($excludes)) {
            $text = str_replace($excludes,array_keys($excludes),$text);
        }

        //Now avoid searching inside links
        $links = array();
        preg_match_all('/<A[\s](.+?)>(.+?)<\/A>/is',$text,$list_of_links);
        foreach (array_unique($list_of_links[0]) as $key=>$value) {
            $links['<@'.$key.'@>'] = $value;
        }

        if (!empty($links)) {
            $text = str_replace($links,array_keys($links),$text);
        }

        //Now avoid searching inside every tag
        $final = array();
        preg_match_all('/<(.+?)>/is',$text,$list_of_tags);
        foreach (array_unique($list_of_tags[0]) as $key=>$value) {
            $final['<|'.$key.'|>'] = $value;
        }

        if (!empty($final)) {
            $text = str_replace($final,array_keys($final),$text);
        }

        $text = preg_replace('/('.$list_of_words_cp.')/is', $href_tag_begin.'$1'.$href_tag_end,$text);

        //Now rebuild excluded areas
        if (!empty($final)) {
            $text = str_replace(array_keys($final),$final,$text);
        }

        if (!empty($links)) {
            $text = str_replace(array_keys($links),$links,$text);
        }

        if (!empty($excludes)) {
            $text = str_replace(array_keys($excludes),$excludes,$text);
        }

        if (!empty($words)) {
            $text = str_replace(array_keys($words),$words,$text);
        }

        return $text;
    }
?>
\ No newline at end of file
+<?PHP // $Id$
+    //This function provides automatic linking to
+    //wiki pages when its page title is found inside every Moodle text
+    //It's based in the glosssary filter by Williams Castillo
+    //Modifications by mchurch. Enjoy! :-)
+
+    require_once($CFG->dirroot.'/mod/wiki/lib.php');
+
+    function wiki_filter($courseid, $text) {
+
+        global $CFG;
+
+        if (empty($courseid)) {
+            if ($site = get_site()) {
+                $courseid = $site->id;
+            }
+        }
+
+        if (!($course = get_record('course', 'id', $courseid))) {
+            return $text;
+        }
+
+//      Get all wikis for this course.
+        $wikis = wiki_get_course_wikis($courseid);
+        if (empty($wikis)) {
+            return $text;
+        }
+
+//      Walk through each wiki, and get entries.
+        foreach ($wikis as $wiki) {
+            if ($wiki_entries = wiki_get_entries($wiki)) {
+
+//              Walk through each entry and get the pages.
+                foreach ($wiki_entries as $wiki_entry) {
+                    if ($wiki_pages = get_records('wiki_pages', 'wiki', $wiki_entry->id)) {
+
+//                      Walk through each page and filter.
+                        foreach ($wiki_pages as $wiki_page) {
+                            $startlink = '<a class="autolink" title="Wiki" href="'
+                                        .$CFG->wwwroot.'/mod/wiki/view.php?wid='.$wiki->id
+                                        .'&amp;userid='.$wiki_entry->userid
+                                        .'&amp;groupid='.$wiki_entry->groupid
+                                        .'&amp;page='.$wiki_page->pagename.'">';
+                            $text = wiki_link_names($text, $wiki_page->pagename, $startlink, '</a>');
+                        }
+                    }
+                }
+            }
+        }
+
+        return $text;
+    }
+
+    function wiki_link_names($text,$name,$href_tag_begin,$href_tag_end = "</a>") {
+
+        $list_of_words_cp = strip_tags($name);
+
+        $list_of_words_cp = trim($list_of_words_cp,'|');
+
+        $list_of_words_cp = trim($list_of_words_cp);
+
+        $list_of_words_cp = preg_quote($list_of_words_cp,'/');
+
+        $invalidprefixs = "([a-zA-Z0-9])";
+        $invalidsufixs  = "([a-zA-Z0-9])";
+
+        //Avoid seaching in the string if it's inside invalidprefixs and invalidsufixs
+        $words = array();
+        $regexp = '/'.$invalidprefixs.'('.$list_of_words_cp.')|('.$list_of_words_cp.')'.$invalidsufixs.'/is';
+        preg_match_all($regexp,$text,$list_of_words);
+
+        foreach (array_unique($list_of_words[0]) as $key=>$value) {
+            $words['<*'.$key.'*>'] = $value;
+        }
+
+        if (!empty($words)) {
+            $text = str_replace($words,array_keys($words),$text);
+        }
+
+        //Now avoid searching inside the <nolink>tag
+        $excludes = array();
+        preg_match_all('/<nolink>(.+?)<\/nolink>/is',$text,$list_of_excludes);
+        foreach (array_unique($list_of_excludes[0]) as $key=>$value) {
+            $excludes['<+'.$key.'+>'] = $value;
+        }
+
+        if (!empty($excludes)) {
+            $text = str_replace($excludes,array_keys($excludes),$text);
+        }
+
+        //Now avoid searching inside the <span class="nolink">tag
+        // This style doesn't break in editor. See bug #2428
+        $nolinkspan = array();
+        preg_match_all('/<span class=\"nolink\">(.+?)<\/span>/is',$text,$list_of_span);
+        foreach (array_unique($list_of_span[0]) as $key=>$value) {
+            $nolinkspan['<%'.$key.'%>'] = $value;
+        }
+
+        if (!empty($nolinkspan)) {
+            $text = str_replace($nolinkspan,array_keys($nolinkspan),$text);
+        }
+
+        //Now avoid searching inside links
+        $links = array();
+        preg_match_all('/<A[\s](.+?)>(.+?)<\/A>/is',$text,$list_of_links);
+        foreach (array_unique($list_of_links[0]) as $key=>$value) {
+            $links['<@'.$key.'@>'] = $value;
+        }
+
+        if (!empty($links)) {
+            $text = str_replace($links,array_keys($links),$text);
+        }
+
+        //Now avoid searching inside every tag
+        $final = array();
+        preg_match_all('/<(.+?)>/is',$text,$list_of_tags);
+        foreach (array_unique($list_of_tags[0]) as $key=>$value) {
+            $final['<|'.$key.'|>'] = $value;
+        }
+
+        if (!empty($final)) {
+            $text = str_replace($final,array_keys($final),$text);
+        }
+
+        $text = preg_replace('/('.$list_of_words_cp.')/is', $href_tag_begin.'$1'.$href_tag_end,$text);
+
+        //Now rebuild excluded areas
+        if (!empty($final)) {
+            $text = str_replace(array_keys($final),$final,$text);
+        }
+
+        if (!empty($links)) {
+            $text = str_replace(array_keys($links),$links,$text);
+        }
+
+        if (!empty($excludes)) {
+            $text = str_replace(array_keys($excludes),$excludes,$text);
+        }
+
+        if (!empty( $nolinkspan)) {
+            $text = str_replace(array_keys($nolinkspan),$nolinkspan,$text);
+        }
+
+        if (!empty($words)) {
+            $text = str_replace(array_keys($words),$words,$text);
+        }
+
+        return $text;
+    }
+?>
\ No newline at end of file