Project

General

Profile

« Previous | Next » 

Revision 1993

Added by darkviper over 10 years ago

! /module/output_filter/filters/filterWbLink added extended format to [wblinkXX] - tag
+ /framework/WbLinkAbstract.php added abstract class/interface for filterWbLink

View differences:

filterWbLink.php
1 1
<?php
2 2
/*
3
 * replace all "[wblink{page_id}]" with real links
4
 * @param string &$content : reference to global $content
5
 * @return void
6
 * @history 100216 17:00:00 optimise errorhandling, speed, SQL-strict
3
 * @param  string $content : contains the full content of the current page
4
 * @return string
5
 * @description replace all "[wblink{page_id}{?addon=n&item=n...}]" with real links to accessfiles<br />
6
 *     All modules must offer the class 'WbLink'(implementing 'WbLinkAbstract'), to be taken into consideration.
7 7
 */
8
	function doFilterWbLink($content)
8
	function doFilterWbLink($sContent)
9 9
	{
10
		global $database, $wb;
11
		$replace_list = array();
12
		$pattern = '/\[wblink([0-9]+)\]/isU';
13
		if(preg_match_all($pattern,$content,$ids))
10
		$oDb  = WbDatabase::getInstance();
11
		$oReg = WbAdaptor::getInstance();
12
		$aReplaceList = array();
13
		$sPattern = '/\[wblink([0-9]+)\??([^\]]*)\]/is';
14
		if(preg_match_all($sPattern,$sContent,$aMatches))
14 15
		{
15
			foreach($ids[1] as $key => $page_id) {
16
				$replace_list[$page_id] = $ids[0][$key];
17
			}
18
			foreach($replace_list as $page_id => $tag)
16
		// iterate through all found matches
17
			foreach($aMatches[0] as $iKey => $sKeyString )
19 18
			{
20
				$sql = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.(int)$page_id;
21
				$link = $database->get_one($sql);
22
				if(!is_null($link)) {
23
					$link = $wb->page_link($link);
24
					$content = str_replace($tag, $link, $content);
19
				$aReplaceList[$sKeyString] = array();
20
			// use original Tag to save PageId
21
				$aReplaceList[$sKeyString]['PageId'] = $aMatches[1][$iKey];
22
			// if there are additional arguments given
23
				if($aMatches[2][$iKey])
24
				{
25
					$aReplaceList[$sKeyString]['Args'] = array();
26
					$aArgs = explode('&', $aMatches[2][$iKey]);
27
					foreach($aArgs as $sArgument)
28
					{
29
						$aTmp = explode('=', $sArgument);
30
						$aReplaceList[$sKeyString]['Args'][$aTmp[0]] = $aTmp[1];
31
					}
25 32
				}
26 33
			}
34
			if(sizeof($aReplaceList) > 0)
35
			{ // iterate list if replacements are available
36
				foreach($aReplaceList as $sKey => $aReplacement)
37
				{
38
				// set link on failure ('#' means, still stay on current page)
39
					$aReplaceList[$sKey] = '#';
40
				// handle normal pages links
41
					if(!isset($aReplacement['Args'])) 
42
					{
43
					// read corresponding link from table 'pages'
44
						$sql = 'SELECT `link` FROM `'.$oDb->TablePrefix.'pages` WHERE `page_id` = '.(int)$aReplacement['PageId'];
45
						if(($sLink = $oDb->get_one($sql)))
46
						{
47
							$sLink = trim(str_replace('\\', '/', $sLink), '/');
48
						// test if valid accessfile is available
49
							if(is_readable($oReg->AppPath.$oReg->PagesDir.$sLink.$oReg->PageExtension))
50
							{
51
							// create absolute URL
52
								$aReplaceList[$sKey] = $oReg->AppUrl.$oReg->PagesDir.$sLink.$oReg->PageExtension;
53
							}
54
						}
55
					// handle links of modules
56
					}else 
57
					{
58
					// build name of the needed class
59
						$sClass = 'm_'.$aReplacement['Args']['addon'].'_WbLink';
60
					// remove addon name from replacement array
61
						unset($aReplacement['Args']['addon']);
62
						if(class_exists($sClass))
63
						{
64
						// instantiate class
65
							$oWbLink = new $sClass();
66
						// the class must implement the interface
67
							if($oWbLink instanceof WbLinkAbstract)
68
							{
69
							// create real link from replacement data
70
								$aReplaceList[$sKey] = $oWbLink->makeLinkFromTag($aReplacement);
71
							}
72
						}
73
					}
74
				}
75
			// extract indexes into a new array
76
				$aSearchList = array_keys($aReplaceList);
77
			// replace all identified wblink-tags in content
78
				$sContent = str_replace($aSearchList, $aReplaceList, $sContent);
79
			}
27 80
		}
28
		return $content;
81
		return $sContent;
29 82
	}

Also available in: Unified diff