Standardised file description headers; first path
[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 $opts->add( 'diffonly', '' );
55
56 // Set values
57 $opts->fetchValuesFromRequest( $wgRequest );
58
59 $title1 = Title::newFromText( $opts->getValue( 'page1' ) );
60 $title2 = Title::newFromText( $opts->getValue( 'page2' ) );
61
62 if( $title1 && $title1->exists() && $opts->getValue( 'rev1' ) == '' ) {
63 $pda = new Article( $title1 );
64 $pdi = $pda->getID();
65 $pdLastRevision = Revision::loadFromPageId( wfGetDB( DB_SLAVE ), $pdi );
66 $opts->setValue( 'rev1', $pdLastRevision->getId() );
67 } elseif ( $opts->getValue( 'rev1' ) != '' ) {
68 $pdrev = Revision::newFromId( $opts->getValue( 'rev1' ) );
69 if( $pdrev ) $opts->setValue( 'page1', $pdrev->getTitle()->getPrefixedText() );
70 }
71 if( $title2 && $title2->exists() && $opts->getValue( 'rev2' ) == '' ) {
72 $pda = new Article( $title2 );
73 $pdi = $pda->getID();
74 $pdLastRevision = Revision::loadFromPageId( wfGetDB( DB_SLAVE ), $pdi );
75 $opts->setValue('rev2', $pdLastRevision->getId() );
76 } elseif ( $opts->getValue( 'rev2' ) != '' ) {
77 $pdrev = Revision::newFromId( $opts->getValue( 'rev2' ) );
78 if( $pdrev ) $opts->setValue( 'page2', $pdrev->getTitle()->getPrefixedText() );
79 }
80
81 // Store some objects
82 $this->skin = $wgUser->getSkin();
83 }
84
85 /**
86 * Show a form for filtering namespace and username
87 *
88 * @param $par String
89 * @return String
90 */
91 public function execute( $par ) {
92 $this->setHeaders();
93 $this->outputHeader();
94
95 $this->setup( $par );
96
97 // Settings
98 $this->form();
99
100 if( $this->opts->getValue( 'rev1' ) && $this->opts->getValue( 'rev2' ) ) {
101 $title = Title::newFromText( $this->opts->getValue( 'page2' ) );
102 $de = new DifferenceEngine( $title,
103 $this->opts->getValue( 'rev1' ),
104 $this->opts->getValue( 'rev2' ),
105 null, // rcid
106 ( $this->opts->getValue( 'action' ) == 'purge' ),
107 false );
108 $de->showDiffPage( (bool)$this->opts->getValue( 'diffonly' ) );
109 }
110 }
111
112 protected function form() {
113 global $wgOut, $wgScript;
114
115 // Consume values
116 $page1 = $this->opts->consumeValue( 'page1' );
117 $page2 = $this->opts->consumeValue( 'page2' );
118 $rev1 = $this->opts->consumeValue( 'rev1' );
119 $rev2 = $this->opts->consumeValue( 'rev2' );
120
121 // Store query values in hidden fields so that form submission doesn't lose them
122 $hidden = array();
123 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
124 $hidden[] = Xml::hidden( $key, $value );
125 }
126 $hidden = implode( "\n", $hidden );
127
128 $form = Html::openElement( 'form', array( 'action' => $wgScript ) ) .
129 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
130 Xml::fieldset( wfMsg( 'compare-selector' ) ) .
131 Html::openElement( 'table', array( 'id' => 'mw-diff-table', 'style' => 'width:100%' ) ) .
132 "<tr>
133 <td class='mw-label' style='width:10%'>" .
134 Html::element( 'label', array( 'for' => 'page1' ), wfMsg( 'compare-page1' ) ) .
135 "</td>
136 <td class='mw-input' style='width:40%'>" .
137 Html::input( 'page1', $page1, 'text', array( 'size' => 40, 'id' => 'page1' ) ) .
138 "</td>
139 <td class='mw-label' style='width:10%'>" .
140 Html::element( 'label', array( 'for' => 'page2' ), wfMsg( 'compare-page2' ) ) .
141 "</td>
142 <td class='mw-input' style='width:40%'>" .
143 Html::input( 'page2', $page2, 'text', array( 'size' => 40, 'id' => 'page2' ) ) .
144 "</td>
145 </tr>" .
146 "<tr>
147 <td class='mw-label'>" .
148 Html::element( 'label', array( 'for' => 'rev1' ), wfMsg( 'compare-rev1' ) ) .
149 "</td>
150 <td class='mw-input'>" .
151 Html::input( 'rev1', $rev1, 'text', array( 'size' => 8, 'id' => 'rev1' ) ) .
152 "</td>
153 <td class='mw-label'>" .
154 Html::element( 'label', array( 'for' => 'rev2' ), wfMsg( 'compare-rev2' ) ) .
155 "</td>
156 <td class='mw-input'>" .
157 Html::input( 'rev2', $rev2, 'text', array( 'size' => 8, 'id' => 'rev2' ) ) .
158 "</td>
159 </tr>" .
160 "<tr> <td></td>
161 <td class='mw-submit' colspan='3'>" .
162 Xml::submitButton( wfMsg( 'compare-submit' ) ) .
163 "</td>
164 </tr>" .
165 Html::closeElement( 'table' ) .
166 Html::closeElement( 'fieldset' ) .
167 $hidden .
168 Html::closeElement( 'form' );
169
170 $wgOut->addHTML( $form );
171 }
172 }