Merge "Allow editing transcluded sections via the API"
[lhc/web/wiklou.git] / includes / specials / SpecialLog.php
1 <?php
2 /**
3 * Implements Special:Log
4 *
5 * Copyright © 2008 Aaron Schulz
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 * A special page that lists log entries
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialLog extends SpecialPage {
32 /**
33 * List log type for which the target is a user
34 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
35 * Title user instead.
36 */
37 private $typeOnUser = array(
38 'block',
39 'newusers',
40 'rights',
41 );
42
43 public function __construct() {
44 parent::__construct( 'Log' );
45 }
46
47 public function execute( $par ) {
48 global $wgLogRestrictions;
49
50 $this->setHeaders();
51 $this->outputHeader();
52
53 $opts = new FormOptions;
54 $opts->add( 'type', '' );
55 $opts->add( 'user', '' );
56 $opts->add( 'page', '' );
57 $opts->add( 'pattern', false );
58 $opts->add( 'year', null, FormOptions::INTNULL );
59 $opts->add( 'month', null, FormOptions::INTNULL );
60 $opts->add( 'tagfilter', '' );
61 $opts->add( 'offset', '' );
62 $opts->add( 'dir', '' );
63 $opts->add( 'offender', '' );
64
65 // Set values
66 $opts->fetchValuesFromRequest( $this->getRequest() );
67 if ( $par !== null ) {
68 $this->parseParams( $opts, (string)$par );
69 }
70
71 # Don't let the user get stuck with a certain date
72 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
73 $opts->setValue( 'year', '' );
74 $opts->setValue( 'month', '' );
75 }
76
77 // If the user doesn't have the right permission to view the specific
78 // log type, throw a PermissionsError
79 // If the log type is invalid, just show all public logs
80 $type = $opts->getValue( 'type' );
81 if ( !LogPage::isLogType( $type ) ) {
82 $opts->setValue( 'type', '' );
83 } elseif ( isset( $wgLogRestrictions[$type] )
84 && !$this->getUser()->isAllowed( $wgLogRestrictions[$type] )
85 ) {
86 throw new PermissionsError( $wgLogRestrictions[$type] );
87 }
88
89 # Handle type-specific inputs
90 $qc = array();
91 if ( $opts->getValue( 'type' ) == 'suppress' ) {
92 $offender = User::newFromName( $opts->getValue( 'offender' ), false );
93 if ( $offender && $offender->getId() > 0 ) {
94 $qc = array( 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() );
95 } elseif ( $offender && IP::isIPAddress( $offender->getName() ) ) {
96 $qc = array( 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() );
97 }
98 }
99
100 # Some log types are only for a 'User:' title but we might have been given
101 # only the username instead of the full title 'User:username'. This part try
102 # to lookup for a user by that name and eventually fix user input. See bug 1697.
103 wfRunHooks( 'GetLogTypesOnUser', array( &$this->typeOnUser ) );
104 if ( in_array( $opts->getValue( 'type' ), $this->typeOnUser ) ) {
105 # ok we have a type of log which expect a user title.
106 $target = Title::newFromText( $opts->getValue( 'page' ) );
107 if ( $target && $target->getNamespace() === NS_MAIN ) {
108 # User forgot to add 'User:', we are adding it for him
109 $opts->setValue( 'page',
110 Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
111 );
112 }
113 }
114
115 $this->show( $opts, $qc );
116 }
117
118 private function parseParams( FormOptions $opts, $par ) {
119 global $wgLogTypes;
120
121 # Get parameters
122 $parms = explode( '/', ( $par = ( $par !== null ) ? $par : '' ) );
123 $symsForAll = array( '*', 'all' );
124 if ( $parms[0] != '' &&
125 ( in_array( $par, $wgLogTypes ) || in_array( $par, $symsForAll ) )
126 ) {
127 $opts->setValue( 'type', $par );
128 } elseif ( count( $parms ) == 2 ) {
129 $opts->setValue( 'type', $parms[0] );
130 $opts->setValue( 'user', $parms[1] );
131 } elseif ( $par != '' ) {
132 $opts->setValue( 'user', $par );
133 }
134 }
135
136 private function show( FormOptions $opts, array $extraConds ) {
137 # Create a LogPager item to get the results and a LogEventsList item to format them...
138 $loglist = new LogEventsList(
139 $this->getContext(),
140 null,
141 LogEventsList::USE_REVDEL_CHECKBOXES
142 );
143 $pager = new LogPager(
144 $loglist,
145 $opts->getValue( 'type' ),
146 $opts->getValue( 'user' ),
147 $opts->getValue( 'page' ),
148 $opts->getValue( 'pattern' ),
149 $extraConds,
150 $opts->getValue( 'year' ),
151 $opts->getValue( 'month' ),
152 $opts->getValue( 'tagfilter' )
153 );
154
155 $this->addHeader( $opts->getValue( 'type' ) );
156
157 # Set relevant user
158 if ( $pager->getPerformer() ) {
159 $this->getSkin()->setRelevantUser( User::newFromName( $pager->getPerformer() ) );
160 }
161
162 # Show form options
163 $loglist->showOptions(
164 $pager->getType(),
165 $opts->getValue( 'user' ),
166 $pager->getPage(),
167 $pager->getPattern(),
168 $pager->getYear(),
169 $pager->getMonth(),
170 $pager->getFilterParams(),
171 $opts->getValue( 'tagfilter' )
172 );
173
174 # Insert list
175 $logBody = $pager->getBody();
176 if ( $logBody ) {
177 $this->getOutput()->addHTML(
178 $pager->getNavigationBar() .
179 $this->getRevisionButton(
180 $loglist->beginLogEventsList() .
181 $logBody .
182 $loglist->endLogEventsList()
183 ) .
184 $pager->getNavigationBar()
185 );
186 } else {
187 $this->getOutput()->addWikiMsg( 'logempty' );
188 }
189 }
190
191 private function getRevisionButton( $formcontents ) {
192 # If the user doesn't have the ability to delete log entries,
193 # don't bother showing them the button.
194 if ( !$this->getUser()->isAllowedAll( 'deletedhistory', 'deletelogentry' ) ) {
195 return $formcontents;
196 }
197
198 # Show button to hide log entries
199 global $wgScript;
200 $s = Html::openElement(
201 'form',
202 array( 'action' => $wgScript, 'id' => 'mw-log-deleterevision-submit' )
203 ) . "\n";
204 $s .= Html::hidden( 'title', SpecialPage::getTitleFor( 'Revisiondelete' ) ) . "\n";
205 $s .= Html::hidden( 'target', SpecialPage::getTitleFor( 'Log' ) ) . "\n";
206 $s .= Html::hidden( 'type', 'logging' ) . "\n";
207 $button = Html::element(
208 'button',
209 array(
210 'type' => 'submit',
211 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
212 ),
213 $this->msg( 'showhideselectedlogentries' )->text()
214 ) . "\n";
215 $s .= $button . $formcontents . $button;
216 $s .= Html::closeElement( 'form' );
217
218 return $s;
219 }
220
221 /**
222 * Set page title and show header for this log type
223 * @param string $type
224 * @since 1.19
225 */
226 protected function addHeader( $type ) {
227 $page = new LogPage( $type );
228 $this->getOutput()->setPageTitle( $page->getName()->text() );
229 $this->getOutput()->addHTML( $page->getDescription()->parseAsBlock() );
230 }
231
232 protected function getGroupName() {
233 return 'changes';
234 }
235 }