More reversion of r77297, 2 of 2 commits to keep it readable in CR (hopefully)
[lhc/web/wiklou.git] / includes / specials / SpecialComparePages.php
1 <?php
2 /**
3 * Implements Special:ComparePages
4 *
5 * Copyright © 2010 Derk-Jan Hartman <hartman@videolan.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * Implements Special:ComparePages
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialComparePages extends SpecialPage {
32
33 // Stored objects
34 protected $opts, $skin;
35
36 // Some internal settings
37 protected $showNavigation = false;
38
39 public function __construct() {
40 parent::__construct( 'ComparePages' );
41 }
42
43 protected function setup( $par ) {
44 global $wgRequest, $wgUser;
45
46 // Options
47 $opts = new FormOptions();
48 $this->opts = $opts; // bind
49 $opts->add( 'page1', '' );
50 $opts->add( 'page2', '' );
51 $opts->add( 'rev1', '' );
52 $opts->add( 'rev2', '' );
53 $opts->add( 'action', '' );
54
55 // Set values
56 $opts->fetchValuesFromRequest( $wgRequest );
57
58 $title1 = Title::newFromText( $opts->getValue( 'page1' ) );
59 $title2 = Title::newFromText( $opts->getValue( 'page2' ) );
60
61 if( $title1 && $title1->exists() && $opts->getValue( 'rev1' ) == '' ) {
62 $pda = new Article( $title1 );
63 $pdi = $pda->getID();
64 $pdLastRevision = Revision::loadFromPageId( wfGetDB( DB_SLAVE ), $pdi );
65 $opts->setValue( 'rev1', $pdLastRevision->getId() );
66 } elseif ( $opts->getValue( 'rev1' ) != '' ) {
67 $pdrev = Revision::newFromId( $opts->getValue( 'rev1' ) );
68 if( $pdrev ) $opts->setValue( 'page1', $pdrev->getTitle()->getPrefixedText() );
69 }
70 if( $title2 && $title2->exists() && $opts->getValue( 'rev2' ) == '' ) {
71 $pda = new Article( $title2 );
72 $pdi = $pda->getID();
73 $pdLastRevision = Revision::loadFromPageId( wfGetDB( DB_SLAVE ), $pdi );
74 $opts->setValue('rev2', $pdLastRevision->getId() );
75 } elseif ( $opts->getValue( 'rev2' ) != '' ) {
76 $pdrev = Revision::newFromId( $opts->getValue( 'rev2' ) );
77 if( $pdrev ) $opts->setValue( 'page2', $pdrev->getTitle()->getPrefixedText() );
78 }
79
80 // Store some objects
81 $this->skin = $wgUser->getSkin();
82 }
83
84 /**
85 * Show a form for filtering namespace and username
86 *
87 * @param $par String
88 * @return String
89 */
90 public function execute( $par ) {
91 $this->setHeaders();
92 $this->outputHeader();
93
94 $this->setup( $par );
95
96 // Settings
97 $this->form();
98
99 if( $this->opts->getValue( 'rev1' ) && $this->opts->getValue( 'rev2' ) ) {
100 $de = new DifferenceEngine( null,
101 $this->opts->getValue( 'rev1' ),
102 $this->opts->getValue( 'rev2' ),
103 null, // rcid
104 ( $this->opts->getValue( 'action' ) == 'purge' ),
105 false );
106 $de->showDiffPage( true );
107 }
108 }
109
110 protected function form() {
111 global $wgOut, $wgScript;
112
113 // Consume values
114 $page1 = $this->opts->consumeValue( 'page1' );
115 $page2 = $this->opts->consumeValue( 'page2' );
116 $rev1 = $this->opts->consumeValue( 'rev1' );
117 $rev2 = $this->opts->consumeValue( 'rev2' );
118
119 // Store query values in hidden fields so that form submission doesn't lose them
120 $hidden = array();
121 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
122 $hidden[] = Html::hidden( $key, $value );
123 }
124 $hidden = implode( "\n", $hidden );
125
126 $form = Html::openElement( 'form', array( 'action' => $wgScript ) ) .
127 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
128 Xml::fieldset( wfMsg( 'compare-selector' ) ) .
129 Html::openElement( 'table', array( 'id' => 'mw-diff-table', 'style' => 'width:100%' ) ) .
130 "<tr>
131 <td class='mw-label' style='width:10%'>" .
132 Html::element( 'label', array( 'for' => 'page1' ), wfMsg( 'compare-page1' ) ) .
133 "</td>
134 <td class='mw-input' style='width:40%'>" .
135 Html::input( 'page1', $page1, 'text', array( 'size' => 40, 'id' => 'page1' ) ) .
136 "</td>
137 <td class='mw-label' style='width:10%'>" .
138 Html::element( 'label', array( 'for' => 'page2' ), wfMsg( 'compare-page2' ) ) .
139 "</td>
140 <td class='mw-input' style='width:40%'>" .
141 Html::input( 'page2', $page2, 'text', array( 'size' => 40, 'id' => 'page2' ) ) .
142 "</td>
143 </tr>" .
144 "<tr>
145 <td class='mw-label'>" .
146 Html::element( 'label', array( 'for' => 'rev1' ), wfMsg( 'compare-rev1' ) ) .
147 "</td>
148 <td class='mw-input'>" .
149 Html::input( 'rev1', $rev1, 'text', array( 'size' => 8, 'id' => 'rev1' ) ) .
150 "</td>
151 <td class='mw-label'>" .
152 Html::element( 'label', array( 'for' => 'rev2' ), wfMsg( 'compare-rev2' ) ) .
153 "</td>
154 <td class='mw-input'>" .
155 Html::input( 'rev2', $rev2, 'text', array( 'size' => 8, 'id' => 'rev2' ) ) .
156 "</td>
157 </tr>" .
158 "<tr> <td></td>
159 <td class='mw-submit' colspan='3'>" .
160 Xml::submitButton( wfMsg( 'compare-submit' ) ) .
161 "</td>
162 </tr>" .
163 Html::closeElement( 'table' ) .
164 Html::closeElement( 'fieldset' ) .
165 $hidden .
166 Html::closeElement( 'form' );
167
168 $wgOut->addHTML( $form );
169 }
170 }