[mediawiki.api] write mediawiki.api.watch module
authorKrinkle <krinkle@users.mediawiki.org>
Tue, 27 Dec 2011 00:44:49 +0000 (00:44 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Tue, 27 Dec 2011 00:44:49 +0000 (00:44 +0000)
* add mediawiki.api.watch module
* mediawiki.api.parse.js: remove 'data && ' check to match the other modules. If data is not good, then the internal error handler will have already handled it and never call the ok-callback in the first place.
* mw.Api.errors: adding error codes by ApiWatch.php

resources/Resources.php
resources/mediawiki/mediawiki.api.js
resources/mediawiki/mediawiki.api.parse.js
resources/mediawiki/mediawiki.api.watch.js [new file with mode: 0644]

index 9e0d689..8edbc24 100644 (file)
@@ -526,6 +526,10 @@ return array(
                        'mediawiki.Title' 
                ),
        ),
+       'mediawiki.api.watch' => array( 
+               'scripts' => 'resources/mediawiki/mediawiki.api.watch.js',
+               'dependencies' => 'mediawiki.api',
+       ),
        'mediawiki.debug' => array(
                'scripts' => 'resources/mediawiki/mediawiki.debug.js',
                'styles' => 'resources/mediawiki/mediawiki.debug.css',
@@ -614,6 +618,7 @@ return array(
        ),
        'mediawiki.action.watch.ajax' => array(
                'scripts' => 'resources/mediawiki.action/mediawiki.action.watch.ajax.js',
+               'dependencies' => 'mediawiki.api.watch',
                'messages' => array(
                        'watch',
                        'unwatch',
index ac8bfd4..dd3a561 100644 (file)
                // upload succeeded, but no image info.
                // this is probably impossible, but might as well check for it
                'noimageinfo',
-
                // remote errors, defined in API
                'uploaddisabled',
                'nomodule',
                'overwrite',
                'badtoken',
                'fetchfileerror',
-               'fileexists-shared-forbidden'
+               'fileexists-shared-forbidden',
+               'invalidtitle',
+               'notloggedin'
        ];
 
        /**
index c29d734..1cc68f2 100644 (file)
@@ -19,7 +19,7 @@
                                        action: 'parse'
                                },
                                ok = function( data ) {
-                                       if ( data && data.parse && data.parse.text && data.parse.text['*'] ) {
+                                       if ( data.parse && data.parse.text && data.parse.text['*'] ) {
                                                success( data.parse.text['*'] );
                                        }
                                };
diff --git a/resources/mediawiki/mediawiki.api.watch.js b/resources/mediawiki/mediawiki.api.watch.js
new file mode 100644 (file)
index 0000000..84af419
--- /dev/null
@@ -0,0 +1,59 @@
+/**
+ * Additional mw.Api methods to assist with (un)watching wiki pages.
+ */
+( function( $, mw ) {
+
+       $.extend( mw.Api.prototype, {
+               /**
+                * Convinience method for 'action=watch'.
+                *
+                * @param page {String|mw.Title} Full page name or instance of mw.Title
+                * @param success {Function} callback to which the watch object will be passed
+                * watch object contains 'title' (full page name), 'watched' (boolean) and
+                * 'message' (parsed HTML of the 'addedwatchtext' message).
+                * @param err {Function} callback if error (optional)
+                * @param unwatch {Boolean} Internal variable, do not use. Used by unwatch() to
+                * reuse this function.
+                * @return {jqXHR}
+                */
+               watch: function( page, success, err, unwatch ) {
+                       var params, ok;
+                       params = {
+                               action: 'watch',
+                               title: String( page ),
+                               token: mw.user.tokens.get( 'watchToken' ),
+                               uselang: mw.config.get( 'wgUserLanguage' )
+                       };
+                       ok = function( data ) {
+                               success( data.watch );
+                       };
+                       return this.post( params, { ok: ok, err: err } );
+               },
+               /**
+                * Convinience method for 'action=watch&unwatch='.
+                *
+                * @param page {String|mw.Title} Full page name or instance of mw.Title
+                * @param success {Function} callback to which the watch object will be passed
+                * watch object contains 'title' (full page name), 'unwatched' (boolean) and
+                * 'message' (parsed HTML of the 'removedwatchtext' message).
+                * @param err {Function} callback if error (optional)
+                * @return {jqXHR}
+                */
+               unwatch: function( page, success, err, unwatch ) {
+                       var params, ok;
+                       params = {
+                               action: 'watch',
+                               unwatch: 1,
+                               title: String( page ),
+                               token: mw.user.tokens.get( 'watchToken' ),
+                               uselang: mw.config.get( 'wgUserLanguage' )
+                       };
+                       ok = function( data ) {
+                               success( data.watch );
+                       };
+                       return this.post( params, { ok: ok, err: err } );
+               }
+
+       } );
+
+} )( jQuery, mediaWiki );