page-move code moved to Title and disentangled from the user interface
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
1 <?php
2 include_once( "LinksUpdate.php" );
3
4 function wfSpecialMovepage()
5 {
6 global $wgUser, $wgOut, $wgRequest, $action;
7
8 if ( 0 == $wgUser->getID() or $wgUser->isBlocked() ) {
9 $wgOut->errorpage( "movenologin", "movenologintext" );
10 return;
11 }
12 if ( wfReadOnly() ) {
13 $wgOut->readOnlyPage();
14 return;
15 }
16
17 $f = new MovePageForm();
18
19 if ( "success" == $action ) { $f->showSuccess(); }
20 else if ( "submit" == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
21 else { $f->showForm( "" ); }
22 }
23
24 class MovePageForm {
25 var $oldTitle, $newTitle; # Text input
26
27 function MovePageForm() {
28 global $wgRequest;
29 $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $wgRequest->getVal( 'target' ) );
30 $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
31 }
32
33 function showForm( $err )
34 {
35 global $wgOut, $wgUser, $wgLang;
36
37 $wgOut->setPagetitle( wfMsg( "movepage" ) );
38
39 if ( empty( $this->oldTitle ) ) {
40 $wgOut->errorpage( "notargettitle", "notargettext" );
41 return;
42 }
43
44 $encOldTitle = htmlspecialchars( $this->oldTitle );
45 $encNewTitle = htmlspecialchars( $this->newTitle );
46 $ot = Title::newFromURL( $this->oldTitle );
47 $ott = $ot->getPrefixedText();
48
49 $wgOut->addWikiText( wfMsg( "movepagetext" ) );
50 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
51 $wgOut->addWikiText( "\n\n" . wfMsg( "movepagetalktext" ) );
52 }
53
54 $ma = wfMsg( "movearticle" );
55 $newt = wfMsg( "newtitle" );
56 $mpb = wfMsg( "movepagebtn" );
57 $movetalk = wfMsg( "movetalk" );
58
59 $titleObj = Title::makeTitle( NS_SPECIAL, "Movepage" );
60 $action = $titleObj->escapeLocalURL( "action=submit" );
61
62 if ( "" != $err ) {
63 $wgOut->setSubtitle( wfMsg( "formerror" ) );
64 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
65 }
66 $wgOut->addHTML( "<p>
67 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
68 <table border=0><tr>
69 <td align=right>{$ma}:</td>
70 <td align=left><strong>{$ott}</strong></td>
71 </tr><tr>
72 <td align=right>{$newt}:</td>
73 <td align=left>
74 <input type=text size=40 name=\"wpNewTitle\" value=\"{$encNewTitle}\">
75 <input type=hidden name=\"wpOldTitle\" value=\"{$encOldTitle}\">
76 </td>
77 </tr>" );
78
79 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
80 $wgOut->addHTML(
81 "<tr>
82 <td align=right>
83 <input type=checkbox name=\"wpMovetalk\" checked value=\"1\">
84 </td><td>{$movetalk}</td>
85 </tr>" );
86 }
87 $wgOut->addHTML(
88 "<tr>
89 <td>&nbsp;</td><td align=left>
90 <input type=submit name=\"wpMove\" value=\"{$mpb}\">
91 </td></tr></table>
92 </form>\n" );
93
94 }
95
96 function doSubmit()
97 {
98 global $wgOut, $wgUser, $wgLang;
99 global $wgDeferredUpdateList, $wgMessageCache;
100 global $wgUseSquid;
101 $fname = "MovePageForm::doSubmit";
102
103 $ot = Title::newFromText( $this->oldTitle );
104 $nt = Title::newFromText( $this->newTitle );
105
106 $error = $ot->moveTo( $nt );
107 if ( $error !== true ) {
108 $this->showForm( wfMsg( $error ) );
109 return;
110 }
111
112 # Move talk page if
113 # (1) the checkbox says to,
114 # (2) the namespaces are not themselves talk namespaces, and of course
115 # (3) it exists.
116
117 $ons = $ot->getNamespace();
118 $nns = $nt->getNamespace();
119
120 if ( ( 1 == $_REQUEST['wpMovetalk'] ) &&
121 ( ! Namespace::isTalk( $ons ) ) &&
122 ( ! Namespace::isTalk( $nns ) ) ) {
123
124 # get old talk page namespace
125 $ons = Namespace::getTalk( $ons );
126 # get new talk page namespace
127 $nns = Namespace::getTalk( $nns );
128
129 # make talk page title objects
130 $ott = Title::makeTitle( $ons, $ot->getDBkey() );
131 $ntt = Title::makeTitle( $nns, $nt->getDBkey() );
132
133 # Attempt the move
134 $error = $ott->moveTo( $ntt );
135 if ( $error === true ) {
136 $talkmoved = 1;
137 } else {
138 $talkmoved = $error;
139 }
140 }
141
142 $titleObj = Title::makeTitle( NS_SPECIAL, "Movepage" );
143 $success = $titleObj->getFullURL(
144 "action=success&oldtitle=" . wfUrlencode( $ot->getPrefixedText() ) .
145 "&newtitle=" . wfUrlencode( $nt->getPrefixedText() ) .
146 "&talkmoved={$talkmoved}" );
147
148 $wgOut->redirect( $success );
149 }
150
151 function showSuccess()
152 {
153 global $wgOut, $wgUser;
154
155 $wgOut->setPagetitle( wfMsg( "movepage" ) );
156 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
157
158 $text = wfMsg( "pagemovedtext", $_REQUEST['oldtitle'], $_REQUEST['newtitle'] );
159 $wgOut->addWikiText( $text );
160
161 $talkmoved = $_REQUEST['talkmoved'];
162 if ( 1 == $talkmoved ) {
163 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
164 } elseif( 'articleexists' == $talkmoved ) {
165 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
166 } else {
167 $ot = Title::newFromURL( $_REQUEST['oldtitle'] );
168 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
169 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved", wfMsg( $talkmoved ) ) );
170 }
171 }
172 }
173 }
174 ?>