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