8f7d02430746cd9958ddd9165b43822d5d77ef0c
[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 $title = Title::newFromText( $this->opts->getValue( 'page2' ) );
101 $de = new DifferenceEngine( null,
102 $this->opts->getValue( 'rev1' ),
103 $this->opts->getValue( 'rev2' ),
104 null, // rcid
105 ( $this->opts->getValue( 'action' ) == 'purge' ),
106 false );
107 $de->showDiffPage( true );
108 }
109 }
110
111 protected function form() {
112 global $wgOut, $wgScript;
113
114 // Consume values
115 $page1 = $this->opts->consumeValue( 'page1' );
116 $page2 = $this->opts->consumeValue( 'page2' );
117 $rev1 = $this->opts->consumeValue( 'rev1' );
118 $rev2 = $this->opts->consumeValue( 'rev2' );
119
120 // Store query values in hidden fields so that form submission doesn't lose them
121 $hidden = array();
122 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
123 $hidden[] = Html::hidden( $key, $value );
124 }
125 $hidden = implode( "\n", $hidden );
126
127 $form = Html::openElement( 'form', array( 'action' => $wgScript ) ) .
128 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
129 Xml::fieldset( wfMsg( 'compare-selector' ) ) .
130 Html::openElement( 'table', array( 'id' => 'mw-diff-table', 'style' => 'width:100%' ) ) .
131 "<tr>
132 <td class='mw-label' style='width:10%'>" .
133 Html::element( 'label', array( 'for' => 'page1' ), wfMsg( 'compare-page1' ) ) .
134 "</td>
135 <td class='mw-input' style='width:40%'>" .
136 Html::input( 'page1', $page1, 'text', array( 'size' => 40, 'id' => 'page1' ) ) .
137 "</td>
138 <td class='mw-label' style='width:10%'>" .
139 Html::element( 'label', array( 'for' => 'page2' ), wfMsg( 'compare-page2' ) ) .
140 "</td>
141 <td class='mw-input' style='width:40%'>" .
142 Html::input( 'page2', $page2, 'text', array( 'size' => 40, 'id' => 'page2' ) ) .
143 "</td>
144 </tr>" .
145 "<tr>
146 <td class='mw-label'>" .
147 Html::element( 'label', array( 'for' => 'rev1' ), wfMsg( 'compare-rev1' ) ) .
148 "</td>
149 <td class='mw-input'>" .
150 Html::input( 'rev1', $rev1, 'text', array( 'size' => 8, 'id' => 'rev1' ) ) .
151 "</td>
152 <td class='mw-label'>" .
153 Html::element( 'label', array( 'for' => 'rev2' ), wfMsg( 'compare-rev2' ) ) .
154 "</td>
155 <td class='mw-input'>" .
156 Html::input( 'rev2', $rev2, 'text', array( 'size' => 8, 'id' => 'rev2' ) ) .
157 "</td>
158 </tr>" .
159 "<tr> <td></td>
160 <td class='mw-submit' colspan='3'>" .
161 Xml::submitButton( wfMsg( 'compare-submit' ) ) .
162 "</td>
163 </tr>" .
164 Html::closeElement( 'table' ) .
165 Html::closeElement( 'fieldset' ) .
166 $hidden .
167 Html::closeElement( 'form' );
168
169 $wgOut->addHTML( $form );
170 }
171 }