Ticket #3625: FCKeditorSajax.body.php.patch

File FCKeditorSajax.body.php.patch, 12.1 KB (added by Don Stringham, 14 years ago)

Added two functions and additional logic to use different search mechanisms with FCKeditor+Mediawiki

  • FCKeditorSajax.body.php

     
    1 <?php
    2 
    3 function wfSajaxGetMathUrl( $term ) {
    4         $originalLink = MathRenderer::renderMath( $term );
    5 
    6         if (false == strpos($originalLink, "src=\"")) {
    7                 return "";
    8         }
    9 
    10         $srcPart = substr($originalLink, strpos($originalLink, "src=")+ 5);
    11         $url = strtok($srcPart, '"');
    12 
    13         return $url;
    14 }
    15 
    16 function wfSajaxGetImageUrl( $term ) {
    17         global $wgExtensionFunctions, $wgTitle;
    18 
    19         $options = new FCKeditorParserOptions();
    20         $options->setTidy(true);
    21         $parser = new FCKeditorParser();
    22 
    23         if (in_array("wfCite", $wgExtensionFunctions)) {
    24                 $parser->setHook('ref', array($parser, 'ref'));
    25                 $parser->setHook('references', array($parser, 'references'));
    26         }
    27         $parser->setOutputType(OT_HTML);
    28         $originalLink = $parser->parse("[[Image:".$term."]]", $wgTitle, $options)->getText();
    29         if (false == strpos($originalLink, "src=\"")) {
    30                 return "";
    31         }
    32 
    33         $srcPart = substr($originalLink, strpos($originalLink, "src=")+ 5);
    34         $url = strtok($srcPart, '"');
    35 
    36         return $url;
    37 }
    38 
    39 function wfSajaxSearchSpecialTagFCKeditor($empty) {
    40         global $wgParser, $wgRawHtml;
    41 
    42         $ret = "nowiki\nincludeonly\nonlyinclude\nnoinclude\ngallery\n";
    43         if( $wgRawHtml )
    44         {
    45                 $ret.="html\n";
    46         }
    47         foreach ($wgParser->getTags() as $h) {
    48                 if (!in_array($h, array("pre", "math", "ref", "references"))) {
    49                         $ret .= $h ."\n";
    50                 }
    51         }
    52         $arr = explode("\n", $ret);
    53         sort($arr);
    54         $ret = implode("\n", $arr);
    55        
    56         return $ret;
    57 }
    58 
    59 function wfSajaxSearchImageFCKeditor( $term ) {
    60         global $wgContLang, $wgOut;
    61         $limit = 10;
    62 
    63         $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
    64         $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
    65         $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
    66         $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
    67         $term4 = str_replace( ' ', '_', $wgContLang->ucfirst( $term2 ) );
    68         $term = $term1;
    69 
    70         if ( strlen( str_replace( '_', '', $term ) )<3 )
    71         return "";
    72 
    73         $db = wfGetDB( DB_SLAVE );
    74         $res = $db->select( 'page', 'page_title',
    75         array(  'page_namespace' => NS_IMAGE,
    76         "page_title LIKE '%". $db->strencode( $term1 ) ."%'".
    77         "OR (page_title LIKE '%". $db->strencode( $term2 ) ."%') ".
    78         "OR (page_title LIKE '%". $db->strencode( $term3 ) ."%') ".
    79         "OR (page_title LIKE '%". $db->strencode( $term4 ) ."%') " ),
    80         "wfSajaxSearch",
    81         array( 'LIMIT' => $limit+1 )
    82         );
    83 
    84         $ret = "";
    85         $i = 0;
    86         while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
    87                 $ret .= $row->page_title ."\n";
    88         }
    89 
    90         $term = htmlspecialchars( $term );
    91 
    92         return $ret;
    93 }
    94 
    95 function wfSajaxSearchArticleFCKeditor( $term ) {
    96         global $wgContLang, $wgOut;
    97         $limit = 10;
    98         $ns = NS_MAIN;
    99 
    100         $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
    101 
    102         if (strpos($term, "Category:") === 0) {
    103                 $ns = NS_CATEGORY;
    104                 $term = substr($term, 9);
    105                 $prefix = "Category:";
    106         }
    107         else if (strpos($term, ":Category:") === 0) {
    108                 $ns = NS_CATEGORY;
    109                 $term = substr($term, 10);
    110                 $prefix = ":Category:";
    111         }
    112 
    113         $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
    114         $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
    115         $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
    116         $term4 = str_replace( ' ', '_', $wgContLang->ucfirst( $term2 ) );
    117         $term = $term1;
    118 
    119         if ( strlen( str_replace( '_', '', $term ) )<3 ) {
    120                 return "";
    121         }
    122 
    123         $db = wfGetDB( DB_SLAVE );
    124         $res = $db->select( 'page', 'page_title',
    125         array(  'page_namespace' => $ns,
    126         "page_title LIKE '%". $db->strencode( $term1 ) ."%' ".
    127         "OR (page_title LIKE '%". $db->strencode( $term2 ) ."%') ".
    128         "OR (page_title LIKE '%". $db->strencode( $term3 ) ."%') ".
    129         "OR (page_title LIKE '%". $db->strencode( $term4 ) ."%') " ),
    130         "wfSajaxSearch",
    131         array( 'LIMIT' => $limit+1 )
    132         );
    133 
    134         $ret = "";
    135         $i = 0;
    136         while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
    137                 if (isset($prefix) && !is_null($prefix)) {
    138                         $ret .= $prefix;
    139                 }
    140                 $ret .= $row->page_title ."\n";
    141         }
    142 
    143         $term = htmlspecialchars( $term );
    144 
    145         return $ret;
    146 }
    147 
    148 function wfSajaxSearchTemplateFCKeditor($empty) {
    149         global $wgContLang, $wgOut;
    150         $ns = NS_TEMPLATE;
    151 
    152         $db = wfGetDB( DB_SLAVE );
    153         $res = $db->select( 'page', 'page_title',
    154         array( 'page_namespace' => $ns),
    155         "wfSajaxSearch"
    156         );
    157 
    158         $ret = "";
    159         while ( $row = $db->fetchObject( $res ) ) {
    160                 $ret .= $row->page_title ."\n";
    161         }
    162 
    163         return $ret;
    164 }
    165 
    166 function wfSajaxWikiToHTML( $wiki ) {
    167         global $wgTitle;
    168 
    169         $options = new FCKeditorParserOptions();
    170         $options->setTidy(true);
    171         $parser = new FCKeditorParser();
    172         $parser->setOutputType(OT_HTML);
    173 
    174         wfSajaxToggleFCKeditor('show');         //FCKeditor was switched to visible
    175         return $parser->parse($wiki, $wgTitle, $options)->getText();
    176 }
    177 function wfSajaxToggleFCKeditor($data) {
    178         global $wgFCKEditorSaveUserSetting;
    179 
    180         if($data == 'show'){
    181                 $_SESSION['showMyFCKeditor'] = RTE_VISIBLE;     //visible last time
    182         }
    183         else {
    184                 $_SESSION['showMyFCKeditor'] = 0;       //invisible
    185         }
    186         return "SUCCESS";
    187 }
    188 
    189 
     1<?php
     2
     3function wfSajaxGetMathUrl( $term ) {
     4        $originalLink = MathRenderer::renderMath( $term );
     5
     6        if (false == strpos($originalLink, "src=\"")) {
     7                return "";
     8        }
     9
     10        $srcPart = substr($originalLink, strpos($originalLink, "src=")+ 5);
     11        $url = strtok($srcPart, '"');
     12
     13        return $url;
     14}
     15
     16function wfSajaxGetImageUrl( $term ) {
     17        global $wgExtensionFunctions, $wgTitle;
     18
     19        $options = new FCKeditorParserOptions();
     20        $options->setTidy(true);
     21        $parser = new FCKeditorParser();
     22
     23        if (in_array("wfCite", $wgExtensionFunctions)) {
     24                $parser->setHook('ref', array($parser, 'ref'));
     25                $parser->setHook('references', array($parser, 'references'));
     26        }
     27        $parser->setOutputType(OT_HTML);
     28        $originalLink = $parser->parse("[[Image:".$term."]]", $wgTitle, $options)->getText();
     29        if (false == strpos($originalLink, "src=\"")) {
     30                return "";
     31        }
     32
     33        $srcPart = substr($originalLink, strpos($originalLink, "src=")+ 5);
     34        $url = strtok($srcPart, '"');
     35
     36        return $url;
     37}
     38
     39function wfSajaxSearchSpecialTagFCKeditor($empty) {
     40        global $wgParser, $wgRawHtml;
     41
     42        $ret = "nowiki\nincludeonly\nonlyinclude\nnoinclude\ngallery\n";
     43        if( $wgRawHtml )
     44        {
     45                $ret.="html\n";
     46        }
     47        foreach ($wgParser->getTags() as $h) {
     48                if (!in_array($h, array("pre", "math", "ref", "references"))) {
     49                        $ret .= $h ."\n";
     50                }
     51        }
     52        $arr = explode("\n", $ret);
     53        sort($arr);
     54        $ret = implode("\n", $arr);
     55       
     56        return $ret;
     57}
     58
     59function wfSajaxSearchImageFCKeditor( $term ) {
     60        global $wgContLang, $wgOut;
     61        $limit = 10;
     62
     63        $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
     64        $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
     65        $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
     66        $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
     67        $term4 = str_replace( ' ', '_', $wgContLang->ucfirst( $term2 ) );
     68        $term = $term1;
     69
     70        if ( strlen( str_replace( '_', '', $term ) )<3 )
     71        return "";
     72
     73        $db = wfGetDB( DB_SLAVE );
     74        $res = $db->select( 'page', 'page_title',
     75        array(  'page_namespace' => NS_IMAGE,
     76        "page_title LIKE '%". $db->strencode( $term1 ) ."%'".
     77        "OR (page_title LIKE '%". $db->strencode( $term2 ) ."%') ".
     78        "OR (page_title LIKE '%". $db->strencode( $term3 ) ."%') ".
     79        "OR (page_title LIKE '%". $db->strencode( $term4 ) ."%') " ),
     80        "wfSajaxSearch",
     81        array( 'LIMIT' => $limit+1 )
     82        );
     83
     84        $ret = "";
     85        $i = 0;
     86        while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
     87                $ret .= $row->page_title ."\n";
     88        }
     89
     90        $term = htmlspecialchars( $term );
     91
     92        return $ret;
     93}
     94
     95function wfSajaxSearchArticleFCKeditor( $term ) {
     96        global $wgContLang, $wgOut, $wgFCKeditorSearch;
     97   
     98        $limit = 10;
     99        $ns = NS_MAIN;
     100
     101        $term = $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) );
     102
     103        if (strpos($term, "Category:") === 0) {
     104                $ns = NS_CATEGORY;
     105                $term = substr($term, 9);
     106                $prefix = "Category:";
     107        }
     108        else if (strpos($term, ":Category:") === 0) {
     109                $ns = NS_CATEGORY;
     110                $term = substr($term, 10);
     111                $prefix = ":Category:";
     112        }
     113
     114        $term1 = str_replace( ' ', '_', $wgContLang->ucfirst( $term ) );
     115        $term2 = str_replace( ' ', '_', $wgContLang->lc( $term ) );
     116        $term3 = str_replace( ' ', '_', $wgContLang->uc( $term ) );
     117        $term4 = str_replace( ' ', '_', $wgContLang->ucfirst( $term2 ) );
     118        $term = $term1;
     119
     120        if ( strlen( str_replace( '_', '', $term ) )<3 ) {
     121                return "";
     122        }
     123   
     124    if($wgFCKeditorSearch == FCK_DEFAULT_SEARCH) {
     125        $db = wfGetDB( DB_SLAVE );
     126        $res = $db->select( 'page', 'page_title',
     127        array(  /*'page_namespace' => $ns,*/
     128        "page_title LIKE '%". $db->strencode( $term1 ) ."%' ".
     129        "OR (page_title LIKE '%". $db->strencode( $term2 ) ."%') ".
     130        "OR (page_title LIKE '%". $db->strencode( $term3 ) ."%') ".
     131        "OR (page_title LIKE '%". $db->strencode( $term4 ) ."%') " ),
     132        "wfSajaxSearch",
     133        array( 'LIMIT' => $limit+1 )
     134        );
     135
     136        $ret = "";
     137        $i = 0;
     138        while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
     139            if (isset($prefix) && !is_null($prefix)) {
     140                $ret .= $prefix;
     141            }
     142            $ret .= $row->page_title ."\n";
     143        }
     144    }
     145    elseif($wgFCKeditorSearch == FCK_MEDIAWIKI_SEARCH) {
     146        $ret = wfSajaxSearchMWSearch($term);
     147    }
     148        $term = htmlspecialchars( $term );
     149
     150        return $ret;
     151}
     152
     153function wfSajaxSearchTemplateFCKeditor($empty) {
     154        global $wgContLang, $wgOut;
     155        $ns = NS_TEMPLATE;
     156
     157        $db = wfGetDB( DB_SLAVE );
     158        $res = $db->select( 'page', 'page_title',
     159        array( 'page_namespace' => $ns),
     160        "wfSajaxSearch"
     161        );
     162
     163        $ret = "";
     164        while ( $row = $db->fetchObject( $res ) ) {
     165                $ret .= $row->page_title ."\n";
     166        }
     167
     168        return $ret;
     169}
     170
     171function wfSajaxWikiToHTML( $wiki ) {
     172        global $wgTitle;
     173
     174        $options = new FCKeditorParserOptions();
     175        $options->setTidy(true);
     176        $parser = new FCKeditorParser();
     177        $parser->setOutputType(OT_HTML);
     178
     179        wfSajaxToggleFCKeditor('show');         //FCKeditor was switched to visible
     180        return $parser->parse($wiki, $wgTitle, $options)->getText();
     181}
     182function wfSajaxToggleFCKeditor($data) {
     183        global $wgFCKEditorSaveUserSetting;
     184
     185        if($data == 'show'){
     186                $_SESSION['showMyFCKeditor'] = RTE_VISIBLE;     //visible last time
     187        }
     188        else {
     189                $_SESSION['showMyFCKeditor'] = 0;       //invisible
     190        }
     191        return "SUCCESS";
     192}
     193/**
     194 * Does a search using the MWSearch extension.  That extension uses Lucene.
     195 *
     196 * @author Don B. Stringham
     197 *
     198 * @param String: $term = word to search on.
     199 * @return String: $results = a \n delimited string contains search results.
     200 */
     201function wfSajaxSearchMWSearch($term) {
     202    global $wgUser, $wgRequest, $wgFCKeditorSearchType;
     203   
     204    $search = SearchEngine::create();
     205    $search->setLimitOffset(20, 'searchlimit');
     206    $search->setNamespaces(SearchEngine::userNamespaces($wgUser));
     207    $search->showRedirects = $wgRequest->getcheck('redirs')?true:false;
     208    $rewritten = $search->replacePrefixes($term);
     209    if($wgFCKeditorSearchType == FCK_MEDIAWIKI_FULLTEXT) {
     210        $textMatches = $search->searchText($rewritten);
     211    }
     212    elseif($wgFCKeditorSearchType == FCK_MEDIAWIKI_TITLE) {
     213        $textMatches = $search->searchTitle($rewritten);
     214    }
     215   
     216    if($textMatches->mResultCount <= 0 || $textMatches == null) {
     217        return "";
     218    }
     219    $results = "";
     220    foreach($textMatches->mResults as $row){
     221        $res = split(" ", $row);
     222        $results .= wfSajaxReplaceSpecialChars($res[2])."\n";
     223    }
     224    return $results;
     225}
     226/**
     227 * Replaces '_' and '%??' with the space character.  Used regex to do the
     228 * replacement.
     229 *
     230 * @author Don B. Stringham
     231 *
     232 * @param String: $subject = original string to be cleaned-up
     233 * @return String: $result = cleaned-up string.
     234 */
     235function wfSajaxReplaceSpecialChars($subject){
     236    $result = preg_replace('/_/', ' ', $subject);
     237    $result = preg_replace('/%../', ' ', $result);
     238    return $result;
     239}
     240
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy