EditPage.php 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  1. <?php
  2. /**
  3. * Contains the EditPage class
  4. * @file
  5. */
  6. /**
  7. * The edit page/HTML interface (split from Article)
  8. * The actual database and text munging is still in Article,
  9. * but it should get easier to call those from alternate
  10. * interfaces.
  11. *
  12. * EditPage cares about two distinct titles:
  13. * $wgTitle is the page that forms submit to, links point to,
  14. * redirects go to, etc. $this->mTitle (as well as $mArticle) is the
  15. * page in the database that is actually being edited. These are
  16. * usually the same, but they are now allowed to be different.
  17. */
  18. class EditPage {
  19. const AS_SUCCESS_UPDATE = 200;
  20. const AS_SUCCESS_NEW_ARTICLE = 201;
  21. const AS_HOOK_ERROR = 210;
  22. const AS_FILTERING = 211;
  23. const AS_HOOK_ERROR_EXPECTED = 212;
  24. const AS_BLOCKED_PAGE_FOR_USER = 215;
  25. const AS_CONTENT_TOO_BIG = 216;
  26. const AS_USER_CANNOT_EDIT = 217;
  27. const AS_READ_ONLY_PAGE_ANON = 218;
  28. const AS_READ_ONLY_PAGE_LOGGED = 219;
  29. const AS_READ_ONLY_PAGE = 220;
  30. const AS_RATE_LIMITED = 221;
  31. const AS_ARTICLE_WAS_DELETED = 222;
  32. const AS_NO_CREATE_PERMISSION = 223;
  33. const AS_BLANK_ARTICLE = 224;
  34. const AS_CONFLICT_DETECTED = 225;
  35. const AS_SUMMARY_NEEDED = 226;
  36. const AS_TEXTBOX_EMPTY = 228;
  37. const AS_MAX_ARTICLE_SIZE_EXCEEDED = 229;
  38. const AS_OK = 230;
  39. const AS_END = 231;
  40. const AS_SPAM_ERROR = 232;
  41. const AS_IMAGE_REDIRECT_ANON = 233;
  42. const AS_IMAGE_REDIRECT_LOGGED = 234;
  43. var $mArticle;
  44. var $mTitle;
  45. var $action;
  46. var $mMetaData = '';
  47. var $isConflict = false;
  48. var $isCssJsSubpage = false;
  49. var $deletedSinceEdit = false;
  50. var $formtype;
  51. var $firsttime;
  52. var $lastDelete;
  53. var $mTokenOk = false;
  54. var $mTokenOkExceptSuffix = false;
  55. var $mTriedSave = false;
  56. var $tooBig = false;
  57. var $kblength = false;
  58. var $missingComment = false;
  59. var $missingSummary = false;
  60. var $allowBlankSummary = false;
  61. var $autoSumm = '';
  62. var $hookError = '';
  63. #var $mPreviewTemplates;
  64. var $mParserOutput;
  65. var $mBaseRevision = false;
  66. # Form values
  67. var $save = false, $preview = false, $diff = false;
  68. var $minoredit = false, $watchthis = false, $recreate = false;
  69. var $textbox1 = '', $textbox2 = '', $summary = '';
  70. var $edittime = '', $section = '', $starttime = '';
  71. var $oldid = 0, $editintro = '', $scrolltop = null;
  72. # Placeholders for text injection by hooks (must be HTML)
  73. # extensions should take care to _append_ to the present value
  74. public $editFormPageTop; // Before even the preview
  75. public $editFormTextTop;
  76. public $editFormTextBeforeContent;
  77. public $editFormTextAfterWarn;
  78. public $editFormTextAfterTools;
  79. public $editFormTextBottom;
  80. /* $didSave should be set to true whenever an article was succesfully altered. */
  81. public $didSave = false;
  82. public $undidRev = 0;
  83. public $suppressIntro = false;
  84. /**
  85. * @todo document
  86. * @param $article
  87. */
  88. function EditPage( $article ) {
  89. $this->mArticle =& $article;
  90. $this->mTitle = $article->getTitle();
  91. $this->action = 'submit';
  92. # Placeholders for text injection by hooks (empty per default)
  93. $this->editFormPageTop =
  94. $this->editFormTextTop =
  95. $this->editFormTextBeforeContent =
  96. $this->editFormTextAfterWarn =
  97. $this->editFormTextAfterTools =
  98. $this->editFormTextBottom = "";
  99. }
  100. function getArticle() {
  101. return $this->mArticle;
  102. }
  103. /**
  104. * Fetch initial editing page content.
  105. * @private
  106. */
  107. function getContent( $def_text = '' ) {
  108. global $wgOut, $wgRequest, $wgParser, $wgContLang, $wgMessageCache;
  109. wfProfileIn( __METHOD__ );
  110. # Get variables from query string :P
  111. $section = $wgRequest->getVal( 'section' );
  112. $preload = $wgRequest->getVal( 'preload' );
  113. $undoafter = $wgRequest->getVal( 'undoafter' );
  114. $undo = $wgRequest->getVal( 'undo' );
  115. $text = '';
  116. // For message page not locally set, use the i18n message.
  117. // For other non-existent articles, use preload text if any.
  118. if ( !$this->mTitle->exists() ) {
  119. if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
  120. # If this is a system message, get the default text.
  121. list( $message, $lang ) = $wgMessageCache->figureMessage( $wgContLang->lcfirst( $this->mTitle->getText() ) );
  122. $wgMessageCache->loadAllMessages( $lang );
  123. $text = wfMsgGetKey( $message, false, $lang, false );
  124. if( wfEmptyMsg( $message, $text ) )
  125. $text = '';
  126. } else {
  127. # If requested, preload some text.
  128. $text = $this->getPreloadedText( $preload );
  129. }
  130. // For existing pages, get text based on "undo" or section parameters.
  131. } else {
  132. $text = $this->mArticle->getContent();
  133. if ( $undo > 0 && $undoafter > 0 && $undo < $undoafter ) {
  134. # If they got undoafter and undo round the wrong way, switch them
  135. list( $undo, $undoafter ) = array( $undoafter, $undo );
  136. }
  137. if ( $undo > 0 && $undo > $undoafter ) {
  138. # Undoing a specific edit overrides section editing; section-editing
  139. # doesn't work with undoing.
  140. if ( $undoafter ) {
  141. $undorev = Revision::newFromId($undo);
  142. $oldrev = Revision::newFromId($undoafter);
  143. } else {
  144. $undorev = Revision::newFromId($undo);
  145. $oldrev = $undorev ? $undorev->getPrevious() : null;
  146. }
  147. # Sanity check, make sure it's the right page,
  148. # the revisions exist and they were not deleted.
  149. # Otherwise, $text will be left as-is.
  150. if ( !is_null( $undorev ) && !is_null( $oldrev ) &&
  151. $undorev->getPage() == $oldrev->getPage() &&
  152. $undorev->getPage() == $this->mArticle->getID() &&
  153. !$undorev->isDeleted( Revision::DELETED_TEXT ) &&
  154. !$oldrev->isDeleted( Revision::DELETED_TEXT ) ) {
  155. $undotext = $this->mArticle->getUndoText( $undorev, $oldrev );
  156. if ( $undotext === false ) {
  157. # Warn the user that something went wrong
  158. $this->editFormPageTop .= $wgOut->parse( '<div class="error mw-undo-failure">' . wfMsgNoTrans( 'undo-failure' ) . '</div>' );
  159. } else {
  160. $text = $undotext;
  161. # Inform the user of our success and set an automatic edit summary
  162. $this->editFormPageTop .= $wgOut->parse( '<div class="mw-undo-success">' . wfMsgNoTrans( 'undo-success' ) . '</div>' );
  163. $firstrev = $oldrev->getNext();
  164. # If we just undid one rev, use an autosummary
  165. if ( $firstrev->mId == $undo ) {
  166. $this->summary = wfMsgForContent( 'undo-summary', $undo, $undorev->getUserText() );
  167. $this->undidRev = $undo;
  168. }
  169. $this->formtype = 'diff';
  170. }
  171. } else {
  172. // Failed basic sanity checks.
  173. // Older revisions may have been removed since the link
  174. // was created, or we may simply have got bogus input.
  175. $this->editFormPageTop .= $wgOut->parse( '<div class="error mw-undo-norev">' . wfMsgNoTrans( 'undo-norev' ) . '</div>' );
  176. }
  177. } else if ( $section != '' ) {
  178. if ( $section == 'new' ) {
  179. $text = $this->getPreloadedText( $preload );
  180. } else {
  181. $text = $wgParser->getSection( $text, $section, $def_text );
  182. }
  183. }
  184. }
  185. wfProfileOut( __METHOD__ );
  186. return $text;
  187. }
  188. /**
  189. * Get the contents of a page from its title and remove includeonly tags
  190. *
  191. * @param $preload String: the title of the page.
  192. * @return string The contents of the page.
  193. */
  194. protected function getPreloadedText( $preload ) {
  195. if ( $preload === '' ) {
  196. return '';
  197. } else {
  198. $preloadTitle = Title::newFromText( $preload );
  199. if ( isset( $preloadTitle ) && $preloadTitle->userCanRead() ) {
  200. $rev = Revision::newFromTitle($preloadTitle);
  201. if ( is_object( $rev ) ) {
  202. $text = $rev->getText();
  203. // TODO FIXME: AAAAAAAAAAA, this shouldn't be implementing
  204. // its own mini-parser! -ævar
  205. $text = preg_replace( '~</?includeonly>~', '', $text );
  206. return $text;
  207. } else
  208. return '';
  209. }
  210. }
  211. }
  212. /**
  213. * This is the function that extracts metadata from the article body on the first view.
  214. * To turn the feature on, set $wgUseMetadataEdit = true ; in LocalSettings
  215. * and set $wgMetadataWhitelist to the *full* title of the template whitelist
  216. */
  217. function extractMetaDataFromArticle () {
  218. global $wgUseMetadataEdit, $wgMetadataWhitelist, $wgContLang;
  219. $this->mMetaData = '';
  220. if ( !$wgUseMetadataEdit ) return;
  221. if ( $wgMetadataWhitelist == '' ) return;
  222. $s = '';
  223. $t = $this->getContent();
  224. # MISSING : <nowiki> filtering
  225. # Categories and language links
  226. $t = explode ( "\n" , $t );
  227. $catlow = strtolower ( $wgContLang->getNsText( NS_CATEGORY ) );
  228. $cat = $ll = array();
  229. foreach ( $t AS $key => $x ) {
  230. $y = trim ( strtolower ( $x ) );
  231. while ( substr ( $y , 0 , 2 ) == '[[' ) {
  232. $y = explode ( ']]' , trim ( $x ) );
  233. $first = array_shift ( $y );
  234. $first = explode ( ':' , $first );
  235. $ns = array_shift ( $first );
  236. $ns = trim ( str_replace ( '[' , '' , $ns ) );
  237. if ( $wgContLang->getLanguageName( $ns ) || strtolower ( $ns ) == $catlow ) {
  238. $add = '[[' . $ns . ':' . implode ( ':' , $first ) . ']]';
  239. if ( strtolower ( $ns ) == $catlow ) $cat[] = $add;
  240. else $ll[] = $add;
  241. $x = implode ( ']]' , $y );
  242. $t[$key] = $x;
  243. $y = trim ( strtolower ( $x ) );
  244. } else {
  245. $x = implode ( ']]' , $y );
  246. $y = trim ( strtolower ( $x ) );
  247. }
  248. }
  249. }
  250. if ( count ( $cat ) ) $s .= implode ( ' ' , $cat ) . "\n";
  251. if ( count ( $ll ) ) $s .= implode ( ' ' , $ll ) . "\n";
  252. $t = implode ( "\n" , $t );
  253. # Load whitelist
  254. $sat = array () ; # stand-alone-templates; must be lowercase
  255. $wl_title = Title::newFromText ( $wgMetadataWhitelist );
  256. $wl_article = new Article ( $wl_title );
  257. $wl = explode ( "\n" , $wl_article->getContent() );
  258. foreach ( $wl AS $x ) {
  259. $isentry = false;
  260. $x = trim ( $x );
  261. while ( substr ( $x , 0 , 1 ) == '*' ) {
  262. $isentry = true;
  263. $x = trim ( substr ( $x , 1 ) );
  264. }
  265. if ( $isentry ) {
  266. $sat[] = strtolower ( $x );
  267. }
  268. }
  269. # Templates, but only some
  270. $t = explode ( '{{' , $t );
  271. $tl = array () ;
  272. foreach ( $t AS $key => $x ) {
  273. $y = explode ( '}}' , $x , 2 );
  274. if ( count ( $y ) == 2 ) {
  275. $z = $y[0];
  276. $z = explode ( '|' , $z );
  277. $tn = array_shift ( $z );
  278. if ( in_array ( strtolower ( $tn ) , $sat ) ) {
  279. $tl[] = '{{' . $y[0] . '}}';
  280. $t[$key] = $y[1];
  281. $y = explode ( '}}' , $y[1] , 2 );
  282. }
  283. else $t[$key] = '{{' . $x;
  284. }
  285. else if ( $key != 0 ) $t[$key] = '{{' . $x;
  286. else $t[$key] = $x;
  287. }
  288. if ( count ( $tl ) ) $s .= implode ( ' ' , $tl );
  289. $t = implode ( '' , $t );
  290. $t = str_replace ( "\n\n\n" , "\n" , $t );
  291. $this->mArticle->mContent = $t;
  292. $this->mMetaData = $s;
  293. }
  294. /*
  295. * Check if a page was deleted while the user was editing it, before submit.
  296. * Note that we rely on the logging table, which hasn't been always there,
  297. * but that doesn't matter, because this only applies to brand new
  298. * deletes.
  299. */
  300. protected function wasDeletedSinceLastEdit() {
  301. if ( $this->deletedSinceEdit )
  302. return true;
  303. if ( $this->mTitle->isDeletedQuick() ) {
  304. $this->lastDelete = $this->getLastDelete();
  305. if ( $this->lastDelete ) {
  306. $deleteTime = wfTimestamp( TS_MW, $this->lastDelete->log_timestamp );
  307. if ( $deleteTime > $this->starttime ) {
  308. $this->deletedSinceEdit = true;
  309. }
  310. }
  311. }
  312. return $this->deletedSinceEdit;
  313. }
  314. function submit() {
  315. $this->edit();
  316. }
  317. /**
  318. * This is the function that gets called for "action=edit". It
  319. * sets up various member variables, then passes execution to
  320. * another function, usually showEditForm()
  321. *
  322. * The edit form is self-submitting, so that when things like
  323. * preview and edit conflicts occur, we get the same form back
  324. * with the extra stuff added. Only when the final submission
  325. * is made and all is well do we actually save and redirect to
  326. * the newly-edited page.
  327. */
  328. function edit() {
  329. global $wgOut, $wgUser, $wgRequest;
  330. // Allow extensions to modify/prevent this form or submission
  331. if ( !wfRunHooks( 'AlternateEdit', array( &$this ) ) ) {
  332. return;
  333. }
  334. wfProfileIn( __METHOD__ );
  335. wfDebug( __METHOD__.": enter\n" );
  336. // This is not an article
  337. $wgOut->setArticleFlag( false );
  338. $this->importFormData( $wgRequest );
  339. $this->firsttime = false;
  340. if ( $this->live ) {
  341. $this->livePreview();
  342. wfProfileOut( __METHOD__ );
  343. return;
  344. }
  345. if ( wfReadOnly() && $this->save ) {
  346. // Force preview
  347. $this->save = false;
  348. $this->preview = true;
  349. }
  350. $wgOut->addScriptFile( 'edit.js' );
  351. $permErrors = $this->getEditPermissionErrors();
  352. if ( $permErrors ) {
  353. wfDebug( __METHOD__.": User can't edit\n" );
  354. $this->readOnlyPage( $this->getContent(), true, $permErrors, 'edit' );
  355. wfProfileOut( __METHOD__ );
  356. return;
  357. } else {
  358. if ( $this->save ) {
  359. $this->formtype = 'save';
  360. } else if ( $this->preview ) {
  361. $this->formtype = 'preview';
  362. } else if ( $this->diff ) {
  363. $this->formtype = 'diff';
  364. } else { # First time through
  365. $this->firsttime = true;
  366. if ( $this->previewOnOpen() ) {
  367. $this->formtype = 'preview';
  368. } else {
  369. $this->extractMetaDataFromArticle () ;
  370. $this->formtype = 'initial';
  371. }
  372. }
  373. }
  374. // If they used redlink=1 and the page exists, redirect to the main article
  375. if ( $wgRequest->getBool( 'redlink' ) && $this->mTitle->exists() ) {
  376. $wgOut->redirect( $this->mTitle->getFullURL() );
  377. }
  378. wfProfileIn( __METHOD__."-business-end" );
  379. $this->isConflict = false;
  380. // css / js subpages of user pages get a special treatment
  381. $this->isCssJsSubpage = $this->mTitle->isCssJsSubpage();
  382. $this->isValidCssJsSubpage = $this->mTitle->isValidCssJsSubpage();
  383. # Show applicable editing introductions
  384. if ( $this->formtype == 'initial' || $this->firsttime )
  385. $this->showIntro();
  386. if ( $this->mTitle->isTalkPage() ) {
  387. $wgOut->addWikiMsg( 'talkpagetext' );
  388. }
  389. # Optional notices on a per-namespace and per-page basis
  390. $editnotice_ns = 'editnotice-'.$this->mTitle->getNamespace();
  391. if ( !wfEmptyMsg( $editnotice_ns, wfMsgForContent( $editnotice_ns ) ) ) {
  392. $wgOut->addWikiText( wfMsgForContent( $editnotice_ns ) );
  393. }
  394. if ( MWNamespace::hasSubpages( $this->mTitle->getNamespace() ) ) {
  395. $parts = explode( '/', $this->mTitle->getDBkey() );
  396. $editnotice_base = $editnotice_ns;
  397. while ( count( $parts ) > 0 ) {
  398. $editnotice_base .= '-'.array_shift( $parts );
  399. if ( !wfEmptyMsg( $editnotice_base, wfMsgForContent( $editnotice_base ) ) ) {
  400. $wgOut->addWikiText( wfMsgForContent( $editnotice_base ) );
  401. }
  402. }
  403. }
  404. # Attempt submission here. This will check for edit conflicts,
  405. # and redundantly check for locked database, blocked IPs, etc.
  406. # that edit() already checked just in case someone tries to sneak
  407. # in the back door with a hand-edited submission URL.
  408. if ( 'save' == $this->formtype ) {
  409. if ( !$this->attemptSave() ) {
  410. wfProfileOut( __METHOD__."-business-end" );
  411. wfProfileOut( __METHOD__ );
  412. return;
  413. }
  414. }
  415. # First time through: get contents, set time for conflict
  416. # checking, etc.
  417. if ( 'initial' == $this->formtype || $this->firsttime ) {
  418. if ( $this->initialiseForm() === false) {
  419. $this->noSuchSectionPage();
  420. wfProfileOut( __METHOD__."-business-end" );
  421. wfProfileOut( __METHOD__ );
  422. return;
  423. }
  424. if ( !$this->mTitle->getArticleId() )
  425. wfRunHooks( 'EditFormPreloadText', array( &$this->textbox1, &$this->mTitle ) );
  426. }
  427. $this->showEditForm();
  428. wfProfileOut( __METHOD__."-business-end" );
  429. wfProfileOut( __METHOD__ );
  430. }
  431. protected function getEditPermissionErrors() {
  432. global $wgUser;
  433. $permErrors = $this->mTitle->getUserPermissionsErrors( 'edit', $wgUser );
  434. # Can this title be created?
  435. if ( !$this->mTitle->exists() ) {
  436. $permErrors = array_merge( $permErrors,
  437. wfArrayDiff2( $this->mTitle->getUserPermissionsErrors( 'create', $wgUser ), $permErrors ) );
  438. }
  439. # Ignore some permissions errors when a user is just previewing/viewing diffs
  440. $remove = array();
  441. foreach( $permErrors as $error ) {
  442. if ( ($this->preview || $this->diff) &&
  443. ($error[0] == 'blockedtext' || $error[0] == 'autoblockedtext') )
  444. {
  445. $remove[] = $error;
  446. }
  447. }
  448. $permErrors = wfArrayDiff2( $permErrors, $remove );
  449. return $permErrors;
  450. }
  451. /**
  452. * Show a read-only error
  453. * Parameters are the same as OutputPage:readOnlyPage()
  454. * Redirect to the article page if redlink=1
  455. */
  456. function readOnlyPage( $source = null, $protected = false, $reasons = array(), $action = null ) {
  457. global $wgRequest, $wgOut;
  458. if ( $wgRequest->getBool( 'redlink' ) ) {
  459. // The edit page was reached via a red link.
  460. // Redirect to the article page and let them click the edit tab if
  461. // they really want a permission error.
  462. $wgOut->redirect( $this->mTitle->getFullUrl() );
  463. } else {
  464. $wgOut->readOnlyPage( $source, $protected, $reasons, $action );
  465. }
  466. }
  467. /**
  468. * Should we show a preview when the edit form is first shown?
  469. *
  470. * @return bool
  471. */
  472. protected function previewOnOpen() {
  473. global $wgRequest, $wgUser;
  474. if ( $wgRequest->getVal( 'preview' ) == 'yes' ) {
  475. // Explicit override from request
  476. return true;
  477. } elseif ( $wgRequest->getVal( 'preview' ) == 'no' ) {
  478. // Explicit override from request
  479. return false;
  480. } elseif ( $this->section == 'new' ) {
  481. // Nothing *to* preview for new sections
  482. return false;
  483. } elseif ( ( $wgRequest->getVal( 'preload' ) !== null || $this->mTitle->exists() ) && $wgUser->getOption( 'previewonfirst' ) ) {
  484. // Standard preference behaviour
  485. return true;
  486. } elseif ( !$this->mTitle->exists() && $this->mTitle->getNamespace() == NS_CATEGORY ) {
  487. // Categories are special
  488. return true;
  489. } else {
  490. return false;
  491. }
  492. }
  493. /**
  494. * @todo document
  495. * @param $request
  496. */
  497. function importFormData( &$request ) {
  498. global $wgLang, $wgUser;
  499. $fname = 'EditPage::importFormData';
  500. wfProfileIn( $fname );
  501. # Section edit can come from either the form or a link
  502. $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
  503. if ( $request->wasPosted() ) {
  504. # These fields need to be checked for encoding.
  505. # Also remove trailing whitespace, but don't remove _initial_
  506. # whitespace from the text boxes. This may be significant formatting.
  507. $this->textbox1 = $this->safeUnicodeInput( $request, 'wpTextbox1' );
  508. $this->textbox2 = $this->safeUnicodeInput( $request, 'wpTextbox2' );
  509. $this->mMetaData = rtrim( $request->getText( 'metadata' ) );
  510. # Truncate for whole multibyte characters. +5 bytes for ellipsis
  511. $this->summary = $wgLang->truncate( $request->getText( 'wpSummary' ), 250, '' );
  512. # Remove extra headings from summaries and new sections.
  513. $this->summary = preg_replace('/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->summary);
  514. $this->edittime = $request->getVal( 'wpEdittime' );
  515. $this->starttime = $request->getVal( 'wpStarttime' );
  516. $this->scrolltop = $request->getIntOrNull( 'wpScrolltop' );
  517. if ( is_null( $this->edittime ) ) {
  518. # If the form is incomplete, force to preview.
  519. wfDebug( "$fname: Form data appears to be incomplete\n" );
  520. wfDebug( "POST DATA: " . var_export( $_POST, true ) . "\n" );
  521. $this->preview = true;
  522. } else {
  523. /* Fallback for live preview */
  524. $this->preview = $request->getCheck( 'wpPreview' ) || $request->getCheck( 'wpLivePreview' );
  525. $this->diff = $request->getCheck( 'wpDiff' );
  526. // Remember whether a save was requested, so we can indicate
  527. // if we forced preview due to session failure.
  528. $this->mTriedSave = !$this->preview;
  529. if ( $this->tokenOk( $request ) ) {
  530. # Some browsers will not report any submit button
  531. # if the user hits enter in the comment box.
  532. # The unmarked state will be assumed to be a save,
  533. # if the form seems otherwise complete.
  534. wfDebug( "$fname: Passed token check.\n" );
  535. } else if ( $this->diff ) {
  536. # Failed token check, but only requested "Show Changes".
  537. wfDebug( "$fname: Failed token check; Show Changes requested.\n" );
  538. } else {
  539. # Page might be a hack attempt posted from
  540. # an external site. Preview instead of saving.
  541. wfDebug( "$fname: Failed token check; forcing preview\n" );
  542. $this->preview = true;
  543. }
  544. }
  545. $this->save = !$this->preview && !$this->diff;
  546. if ( !preg_match( '/^\d{14}$/', $this->edittime )) {
  547. $this->edittime = null;
  548. }
  549. if ( !preg_match( '/^\d{14}$/', $this->starttime )) {
  550. $this->starttime = null;
  551. }
  552. $this->recreate = $request->getCheck( 'wpRecreate' );
  553. $this->minoredit = $request->getCheck( 'wpMinoredit' );
  554. $this->watchthis = $request->getCheck( 'wpWatchthis' );
  555. # Don't force edit summaries when a user is editing their own user or talk page
  556. if ( ( $this->mTitle->mNamespace == NS_USER || $this->mTitle->mNamespace == NS_USER_TALK ) &&
  557. $this->mTitle->getText() == $wgUser->getName() )
  558. {
  559. $this->allowBlankSummary = true;
  560. } else {
  561. $this->allowBlankSummary = $request->getBool( 'wpIgnoreBlankSummary' ) || !$wgUser->getOption( 'forceeditsummary');
  562. }
  563. $this->autoSumm = $request->getText( 'wpAutoSummary' );
  564. } else {
  565. # Not a posted form? Start with nothing.
  566. wfDebug( "$fname: Not a posted form.\n" );
  567. $this->textbox1 = '';
  568. $this->textbox2 = '';
  569. $this->mMetaData = '';
  570. $this->summary = '';
  571. $this->edittime = '';
  572. $this->starttime = wfTimestampNow();
  573. $this->edit = false;
  574. $this->preview = false;
  575. $this->save = false;
  576. $this->diff = false;
  577. $this->minoredit = false;
  578. $this->watchthis = false;
  579. $this->recreate = false;
  580. if ( $this->section == 'new' && $request->getVal( 'preloadtitle' ) ) {
  581. $this->summary = $request->getVal( 'preloadtitle' );
  582. }
  583. elseif ( $this->section != 'new' && $request->getVal( 'summary' ) ) {
  584. $this->summary = $request->getText( 'summary' );
  585. }
  586. if ( $request->getVal( 'minor' ) ) {
  587. $this->minoredit = true;
  588. }
  589. }
  590. $this->oldid = $request->getInt( 'oldid' );
  591. $this->live = $request->getCheck( 'live' );
  592. $this->editintro = $request->getText( 'editintro' );
  593. wfProfileOut( $fname );
  594. }
  595. /**
  596. * Make sure the form isn't faking a user's credentials.
  597. *
  598. * @param $request WebRequest
  599. * @return bool
  600. * @private
  601. */
  602. function tokenOk( &$request ) {
  603. global $wgUser;
  604. $token = $request->getVal( 'wpEditToken' );
  605. $this->mTokenOk = $wgUser->matchEditToken( $token );
  606. $this->mTokenOkExceptSuffix = $wgUser->matchEditTokenNoSuffix( $token );
  607. return $this->mTokenOk;
  608. }
  609. /**
  610. * Show all applicable editing introductions
  611. */
  612. protected function showIntro() {
  613. global $wgOut, $wgUser;
  614. if ( $this->suppressIntro ) {
  615. return;
  616. }
  617. $namespace = $this->mTitle->getNamespace();
  618. if ( $namespace == NS_MEDIAWIKI ) {
  619. # Show a warning if editing an interface message
  620. $wgOut->wrapWikiMsg( "<div class='mw-editinginterface'>\n$1</div>", 'editinginterface' );
  621. }
  622. # Show a warning message when someone creates/edits a user (talk) page but the user does not exists
  623. if ( $namespace == NS_USER || $namespace == NS_USER_TALK ) {
  624. $parts = explode( '/', $this->mTitle->getText(), 2 );
  625. $username = $parts[0];
  626. $id = User::idFromName( $username );
  627. $ip = User::isIP( $username );
  628. if ( $id == 0 && !$ip ) {
  629. $wgOut->wrapWikiMsg( '<div class="mw-userpage-userdoesnotexist error">$1</div>',
  630. array( 'userpage-userdoesnotexist', $username ) );
  631. }
  632. }
  633. # Try to add a custom edit intro, or use the standard one if this is not possible.
  634. if ( !$this->showCustomIntro() && !$this->mTitle->exists() ) {
  635. if ( $wgUser->isLoggedIn() ) {
  636. $wgOut->wrapWikiMsg( '<div class="mw-newarticletext">$1</div>', 'newarticletext' );
  637. } else {
  638. $wgOut->wrapWikiMsg( '<div class="mw-newarticletextanon">$1</div>', 'newarticletextanon' );
  639. }
  640. }
  641. # Give a notice if the user is editing a deleted page...
  642. if ( !$this->mTitle->exists() ) {
  643. $this->showDeletionLog( $wgOut );
  644. }
  645. }
  646. /**
  647. * Attempt to show a custom editing introduction, if supplied
  648. *
  649. * @return bool
  650. */
  651. protected function showCustomIntro() {
  652. if ( $this->editintro ) {
  653. $title = Title::newFromText( $this->editintro );
  654. if ( $title instanceof Title && $title->exists() && $title->userCanRead() ) {
  655. global $wgOut;
  656. $revision = Revision::newFromTitle( $title );
  657. $wgOut->addWikiTextTitleTidy( $revision->getText(), $this->mTitle );
  658. return true;
  659. } else {
  660. return false;
  661. }
  662. } else {
  663. return false;
  664. }
  665. }
  666. /**
  667. * Attempt submission (no UI)
  668. * @return one of the constants describing the result
  669. */
  670. function internalAttemptSave( &$result, $bot = false ) {
  671. global $wgFilterCallback, $wgUser, $wgOut, $wgParser;
  672. global $wgMaxArticleSize;
  673. $fname = 'EditPage::attemptSave';
  674. wfProfileIn( $fname );
  675. wfProfileIn( "$fname-checks" );
  676. if ( !wfRunHooks( 'EditPage::attemptSave', array( &$this ) ) )
  677. {
  678. wfDebug( "Hook 'EditPage::attemptSave' aborted article saving\n" );
  679. return self::AS_HOOK_ERROR;
  680. }
  681. # Check image redirect
  682. if ( $this->mTitle->getNamespace() == NS_FILE &&
  683. Title::newFromRedirect( $this->textbox1 ) instanceof Title &&
  684. !$wgUser->isAllowed( 'upload' ) ) {
  685. if ( $wgUser->isAnon() ) {
  686. return self::AS_IMAGE_REDIRECT_ANON;
  687. } else {
  688. return self::AS_IMAGE_REDIRECT_LOGGED;
  689. }
  690. }
  691. # Reintegrate metadata
  692. if ( $this->mMetaData != '' ) $this->textbox1 .= "\n" . $this->mMetaData ;
  693. $this->mMetaData = '' ;
  694. # Check for spam
  695. $match = self::matchSummarySpamRegex( $this->summary );
  696. if ( $match === false ) {
  697. $match = self::matchSpamRegex( $this->textbox1 );
  698. }
  699. if ( $match !== false ) {
  700. $result['spam'] = $match;
  701. $ip = wfGetIP();
  702. $pdbk = $this->mTitle->getPrefixedDBkey();
  703. $match = str_replace( "\n", '', $match );
  704. wfDebugLog( 'SpamRegex', "$ip spam regex hit [[$pdbk]]: \"$match\"" );
  705. wfProfileOut( "$fname-checks" );
  706. wfProfileOut( $fname );
  707. return self::AS_SPAM_ERROR;
  708. }
  709. if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle, $this->textbox1, $this->section, $this->hookError, $this->summary ) ) {
  710. # Error messages or other handling should be performed by the filter function
  711. wfProfileOut( "$fname-checks" );
  712. wfProfileOut( $fname );
  713. return self::AS_FILTERING;
  714. }
  715. if ( !wfRunHooks( 'EditFilter', array( $this, $this->textbox1, $this->section, &$this->hookError, $this->summary ) ) ) {
  716. # Error messages etc. could be handled within the hook...
  717. wfProfileOut( "$fname-checks" );
  718. wfProfileOut( $fname );
  719. return self::AS_HOOK_ERROR;
  720. } elseif ( $this->hookError != '' ) {
  721. # ...or the hook could be expecting us to produce an error
  722. wfProfileOut( "$fname-checks" );
  723. wfProfileOut( $fname );
  724. return self::AS_HOOK_ERROR_EXPECTED;
  725. }
  726. if ( $wgUser->isBlockedFrom( $this->mTitle, false ) ) {
  727. # Check block state against master, thus 'false'.
  728. wfProfileOut( "$fname-checks" );
  729. wfProfileOut( $fname );
  730. return self::AS_BLOCKED_PAGE_FOR_USER;
  731. }
  732. $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
  733. if ( $this->kblength > $wgMaxArticleSize ) {
  734. // Error will be displayed by showEditForm()
  735. $this->tooBig = true;
  736. wfProfileOut( "$fname-checks" );
  737. wfProfileOut( $fname );
  738. return self::AS_CONTENT_TOO_BIG;
  739. }
  740. if ( !$wgUser->isAllowed('edit') ) {
  741. if ( $wgUser->isAnon() ) {
  742. wfProfileOut( "$fname-checks" );
  743. wfProfileOut( $fname );
  744. return self::AS_READ_ONLY_PAGE_ANON;
  745. }
  746. else {
  747. wfProfileOut( "$fname-checks" );
  748. wfProfileOut( $fname );
  749. return self::AS_READ_ONLY_PAGE_LOGGED;
  750. }
  751. }
  752. if ( wfReadOnly() ) {
  753. wfProfileOut( "$fname-checks" );
  754. wfProfileOut( $fname );
  755. return self::AS_READ_ONLY_PAGE;
  756. }
  757. if ( $wgUser->pingLimiter() ) {
  758. wfProfileOut( "$fname-checks" );
  759. wfProfileOut( $fname );
  760. return self::AS_RATE_LIMITED;
  761. }
  762. # If the article has been deleted while editing, don't save it without
  763. # confirmation
  764. if ( $this->wasDeletedSinceLastEdit() && !$this->recreate ) {
  765. wfProfileOut( "$fname-checks" );
  766. wfProfileOut( $fname );
  767. return self::AS_ARTICLE_WAS_DELETED;
  768. }
  769. wfProfileOut( "$fname-checks" );
  770. # If article is new, insert it.
  771. $aid = $this->mTitle->getArticleID( GAID_FOR_UPDATE );
  772. if ( 0 == $aid ) {
  773. // Late check for create permission, just in case *PARANOIA*
  774. if ( !$this->mTitle->userCan( 'create' ) ) {
  775. wfDebug( "$fname: no create permission\n" );
  776. wfProfileOut( $fname );
  777. return self::AS_NO_CREATE_PERMISSION;
  778. }
  779. # Don't save a new article if it's blank.
  780. if ( '' == $this->textbox1 ) {
  781. wfProfileOut( $fname );
  782. return self::AS_BLANK_ARTICLE;
  783. }
  784. // Run post-section-merge edit filter
  785. if ( !wfRunHooks( 'EditFilterMerged', array( $this, $this->textbox1, &$this->hookError, $this->summary ) ) ) {
  786. # Error messages etc. could be handled within the hook...
  787. wfProfileOut( $fname );
  788. return self::AS_HOOK_ERROR;
  789. }
  790. # Handle the user preference to force summaries here. Check if it's not a redirect.
  791. if ( !$this->allowBlankSummary && !Title::newFromRedirect( $this->textbox1 ) ) {
  792. if ( md5( $this->summary ) == $this->autoSumm ) {
  793. $this->missingSummary = true;
  794. wfProfileOut( $fname );
  795. return self::AS_SUMMARY_NEEDED;
  796. }
  797. }
  798. $isComment = ( $this->section == 'new' );
  799. $this->mArticle->insertNewArticle( $this->textbox1, $this->summary,
  800. $this->minoredit, $this->watchthis, false, $isComment, $bot );
  801. wfProfileOut( $fname );
  802. return self::AS_SUCCESS_NEW_ARTICLE;
  803. }
  804. # Article exists. Check for edit conflict.
  805. $this->mArticle->clear(); # Force reload of dates, etc.
  806. $this->mArticle->forUpdate( true ); # Lock the article
  807. wfDebug("timestamp: {$this->mArticle->getTimestamp()}, edittime: {$this->edittime}\n");
  808. if ( $this->mArticle->getTimestamp() != $this->edittime ) {
  809. $this->isConflict = true;
  810. if ( $this->section == 'new' ) {
  811. if ( $this->mArticle->getUserText() == $wgUser->getName() &&
  812. $this->mArticle->getComment() == $this->summary ) {
  813. // Probably a duplicate submission of a new comment.
  814. // This can happen when squid resends a request after
  815. // a timeout but the first one actually went through.
  816. wfDebug( "EditPage::editForm duplicate new section submission; trigger edit conflict!\n" );
  817. } else {
  818. // New comment; suppress conflict.
  819. $this->isConflict = false;
  820. wfDebug( "EditPage::editForm conflict suppressed; new section\n" );
  821. }
  822. }
  823. }
  824. $userid = $wgUser->getId();
  825. # Suppress edit conflict with self, except for section edits where merging is required.
  826. if ( $this->isConflict && $this->section == '' && $this->userWasLastToEdit($userid,$this->edittime) ) {
  827. wfDebug( "EditPage::editForm Suppressing edit conflict, same user.\n" );
  828. $this->isConflict = false;
  829. }
  830. if ( $this->isConflict ) {
  831. wfDebug( "EditPage::editForm conflict! getting section '$this->section' for time '$this->edittime' (article time '" .
  832. $this->mArticle->getTimestamp() . "')\n" );
  833. $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $this->summary, $this->edittime );
  834. } else {
  835. wfDebug( "EditPage::editForm getting section '$this->section'\n" );
  836. $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $this->summary );
  837. }
  838. if ( is_null( $text ) ) {
  839. wfDebug( "EditPage::editForm activating conflict; section replace failed.\n" );
  840. $this->isConflict = true;
  841. $text = $this->textbox1; // do not try to merge here!
  842. } else if ( $this->isConflict ) {
  843. # Attempt merge
  844. if ( $this->mergeChangesInto( $text ) ) {
  845. // Successful merge! Maybe we should tell the user the good news?
  846. $this->isConflict = false;
  847. wfDebug( "EditPage::editForm Suppressing edit conflict, successful merge.\n" );
  848. } else {
  849. $this->section = '';
  850. $this->textbox1 = $text;
  851. wfDebug( "EditPage::editForm Keeping edit conflict, failed merge.\n" );
  852. }
  853. }
  854. if ( $this->isConflict ) {
  855. wfProfileOut( $fname );
  856. return self::AS_CONFLICT_DETECTED;
  857. }
  858. $oldtext = $this->mArticle->getContent();
  859. // Run post-section-merge edit filter
  860. if ( !wfRunHooks( 'EditFilterMerged', array( $this, $text, &$this->hookError, $this->summary ) ) ) {
  861. # Error messages etc. could be handled within the hook...
  862. wfProfileOut( $fname );
  863. return self::AS_HOOK_ERROR;
  864. }
  865. # Handle the user preference to force summaries here, but not for null edits
  866. if ( $this->section != 'new' && !$this->allowBlankSummary && 0 != strcmp($oldtext,$text)
  867. && !Title::newFromRedirect( $text ) ) # check if it's not a redirect
  868. {
  869. if ( md5( $this->summary ) == $this->autoSumm ) {
  870. $this->missingSummary = true;
  871. wfProfileOut( $fname );
  872. return self::AS_SUMMARY_NEEDED;
  873. }
  874. }
  875. # And a similar thing for new sections
  876. if ( $this->section == 'new' && !$this->allowBlankSummary ) {
  877. if (trim($this->summary) == '') {
  878. $this->missingSummary = true;
  879. wfProfileOut( $fname );
  880. return self::AS_SUMMARY_NEEDED;
  881. }
  882. }
  883. # All's well
  884. wfProfileIn( "$fname-sectionanchor" );
  885. $sectionanchor = '';
  886. if ( $this->section == 'new' ) {
  887. if ( $this->textbox1 == '' ) {
  888. $this->missingComment = true;
  889. return self::AS_TEXTBOX_EMPTY;
  890. }
  891. if ( $this->summary != '' ) {
  892. $sectionanchor = $wgParser->guessSectionNameFromWikiText( $this->summary );
  893. # This is a new section, so create a link to the new section
  894. # in the revision summary.
  895. $cleanSummary = $wgParser->stripSectionName( $this->summary );
  896. $this->summary = wfMsgForContent( 'newsectionsummary', $cleanSummary );
  897. }
  898. } elseif ( $this->section != '' ) {
  899. # Try to get a section anchor from the section source, redirect to edited section if header found
  900. # XXX: might be better to integrate this into Article::replaceSection
  901. # for duplicate heading checking and maybe parsing
  902. $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1, $matches );
  903. # we can't deal with anchors, includes, html etc in the header for now,
  904. # headline would need to be parsed to improve this
  905. if ( $hasmatch and strlen($matches[2]) > 0 ) {
  906. $sectionanchor = $wgParser->guessSectionNameFromWikiText( $matches[2] );
  907. }
  908. }
  909. wfProfileOut( "$fname-sectionanchor" );
  910. // Save errors may fall down to the edit form, but we've now
  911. // merged the section into full text. Clear the section field
  912. // so that later submission of conflict forms won't try to
  913. // replace that into a duplicated mess.
  914. $this->textbox1 = $text;
  915. $this->section = '';
  916. // Check for length errors again now that the section is merged in
  917. $this->kblength = (int)(strlen( $text ) / 1024);
  918. if ( $this->kblength > $wgMaxArticleSize ) {
  919. $this->tooBig = true;
  920. wfProfileOut( $fname );
  921. return self::AS_MAX_ARTICLE_SIZE_EXCEEDED;
  922. }
  923. # update the article here
  924. if ( $this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
  925. $this->watchthis, $bot, $sectionanchor ) )
  926. {
  927. wfProfileOut( $fname );
  928. return self::AS_SUCCESS_UPDATE;
  929. } else {
  930. $this->isConflict = true;
  931. }
  932. wfProfileOut( $fname );
  933. return self::AS_END;
  934. }
  935. /**
  936. * Check if no edits were made by other users since
  937. * the time a user started editing the page. Limit to
  938. * 50 revisions for the sake of performance.
  939. */
  940. protected function userWasLastToEdit( $id, $edittime ) {
  941. if( !$id ) return false;
  942. $dbw = wfGetDB( DB_MASTER );
  943. $res = $dbw->select( 'revision',
  944. 'rev_user',
  945. array(
  946. 'rev_page' => $this->mArticle->getId(),
  947. 'rev_timestamp > '.$dbw->addQuotes( $dbw->timestamp($edittime) )
  948. ),
  949. __METHOD__,
  950. array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
  951. while( $row = $res->fetchObject() ) {
  952. if( $row->rev_user != $id ) {
  953. return false;
  954. }
  955. }
  956. return true;
  957. }
  958. /**
  959. * Check given input text against $wgSpamRegex, and return the text of the first match.
  960. * @return mixed -- matching string or false
  961. */
  962. public static function matchSpamRegex( $text ) {
  963. global $wgSpamRegex;
  964. // For back compatibility, $wgSpamRegex may be a single string or an array of regexes.
  965. $regexes = (array)$wgSpamRegex;
  966. return self::matchSpamRegexInternal( $text, $regexes );
  967. }
  968. /**
  969. * Check given input text against $wgSpamRegex, and return the text of the first match.
  970. * @return mixed -- matching string or false
  971. */
  972. public static function matchSummarySpamRegex( $text ) {
  973. global $wgSummarySpamRegex;
  974. $regexes = (array)$wgSummarySpamRegex;
  975. return self::matchSpamRegexInternal( $text, $regexes );
  976. }
  977. protected static function matchSpamRegexInternal( $text, $regexes ) {
  978. foreach( $regexes as $regex ) {
  979. $matches = array();
  980. if( preg_match( $regex, $text, $matches ) ) {
  981. return $matches[0];
  982. }
  983. }
  984. return false;
  985. }
  986. /**
  987. * Initialise form fields in the object
  988. * Called on the first invocation, e.g. when a user clicks an edit link
  989. */
  990. function initialiseForm() {
  991. $this->edittime = $this->mArticle->getTimestamp();
  992. $this->textbox1 = $this->getContent( false );
  993. if ( $this->textbox1 === false ) return false;
  994. wfProxyCheck();
  995. return true;
  996. }
  997. function setHeaders() {
  998. global $wgOut, $wgTitle;
  999. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  1000. if ( $this->formtype == 'preview' ) {
  1001. $wgOut->setPageTitleActionText( wfMsg( 'preview' ) );
  1002. }
  1003. if ( $this->isConflict ) {
  1004. $wgOut->setPageTitle( wfMsg( 'editconflict', $wgTitle->getPrefixedText() ) );
  1005. } elseif ( $this->section != '' ) {
  1006. $msg = $this->section == 'new' ? 'editingcomment' : 'editingsection';
  1007. $wgOut->setPageTitle( wfMsg( $msg, $wgTitle->getPrefixedText() ) );
  1008. } else {
  1009. # Use the title defined by DISPLAYTITLE magic word when present
  1010. if ( isset($this->mParserOutput)
  1011. && ( $dt = $this->mParserOutput->getDisplayTitle() ) !== false ) {
  1012. $title = $dt;
  1013. } else {
  1014. $title = $wgTitle->getPrefixedText();
  1015. }
  1016. $wgOut->setPageTitle( wfMsg( 'editing', $title ) );
  1017. }
  1018. }
  1019. /**
  1020. * Send the edit form and related headers to $wgOut
  1021. * @param $formCallback Optional callable that takes an OutputPage
  1022. * parameter; will be called during form output
  1023. * near the top, for captchas and the like.
  1024. */
  1025. function showEditForm( $formCallback=null ) {
  1026. global $wgOut, $wgUser, $wgLang, $wgContLang, $wgMaxArticleSize, $wgTitle, $wgRequest;
  1027. # If $wgTitle is null, that means we're in API mode.
  1028. # Some hook probably called this function without checking
  1029. # for is_null($wgTitle) first. Bail out right here so we don't
  1030. # do lots of work just to discard it right after.
  1031. if (is_null($wgTitle))
  1032. return;
  1033. $fname = 'EditPage::showEditForm';
  1034. wfProfileIn( $fname );
  1035. $sk = $wgUser->getSkin();
  1036. wfRunHooks( 'EditPage::showEditForm:initial', array( &$this ) ) ;
  1037. #need to parse the preview early so that we know which templates are used,
  1038. #otherwise users with "show preview after edit box" will get a blank list
  1039. #we parse this near the beginning so that setHeaders can do the title
  1040. #setting work instead of leaving it in getPreviewText
  1041. $previewOutput = '';
  1042. if ( $this->formtype == 'preview' ) {
  1043. $previewOutput = $this->getPreviewText();
  1044. }
  1045. $this->setHeaders();
  1046. # Enabled article-related sidebar, toplinks, etc.
  1047. $wgOut->setArticleRelated( true );
  1048. if ( $this->isConflict ) {
  1049. $wgOut->wrapWikiMsg( "<div class='mw-explainconflict'>\n$1</div>", 'explainconflict' );
  1050. $this->textbox2 = $this->textbox1;
  1051. $this->textbox1 = $this->getContent();
  1052. $this->edittime = $this->mArticle->getTimestamp();
  1053. } else {
  1054. if ( $this->section != '' && $this->section != 'new' ) {
  1055. $matches = array();
  1056. if ( !$this->summary && !$this->preview && !$this->diff ) {
  1057. preg_match( "/^(=+)(.+)\\1/mi", $this->textbox1, $matches );
  1058. if ( !empty( $matches[2] ) ) {
  1059. global $wgParser;
  1060. $this->summary = "/* " .
  1061. $wgParser->stripSectionName(trim($matches[2])) .
  1062. " */ ";
  1063. }
  1064. }
  1065. }
  1066. if ( $this->missingComment ) {
  1067. $wgOut->wrapWikiMsg( '<div id="mw-missingcommenttext">$1</div>', 'missingcommenttext' );
  1068. }
  1069. if ( $this->missingSummary && $this->section != 'new' ) {
  1070. $wgOut->wrapWikiMsg( '<div id="mw-missingsummary">$1</div>', 'missingsummary' );
  1071. }
  1072. if ( $this->missingSummary && $this->section == 'new' ) {
  1073. $wgOut->wrapWikiMsg( '<div id="mw-missingcommentheader">$1</div>', 'missingcommentheader' );
  1074. }
  1075. if ( $this->hookError !== '' ) {
  1076. $wgOut->addWikiText( $this->hookError );
  1077. }
  1078. if ( !$this->checkUnicodeCompliantBrowser() ) {
  1079. $wgOut->addWikiMsg( 'nonunicodebrowser' );
  1080. }
  1081. if ( isset( $this->mArticle ) && isset( $this->mArticle->mRevision ) ) {
  1082. // Let sysop know that this will make private content public if saved
  1083. if ( !$this->mArticle->mRevision->userCan( Revision::DELETED_TEXT ) ) {
  1084. $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-permission' );
  1085. } else if ( $this->mArticle->mRevision->isDeleted( Revision::DELETED_TEXT ) ) {
  1086. $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-view' );
  1087. }
  1088. if ( !$this->mArticle->mRevision->isCurrent() ) {
  1089. $this->mArticle->setOldSubtitle( $this->mArticle->mRevision->getId() );
  1090. $wgOut->addWikiMsg( 'editingold' );
  1091. }
  1092. }
  1093. }
  1094. if ( wfReadOnly() ) {
  1095. $wgOut->wrapWikiMsg( "<div id=\"mw-read-only-warning\">\n$1\n</div>", array( 'readonlywarning', wfReadOnlyReason() ) );
  1096. } elseif ( $wgUser->isAnon() && $this->formtype != 'preview' ) {
  1097. $wgOut->wrapWikiMsg( '<div id="mw-anon-edit-warning">$1</div>', 'anoneditwarning' );
  1098. } else {
  1099. if ( $this->isCssJsSubpage ) {
  1100. # Check the skin exists
  1101. if ( $this->isValidCssJsSubpage ) {
  1102. if ( $this->formtype !== 'preview' ) {
  1103. $wgOut->addWikiMsg( 'usercssjsyoucanpreview' );
  1104. }
  1105. } else {
  1106. $wgOut->addWikiMsg( 'userinvalidcssjstitle', $wgTitle->getSkinFromCssJsSubpage() );
  1107. }
  1108. }
  1109. }
  1110. $classes = array(); // Textarea CSS
  1111. if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
  1112. } elseif ( $this->mTitle->isProtected( 'edit' ) ) {
  1113. # Is the title semi-protected?
  1114. if ( $this->mTitle->isSemiProtected() ) {
  1115. $noticeMsg = 'semiprotectedpagewarning';
  1116. $classes[] = 'mw-textarea-sprotected';
  1117. } else {
  1118. # Then it must be protected based on static groups (regular)
  1119. $noticeMsg = 'protectedpagewarning';
  1120. $classes[] = 'mw-textarea-protected';
  1121. }
  1122. $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
  1123. $wgOut->addWikiMsg( $noticeMsg );
  1124. LogEventsList::showLogExtract( $wgOut, 'protect', $this->mTitle->getPrefixedText(), '', 1 );
  1125. $wgOut->addHTML( "</div>\n" );
  1126. }
  1127. if ( $this->mTitle->isCascadeProtected() ) {
  1128. # Is this page under cascading protection from some source pages?
  1129. list($cascadeSources, /* $restrictions */) = $this->mTitle->getCascadeProtectionSources();
  1130. $notice = "<div class='mw-cascadeprotectedwarning'>$1\n";
  1131. $cascadeSourcesCount = count( $cascadeSources );
  1132. if ( $cascadeSourcesCount > 0 ) {
  1133. # Explain, and list the titles responsible
  1134. foreach( $cascadeSources as $page ) {
  1135. $notice .= '* [[:' . $page->getPrefixedText() . "]]\n";
  1136. }
  1137. }
  1138. $notice .= '</div>';
  1139. $wgOut->wrapWikiMsg( $notice, array( 'cascadeprotectedwarning', $cascadeSourcesCount ) );
  1140. }
  1141. if ( !$this->mTitle->exists() && $this->mTitle->getRestrictions( 'create' ) ) {
  1142. $wgOut->wrapWikiMsg( '<div class="mw-titleprotectedwarning">$1</div>', 'titleprotectedwarning' );
  1143. }
  1144. if ( $this->kblength === false ) {
  1145. $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
  1146. }
  1147. if ( $this->tooBig || $this->kblength > $wgMaxArticleSize ) {
  1148. $wgOut->addHTML( "<div class='error' id='mw-edit-longpageerror'>\n" );
  1149. $wgOut->addWikiMsg( 'longpageerror', $wgLang->formatNum( $this->kblength ), $wgLang->formatNum( $wgMaxArticleSize ) );
  1150. $wgOut->addHTML( "</div>\n" );
  1151. } elseif ( $this->kblength > 29 ) {
  1152. $wgOut->addHTML( "<div id='mw-edit-longpagewarning'>\n" );
  1153. $wgOut->addWikiMsg( 'longpagewarning', $wgLang->formatNum( $this->kblength ) );
  1154. $wgOut->addHTML( "</div>\n" );
  1155. }
  1156. $q = 'action='.$this->action;
  1157. #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
  1158. $action = $wgTitle->escapeLocalURL( $q );
  1159. $summary = wfMsg( 'summary' );
  1160. $subject = wfMsg( 'subject' );
  1161. $cancel = $sk->makeKnownLink( $wgTitle->getPrefixedText(),
  1162. wfMsgExt('cancel', array('parseinline')) );
  1163. $separator = wfMsgExt( 'pipe-separator' , 'escapenoentities' );
  1164. $edithelpurl = Skin::makeInternalOrExternalUrl( wfMsgForContent( 'edithelppage' ));
  1165. $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
  1166. htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
  1167. htmlspecialchars( wfMsg( 'newwindow' ) );
  1168. global $wgRightsText;
  1169. if ( $wgRightsText ) {
  1170. $copywarnMsg = array( 'copyrightwarning',
  1171. '[[' . wfMsgForContent( 'copyrightpage' ) . ']]',
  1172. $wgRightsText );
  1173. } else {
  1174. $copywarnMsg = array( 'copyrightwarning2',
  1175. '[[' . wfMsgForContent( 'copyrightpage' ) . ']]' );
  1176. }
  1177. if ( $wgUser->getOption('showtoolbar') and !$this->isCssJsSubpage ) {
  1178. # prepare toolbar for edit buttons
  1179. $toolbar = EditPage::getEditToolbar();
  1180. } else {
  1181. $toolbar = '';
  1182. }
  1183. // activate checkboxes if user wants them to be always active
  1184. if ( !$this->preview && !$this->diff ) {
  1185. # Sort out the "watch" checkbox
  1186. if ( $wgUser->getOption( 'watchdefault' ) ) {
  1187. # Watch all edits
  1188. $this->watchthis = true;
  1189. } elseif ( $wgUser->getOption( 'watchcreations' ) && !$this->mTitle->exists() ) {
  1190. # Watch creations
  1191. $this->watchthis = true;
  1192. } elseif ( $this->mTitle->userIsWatching() ) {
  1193. # Already watched
  1194. $this->watchthis = true;
  1195. }
  1196. # May be overriden by request parameters
  1197. if( $wgRequest->getBool( 'watchthis' ) ) {
  1198. $this->watchthis = true;
  1199. }
  1200. if ( $wgUser->getOption( 'minordefault' ) ) $this->minoredit = true;
  1201. }
  1202. $wgOut->addHTML( $this->editFormPageTop );
  1203. if ( $wgUser->getOption( 'previewontop' ) ) {
  1204. $this->displayPreviewArea( $previewOutput, true );
  1205. }
  1206. $wgOut->addHTML( $this->editFormTextTop );
  1207. # if this is a comment, show a subject line at the top, which is also the edit summary.
  1208. # Otherwise, show a summary field at the bottom
  1209. $summarytext = $wgContLang->recodeForEdit( $this->summary );
  1210. # If a blank edit summary was previously provided, and the appropriate
  1211. # user preference is active, pass a hidden tag as wpIgnoreBlankSummary. This will stop the
  1212. # user being bounced back more than once in the event that a summary
  1213. # is not required.
  1214. #####
  1215. # For a bit more sophisticated detection of blank summaries, hash the
  1216. # automatic one and pass that in the hidden field wpAutoSummary.
  1217. $summaryhiddens = '';
  1218. if ( $this->missingSummary ) $summaryhiddens .= Xml::hidden( 'wpIgnoreBlankSummary', true );
  1219. $autosumm = $this->autoSumm ? $this->autoSumm : md5( $this->summary );
  1220. $summaryhiddens .= Xml::hidden( 'wpAutoSummary', $autosumm );
  1221. if ( $this->section == 'new' ) {
  1222. $commentsubject = '';
  1223. if ( !$wgRequest->getBool( 'nosummary' ) ) {
  1224. # Add a class if 'missingsummary' is triggered to allow styling of the summary line
  1225. $summaryClass = $this->missingSummary ? 'mw-summarymissed' : 'mw-summary';
  1226. $commentsubject =
  1227. Xml::tags( 'label', array( 'for' => 'wpSummary' ), $subject );
  1228. $commentsubject =
  1229. Xml::tags( 'span', array( 'class' => $summaryClass, 'id' => "wpSummaryLabel" ),
  1230. $commentsubject );
  1231. $commentsubject .= '&nbsp;';
  1232. $commentsubject .= Xml::input( 'wpSummary',
  1233. 60,
  1234. $summarytext,
  1235. array(
  1236. 'id' => 'wpSummary',
  1237. 'maxlength' => '200',
  1238. 'tabindex' => '1'
  1239. ) );
  1240. }
  1241. $editsummary = "<div class='editOptions'>\n";
  1242. global $wgParser;
  1243. $formattedSummary = wfMsgForContent( 'newsectionsummary', $wgParser->stripSectionName( $this->summary ) );
  1244. $subjectpreview = $summarytext && $this->preview ? "<div class=\"mw-summary-preview\">". wfMsg('subject-preview') . $sk->commentBlock( $formattedSummary, $this->mTitle, true )."</div>\n" : '';
  1245. $summarypreview = '';
  1246. } else {
  1247. $commentsubject = '';
  1248. # Add a class if 'missingsummary' is triggered to allow styling of the summary line
  1249. $summaryClass = $this->missingSummary ? 'mw-summarymissed' : 'mw-summary';
  1250. $editsummary = Xml::tags( 'label', array( 'for' => 'wpSummary' ), $summary );
  1251. $editsummary = Xml::tags( 'span', array( 'class' => $summaryClass, 'id' => "wpSummaryLabel" ),
  1252. $editsummary ) . ' ';
  1253. $editsummary .= Xml::input( 'wpSummary',
  1254. 60,
  1255. $summarytext,
  1256. array(
  1257. 'id' => 'wpSummary',
  1258. 'maxlength' => '200',
  1259. 'tabindex' => '1'
  1260. ) );
  1261. // No idea where this is closed.
  1262. $editsummary = Xml::openElement( 'div', array( 'class' => 'editOptions' ) )
  1263. . $editsummary . '<br/>';
  1264. $summarypreview = '';
  1265. if ( $summarytext && $this->preview ) {
  1266. $summarypreview =
  1267. Xml::tags( 'div',
  1268. array( 'class' => 'mw-summary-preview' ),
  1269. wfMsg( 'summary-preview' ) .
  1270. $sk->commentBlock( $this->summary, $this->mTitle )
  1271. );
  1272. }
  1273. $subjectpreview = '';
  1274. }
  1275. $commentsubject .= $summaryhiddens;
  1276. # Set focus to the edit box on load, except on preview or diff, where it would interfere with the display
  1277. if ( !$this->preview && !$this->diff ) {
  1278. $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
  1279. }
  1280. $templates = $this->getTemplates();
  1281. $formattedtemplates = $sk->formatTemplates( $templates, $this->preview, $this->section != '');
  1282. $hiddencats = $this->mArticle->getHiddenCategories();
  1283. $formattedhiddencats = $sk->formatHiddenCategories( $hiddencats );
  1284. global $wgUseMetadataEdit ;
  1285. if ( $wgUseMetadataEdit ) {
  1286. $metadata = $this->mMetaData ;
  1287. $metadata = htmlspecialchars( $wgContLang->recodeForEdit( $metadata ) ) ;
  1288. $top = wfMsgWikiHtml( 'metadata_help' );
  1289. /* ToDo: Replace with clean code */
  1290. $ew = $wgUser->getOption( 'editwidth' );
  1291. if ( $ew ) $ew = " style=\"width:100%\"";
  1292. else $ew = '';
  1293. $cols = $wgUser->getIntOption( 'cols' );
  1294. /* /ToDo */
  1295. $metadata = $top . "<textarea name='metadata' rows='3' cols='{$cols}'{$ew}>{$metadata}</textarea>" ;
  1296. }
  1297. else $metadata = "" ;
  1298. $recreate = '';
  1299. if ( $this->wasDeletedSinceLastEdit() ) {
  1300. if ( 'save' != $this->formtype ) {
  1301. $wgOut->wrapWikiMsg(
  1302. "<div class='error mw-deleted-while-editing'>\n$1</div>",
  1303. 'deletedwhileediting' );
  1304. } else {
  1305. // Hide the toolbar and edit area, user can click preview to get it back
  1306. // Add an confirmation checkbox and explanation.
  1307. $toolbar = '';
  1308. $recreate = '<div class="mw-confirm-recreate">' .
  1309. $wgOut->parse( wfMsg( 'confirmrecreate', $this->lastDelete->user_name , $this->lastDelete->log_comment ) ) .
  1310. Xml::checkLabel( wfMsg( 'recreate' ), 'wpRecreate', 'wpRecreate', false,
  1311. array( 'title' => $sk->titleAttrib( 'recreate' ), 'tabindex' => 1, 'id' => 'wpRecreate' )
  1312. ) . '</div>';
  1313. }
  1314. }
  1315. $tabindex = 2;
  1316. $checkboxes = $this->getCheckboxes( $tabindex, $sk,
  1317. array( 'minor' => $this->minoredit, 'watch' => $this->watchthis ) );
  1318. $checkboxhtml = implode( $checkboxes, "\n" );
  1319. $buttons = $this->getEditButtons( $tabindex );
  1320. $buttonshtml = implode( $buttons, "\n" );
  1321. $safemodehtml = $this->checkUnicodeCompliantBrowser()
  1322. ? '' : Xml::hidden( 'safemode', '1' );
  1323. $wgOut->addHTML( <<<END
  1324. {$toolbar}
  1325. <form id="editform" name="editform" method="post" action="$action" enctype="multipart/form-data">
  1326. END
  1327. );
  1328. if ( is_callable( $formCallback ) ) {
  1329. call_user_func_array( $formCallback, array( &$wgOut ) );
  1330. }
  1331. wfRunHooks( 'EditPage::showEditForm:fields', array( &$this, &$wgOut ) );
  1332. // Put these up at the top to ensure they aren't lost on early form submission
  1333. $this->showFormBeforeText();
  1334. $wgOut->addHTML( <<<END
  1335. {$recreate}
  1336. {$commentsubject}
  1337. {$subjectpreview}
  1338. {$this->editFormTextBeforeContent}
  1339. END
  1340. );
  1341. $this->showTextbox1( $classes );
  1342. $wgOut->wrapWikiMsg( "<div id=\"editpage-copywarn\">\n$1\n</div>", $copywarnMsg );
  1343. $wgOut->addHTML( <<<END
  1344. {$this->editFormTextAfterWarn}
  1345. {$metadata}
  1346. {$editsummary}
  1347. {$summarypreview}
  1348. {$checkboxhtml}
  1349. {$safemodehtml}
  1350. END
  1351. );
  1352. $wgOut->addHTML(
  1353. "<div class='editButtons'>
  1354. {$buttonshtml}
  1355. <span class='editHelp'>{$cancel}{$separator}{$edithelp}</span>
  1356. </div><!-- editButtons -->
  1357. </div><!-- editOptions -->");
  1358. /**
  1359. * To make it harder for someone to slip a user a page
  1360. * which submits an edit form to the wiki without their
  1361. * knowledge, a random token is associated with the login
  1362. * session. If it's not passed back with the submission,
  1363. * we won't save the page, or render user JavaScript and
  1364. * CSS previews.
  1365. *
  1366. * For anon editors, who may not have a session, we just
  1367. * include the constant suffix to prevent editing from
  1368. * broken text-mangling proxies.
  1369. */
  1370. $token = htmlspecialchars( $wgUser->editToken() );
  1371. $wgOut->addHTML( "\n<input type='hidden' value=\"$token\" name=\"wpEditToken\" />\n" );
  1372. $this->showEditTools();
  1373. $wgOut->addHTML( <<<END
  1374. {$this->editFormTextAfterTools}
  1375. <div class='templatesUsed'>
  1376. {$formattedtemplates}
  1377. </div>
  1378. <div class='hiddencats'>
  1379. {$formattedhiddencats}
  1380. </div>
  1381. END
  1382. );
  1383. if ( $this->isConflict && wfRunHooks( 'EditPageBeforeConflictDiff', array( &$this, &$wgOut ) ) ) {
  1384. $wgOut->wrapWikiMsg( '==$1==', "yourdiff" );
  1385. $de = new DifferenceEngine( $this->mTitle );
  1386. $de->setText( $this->textbox2, $this->textbox1 );
  1387. $de->showDiff( wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
  1388. $wgOut->wrapWikiMsg( '==$1==', "yourtext" );
  1389. $this->showTextbox2();
  1390. }
  1391. $wgOut->addHTML( $this->editFormTextBottom );
  1392. $wgOut->addHTML( "</form>\n" );
  1393. if ( !$wgUser->getOption( 'previewontop' ) ) {
  1394. $this->displayPreviewArea( $previewOutput, false );
  1395. }
  1396. wfProfileOut( $fname );
  1397. }
  1398. protected function showFormBeforeText() {
  1399. global $wgOut;
  1400. $wgOut->addHTML( "
  1401. <input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
  1402. <input type='hidden' value=\"{$this->starttime}\" name=\"wpStarttime\" />\n
  1403. <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n
  1404. <input type='hidden' value=\"{$this->scrolltop}\" name=\"wpScrolltop\" id=\"wpScrolltop\" />\n" );
  1405. }
  1406. protected function showTextbox1( $classes ) {
  1407. $attribs = array( 'tabindex' => 1 );
  1408. if ( $this->wasDeletedSinceLastEdit() )
  1409. $attribs['type'] = 'hidden';
  1410. if ( !empty($classes) )
  1411. $attribs['class'] = implode(' ',$classes);
  1412. $this->showTextbox( $this->textbox1, 'wpTextbox1', $attribs );
  1413. }
  1414. protected function showTextbox2() {
  1415. $this->showTextbox( $this->textbox2, 'wpTextbox2', array( 'tabindex' => 6 ) );
  1416. }
  1417. protected function showTextbox( $content, $name, $attribs = array() ) {
  1418. global $wgOut, $wgUser;
  1419. $wikitext = $this->safeUnicodeOutput( $content );
  1420. if ( $wikitext !== '' ) {
  1421. // Ensure there's a newline at the end, otherwise adding lines
  1422. // is awkward.
  1423. // But don't add a newline if the ext is empty, or Firefox in XHTML
  1424. // mode will show an extra newline. A bit annoying.
  1425. $wikitext .= "\n";
  1426. }
  1427. $attribs['accesskey'] = ',';
  1428. $attribs['id'] = $name;
  1429. if ( $wgUser->getOption( 'editwidth' ) )
  1430. $attribs['style'] = 'width: 100%';
  1431. $wgOut->addHTML( Xml::textarea(
  1432. $name,
  1433. $wikitext,
  1434. $wgUser->getIntOption( 'cols' ), $wgUser->getIntOption( 'rows' ),
  1435. $attribs ) );
  1436. }
  1437. protected function displayPreviewArea( $previewOutput, $isOnTop = false ) {
  1438. global $wgOut;
  1439. $classes = array();
  1440. if ( $isOnTop )
  1441. $classes[] = 'ontop';
  1442. $attribs = array( 'id' => 'wikiPreview', 'class' => implode( ' ', $classes ) );
  1443. if ( $this->formtype != 'preview' )
  1444. $attribs['style'] = 'display: none;';
  1445. $wgOut->addHTML( Xml::openElement( 'div', $attribs ) );
  1446. if ( $this->formtype == 'preview' ) {
  1447. $this->showPreview( $previewOutput );
  1448. }
  1449. $wgOut->addHTML( '</div>' );
  1450. if ( $this->formtype == 'diff') {
  1451. $this->showDiff();
  1452. }
  1453. }
  1454. /**
  1455. * Append preview output to $wgOut.
  1456. * Includes category rendering if this is a category page.
  1457. *
  1458. * @param string $text The HTML to be output for the preview.
  1459. */
  1460. protected function showPreview( $text ) {
  1461. global $wgOut;
  1462. if ( $this->mTitle->getNamespace() == NS_CATEGORY) {
  1463. $this->mArticle->openShowCategory();
  1464. }
  1465. # This hook seems slightly odd here, but makes things more
  1466. # consistent for extensions.
  1467. wfRunHooks( 'OutputPageBeforeHTML',array( &$wgOut, &$text ) );
  1468. $wgOut->addHTML( $text );
  1469. if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
  1470. $this->mArticle->closeShowCategory();
  1471. }
  1472. }
  1473. /**
  1474. * Live Preview lets us fetch rendered preview page content and
  1475. * add it to the page without refreshing the whole page.
  1476. * If not supported by the browser it will fall through to the normal form
  1477. * submission method.
  1478. *
  1479. * This function outputs a script tag to support live preview, and
  1480. * returns an onclick handler which should be added to the attributes
  1481. * of the preview button
  1482. */
  1483. function doLivePreviewScript() {
  1484. global $wgOut, $wgTitle;
  1485. $wgOut->addScriptFile( 'preview.js' );
  1486. $liveAction = $wgTitle->getLocalUrl( "action={$this->action}&wpPreview=true&live=true" );
  1487. return "return !lpDoPreview(" .
  1488. "editform.wpTextbox1.value," .
  1489. '"' . $liveAction . '"' . ")";
  1490. }
  1491. protected function showEditTools() {
  1492. global $wgOut;
  1493. $wgOut->addHTML( '<div class="mw-editTools">' );
  1494. $wgOut->addWikiMsgArray( 'edittools', array(), array( 'content' ) );
  1495. $wgOut->addHTML( '</div>' );
  1496. }
  1497. protected function getLastDelete() {
  1498. $dbr = wfGetDB( DB_SLAVE );
  1499. $data = $dbr->selectRow(
  1500. array( 'logging', 'user' ),
  1501. array( 'log_type',
  1502. 'log_action',
  1503. 'log_timestamp',
  1504. 'log_user',
  1505. 'log_namespace',
  1506. 'log_title',
  1507. 'log_comment',
  1508. 'log_params',
  1509. 'log_deleted',
  1510. 'user_name' ),
  1511. array( 'log_namespace' => $this->mTitle->getNamespace(),
  1512. 'log_title' => $this->mTitle->getDBkey(),
  1513. 'log_type' => 'delete',
  1514. 'log_action' => 'delete',
  1515. 'user_id=log_user' ),
  1516. __METHOD__,
  1517. array( 'LIMIT' => 1, 'ORDER BY' => 'log_timestamp DESC' )
  1518. );
  1519. // Quick paranoid permission checks...
  1520. if( is_object($data) ) {
  1521. if( $data->log_deleted & LogPage::DELETED_USER )
  1522. $data->user_name = wfMsgHtml('rev-deleted-user');
  1523. if( $data->log_deleted & LogPage::DELETED_COMMENT )
  1524. $data->log_comment = wfMsgHtml('rev-deleted-comment');
  1525. }
  1526. return $data;
  1527. }
  1528. /**
  1529. * Get the rendered text for previewing.
  1530. * @return string
  1531. */
  1532. function getPreviewText() {
  1533. global $wgOut, $wgUser, $wgTitle, $wgParser, $wgLang, $wgContLang, $wgMessageCache;
  1534. wfProfileIn( __METHOD__ );
  1535. if ( $this->mTriedSave && !$this->mTokenOk ) {
  1536. if ( $this->mTokenOkExceptSuffix ) {
  1537. $note = wfMsg( 'token_suffix_mismatch' );
  1538. } else {
  1539. $note = wfMsg( 'session_fail_preview' );
  1540. }
  1541. } else {
  1542. $note = wfMsg( 'previewnote' );
  1543. }
  1544. $parserOptions = ParserOptions::newFromUser( $wgUser );
  1545. $parserOptions->setEditSection( false );
  1546. $parserOptions->setIsPreview( true );
  1547. $parserOptions->setIsSectionPreview( !is_null($this->section) && $this->section !== '' );
  1548. global $wgRawHtml;
  1549. if ( $wgRawHtml && !$this->mTokenOk ) {
  1550. // Could be an offsite preview attempt. This is very unsafe if
  1551. // HTML is enabled, as it could be an attack.
  1552. return $wgOut->parse( "<div class='previewnote'>" .
  1553. wfMsg( 'session_fail_preview_html' ) . "</div>" );
  1554. }
  1555. # don't parse user css/js, show message about preview
  1556. # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
  1557. if ( $this->isCssJsSubpage ) {
  1558. if (preg_match("/\\.css$/", $this->mTitle->getText() ) ) {
  1559. $previewtext = wfMsg('usercsspreview');
  1560. } else if (preg_match("/\\.js$/", $this->mTitle->getText() ) ) {
  1561. $previewtext = wfMsg('userjspreview');
  1562. }
  1563. $parserOptions->setTidy(true);
  1564. $parserOutput = $wgParser->parse( $previewtext, $this->mTitle, $parserOptions );
  1565. $previewHTML = $parserOutput->mText;
  1566. } elseif ( $rt = Title::newFromRedirectArray( $this->textbox1 ) ) {
  1567. $previewHTML = $this->mArticle->viewRedirect( $rt, false );
  1568. } else {
  1569. $toparse = $this->textbox1;
  1570. # If we're adding a comment, we need to show the
  1571. # summary as the headline
  1572. if ( $this->section=="new" && $this->summary!="" ) {
  1573. $toparse="== {$this->summary} ==\n\n".$toparse;
  1574. }
  1575. if ( $this->mMetaData != "" ) $toparse .= "\n" . $this->mMetaData;
  1576. // Parse mediawiki messages with correct target language
  1577. if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
  1578. list( /* $unused */, $lang ) = $wgMessageCache->figureMessage( $this->mTitle->getText() );
  1579. $obj = wfGetLangObj( $lang );
  1580. $parserOptions->setTargetLanguage( $obj );
  1581. }
  1582. $parserOptions->setTidy(true);
  1583. $parserOptions->enableLimitReport();
  1584. $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $toparse ),
  1585. $this->mTitle, $parserOptions );
  1586. $previewHTML = $parserOutput->getText();
  1587. $this->mParserOutput = $parserOutput;
  1588. $wgOut->addParserOutputNoText( $parserOutput );
  1589. if ( count( $parserOutput->getWarnings() ) ) {
  1590. $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() );
  1591. }
  1592. }
  1593. $previewhead = '<h2>' . htmlspecialchars( wfMsg( 'preview' ) ) . "</h2>\n" .
  1594. "<div class='previewnote'>" . $wgOut->parse( $note ) . "</div>\n";
  1595. if ( $this->isConflict ) {
  1596. $previewhead .='<h2>' . htmlspecialchars( wfMsg( 'previewconflict' ) ) . "</h2>\n";
  1597. }
  1598. wfProfileOut( __METHOD__ );
  1599. return $previewhead . $previewHTML;
  1600. }
  1601. function getTemplates() {
  1602. if ( $this->preview || $this->section != '' ) {
  1603. $templates = array();
  1604. if ( !isset($this->mParserOutput) ) return $templates;
  1605. foreach( $this->mParserOutput->getTemplates() as $ns => $template) {
  1606. foreach( array_keys( $template ) as $dbk ) {
  1607. $templates[] = Title::makeTitle($ns, $dbk);
  1608. }
  1609. }
  1610. return $templates;
  1611. } else {
  1612. return $this->mArticle->getUsedTemplates();
  1613. }
  1614. }
  1615. /**
  1616. * Call the stock "user is blocked" page
  1617. */
  1618. function blockedPage() {
  1619. global $wgOut, $wgUser;
  1620. $wgOut->blockedPage( false ); # Standard block notice on the top, don't 'return'
  1621. # If the user made changes, preserve them when showing the markup
  1622. # (This happens when a user is blocked during edit, for instance)
  1623. $first = $this->firsttime || ( !$this->save && $this->textbox1 == '' );
  1624. if ( $first ) {
  1625. $source = $this->mTitle->exists() ? $this->getContent() : false;
  1626. } else {
  1627. $source = $this->textbox1;
  1628. }
  1629. # Spit out the source or the user's modified version
  1630. if ( $source !== false ) {
  1631. $rows = $wgUser->getIntOption( 'rows' );
  1632. $cols = $wgUser->getIntOption( 'cols' );
  1633. $attribs = array( 'id' => 'wpTextbox1', 'name' => 'wpTextbox1', 'cols' => $cols, 'rows' => $rows, 'readonly' => 'readonly' );
  1634. $wgOut->addHTML( '<hr />' );
  1635. $wgOut->addWikiMsg( $first ? 'blockedoriginalsource' : 'blockededitsource', $this->mTitle->getPrefixedText() );
  1636. # Why we don't use Xml::element here?
  1637. # Is it because if $source is '', it returns <textarea />?
  1638. $wgOut->addHTML( Xml::openElement( 'textarea', $attribs ) . htmlspecialchars( $source ) . Xml::closeElement( 'textarea' ) );
  1639. }
  1640. }
  1641. /**
  1642. * Produce the stock "please login to edit pages" page
  1643. */
  1644. function userNotLoggedInPage() {
  1645. global $wgUser, $wgOut, $wgTitle;
  1646. $skin = $wgUser->getSkin();
  1647. $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
  1648. $loginLink = $skin->makeKnownLinkObj( $loginTitle, wfMsgHtml( 'loginreqlink' ), 'returnto=' . $wgTitle->getPrefixedUrl() );
  1649. $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
  1650. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  1651. $wgOut->setArticleRelated( false );
  1652. $wgOut->addHTML( wfMsgWikiHtml( 'whitelistedittext', $loginLink ) );
  1653. $wgOut->returnToMain( false, $wgTitle );
  1654. }
  1655. /**
  1656. * Creates a basic error page which informs the user that
  1657. * they have attempted to edit a nonexistent section.
  1658. */
  1659. function noSuchSectionPage() {
  1660. global $wgOut, $wgTitle;
  1661. $wgOut->setPageTitle( wfMsg( 'nosuchsectiontitle' ) );
  1662. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  1663. $wgOut->setArticleRelated( false );
  1664. $wgOut->addWikiMsg( 'nosuchsectiontext', $this->section );
  1665. $wgOut->returnToMain( false, $wgTitle );
  1666. }
  1667. /**
  1668. * Produce the stock "your edit contains spam" page
  1669. *
  1670. * @param $match Text which triggered one or more filters
  1671. */
  1672. function spamPage( $match = false ) {
  1673. global $wgOut, $wgTitle;
  1674. $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
  1675. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  1676. $wgOut->setArticleRelated( false );
  1677. $wgOut->addHTML( '<div id="spamprotected">' );
  1678. $wgOut->addWikiMsg( 'spamprotectiontext' );
  1679. if ( $match )
  1680. $wgOut->addWikiMsg( 'spamprotectionmatch', wfEscapeWikiText( $match ) );
  1681. $wgOut->addHTML( '</div>' );
  1682. $wgOut->returnToMain( false, $wgTitle );
  1683. }
  1684. /**
  1685. * @private
  1686. * @todo document
  1687. */
  1688. function mergeChangesInto( &$editText ){
  1689. $fname = 'EditPage::mergeChangesInto';
  1690. wfProfileIn( $fname );
  1691. $db = wfGetDB( DB_MASTER );
  1692. // This is the revision the editor started from
  1693. $baseRevision = $this->getBaseRevision();
  1694. if ( is_null( $baseRevision ) ) {
  1695. wfProfileOut( $fname );
  1696. return false;
  1697. }
  1698. $baseText = $baseRevision->getText();
  1699. // The current state, we want to merge updates into it
  1700. $currentRevision = Revision::loadFromTitle( $db, $this->mTitle );
  1701. if ( is_null( $currentRevision ) ) {
  1702. wfProfileOut( $fname );
  1703. return false;
  1704. }
  1705. $currentText = $currentRevision->getText();
  1706. $result = '';
  1707. if ( wfMerge( $baseText, $editText, $currentText, $result ) ) {
  1708. $editText = $result;
  1709. wfProfileOut( $fname );
  1710. return true;
  1711. } else {
  1712. wfProfileOut( $fname );
  1713. return false;
  1714. }
  1715. }
  1716. /**
  1717. * Check if the browser is on a blacklist of user-agents known to
  1718. * mangle UTF-8 data on form submission. Returns true if Unicode
  1719. * should make it through, false if it's known to be a problem.
  1720. * @return bool
  1721. * @private
  1722. */
  1723. function checkUnicodeCompliantBrowser() {
  1724. global $wgBrowserBlackList;
  1725. if ( empty( $_SERVER["HTTP_USER_AGENT"] ) ) {
  1726. // No User-Agent header sent? Trust it by default...
  1727. return true;
  1728. }
  1729. $currentbrowser = $_SERVER["HTTP_USER_AGENT"];
  1730. foreach ( $wgBrowserBlackList as $browser ) {
  1731. if ( preg_match($browser, $currentbrowser) ) {
  1732. return false;
  1733. }
  1734. }
  1735. return true;
  1736. }
  1737. /**
  1738. * @deprecated use $wgParser->stripSectionName()
  1739. */
  1740. function pseudoParseSectionAnchor( $text ) {
  1741. global $wgParser;
  1742. return $wgParser->stripSectionName( $text );
  1743. }
  1744. /**
  1745. * Format an anchor fragment as it would appear for a given section name
  1746. * @param string $text
  1747. * @return string
  1748. * @private
  1749. */
  1750. function sectionAnchor( $text ) {
  1751. global $wgParser;
  1752. return $wgParser->guessSectionNameFromWikiText( $text );
  1753. }
  1754. /**
  1755. * Shows a bulletin board style toolbar for common editing functions.
  1756. * It can be disabled in the user preferences.
  1757. * The necessary JavaScript code can be found in skins/common/edit.js.
  1758. *
  1759. * @return string
  1760. */
  1761. static function getEditToolbar() {
  1762. global $wgStylePath, $wgContLang, $wgLang, $wgJsMimeType;
  1763. /**
  1764. * toolarray an array of arrays which each include the filename of
  1765. * the button image (without path), the opening tag, the closing tag,
  1766. * and optionally a sample text that is inserted between the two when no
  1767. * selection is highlighted.
  1768. * The tip text is shown when the user moves the mouse over the button.
  1769. *
  1770. * Already here are accesskeys (key), which are not used yet until someone
  1771. * can figure out a way to make them work in IE. However, we should make
  1772. * sure these keys are not defined on the edit page.
  1773. */
  1774. $toolarray = array(
  1775. array(
  1776. 'image' => $wgLang->getImageFile('button-bold'),
  1777. 'id' => 'mw-editbutton-bold',
  1778. 'open' => '\'\'\'',
  1779. 'close' => '\'\'\'',
  1780. 'sample' => wfMsg('bold_sample'),
  1781. 'tip' => wfMsg('bold_tip'),
  1782. 'key' => 'B'
  1783. ),
  1784. array(
  1785. 'image' => $wgLang->getImageFile('button-italic'),
  1786. 'id' => 'mw-editbutton-italic',
  1787. 'open' => '\'\'',
  1788. 'close' => '\'\'',
  1789. 'sample' => wfMsg('italic_sample'),
  1790. 'tip' => wfMsg('italic_tip'),
  1791. 'key' => 'I'
  1792. ),
  1793. array(
  1794. 'image' => $wgLang->getImageFile('button-link'),
  1795. 'id' => 'mw-editbutton-link',
  1796. 'open' => '[[',
  1797. 'close' => ']]',
  1798. 'sample' => wfMsg('link_sample'),
  1799. 'tip' => wfMsg('link_tip'),
  1800. 'key' => 'L'
  1801. ),
  1802. array(
  1803. 'image' => $wgLang->getImageFile('button-extlink'),
  1804. 'id' => 'mw-editbutton-extlink',
  1805. 'open' => '[',
  1806. 'close' => ']',
  1807. 'sample' => wfMsg('extlink_sample'),
  1808. 'tip' => wfMsg('extlink_tip'),
  1809. 'key' => 'X'
  1810. ),
  1811. array(
  1812. 'image' => $wgLang->getImageFile('button-headline'),
  1813. 'id' => 'mw-editbutton-headline',
  1814. 'open' => "\n== ",
  1815. 'close' => " ==\n",
  1816. 'sample' => wfMsg('headline_sample'),
  1817. 'tip' => wfMsg('headline_tip'),
  1818. 'key' => 'H'
  1819. ),
  1820. array(
  1821. 'image' => $wgLang->getImageFile('button-image'),
  1822. 'id' => 'mw-editbutton-image',
  1823. 'open' => '[['.$wgContLang->getNsText(NS_FILE).':',
  1824. 'close' => ']]',
  1825. 'sample' => wfMsg('image_sample'),
  1826. 'tip' => wfMsg('image_tip'),
  1827. 'key' => 'D'
  1828. ),
  1829. array(
  1830. 'image' => $wgLang->getImageFile('button-media'),
  1831. 'id' => 'mw-editbutton-media',
  1832. 'open' => '[['.$wgContLang->getNsText(NS_MEDIA).':',
  1833. 'close' => ']]',
  1834. 'sample' => wfMsg('media_sample'),
  1835. 'tip' => wfMsg('media_tip'),
  1836. 'key' => 'M'
  1837. ),
  1838. array(
  1839. 'image' => $wgLang->getImageFile('button-math'),
  1840. 'id' => 'mw-editbutton-math',
  1841. 'open' => "<math>",
  1842. 'close' => "</math>",
  1843. 'sample' => wfMsg('math_sample'),
  1844. 'tip' => wfMsg('math_tip'),
  1845. 'key' => 'C'
  1846. ),
  1847. array(
  1848. 'image' => $wgLang->getImageFile('button-nowiki'),
  1849. 'id' => 'mw-editbutton-nowiki',
  1850. 'open' => "<nowiki>",
  1851. 'close' => "</nowiki>",
  1852. 'sample' => wfMsg('nowiki_sample'),
  1853. 'tip' => wfMsg('nowiki_tip'),
  1854. 'key' => 'N'
  1855. ),
  1856. array(
  1857. 'image' => $wgLang->getImageFile('button-sig'),
  1858. 'id' => 'mw-editbutton-signature',
  1859. 'open' => '--~~~~',
  1860. 'close' => '',
  1861. 'sample' => '',
  1862. 'tip' => wfMsg('sig_tip'),
  1863. 'key' => 'Y'
  1864. ),
  1865. array(
  1866. 'image' => $wgLang->getImageFile('button-hr'),
  1867. 'id' => 'mw-editbutton-hr',
  1868. 'open' => "\n----\n",
  1869. 'close' => '',
  1870. 'sample' => '',
  1871. 'tip' => wfMsg('hr_tip'),
  1872. 'key' => 'R'
  1873. )
  1874. );
  1875. $toolbar = "<div id='toolbar'>\n";
  1876. $toolbar.="<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
  1877. foreach($toolarray as $tool) {
  1878. $params = array(
  1879. $image = $wgStylePath.'/common/images/'.$tool['image'],
  1880. // Note that we use the tip both for the ALT tag and the TITLE tag of the image.
  1881. // Older browsers show a "speedtip" type message only for ALT.
  1882. // Ideally these should be different, realistically they
  1883. // probably don't need to be.
  1884. $tip = $tool['tip'],
  1885. $open = $tool['open'],
  1886. $close = $tool['close'],
  1887. $sample = $tool['sample'],
  1888. $cssId = $tool['id'],
  1889. );
  1890. $paramList = implode( ',',
  1891. array_map( array( 'Xml', 'encodeJsVar' ), $params ) );
  1892. $toolbar.="addButton($paramList);\n";
  1893. }
  1894. $toolbar.="/*]]>*/\n</script>";
  1895. $toolbar.="\n</div>";
  1896. return $toolbar;
  1897. }
  1898. /**
  1899. * Returns an array of html code of the following checkboxes:
  1900. * minor and watch
  1901. *
  1902. * @param $tabindex Current tabindex
  1903. * @param $skin Skin object
  1904. * @param $checked Array of checkbox => bool, where bool indicates the checked
  1905. * status of the checkbox
  1906. *
  1907. * @return array
  1908. */
  1909. public function getCheckboxes( &$tabindex, $skin, $checked ) {
  1910. global $wgUser;
  1911. $checkboxes = array();
  1912. $checkboxes['minor'] = '';
  1913. $minorLabel = wfMsgExt('minoredit', array('parseinline'));
  1914. if ( $wgUser->isAllowed('minoredit') ) {
  1915. $attribs = array(
  1916. 'tabindex' => ++$tabindex,
  1917. 'accesskey' => wfMsg( 'accesskey-minoredit' ),
  1918. 'id' => 'wpMinoredit',
  1919. );
  1920. $checkboxes['minor'] =
  1921. Xml::check( 'wpMinoredit', $checked['minor'], $attribs ) .
  1922. "&nbsp;<label for='wpMinoredit'".$skin->tooltip('minoredit', 'withaccess').">{$minorLabel}</label>";
  1923. }
  1924. $watchLabel = wfMsgExt('watchthis', array('parseinline'));
  1925. $checkboxes['watch'] = '';
  1926. if ( $wgUser->isLoggedIn() ) {
  1927. $attribs = array(
  1928. 'tabindex' => ++$tabindex,
  1929. 'accesskey' => wfMsg( 'accesskey-watch' ),
  1930. 'id' => 'wpWatchthis',
  1931. );
  1932. $checkboxes['watch'] =
  1933. Xml::check( 'wpWatchthis', $checked['watch'], $attribs ) .
  1934. "&nbsp;<label for='wpWatchthis'".$skin->tooltip('watch', 'withaccess').">{$watchLabel}</label>";
  1935. }
  1936. wfRunHooks( 'EditPageBeforeEditChecks', array( &$this, &$checkboxes, &$tabindex ) );
  1937. return $checkboxes;
  1938. }
  1939. /**
  1940. * Returns an array of html code of the following buttons:
  1941. * save, diff, preview and live
  1942. *
  1943. * @param $tabindex Current tabindex
  1944. *
  1945. * @return array
  1946. */
  1947. public function getEditButtons(&$tabindex) {
  1948. global $wgLivePreview, $wgUser;
  1949. $buttons = array();
  1950. $temp = array(
  1951. 'id' => 'wpSave',
  1952. 'name' => 'wpSave',
  1953. 'type' => 'submit',
  1954. 'tabindex' => ++$tabindex,
  1955. 'value' => wfMsg('savearticle'),
  1956. 'accesskey' => wfMsg('accesskey-save'),
  1957. 'title' => wfMsg( 'tooltip-save' ).' ['.wfMsg( 'accesskey-save' ).']',
  1958. );
  1959. $buttons['save'] = Xml::element('input', $temp, '');
  1960. ++$tabindex; // use the same for preview and live preview
  1961. if ( $wgLivePreview && $wgUser->getOption( 'uselivepreview' ) ) {
  1962. $temp = array(
  1963. 'id' => 'wpPreview',
  1964. 'name' => 'wpPreview',
  1965. 'type' => 'submit',
  1966. 'tabindex' => $tabindex,
  1967. 'value' => wfMsg('showpreview'),
  1968. 'accesskey' => '',
  1969. 'title' => wfMsg( 'tooltip-preview' ).' ['.wfMsg( 'accesskey-preview' ).']',
  1970. 'style' => 'display: none;',
  1971. );
  1972. $buttons['preview'] = Xml::element('input', $temp, '');
  1973. $temp = array(
  1974. 'id' => 'wpLivePreview',
  1975. 'name' => 'wpLivePreview',
  1976. 'type' => 'submit',
  1977. 'tabindex' => $tabindex,
  1978. 'value' => wfMsg('showlivepreview'),
  1979. 'accesskey' => wfMsg('accesskey-preview'),
  1980. 'title' => '',
  1981. 'onclick' => $this->doLivePreviewScript(),
  1982. );
  1983. $buttons['live'] = Xml::element('input', $temp, '');
  1984. } else {
  1985. $temp = array(
  1986. 'id' => 'wpPreview',
  1987. 'name' => 'wpPreview',
  1988. 'type' => 'submit',
  1989. 'tabindex' => $tabindex,
  1990. 'value' => wfMsg('showpreview'),
  1991. 'accesskey' => wfMsg('accesskey-preview'),
  1992. 'title' => wfMsg( 'tooltip-preview' ).' ['.wfMsg( 'accesskey-preview' ).']',
  1993. );
  1994. $buttons['preview'] = Xml::element('input', $temp, '');
  1995. $buttons['live'] = '';
  1996. }
  1997. $temp = array(
  1998. 'id' => 'wpDiff',
  1999. 'name' => 'wpDiff',
  2000. 'type' => 'submit',
  2001. 'tabindex' => ++$tabindex,
  2002. 'value' => wfMsg('showdiff'),
  2003. 'accesskey' => wfMsg('accesskey-diff'),
  2004. 'title' => wfMsg( 'tooltip-diff' ).' ['.wfMsg( 'accesskey-diff' ).']',
  2005. );
  2006. $buttons['diff'] = Xml::element('input', $temp, '');
  2007. wfRunHooks( 'EditPageBeforeEditButtons', array( &$this, &$buttons, &$tabindex ) );
  2008. return $buttons;
  2009. }
  2010. /**
  2011. * Output preview text only. This can be sucked into the edit page
  2012. * via JavaScript, and saves the server time rendering the skin as
  2013. * well as theoretically being more robust on the client (doesn't
  2014. * disturb the edit box's undo history, won't eat your text on
  2015. * failure, etc).
  2016. *
  2017. * @todo This doesn't include category or interlanguage links.
  2018. * Would need to enhance it a bit, <s>maybe wrap them in XML
  2019. * or something...</s> that might also require more skin
  2020. * initialization, so check whether that's a problem.
  2021. */
  2022. function livePreview() {
  2023. global $wgOut;
  2024. $wgOut->disable();
  2025. header( 'Content-type: text/xml; charset=utf-8' );
  2026. header( 'Cache-control: no-cache' );
  2027. $previewText = $this->getPreviewText();
  2028. #$categories = $skin->getCategoryLinks();
  2029. $s =
  2030. '<?xml version="1.0" encoding="UTF-8" ?>' . "\n" .
  2031. Xml::tags( 'livepreview', null,
  2032. Xml::element( 'preview', null, $previewText )
  2033. #. Xml::element( 'category', null, $categories )
  2034. );
  2035. echo $s;
  2036. }
  2037. /**
  2038. * Get a diff between the current contents of the edit box and the
  2039. * version of the page we're editing from.
  2040. *
  2041. * If this is a section edit, we'll replace the section as for final
  2042. * save and then make a comparison.
  2043. */
  2044. function showDiff() {
  2045. $oldtext = $this->mArticle->fetchContent();
  2046. $newtext = $this->mArticle->replaceSection(
  2047. $this->section, $this->textbox1, $this->summary, $this->edittime );
  2048. $newtext = $this->mArticle->preSaveTransform( $newtext );
  2049. $oldtitle = wfMsgExt( 'currentrev', array('parseinline') );
  2050. $newtitle = wfMsgExt( 'yourtext', array('parseinline') );
  2051. if ( $oldtext !== false || $newtext != '' ) {
  2052. $de = new DifferenceEngine( $this->mTitle );
  2053. $de->setText( $oldtext, $newtext );
  2054. $difftext = $de->getDiff( $oldtitle, $newtitle );
  2055. $de->showDiffStyle();
  2056. } else {
  2057. $difftext = '';
  2058. }
  2059. global $wgOut;
  2060. $wgOut->addHTML( '<div id="wikiDiff">' . $difftext . '</div>' );
  2061. }
  2062. /**
  2063. * Filter an input field through a Unicode de-armoring process if it
  2064. * came from an old browser with known broken Unicode editing issues.
  2065. *
  2066. * @param WebRequest $request
  2067. * @param string $field
  2068. * @return string
  2069. * @private
  2070. */
  2071. function safeUnicodeInput( $request, $field ) {
  2072. $text = rtrim( $request->getText( $field ) );
  2073. return $request->getBool( 'safemode' )
  2074. ? $this->unmakesafe( $text )
  2075. : $text;
  2076. }
  2077. /**
  2078. * Filter an output field through a Unicode armoring process if it is
  2079. * going to an old browser with known broken Unicode editing issues.
  2080. *
  2081. * @param string $text
  2082. * @return string
  2083. * @private
  2084. */
  2085. function safeUnicodeOutput( $text ) {
  2086. global $wgContLang;
  2087. $codedText = $wgContLang->recodeForEdit( $text );
  2088. return $this->checkUnicodeCompliantBrowser()
  2089. ? $codedText
  2090. : $this->makesafe( $codedText );
  2091. }
  2092. /**
  2093. * A number of web browsers are known to corrupt non-ASCII characters
  2094. * in a UTF-8 text editing environment. To protect against this,
  2095. * detected browsers will be served an armored version of the text,
  2096. * with non-ASCII chars converted to numeric HTML character references.
  2097. *
  2098. * Preexisting such character references will have a 0 added to them
  2099. * to ensure that round-trips do not alter the original data.
  2100. *
  2101. * @param string $invalue
  2102. * @return string
  2103. * @private
  2104. */
  2105. function makesafe( $invalue ) {
  2106. // Armor existing references for reversability.
  2107. $invalue = strtr( $invalue, array( "&#x" => "&#x0" ) );
  2108. $bytesleft = 0;
  2109. $result = "";
  2110. $working = 0;
  2111. for( $i = 0; $i < strlen( $invalue ); $i++ ) {
  2112. $bytevalue = ord( $invalue{$i} );
  2113. if ( $bytevalue <= 0x7F ) { //0xxx xxxx
  2114. $result .= chr( $bytevalue );
  2115. $bytesleft = 0;
  2116. } elseif ( $bytevalue <= 0xBF ) { //10xx xxxx
  2117. $working = $working << 6;
  2118. $working += ($bytevalue & 0x3F);
  2119. $bytesleft--;
  2120. if ( $bytesleft <= 0 ) {
  2121. $result .= "&#x" . strtoupper( dechex( $working ) ) . ";";
  2122. }
  2123. } elseif ( $bytevalue <= 0xDF ) { //110x xxxx
  2124. $working = $bytevalue & 0x1F;
  2125. $bytesleft = 1;
  2126. } elseif ( $bytevalue <= 0xEF ) { //1110 xxxx
  2127. $working = $bytevalue & 0x0F;
  2128. $bytesleft = 2;
  2129. } else { //1111 0xxx
  2130. $working = $bytevalue & 0x07;
  2131. $bytesleft = 3;
  2132. }
  2133. }
  2134. return $result;
  2135. }
  2136. /**
  2137. * Reverse the previously applied transliteration of non-ASCII characters
  2138. * back to UTF-8. Used to protect data from corruption by broken web browsers
  2139. * as listed in $wgBrowserBlackList.
  2140. *
  2141. * @param string $invalue
  2142. * @return string
  2143. * @private
  2144. */
  2145. function unmakesafe( $invalue ) {
  2146. $result = "";
  2147. for( $i = 0; $i < strlen( $invalue ); $i++ ) {
  2148. if ( ( substr( $invalue, $i, 3 ) == "&#x" ) && ( $invalue{$i+3} != '0' ) ) {
  2149. $i += 3;
  2150. $hexstring = "";
  2151. do {
  2152. $hexstring .= $invalue{$i};
  2153. $i++;
  2154. } while( ctype_xdigit( $invalue{$i} ) && ( $i < strlen( $invalue ) ) );
  2155. // Do some sanity checks. These aren't needed for reversability,
  2156. // but should help keep the breakage down if the editor
  2157. // breaks one of the entities whilst editing.
  2158. if ( (substr($invalue,$i,1)==";") and (strlen($hexstring) <= 6) ) {
  2159. $codepoint = hexdec($hexstring);
  2160. $result .= codepointToUtf8( $codepoint );
  2161. } else {
  2162. $result .= "&#x" . $hexstring . substr( $invalue, $i, 1 );
  2163. }
  2164. } else {
  2165. $result .= substr( $invalue, $i, 1 );
  2166. }
  2167. }
  2168. // reverse the transform that we made for reversability reasons.
  2169. return strtr( $result, array( "&#x0" => "&#x" ) );
  2170. }
  2171. function noCreatePermission() {
  2172. global $wgOut;
  2173. $wgOut->setPageTitle( wfMsg( 'nocreatetitle' ) );
  2174. $wgOut->addWikiMsg( 'nocreatetext' );
  2175. }
  2176. /**
  2177. * If there are rows in the deletion log for this page, show them,
  2178. * along with a nice little note for the user
  2179. *
  2180. * @param OutputPage $out
  2181. */
  2182. protected function showDeletionLog( $out ) {
  2183. global $wgUser;
  2184. $loglist = new LogEventsList( $wgUser->getSkin(), $out );
  2185. $pager = new LogPager( $loglist, 'delete', false, $this->mTitle->getPrefixedText() );
  2186. $count = $pager->getNumRows();
  2187. if ( $count > 0 ) {
  2188. $pager->mLimit = 10;
  2189. $out->addHTML( '<div class="mw-warning-with-logexcerpt">' );
  2190. $out->addWikiMsg( 'recreate-deleted-warn' );
  2191. $out->addHTML(
  2192. $loglist->beginLogEventsList() .
  2193. $pager->getBody() .
  2194. $loglist->endLogEventsList()
  2195. );
  2196. if($count > 10){
  2197. $out->addHTML( $wgUser->getSkin()->link(
  2198. SpecialPage::getTitleFor( 'Log' ),
  2199. wfMsgHtml( 'deletelog-fulllog' ),
  2200. array(),
  2201. array(
  2202. 'type' => 'delete',
  2203. 'page' => $this->mTitle->getPrefixedText() ) ) );
  2204. }
  2205. $out->addHTML( '</div>' );
  2206. return true;
  2207. }
  2208. return false;
  2209. }
  2210. /**
  2211. * Attempt submission
  2212. * @return bool false if output is done, true if the rest of the form should be displayed
  2213. */
  2214. function attemptSave() {
  2215. global $wgUser, $wgOut, $wgTitle, $wgRequest;
  2216. $resultDetails = false;
  2217. # Allow bots to exempt some edits from bot flagging
  2218. $bot = $wgUser->isAllowed('bot') && $wgRequest->getBool('bot',true);
  2219. $value = $this->internalAttemptSave( $resultDetails, $bot );
  2220. if ( $value == self::AS_SUCCESS_UPDATE || $value == self::AS_SUCCESS_NEW_ARTICLE ) {
  2221. $this->didSave = true;
  2222. }
  2223. switch ($value) {
  2224. case self::AS_HOOK_ERROR_EXPECTED:
  2225. case self::AS_CONTENT_TOO_BIG:
  2226. case self::AS_ARTICLE_WAS_DELETED:
  2227. case self::AS_CONFLICT_DETECTED:
  2228. case self::AS_SUMMARY_NEEDED:
  2229. case self::AS_TEXTBOX_EMPTY:
  2230. case self::AS_MAX_ARTICLE_SIZE_EXCEEDED:
  2231. case self::AS_END:
  2232. return true;
  2233. case self::AS_HOOK_ERROR:
  2234. case self::AS_FILTERING:
  2235. case self::AS_SUCCESS_NEW_ARTICLE:
  2236. case self::AS_SUCCESS_UPDATE:
  2237. return false;
  2238. case self::AS_SPAM_ERROR:
  2239. $this->spamPage ( $resultDetails['spam'] );
  2240. return false;
  2241. case self::AS_BLOCKED_PAGE_FOR_USER:
  2242. $this->blockedPage();
  2243. return false;
  2244. case self::AS_IMAGE_REDIRECT_ANON:
  2245. $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
  2246. return false;
  2247. case self::AS_READ_ONLY_PAGE_ANON:
  2248. $this->userNotLoggedInPage();
  2249. return false;
  2250. case self::AS_READ_ONLY_PAGE_LOGGED:
  2251. case self::AS_READ_ONLY_PAGE:
  2252. $wgOut->readOnlyPage();
  2253. return false;
  2254. case self::AS_RATE_LIMITED:
  2255. $wgOut->rateLimited();
  2256. return false;
  2257. case self::AS_NO_CREATE_PERMISSION;
  2258. $this->noCreatePermission();
  2259. return;
  2260. case self::AS_BLANK_ARTICLE:
  2261. $wgOut->redirect( $wgTitle->getFullURL() );
  2262. return false;
  2263. case self::AS_IMAGE_REDIRECT_LOGGED:
  2264. $wgOut->permissionRequired( 'upload' );
  2265. return false;
  2266. }
  2267. }
  2268. function getBaseRevision() {
  2269. if ( $this->mBaseRevision == false ) {
  2270. $db = wfGetDB( DB_MASTER );
  2271. $baseRevision = Revision::loadFromTimestamp(
  2272. $db, $this->mTitle, $this->edittime );
  2273. return $this->mBaseRevision = $baseRevision;
  2274. } else {
  2275. return $this->mBaseRevision;
  2276. }
  2277. }
  2278. }