AjaxFunctions.php 3.7 KB

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