Merge "mediawiki.action.edit.preview: Disable if there is no #wpTextbox1"
[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 $this->setHeaders();
49 $this->outputHeader();
50 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
51
52 $opts = new FormOptions;
53 $opts->add( 'type', '' );
54 $opts->add( 'user', '' );
55 $opts->add( 'page', '' );
56 $opts->add( 'pattern', false );
57 $opts->add( 'year', null, FormOptions::INTNULL );
58 $opts->add( 'month', null, FormOptions::INTNULL );
59 $opts->add( 'tagfilter', '' );
60 $opts->add( 'offset', '' );
61 $opts->add( 'dir', '' );
62 $opts->add( 'offender', '' );
63
64 // Set values
65 $opts->fetchValuesFromRequest( $this->getRequest() );
66 if ( $par !== null ) {
67 $this->parseParams( $opts, (string)$par );
68 }
69
70 # Don't let the user get stuck with a certain date
71 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
72 $opts->setValue( 'year', '' );
73 $opts->setValue( 'month', '' );
74 }
75
76 // If the user doesn't have the right permission to view the specific
77 // log type, throw a PermissionsError
78 // If the log type is invalid, just show all public logs
79 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
80 $type = $opts->getValue( 'type' );
81 if ( !LogPage::isLogType( $type ) ) {
82 $opts->setValue( 'type', '' );
83 } elseif ( isset( $logRestrictions[$type] )
84 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
85 ) {
86 throw new PermissionsError( $logRestrictions[$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 } else {
99 // Allow extensions to add relations to their search types
100 Hooks::run(
101 'SpecialLogAddLogSearchRelations',
102 array( $opts->getValue( 'type' ), $this->getRequest(), &$qc )
103 );
104 }
105
106 # Some log types are only for a 'User:' title but we might have been given
107 # only the username instead of the full title 'User:username'. This part try
108 # to lookup for a user by that name and eventually fix user input. See bug 1697.
109 Hooks::run( 'GetLogTypesOnUser', array( &$this->typeOnUser ) );
110 if ( in_array( $opts->getValue( 'type' ), $this->typeOnUser ) ) {
111 # ok we have a type of log which expect a user title.
112 $target = Title::newFromText( $opts->getValue( 'page' ) );
113 if ( $target && $target->getNamespace() === NS_MAIN ) {
114 # User forgot to add 'User:', we are adding it for him
115 $opts->setValue( 'page',
116 Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
117 );
118 }
119 }
120
121 $this->show( $opts, $qc );
122 }
123
124 /**
125 * Return an array of subpages that this special page will accept.
126 *
127 * @return string[] subpages
128 */
129 public function getSubpagesForPrefixSearch() {
130 $subpages = $this->getConfig()->get( 'LogTypes' );
131 $subpages[] = 'all';
132 sort( $subpages );
133 return $subpages;
134 }
135
136 private function parseParams( FormOptions $opts, $par ) {
137 # Get parameters
138 $parms = explode( '/', ( $par = ( $par !== null ) ? $par : '' ) );
139 $symsForAll = array( '*', 'all' );
140 if ( $parms[0] != '' &&
141 ( in_array( $par, $this->getConfig()->get( 'LogTypes' ) ) || in_array( $par, $symsForAll ) )
142 ) {
143 $opts->setValue( 'type', $par );
144 } elseif ( count( $parms ) == 2 ) {
145 $opts->setValue( 'type', $parms[0] );
146 $opts->setValue( 'user', $parms[1] );
147 } elseif ( $par != '' ) {
148 $opts->setValue( 'user', $par );
149 }
150 }
151
152 private function show( FormOptions $opts, array $extraConds ) {
153 # Create a LogPager item to get the results and a LogEventsList item to format them...
154 $loglist = new LogEventsList(
155 $this->getContext(),
156 null,
157 LogEventsList::USE_REVDEL_CHECKBOXES
158 );
159 $pager = new LogPager(
160 $loglist,
161 $opts->getValue( 'type' ),
162 $opts->getValue( 'user' ),
163 $opts->getValue( 'page' ),
164 $opts->getValue( 'pattern' ),
165 $extraConds,
166 $opts->getValue( 'year' ),
167 $opts->getValue( 'month' ),
168 $opts->getValue( 'tagfilter' )
169 );
170
171 $this->addHeader( $opts->getValue( 'type' ) );
172
173 # Set relevant user
174 if ( $pager->getPerformer() ) {
175 $this->getSkin()->setRelevantUser( User::newFromName( $pager->getPerformer() ) );
176 }
177
178 # Show form options
179 $loglist->showOptions(
180 $pager->getType(),
181 $opts->getValue( 'user' ),
182 $pager->getPage(),
183 $pager->getPattern(),
184 $pager->getYear(),
185 $pager->getMonth(),
186 $pager->getFilterParams(),
187 $opts->getValue( 'tagfilter' )
188 );
189
190 # Insert list
191 $logBody = $pager->getBody();
192 if ( $logBody ) {
193 $this->getOutput()->addHTML(
194 $pager->getNavigationBar() .
195 $this->getRevisionButton(
196 $loglist->beginLogEventsList() .
197 $logBody .
198 $loglist->endLogEventsList()
199 ) .
200 $pager->getNavigationBar()
201 );
202 } else {
203 $this->getOutput()->addWikiMsg( 'logempty' );
204 }
205 }
206
207 private function getRevisionButton( $formcontents ) {
208 # If the user doesn't have the ability to delete log entries,
209 # don't bother showing them the button.
210 if ( !$this->getUser()->isAllowedAll( 'deletedhistory', 'deletelogentry' ) ) {
211 return $formcontents;
212 }
213
214 # Show button to hide log entries
215 $s = Html::openElement(
216 'form',
217 array( 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' )
218 ) . "\n";
219 $s .= Html::hidden( 'title', SpecialPage::getTitleFor( 'Revisiondelete' ) ) . "\n";
220 $s .= Html::hidden( 'target', SpecialPage::getTitleFor( 'Log' ) ) . "\n";
221 $s .= Html::hidden( 'type', 'logging' ) . "\n";
222 $button = Html::element(
223 'button',
224 array(
225 'type' => 'submit',
226 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
227 ),
228 $this->msg( 'showhideselectedlogentries' )->text()
229 ) . "\n";
230 $s .= $button . $formcontents . $button;
231 $s .= Html::closeElement( 'form' );
232
233 return $s;
234 }
235
236 /**
237 * Set page title and show header for this log type
238 * @param string $type
239 * @since 1.19
240 */
241 protected function addHeader( $type ) {
242 $page = new LogPage( $type );
243 $this->getOutput()->setPageTitle( $page->getName() );
244 $this->getOutput()->addHTML( $page->getDescription()
245 ->setContext( $this->getContext() )->parseAsBlock() );
246 }
247
248 protected function getGroupName() {
249 return 'changes';
250 }
251 }