liberty
[ class tree: liberty ] [ index: liberty ] [ all elements ]

Source for file filter.bitlinks.php

Documentation is available at filter.bitlinks.php

  1. <?php
  2. /**
  3.  * @version  $Header$
  4.  * @package  liberty
  5.  */
  6.  
  7. /**
  8.  * definitions ( guid character limit is 16 chars )
  9.  */
  10. define'PLUGIN_GUID_FILTERWIKILINKS''filterbitlinks' );
  11.  
  12. global $gLibertySystem;
  13.  
  14. $pluginParams array (
  15.     'title'               => 'WikiLinks',
  16.     'description'         => 'If you use links of the format ((Wiki Page)) this filter will convert that to a link to a wiki page entitled <em>Wiki Page</em>',
  17.     'auto_activate'       => TRUE,
  18.     'plugin_type'         => FILTER_PLUGIN,
  19.  
  20.     // filter functions
  21.     'presplit_function'   => 'bitlinks_prefilter',
  22.     'preparse_function'   => 'bitlinks_prefilter',
  23.     'postsplit_function'  => 'bitlinks_postfilter',
  24.     'postparse_function'  => 'bitlinks_postfilter',
  25.     'poststore_function'  => 'bitlinks_storefilter',
  26.     'expunge_function'    => 'bitlinks_expungefilter',
  27. );
  28. $gLibertySystem->registerPluginPLUGIN_GUID_FILTERWIKILINKS$pluginParams );
  29.  
  30. define'WIKI_WORDS_REGEX''[A-z0-9]{2}[\w\d_\-]+[A-Z_][\w\d_\-]+[A-z0-9]+' );
  31.  
  32. /**
  33.  * bitlinks_prefilter
  34.  *
  35.  * @param array $pData 
  36.  * @param array $pFilterHash 
  37.  * @param array $pObject 
  38.  * @access public
  39.  * @return void 
  40.  */
  41. function bitlinks_prefilter&$pData&$pFilterHash$pObject {
  42.     static $sBitLinks;
  43.     ifempty$sBitLinks )) {
  44.         $sBitLinks new BitLinks();
  45.     }
  46.  
  47.     // extract ((Page|Description)) type links that they don't enter the parser.
  48.     // these can cause problems in various places such as tiki tables due to the |
  49.     preg_match_all"/\({2}({$sBitLinks->mWikiWordRegex})\|(.+?)\){2}/"$pData$protected );
  50.  
  51.     if!empty$protected )) {
  52.         foreach$protected[0as $i => $prot {
  53.             $key md5mt_rand() );
  54.             $pFilterHash['bitlinks']['replacements'][$key$protected[0][$i];
  55.             $pData str_replace$prot$key$pData );
  56.         }
  57.     }
  58. }
  59.  
  60. /**
  61.  * convert wiki links to html links e.g.: ((Wiki Page)) --> <a href="/wiki/Wiki+Page">Wiki Page</a>
  62.  *
  63.  * @param string $pData 
  64.  * @param array $pFilterHash 
  65.  * @param object $pObject 
  66.  * @access public
  67.  * @return updated data string
  68.  */
  69. function bitlinks_postfilter&$pData&$pFilterHash$pObject {
  70.     static $sBitLinks;
  71.     ifempty$sBitLinks )) {
  72.         $sBitLinks new BitLinks();
  73.     }
  74.  
  75.     // first we need to put the ((Page|Description)) type links back in that we can parse them below
  76.     if!empty$pFilterHash['bitlinks']['replacements')) {
  77.         foreach$pFilterHash['bitlinks']['replacements'as $key => $replace {
  78.             $pData str_replace$key$replace$pData );
  79.         }
  80.     }
  81.  
  82.     $pData $sBitLinks->parseLinks$pData$pFilterHash$pObject );
  83. }
  84.  
  85. /**
  86.  * store links to existing wiki pages in the database
  87.  *
  88.  * @param string $pData 
  89.  * @param array $pFilterHash 
  90.  * @param object $pObject 
  91.  * @access public
  92.  * @return data string
  93.  */
  94. function bitlinks_storefilter&$pData&$pFilterHash$pObject {
  95.     global $gBitSystem;
  96.     static $sBitLinks;
  97.     ifempty$sBitLinks )) {
  98.         $sBitLinks new BitLinks();
  99.     }
  100.     $sBitLinks->storeLinks$pData$pFilterHash );
  101.  
  102.     // if the title of this object was changed, we need to update links to it
  103.     if(
  104.         $gBitSystem->isPackageActive'wiki' )
  105.         && $pObject->mContentTypeGuid == BITPAGE_CONTENT_TYPE_GUID
  106.         && !empty$pFilterHash['title')
  107.         && !empty$pObject->mInfo['title')
  108.         && $pFilterHash['title'!= $pObject->mInfo['title']
  109.     {
  110.         $sBitLinks->renameLinks$pObject->mContentId$pObject->mInfo['title']$pFilterHash['title');
  111.     }
  112. }
  113.  
  114. /**
  115.  * expunge bitlinks in the database
  116.  *
  117.  * @param string $pData 
  118.  * @param array $pFilterHash 
  119.  * @param object $pObject 
  120.  * @access public
  121.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  122.  */
  123. function bitlinks_expungefilter&$pData&$pFilterHash$pObject {
  124.     static $sBitLinks;
  125.     ifempty$sBitLinks )) {
  126.         $sBitLinks new BitLinks();
  127.     }
  128.     $sBitLinks->expungeLinks$pObject->mContentId );
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. /**
  136.  * BitLinks class
  137.  *
  138.  * @package liberty
  139.  * @uses BitBase
  140.  */
  141. class BitLinks extends BitBase {
  142.     /**
  143.      * mLinks
  144.      *
  145.      * @var array of links pointing to this page
  146.      * @access public
  147.      */
  148.     var $mLinks = NULL;
  149.  
  150.     /**
  151.      * Initiate class
  152.      *
  153.      * @access public
  154.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  155.      */
  156.     function BitLinks({
  157.         parent::__construct();
  158.  
  159.         global $gBitSystem;
  160.         if$gBitSystem->getConfig'wiki_page_regex''strict' == 'strict' {
  161.             $this->mWikiWordRegex '([A-Za-z0-9_])([\'\.: A-Za-z0-9_\-])*([\.:A-Za-z0-9_])';
  162.         elseif$gBitSystem->getConfig'wiki_page_regex''strict' == 'full' {
  163.             $this->mWikiWordRegex '([A-Za-z0-9_]|[\x80-\xFF])([\'\.: A-Za-z0-9_\-]|[\x80-\xFF])*([\.:A-Za-z0-9_]|[\x80-\xFF])';
  164.         else {
  165.             // This is just evil. The middle section means "anything, as long
  166.             // as it's not a | and isn't followed by ))". -rlpowell
  167.             $this->mWikiWordRegex '([^|\(\)])([^|](?!\)\)))*?([^|\(\)])';
  168.         }
  169.  
  170.         // append anchor to regex
  171.         $this->mWikiWordRegex .= "(#\w+)?";
  172.     }
  173.  
  174.     /**
  175.      * Get all pages linking to a given content id
  176.      *
  177.      * @param array $pContentId 
  178.      * @access public
  179.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  180.      */
  181.     function getAllPages$pContentId {
  182.         global $gBitSystem;
  183.         $ret array();
  184.         if$gBitSystem->isPackageActive'wiki' && @BitBase::verifyId$pContentId )) {
  185.             $query "SELECT `page_id`, lc.`content_id`, lc.`last_modified`, lc.`title`, lcds.`data` AS `summary`
  186.                 FROM `".BIT_DB_PREFIX."liberty_content_links` lcl
  187.                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lcl.`to_content_id`=lc.`content_id` )
  188.                 INNER JOIN `".BIT_DB_PREFIX."wiki_pages` wp ON( wp.`content_id`=lc.`content_id` )
  189.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_data` lcds ON (lc.`content_id` = lcds.`content_id` AND lcds.`data_type`='summary')
  190.                 WHERE lcl.`from_content_id`=? ORDER BY lc.`title`";
  191.             if$result $this->mDb->query$queryarray$pContentId ))) {
  192.                 $lastTitle '';
  193.                 while$row $result->fetchRow() ) {
  194.                     ifarray_key_existsstrtolower$row['title')$ret )) {
  195.                         $row['description'tra'Multiple pages with this name' );
  196.                     }
  197.                     $ret[strtolower$row['title')$row;
  198.                 }
  199.             }
  200.         }
  201.         return $ret;
  202.     }
  203.  
  204.     /**
  205.      * see if page has already been created and stored
  206.      *
  207.      * @param array $pTitle title of the page
  208.      * @param array $pObject current object
  209.      * @param array $pContentId content_id of the current page - sometimes we don't have the object but a content_id to work with
  210.      * @access public
  211.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  212.      */
  213.     function pageExists$pTitle$pObject$pContentId {
  214.         global $gBitSystem;
  215.  
  216.         // only update this hash once - this is initiated as NULL and will be set to at least array after first call
  217.         if$this->mLinks === NULL {
  218.             $this->mLinks = $this->getAllPages$pContentId );
  219.         }
  220.  
  221.         $ret FALSE;
  222.         if!empty$pTitle && !empty$this->mLinks )) {
  223.             ifarray_key_existsstrtolower$pTitle )$this->mLinks )) {
  224.                 $ret $this->mLinks[strtolower$pTitle )];
  225.             }
  226.         }
  227.  
  228.         // final attempt to get page details
  229.         ifempty$ret && !empty$pObject && $gBitSystem->isPackageActive'wiki' ) ) {
  230.             require_onceWIKI_PKG_PATH.'BitPage.php' );
  231.             if$ret BitPage::pageExists$pTitleFALSE$pContentId )) {
  232.                 ifcount$ret {
  233.                     $ret[0]['description'tra'Multiple pages with this name' );
  234.                 }
  235.                 $ret $ret[0];
  236.             }
  237.         }
  238.         return $ret;
  239.     }
  240.  
  241.     /**
  242.      * extractWikiWords
  243.      *
  244.      * @param string $pData 
  245.      * @access public
  246.      * @return array of wiki words in the data string
  247.      */
  248.     function extractWikiWords$pData {
  249.         global $gBitSystem;
  250.         // we need to remove text that might contain unexpected wiki words
  251.         $protect array(
  252.             "!<a\b[^>]*>.*?</a>!si"// links
  253.             "!<[^>]*>!",             // any html tags
  254.         );
  255.         $tmpData preg_replace$protect""$pData );
  256.  
  257.         $words1[1$words2[1$words3[1array();
  258.         preg_match_all"/\({2}($this->mWikiWordRegex)\){2}/"$tmpData$words2 );
  259.         preg_match_all"/\({2}($this->mWikiWordRegex)\|(.+?)\){2}/"$tmpData$words3 );
  260.         if$gBitSystem->isFeatureActive'wiki_words' )) {
  261.             preg_match_all'/\b('.WIKI_WORDS_REGEX.')\b/'$tmpData$words1 );
  262.         }
  263.         return array_uniquearray_merge$words1[1]$words2[1]$words3[1));
  264.     }
  265.  
  266.     /**
  267.      * convert wiki links to html links
  268.      *
  269.      * @param string $pData 
  270.      * @param array $pParamHash 
  271.      * @param object $pObject 
  272.      * @access public
  273.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  274.      */
  275.     function parseLinks$pData$pParamHash$pObject {
  276.         global $gBitSystem;
  277.  
  278.         // if wiki isn't active, there isn't much we can do here
  279.         if!$gBitSystem->isPackageActive'wiki' )) {
  280.             return $pData;
  281.         }
  282.  
  283.         // fetch BitPage in case it hasn't been loaded yet
  284.         require_onceWIKI_PKG_PATH.'BitPage.php' );
  285.  
  286.         // We need to remove ))WikiWords(( before links get made.
  287.         // users just need to be strict about not inserting spaces between
  288.         // words and brackets
  289.         preg_match_all"!\){2}(".WIKI_WORDS_REGEX.")\({2}!"$pData$protected );
  290.  
  291.         // this array is used to fill the text with temporary placeholders that get replaced back in further down
  292.         $replacements array();
  293.  
  294.         if!empty$protected )) {
  295.             foreach$protected[0as $i => $prot {
  296.                 $key md5mt_rand() );
  297.                 $replacements[$key$protected[1][$i];
  298.                 $pData str_replace$prot$key$pData );
  299.             }
  300.         }
  301.  
  302.         // Process ((Wiki Page|Wiki Page Description)) type links first. Here 
  303.         // we don't handle plurals and the like since the user should know what 
  304.         // he's linking to when using these links
  305.         preg_match_all"/\({2}({$this->mWikiWordRegex})\|(.+?)\){2}/", $pData, $pages );
  306.         for( $i = 0; $i < count( $pages[1] ); $i++ ) {
  307.             $page = str_replace( $pages[5][$i], "", $pages[1][$i] );
  308.             $exists = $this->pageExists$page$pObject$pParamHash['content_id');
  309.  
  310.             // anchor
  311.             if!empty$pages[5][$i)) {
  312.                 $repl = preg_replace( '!href="([^"]*)"!', "href=\"$1{$pages[5][$i]}\"", BitPage::getPageLink( $page, $exists ));
  313.             } else {
  314.                 $repl = BitPage::getPageLink( $page, $exists );
  315.             }
  316.  
  317.             // alternate title
  318.             if( strlen( trim( $pages[6][$i] )) > 0 ) {
  319.                 $repl = str_replace( $page."</a>", "{$pages[6][$i]}</a>", $repl );
  320.             }
  321.  
  322.             $key = md5( mt_rand() );
  323.             $replacements[$key] = $repl;
  324.             $pData = str_replace( $pages[0][$i], $key, $pData );
  325.         }
  326.  
  327.         // Process the simpler ((Wiki Page)) type links without the description
  328.         preg_match_all( "/\({2}({$this->mWikiWordRegex})\){2}/", $pData, $pages );
  329.         foreach( array_unique( $pages[1] ) as $i => $page ) {
  330.             $page = str_replace( $pages[5][$i], "", $pages[1][$i] );
  331.             $exists = $this->pageExists$page$pObject$pParamHash['content_id');
  332.  
  333.             if!empty$pages[5][$i)) {
  334.                 $repl = preg_replace( '!href="([^"]*)"!', "href=\"$1{$pages[5][$i]}\"", BitPage::getPageLink( $page, $exists ));
  335.             } else {
  336.                 $repl = BitPage::getPageLink( $page, $exists );
  337.             }
  338.  
  339.             $key = md5( mt_rand() );
  340.             $replacements[$key] = $repl;
  341.             $pData = str_replace( "(({$pages[1][$i]}))", $key, $pData );
  342.         }
  343.  
  344.         // Finally we deal with WikiWord links
  345.         if( $gBitSystem->isFeatureActive'wiki_words' )) {
  346.             $pages = $this->extractWikiWords$pData );
  347.             foreach$pages as $page{
  348.                 if( $exists = $this->pageExists$page$pObject$pParamHash['content_id')) {
  349.                     $repl = BitPage::getPageLink( $page, $exists );
  350.                 } elseif( $gBitSystem->isFeatureActive'wiki_plurals' && $this->getLocale(== 'en_US' {
  351.                     // Link plural topic names to singular topic names if the plural
  352.                     // doesn't exist, and the language is english
  353.                     $plural_tmp = $page;
  354.                     // Plurals like policy / policies
  355.                     $plural_tmp = preg_replace( "/ies$/", "y", $plural_tmp );
  356.                     // Plurals like address / addresses
  357.                     $plural_tmp = preg_replace( "/sses$/", "ss", $plural_tmp );
  358.                     // Plurals like box / boxes
  359.                     $plural_tmp = preg_replace( "/([Xx])es$/", "$1", $plural_tmp );
  360.                     // Others, excluding ending ss like address(es)
  361.                     $plural_tmp = preg_replace( "/([A-Za-rt-z])s$/", "$1", $plural_tmp );
  362.                     // prevent redundant pageExists calls if plurals are on, and plural is same as original word
  363.                     if( $page != $plural_tmp ) {
  364.                         $exists = $this->pageExists$plural_tmp$pObject$pParamHash['content_id');
  365.                     }
  366.                     $repl = BitPage::getPageLink( $plural_tmp, $exists );
  367.                 } else {
  368.                     $repl = BitPage::getPageLink( $page, $exists );
  369.                 }
  370.  
  371.                 // old code
  372.                 //$slashed = preg_replace( "/([\/\[\]\(\)])/", "\\\\$1", $page_parse );
  373.                 //$data = preg_replace( "#([\s\,\;])\b$slashed\b([\s\,\;\.])#", "$1 ".$repl."$2", $data);
  374.                 // new code
  375.                 // i never understood why the simple stuff never worked but it
  376.                 // seems to work now - xing - Sunday Jul 22, 2007   17:37:17 CEST
  377.                 $pData = preg_replace( "/\b".preg_quote( $page, "/" )."\b/", $repl, $pData );
  378.             }
  379.         }
  380.  
  381.         // replace protection keys with original words
  382.         foreach( $replacements as $key => $replace ) {
  383.             $pData = str_replace( $key, $replace, $pData );
  384.         }
  385.  
  386.         return $pData;
  387.     }
  388.  
  389.     /**
  390.      * getLocale
  391.      *
  392.      * @access public
  393.      * @return locale
  394.      */
  395.     function getLocale() {
  396.         static $locales = array(
  397.             'cs' => 'cs_CZ',
  398.             'de' => 'de_DE',
  399.             'dk' => 'da_DK',
  400.             'en' => 'en_US',
  401.             'fr' => 'fr_FR',
  402.             'he' => 'he_IL', # hebrew
  403.             'it' => 'it_IT', # italian
  404.             'pl' => 'pl_PL', # polish
  405.             'po' => 'po',
  406.             'ru' => 'ru_RU',
  407.             'es' => 'es_ES',
  408.             'sw' => 'sw_SW', # swahili
  409.             'tw' => 'tw_TW',
  410.         );
  411.  
  412.         if( empty( $locale )) {
  413.             $locale = '';
  414.             if( isset( $locales[$this->getLanguage())) {
  415.                 $locale = $locales[$this->getLanguage()];
  416.             }
  417.         }
  418.  
  419.         return $locale;
  420.     }
  421.  
  422.     /**
  423.      * getLanguage
  424.      *
  425.      * @access public
  426.      * @return language
  427.      */
  428.     function getLanguage() {
  429.         static $sBitLanguage = FALSE;
  430.         global $gBitUser, $gBitSystem;
  431.  
  432.         if( empty( $sBitLanguage )) {
  433.             if( $gBitUser->isValid() ) {
  434.                 $sBitLanguage = $gBitUser->getPreference'bitLanguage''en' );
  435.             } else {
  436.                 $sBitLanguage = $this->getPreference'bitLanguage''en' );
  437.             }
  438.         }
  439.  
  440.         return $sBitLanguage;
  441.     }
  442.  
  443.     /**
  444.      * storeLinks
  445.      *
  446.      * @param string $pData
  447.      * @param array $pFilterHash
  448.      * @access public
  449.      * @return store wiki links in database
  450.      */
  451.     function storeLinks( $pData, $pFilterHash ) {
  452.         global $gBitSystem;
  453.  
  454.         // if we don't have a content_id or wiki isn't active, get out of here.
  455.         if( empty( $pFilterHash['content_id'] ) || !$gBitSystem->isPackageActive'wiki' )) {
  456.             return FALSE;
  457.         }
  458.  
  459.         $from_content_id = $pFilterHash['content_id'];
  460.         $from_title = isset( $pFilterHash['title'] ) ? $pFilterHash['title'] : '';
  461.  
  462.         // we need to remove the cache of any pages pointing to this one
  463.         $query = "SELECT `from_content_id` FROM `".BIT_DB_PREFIX."liberty_content_links` WHERE ( `to_content_id` = ? OR `to_content_id` IS NULL ) AND `to_title` = ?";
  464.         $clearCache = $gBitSystem->mDb->getCol$queryarray$from_content_id$from_title ));
  465.         ifis_array$clearCache )) {
  466.             foreach( $clearCache as $content_id ) {
  467.                 LibertyContent::expungeCacheFile( $content_id );
  468.             }
  469.         }
  470.  
  471.         // if this is a new page, fix up any links that may already point to it
  472.         $query = "UPDATE `".BIT_DB_PREFIX."liberty_content_links` SET `to_content_id` = ? WHERE ( `to_content_id` = ? OR `to_content_id` IS NULL ) AND `to_title` = ?";
  473.         $gBitSystem->mDb->query$queryarray$from_content_id0$from_title ));
  474.  
  475.         // get all the current links from this page
  476.         $query "SELECT LOWER( `to_title` ), `to_content_id` FROM `".BIT_DB_PREFIX."liberty_content_links` WHERE `from_content_id` = ?";
  477.         $oldLinks $gBitSystem->mDb->getAssoc$queryarray$from_content_id ));
  478.  
  479.         // get list of all wiki links on this page
  480.         $extractedWikiWords $this->extractWikiWords$pData );
  481.  
  482.         // wiki links with anchors are pulled out as well. we'll make copies of the wiki pagenames without the anchor to process these correctly as well
  483.         foreach$extractedWikiWords as $link {
  484.             if( strstr( $link, '#' ) !== FALSE ) {
  485.                 $extractedWikiWords[] = preg_replace( "!#.*!", "", $link );
  486.             }
  487.         }
  488.  
  489.         // create list of unique new wiki links on this page
  490.         $uniqueNewWikiLinks = array();
  491.         foreach( $extractedWikiWords as $to_title ) {
  492.             if( !empty( $to_title ) && !isset( $oldLinks[strtolower($to_title)] )) {
  493.                 $uniqueNewWikiLinks[] = $to_title;
  494.             }
  495.         }
  496.         // remove duplicates
  497.         array_unique( $uniqueNewWikiLinks );
  498.  
  499.         // get list of all new links that point to existing content
  500.         if( !empty( $uniqueNewWikiLinks )) {
  501.             $inSql = '?'.str_repeat( ',?', count( $uniqueNewWikiLinks ) - 1 );
  502.             // All arguments to MySQL in() function have to be passed as strings or it doesn't work correctly
  503.             $bindVars = array();
  504.             foreach( $uniqueNewWikiLinks as $var ) {
  505.                 $bindVars[] = ( string )$var;
  506.             }
  507.             $bindVars[] = BITPAGE_CONTENT_TYPE_GUID;
  508.  
  509.             $query = "SELECT LOWER( `title` ), `title` FROM `".BIT_DB_PREFIX."liberty_content` WHERE `title` IN( $inSql ) AND `content_type_guid` = ?";
  510.             $newLinksPointingToExistingContent = $gBitSystem->mDb->getAssoc$query$bindVars );
  511.  
  512.             // insert all new links pointing to existing content
  513.             if!empty$newLinksPointingToExistingContent )) {
  514.                 // All arguments to MySQL in() function have to be passed as strings or it doesn't work correctly
  515.                 $bindVars = array();
  516.                 foreach( $newLinksPointingToExistingContent as $var ) {
  517.                     $bindVars[] = ( string )$var;
  518.                 }
  519.                 $bindVars[] = BITPAGE_CONTENT_TYPE_GUID;
  520.                 $inSql      = '?'.str_repeat( ',?', count( $newLinksPointingToExistingContent ) - 1 );
  521.                 $query      = "
  522.                     INSERT INTO `".BIT_DB_PREFIX."liberty_content_links` ( `from_content_id`, `to_content_id`, `to_title` )
  523.                     SELECT ?, `content_id`, `title` FROM `".BIT_DB_PREFIX."liberty_content`
  524.                     WHERE `title` IN( $inSql ) AND `content_type_guid` = ?";
  525.                 array_unshift( $bindVars, $from_content_id );
  526.                 $result = $gBitSystem->mDb->query$query$bindVars );
  527.             }
  528.  
  529.             // insert all new links pointing to non-existing content and that are not in the db yet
  530.             foreach( $uniqueNewWikiLinks as $to_title ) {
  531.                 if( !isset( $newLinksPointingToExistingContent[strtolower( $to_title )] ) && !in_array( strtolower( $to_title ), array_keys( $oldLinks ))) {
  532.                     $query = "INSERT INTO `".BIT_DB_PREFIX."liberty_content_links` ( `from_content_id`, `to_title` ) VALUES( ?, ? )";
  533.                     $result = $gBitSystem->mDb->query$queryarray$from_content_id$to_title ));
  534.                 }
  535.             }
  536.         }
  537.  
  538.         // now delete any links no longer on page
  539.         foreach( $extractedWikiWords as $to_title ) {
  540.             $obsoleteLinks[strtolower( $to_title )] = 1;
  541.         }
  542.  
  543.         foreach( array_keys( $oldLinks ) as $to_title ) {
  544.             if( !isset( $obsoleteLinks[$to_title] )) {
  545.                 $query = "DELETE FROM `".BIT_DB_PREFIX."liberty_content_links` WHERE `from_content_id`=? AND LOWER( `to_title` ) = ?";
  546.                 $result = $gBitSystem->mDb->query$queryarray$from_content_id$to_title ));
  547.             }
  548.         }
  549.  
  550.         return TRUE;
  551.     }
  552.  
  553.     /**
  554.      * renameLinks
  555.      *
  556.      * @param array $pContentId
  557.      * @param array $pOldName
  558.      * @param array $pNewName
  559.      * @access public
  560.      * @return void
  561.      */
  562.     function renameLinks( $pContentId, $pOldName, $pNewName ) {
  563.         $query = "
  564.             SELECT `from_content_id`, `data`
  565.             FROM `".BIT_DB_PREFIX."liberty_content_links` lcl
  566.                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lcl.`from_content_id`=lc.`content_id` )
  567.             WHERE `to_content_id` = ?";
  568.  
  569.         if( $result = $this->mDb->query$queryarray$pContentId ) ) ) {
  570.             while( $row = $result->fetchRow() ) {
  571.                 // check if there are occasions of the old name with alternate display link name
  572.                 // --- ((Wiki Page|Description))
  573.                 // \({2}              # check for ((
  574.                 // \b$pOldName\b      # make sure the old name is on it's own
  575.                 // \|                 # the seperating deliminator
  576.                 // ([^\)]*)           # get as many characters as possible up to the next ) - put this in $1
  577.                 // \){2}              # closing brackets ))
  578.                 $pattern[] = "!\({2}\b$pOldName\b\|([^\)]*)\){2}!";
  579.  
  580.                 // - replace with new name leaving description in tact
  581.                 $replace[] = "(($pNewName|$1))";
  582.  
  583.  
  584.                 // --- ((Wiki Page)) or WikiPage
  585.                 // (\({2})?           # check for (( - optional - put this in $1
  586.                 // \b$pOldName\b      # make sure the old name is on it's own
  587.                 // (\){2})?           # closing brackets )) - optional - put this in $2
  588.                 $pattern[] = "!(\({2})?\b$pOldName\b(\){2})?!";
  589.  
  590.                 // - the replacement depends on the new name
  591.                 if( preg_match( "! !", $pNewName )) {
  592.                     // since we have a space in the final name, we need to have ((
  593.                     // and )) to make the link work
  594.                     $replace[] = "(($pNewName))";
  595.                 } else {
  596.                     // no spaces in the new name either, so we only insert the ((
  597.                     // and )) if the author used them to start off with
  598.                     $replace[] = "$1$pNewName$2";
  599.                 }
  600.  
  601.                 $data = preg_replace( $pattern, $replace, $row['data'] );
  602.                 if( md5( $data ) != md5( $row['data'] ) ) {
  603.                     $query = "UPDATE `".BIT_DB_PREFIX."liberty_content` SET `data`=? WHERE `content_id`=?";
  604.                     $this->mDb->query$queryarray$data$row['from_content_id') );
  605.  
  606.                     // remove any chached files pointing here
  607.                     LibertyContent::expungeCacheFile$row['from_content_id');
  608.                 }
  609.             }
  610.         }
  611.  
  612.         # Fix up titles in the link table
  613.         $query = "UPDATE `".BIT_DB_PREFIX."liberty_content_links` SET `to_title`=? WHERE `to_content_id`=?";
  614.         $this->mDb->query$queryarray$pNewName$pContentId ) );
  615.     }
  616.  
  617.     /**
  618.      * expunge bitlinks in the database
  619.      *
  620.      * @param numeric $pContentId
  621.      * @access public
  622.      * @return void
  623.      */
  624.     function expungeLinks( $pContentId ) {
  625.         if( !empty( $pContentId )) {
  626.             // remove any cached file pointing to this page
  627.             $links = $this->mDb->getCol"SELECT `from_content_id` FROM `".BIT_DB_PREFIX."liberty_content_links` WHERE to_content_id=?"array$pContentId ));
  628.             foreach$links as $content_id {
  629.                 LibertyContent::expungeCacheFile( $content_id );
  630.             }
  631.             $this->mDb->query"DELETE FROM `".BIT_DB_PREFIX."liberty_content_links` WHERE from_content_id=? OR to_content_id=?"array$pContentId$pContentId ));
  632.         }
  633.     }
  634. }

Documentation generated on Wed, 29 Jul 2015 13:56:29 +0000 by phpDocumentor 1.5.0-lsces