]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/AjaxFunctions.php
MediaWiki 1.11.0-scripts
[autoinstallsdev/mediawiki.git] / includes / AjaxFunctions.php
1 <?php
2
3 /** 
4  * @package MediaWiki
5  * @addtogroup Ajax
6  */
7
8 if( !defined( 'MEDIAWIKI' ) ) {
9         die( 1 );
10 }
11
12 /**
13  * Function converts an Javascript escaped string back into a string with
14  * specified charset (default is UTF-8).
15  * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
16  *
17  * @param $source String escaped with Javascript's escape() function
18  * @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
19  * @return string
20  */
21 function js_unescape($source, $iconv_to = 'UTF-8') {
22         $decodedStr = '';
23         $pos = 0;
24         $len = strlen ($source);
25
26         while ($pos < $len) {
27                 $charAt = substr ($source, $pos, 1);
28                 if ($charAt == '%') {
29                         $pos++;
30                         $charAt = substr ($source, $pos, 1);
31                         if ($charAt == 'u') {
32                                 // we got a unicode character
33                                 $pos++;
34                                 $unicodeHexVal = substr ($source, $pos, 4);
35                                 $unicode = hexdec ($unicodeHexVal);
36                                 $decodedStr .= code2utf($unicode);
37                                 $pos += 4;
38                         } else {
39                                 // we have an escaped ascii character
40                                 $hexVal = substr ($source, $pos, 2);
41                                 $decodedStr .= chr (hexdec ($hexVal));
42                                 $pos += 2;
43                         }
44                 } else {
45                         $decodedStr .= $charAt;
46                         $pos++;
47                 }
48         }
49
50         if ($iconv_to != "UTF-8") {
51                 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
52         }
53
54         return $decodedStr;
55 }
56
57 /**
58  * Function coverts number of utf char into that character.
59  * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
60  *
61  * @param $num Integer
62  * @return utf8char
63  */
64 function code2utf($num){
65    if ( $num<128 )
66         return chr($num);
67    if ( $num<2048 )
68         return chr(($num>>6)+192).chr(($num&63)+128);
69    if ( $num<65536 )
70         return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
71    if ( $num<2097152 )
72         return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
73    return '';
74 }
75
76 function wfSajaxSearch( $term ) {
77         global $wgContLang, $wgOut;
78         $limit = 16;
79
80         $l = new Linker;
81
82         $term = str_replace( ' ', '_', $wgContLang->ucfirst( 
83                         $wgContLang->checkTitleEncoding( $wgContLang->recodeInput( js_unescape( $term ) ) )
84                 ) );
85
86         if ( strlen( str_replace( '_', '', $term ) )<3 )
87                 return;
88
89         $db = wfGetDB( DB_SLAVE );
90         $res = $db->select( 'page', 'page_title',
91                         array(  'page_namespace' => 0,
92                                 "page_title LIKE '". $db->strencode( $term) ."%'" ),
93                                 "wfSajaxSearch",
94                                 array( 'LIMIT' => $limit+1 )
95                         );
96
97         $r = "";
98
99         $i=0;
100         while ( ( $row = $db->fetchObject( $res ) ) && ( ++$i <= $limit ) ) {
101                 $nt = Title::newFromDBkey( $row->page_title );
102                 $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
103         }
104         if ( $i > $limit ) {
105                 $more = '<i>' .  $l->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
106                                                 wfMsg('moredotdotdot'),
107                                                 "namespace=0&from=" . wfUrlEncode ( $term ) ) .
108                         '</i>';
109         } else {
110                 $more = '';
111         }
112
113         $subtitlemsg = ( Title::newFromText($term) ? 'searchsubtitle' : 'searchsubtitleinvalid' );
114         $subtitle = $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ); #FIXME: parser is missing mTitle !
115
116         $term = urlencode( $term );
117         $html = '<div style="float:right; border:solid 1px black;background:gainsboro;padding:2px;"><a onclick="Searching_Hide_Results();">'
118                 . wfMsg( 'hideresults' ) . '</a></div>'
119                 . '<h1 class="firstHeading">'.wfMsg('search')
120                 . '</h1><div id="contentSub">'. $subtitle . '</div><ul><li>'
121                 . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
122                                         wfMsg( 'searchcontaining', $term ),
123                                         "search=$term&fulltext=Search" )
124                 . '</li><li>' . $l->makeKnownLink( $wgContLang->specialPage( 'Search' ),
125                                         wfMsg( 'searchnamed', $term ) ,
126                                         "search=$term&go=Go" )
127                 . "</li></ul><h2>" . wfMsg( 'articletitles', $term ) . "</h2>"
128                 . '<ul>' .$r .'</ul>'.$more;
129
130         $response = new AjaxResponse( $html );
131
132         $response->setCacheDuration( 30*60 );
133
134         return $response;
135 }
136
137 /**
138  * Called for AJAX watch/unwatch requests.
139  * @param $pagename Prefixed title string for page to watch/unwatch
140  * @param $watch String 'w' to watch, 'u' to unwatch
141  * @return String '<w#>' or '<u#>' on successful watch or unwatch, 
142  *   respectively, followed by an HTML message to display in the alert box; or
143  *   '<err#>' on error
144  */
145 function wfAjaxWatch($pagename = "", $watch = "") {
146         if(wfReadOnly()) {
147                 // redirect to action=(un)watch, which will display the database lock
148                 // message
149                 return '<err#>'; 
150         }
151
152         if('w' !== $watch && 'u' !== $watch) {
153                 return '<err#>';
154         }
155         $watch = 'w' === $watch;
156
157         $title = Title::newFromText($pagename);
158         if(!$title) {
159                 // Invalid title
160                 return '<err#>';
161         }
162         $article = new Article($title);
163         $watching = $title->userIsWatching();
164
165         if($watch) {
166                 if(!$watching) {
167                         $dbw = wfGetDB(DB_MASTER);
168                         $dbw->begin();
169                         $article->doWatch();
170                         $dbw->commit();
171                 }
172         } else {
173                 if($watching) {
174                         $dbw = wfGetDB(DB_MASTER);
175                         $dbw->begin();
176                         $article->doUnwatch();
177                         $dbw->commit();
178                 }
179         }
180         if( $watch ) {
181                 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
182         } else {
183                 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
184         }
185 }
186