| 1 |
( function () { |
| 2 |
|
| 3 |
Object.assign( mw.Api.prototype, { |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
postWithEditToken: function ( params, ajaxOptions ) { |
| 20 |
return this.postWithToken( 'csrf', params, ajaxOptions ); |
| 21 |
}, |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
getEditToken: function () { |
| 29 |
return this.getToken( 'csrf' ); |
| 30 |
}, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
create: function ( title, params, content ) { |
| 49 |
return this.postWithEditToken( Object.assign( this.assertCurrentUser( { |
| 50 |
action: 'edit', |
| 51 |
title: String( title ), |
| 52 |
text: content, |
| 53 |
formatversion: '2', |
| 54 |
|
| 55 |
createonly: true |
| 56 |
} ), params ) ).then( ( data ) => data.edit ); |
| 57 |
}, |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
edit: function ( title, transform ) { |
| 117 |
const api = this; |
| 118 |
|
| 119 |
title = String( title ); |
| 120 |
|
| 121 |
let basetimestamp, curtimestamp; |
| 122 |
return api.get( { |
| 123 |
action: 'query', |
| 124 |
prop: 'revisions', |
| 125 |
rvprop: [ 'content', 'timestamp' ], |
| 126 |
titles: [ title ], |
| 127 |
formatversion: '2', |
| 128 |
curtimestamp: true |
| 129 |
} ) |
| 130 |
.then( ( data ) => { |
| 131 |
if ( !data.query || !data.query.pages ) { |
| 132 |
return $.Deferred().reject( 'unknown' ); |
| 133 |
} |
| 134 |
const page = data.query.pages[ 0 ]; |
| 135 |
if ( !page || page.invalid ) { |
| 136 |
return $.Deferred().reject( 'invalidtitle' ); |
| 137 |
} |
| 138 |
if ( page.missing ) { |
| 139 |
return $.Deferred().reject( 'nocreate-missing' ); |
| 140 |
} |
| 141 |
const revision = page.revisions[ 0 ]; |
| 142 |
basetimestamp = revision.timestamp; |
| 143 |
curtimestamp = data.curtimestamp; |
| 144 |
return transform( { |
| 145 |
timestamp: revision.timestamp, |
| 146 |
content: revision.content |
| 147 |
} ); |
| 148 |
} ) |
| 149 |
.then( ( params ) => { |
| 150 |
const editParams = typeof params === 'object' ? params : { text: String( params ) }; |
| 151 |
return api.postWithEditToken( Object.assign( { |
| 152 |
action: 'edit', |
| 153 |
title: title, |
| 154 |
formatversion: '2', |
| 155 |
|
| 156 |
|
| 157 |
assert: mw.config.get( 'wgUserName' ) ? 'user' : undefined, |
| 158 |
basetimestamp: basetimestamp, |
| 159 |
starttimestamp: curtimestamp, |
| 160 |
nocreate: true |
| 161 |
}, editParams ) ); |
| 162 |
} ) |
| 163 |
.then( ( data ) => data.edit ); |
| 164 |
}, |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
newSection: function ( title, header, message, additionalParams ) { |
| 177 |
return this.postWithEditToken( Object.assign( { |
| 178 |
action: 'edit', |
| 179 |
section: 'new', |
| 180 |
title: String( title ), |
| 181 |
summary: header, |
| 182 |
text: message |
| 183 |
}, additionalParams ) ); |
| 184 |
} |
| 185 |
} ); |
| 186 |
|
| 187 |
}() ); |