* Revert revert r39662 of my parser changes.
[lhc/web/wiklou.git] / includes / AjaxFunctions.php
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
6
7 if( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
9 }
10
11 /**
12 * Function converts an Javascript escaped string back into a string with
13 * specified charset (default is UTF-8).
14 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
15 *
16 * @param $source String escaped with Javascript's escape() function
17 * @param $iconv_to String destination character set will be used as second paramether in the iconv function. Default is UTF-8.
18 * @return string
19 */
20 function js_unescape($source, $iconv_to = 'UTF-8') {
21 $decodedStr = '';
22 $pos = 0;
23 $len = strlen ($source);
24
25 while ($pos < $len) {
26 $charAt = substr ($source, $pos, 1);
27 if ($charAt == '%') {
28 $pos++;
29 $charAt = substr ($source, $pos, 1);
30 if ($charAt == 'u') {
31 // we got a unicode character
32 $pos++;
33 $unicodeHexVal = substr ($source, $pos, 4);
34 $unicode = hexdec ($unicodeHexVal);
35 $decodedStr .= code2utf($unicode);
36 $pos += 4;
37 } else {
38 // we have an escaped ascii character
39 $hexVal = substr ($source, $pos, 2);
40 $decodedStr .= chr (hexdec ($hexVal));
41 $pos += 2;
42 }
43 } else {
44 $decodedStr .= $charAt;
45 $pos++;
46 }
47 }
48
49 if ($iconv_to != "UTF-8") {
50 $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
51 }
52
53 return $decodedStr;
54 }
55
56 /**
57 * Function coverts number of utf char into that character.
58 * Function taken from: http://www.php.net/manual/en/function.utf8-encode.php#49336
59 *
60 * @param $num Integer
61 * @return utf8char
62 */
63 function code2utf($num){
64 if ( $num<128 )
65 return chr($num);
66 if ( $num<2048 )
67 return chr(($num>>6)+192).chr(($num&63)+128);
68 if ( $num<65536 )
69 return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
70 if ( $num<2097152 )
71 return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
72 return '';
73 }
74
75 /**
76 * Called for AJAX watch/unwatch requests.
77 * @param $pagename Prefixed title string for page to watch/unwatch
78 * @param $watch String 'w' to watch, 'u' to unwatch
79 * @return String '<w#>' or '<u#>' on successful watch or unwatch,
80 * respectively, followed by an HTML message to display in the alert box; or
81 * '<err#>' on error
82 */
83 function wfAjaxWatch($pagename = "", $watch = "") {
84 if(wfReadOnly()) {
85 // redirect to action=(un)watch, which will display the database lock
86 // message
87 return '<err#>';
88 }
89
90 if('w' !== $watch && 'u' !== $watch) {
91 return '<err#>';
92 }
93 $watch = 'w' === $watch;
94
95 $title = Title::newFromDBkey($pagename);
96 if(!$title) {
97 // Invalid title
98 return '<err#>';
99 }
100 $article = new Article($title);
101 $watching = $title->userIsWatching();
102
103 if($watch) {
104 if(!$watching) {
105 $dbw = wfGetDB(DB_MASTER);
106 $dbw->begin();
107 $ok = $article->doWatch();
108 $dbw->commit();
109 }
110 } else {
111 if($watching) {
112 $dbw = wfGetDB(DB_MASTER);
113 $dbw->begin();
114 $ok = $article->doUnwatch();
115 $dbw->commit();
116 }
117 }
118 // Something stopped the change
119 if( isset($ok) && !$ok ) {
120 return '<err#>';
121 }
122 if( $watch ) {
123 return '<w#>'.wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
124 } else {
125 return '<u#>'.wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
126 }
127 }