64190df187ec402cc045ab6d3f9184ff4f18a9c5
[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 /**
34 * List log type for which the target is a user
35 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
36 * Title user instead.
37 */
38 private $typeOnUser = array(
39 'block',
40 'newusers',
41 'rights',
42 );
43
44 public function __construct() {
45 parent::__construct( 'Log' );
46 }
47
48 public function execute( $par ) {
49 global $wgLogRestrictions;
50
51 $this->setHeaders();
52 $this->outputHeader();
53
54 $opts = new FormOptions;
55 $opts->add( 'type', '' );
56 $opts->add( 'user', '' );
57 $opts->add( 'page', '' );
58 $opts->add( 'pattern', false );
59 $opts->add( 'year', null, FormOptions::INTNULL );
60 $opts->add( 'month', null, FormOptions::INTNULL );
61 $opts->add( 'tagfilter', '' );
62 $opts->add( 'offset', '' );
63 $opts->add( 'dir', '' );
64 $opts->add( 'offender', '' );
65
66 // Set values
67 $opts->fetchValuesFromRequest( $this->getRequest() );
68 if ( $par ) {
69 $this->parseParams( $opts, (string)$par );
70 }
71
72 # Don't let the user get stuck with a certain date
73 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
74 $opts->setValue( 'year', '' );
75 $opts->setValue( 'month', '' );
76 }
77
78 // Reset the log type to default (nothing) if it's invalid or if the
79 // user does not possess the right to view it
80 $type = $opts->getValue( 'type' );
81 if ( !LogPage::isLogType( $type )
82 || ( isset( $wgLogRestrictions[$type] )
83 && !$this->getUser()->isAllowed( $wgLogRestrictions[$type] ) )
84 ) {
85 $opts->setValue( 'type', '' );
86 }
87
88 # Handle type-specific inputs
89 $qc = array();
90 if ( $opts->getValue( 'type' ) == 'suppress' ) {
91 $offender = User::newFromName( $opts->getValue( 'offender' ), false );
92 if ( $offender && $offender->getId() > 0 ) {
93 $qc = array( 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() );
94 } elseif ( $offender && IP::isIPAddress( $offender->getName() ) ) {
95 $qc = array( 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() );
96 }
97 }
98
99 # Some log types are only for a 'User:' title but we might have been given
100 # only the username instead of the full title 'User:username'. This part try
101 # to lookup for a user by that name and eventually fix user input. See bug 1697.
102 if( in_array( $opts->getValue( 'type' ), $this->typeOnUser ) ) {
103 # ok we have a type of log which expect a user title.
104 $target = Title::newFromText( $opts->getValue( 'page' ) );
105 if( $target && $target->getNamespace() === NS_MAIN ) {
106 # User forgot to add 'User:', we are adding it for him
107 $opts->setValue( 'page',
108 Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
109 );
110 }
111 }
112
113 $this->show( $opts, $qc );
114 }
115
116 private function parseParams( FormOptions $opts, $par ) {
117 global $wgLogTypes;
118
119 # Get parameters
120 $parms = explode( '/', ($par = ( $par !== null ) ? $par : '' ) );
121 $symsForAll = array( '*', 'all' );
122 if ( $parms[0] != '' && ( in_array( $par, $wgLogTypes ) || in_array( $par, $symsForAll ) ) ) {
123 $opts->setValue( 'type', $par );
124 } elseif ( count( $parms ) == 2 ) {
125 $opts->setValue( 'type', $parms[0] );
126 $opts->setValue( 'user', $parms[1] );
127 } elseif ( $par != '' ) {
128 $opts->setValue( 'user', $par );
129 }
130 }
131
132 private function show( FormOptions $opts, array $extraConds ) {
133 # Create a LogPager item to get the results and a LogEventsList item to format them...
134 $loglist = new LogEventsList( $this->getSkin(), $this->getOutput(), 0 );
135 $pager = new LogPager( $loglist, $opts->getValue( 'type' ), $opts->getValue( 'user' ),
136 $opts->getValue( 'page' ), $opts->getValue( 'pattern' ), $extraConds, $opts->getValue( 'year' ),
137 $opts->getValue( 'month' ), $opts->getValue( 'tagfilter' ) );
138
139 $this->addHeader( $opts->getValue( 'type' ) );
140
141 # Set relevant user
142 if ( $pager->getPerformer() ) {
143 $this->getSkin()->setRelevantUser( User::newFromName( $pager->getPerformer() ) );
144 }
145
146 # Show form options
147 $loglist->showOptions( $pager->getType(), $opts->getValue( 'user' ), $pager->getPage(), $pager->getPattern(),
148 $pager->getYear(), $pager->getMonth(), $pager->getFilterParams(), $opts->getValue( 'tagfilter' ) );
149
150 # Insert list
151 $logBody = $pager->getBody();
152 if ( $logBody ) {
153 $this->getOutput()->addHTML(
154 $pager->getNavigationBar() .
155 $loglist->beginLogEventsList() .
156 $logBody .
157 $loglist->endLogEventsList() .
158 $pager->getNavigationBar()
159 );
160 } else {
161 $this->getOutput()->addWikiMsg( 'logempty' );
162 }
163 }
164
165 /**
166 * Set page title and show header for this log type
167 * @param $type string
168 * @since 1.19
169 */
170 protected function addHeader( $type ) {
171 $page = new LogPage( $type );
172 $this->getOutput()->setPageTitle( $page->getName()->text() );
173 $this->getOutput()->addHTML( $page->getDescription()->parseAsBlock() );
174 }
175
176 }