Actually format search error Status objects nicely
[mediawiki.git] / includes / specials / SpecialComparePages.php
blobda1a54cd047c225c657820344b68b303a2575c10
1 <?php
2 /**
3 * Implements Special:ComparePages
5 * Copyright © 2010 Derk-Jan Hartman <hartman@videolan.org>
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.
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.
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
22 * @file
23 * @ingroup SpecialPage
26 /**
27 * Implements Special:ComparePages
29 * @ingroup SpecialPage
31 class SpecialComparePages extends SpecialPage {
33 // Stored objects
34 protected $opts, $skin;
36 // Some internal settings
37 protected $showNavigation = false;
39 public function __construct() {
40 parent::__construct( 'ComparePages' );
43 /**
44 * Show a form for filtering namespace and username
46 * @param string $par
47 * @return string
49 public function execute( $par ) {
50 $this->setHeaders();
51 $this->outputHeader();
53 $form = new HTMLForm( array(
54 'Page1' => array(
55 'type' => 'text',
56 'name' => 'page1',
57 'label-message' => 'compare-page1',
58 'size' => '40',
59 'section' => 'page1',
60 'validation-callback' => array( $this, 'checkExistingTitle' ),
62 'Revision1' => array(
63 'type' => 'int',
64 'name' => 'rev1',
65 'label-message' => 'compare-rev1',
66 'size' => '8',
67 'section' => 'page1',
68 'validation-callback' => array( $this, 'checkExistingRevision' ),
70 'Page2' => array(
71 'type' => 'text',
72 'name' => 'page2',
73 'label-message' => 'compare-page2',
74 'size' => '40',
75 'section' => 'page2',
76 'validation-callback' => array( $this, 'checkExistingTitle' ),
78 'Revision2' => array(
79 'type' => 'int',
80 'name' => 'rev2',
81 'label-message' => 'compare-rev2',
82 'size' => '8',
83 'section' => 'page2',
84 'validation-callback' => array( $this, 'checkExistingRevision' ),
86 'Action' => array(
87 'type' => 'hidden',
88 'name' => 'action',
90 'Diffonly' => array(
91 'type' => 'hidden',
92 'name' => 'diffonly',
94 'Unhide' => array(
95 'type' => 'hidden',
96 'name' => 'unhide',
98 ), $this->getContext(), 'compare' );
99 $form->setSubmitTextMsg( 'compare-submit' );
100 $form->suppressReset();
101 $form->setMethod( 'get' );
102 $form->setSubmitCallback( array( __CLASS__, 'showDiff' ) );
104 $form->loadData();
105 $form->displayForm( '' );
106 $form->trySubmit();
109 public static function showDiff( $data, HTMLForm $form ) {
110 $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
111 $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
113 if ( $rev1 && $rev2 ) {
114 $revision = Revision::newFromId( $rev1 );
116 if ( $revision ) { // NOTE: $rev1 was already checked, should exist.
117 $contentHandler = $revision->getContentHandler();
118 $de = $contentHandler->createDifferenceEngine( $form->getContext(),
119 $rev1,
120 $rev2,
121 null, // rcid
122 ( $data['Action'] == 'purge' ),
123 ( $data['Unhide'] == '1' )
125 $de->showDiffPage( true );
130 public static function revOrTitle( $revision, $title ) {
131 if ( $revision ) {
132 return $revision;
133 } elseif ( $title ) {
134 $title = Title::newFromText( $title );
135 if ( $title instanceof Title ) {
136 return $title->getLatestRevID();
140 return null;
143 public function checkExistingTitle( $value, $alldata ) {
144 if ( $value === '' || $value === null ) {
145 return true;
147 $title = Title::newFromText( $value );
148 if ( !$title instanceof Title ) {
149 return $this->msg( 'compare-invalid-title' )->parseAsBlock();
151 if ( !$title->exists() ) {
152 return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
155 return true;
158 public function checkExistingRevision( $value, $alldata ) {
159 if ( $value === '' || $value === null ) {
160 return true;
162 $revision = Revision::newFromId( $value );
163 if ( $revision === null ) {
164 return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
167 return true;
170 protected function getGroupName() {
171 return 'pagetools';