SpecialLockdb.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * Constructor
  8. */
  9. function wfSpecialLockdb() {
  10. global $wgUser, $wgOut, $wgRequest;
  11. if( !$wgUser->isAllowed( 'siteadmin' ) ) {
  12. $wgOut->permissionRequired( 'siteadmin' );
  13. return;
  14. }
  15. # If the lock file isn't writable, we can do sweet bugger all
  16. global $wgReadOnlyFile;
  17. if( !is_writable( dirname( $wgReadOnlyFile ) ) ) {
  18. DBLockForm::notWritable();
  19. return;
  20. }
  21. $action = $wgRequest->getVal( 'action' );
  22. $f = new DBLockForm();
  23. if ( 'success' == $action ) {
  24. $f->showSuccess();
  25. } else if ( 'submit' == $action && $wgRequest->wasPosted() &&
  26. $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
  27. $f->doSubmit();
  28. } else {
  29. $f->showForm( '' );
  30. }
  31. }
  32. /**
  33. * A form to make the database readonly (eg for maintenance purposes).
  34. * @ingroup SpecialPage
  35. */
  36. class DBLockForm {
  37. var $reason = '';
  38. function DBLockForm() {
  39. global $wgRequest;
  40. $this->reason = $wgRequest->getText( 'wpLockReason' );
  41. }
  42. function showForm( $err ) {
  43. global $wgOut, $wgUser;
  44. $wgOut->setPagetitle( wfMsg( 'lockdb' ) );
  45. $wgOut->addWikiMsg( 'lockdbtext' );
  46. if ( "" != $err ) {
  47. $wgOut->setSubtitle( wfMsg( 'formerror' ) );
  48. $wgOut->addHTML( '<p class="error">' . htmlspecialchars( $err ) . "</p>\n" );
  49. }
  50. $lc = htmlspecialchars( wfMsg( 'lockconfirm' ) );
  51. $lb = htmlspecialchars( wfMsg( 'lockbtn' ) );
  52. $elr = htmlspecialchars( wfMsg( 'enterlockreason' ) );
  53. $titleObj = SpecialPage::getTitleFor( 'Lockdb' );
  54. $action = $titleObj->escapeLocalURL( 'action=submit' );
  55. $reason = htmlspecialchars( $this->reason );
  56. $token = htmlspecialchars( $wgUser->editToken() );
  57. $wgOut->addHTML( <<<END
  58. <form id="lockdb" method="post" action="{$action}">
  59. {$elr}:
  60. <textarea name="wpLockReason" rows="10" cols="60" wrap="virtual">{$reason}</textarea>
  61. <table border="0">
  62. <tr>
  63. <td align="right">
  64. <input type="checkbox" name="wpLockConfirm" />
  65. </td>
  66. <td align="left">{$lc}</td>
  67. </tr>
  68. <tr>
  69. <td>&nbsp;</td>
  70. <td align="left">
  71. <input type="submit" name="wpLock" value="{$lb}" />
  72. </td>
  73. </tr>
  74. </table>
  75. <input type="hidden" name="wpEditToken" value="{$token}" />
  76. </form>
  77. END
  78. );
  79. }
  80. function doSubmit() {
  81. global $wgOut, $wgUser, $wgLang, $wgRequest;
  82. global $wgReadOnlyFile;
  83. if ( ! $wgRequest->getCheck( 'wpLockConfirm' ) ) {
  84. $this->showForm( wfMsg( 'locknoconfirm' ) );
  85. return;
  86. }
  87. $fp = @fopen( $wgReadOnlyFile, 'w' );
  88. if ( false === $fp ) {
  89. # This used to show a file not found error, but the likeliest reason for fopen()
  90. # to fail at this point is insufficient permission to write to the file...good old
  91. # is_writable() is plain wrong in some cases, it seems...
  92. self::notWritable();
  93. return;
  94. }
  95. fwrite( $fp, $this->reason );
  96. fwrite( $fp, "\n<p>(by " . $wgUser->getName() . " at " .
  97. $wgLang->timeanddate( wfTimestampNow() ) . ")</p>\n" );
  98. fclose( $fp );
  99. $titleObj = SpecialPage::getTitleFor( 'Lockdb' );
  100. $wgOut->redirect( $titleObj->getFullURL( 'action=success' ) );
  101. }
  102. function showSuccess() {
  103. global $wgOut;
  104. $wgOut->setPagetitle( wfMsg( 'lockdb' ) );
  105. $wgOut->setSubtitle( wfMsg( 'lockdbsuccesssub' ) );
  106. $wgOut->addWikiMsg( 'lockdbsuccesstext' );
  107. }
  108. public static function notWritable() {
  109. global $wgOut;
  110. $wgOut->showErrorPage( 'lockdb', 'lockfilenotwritable' );
  111. }
  112. }