416092d07a7f17fb8c9c8493683bc79b7ebad593
[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 public function __construct() {
34 parent::__construct( 'Log' );
35 }
36
37 public function execute( $par ) {
38 global $wgLogRestrictions;
39
40 $this->setHeaders();
41 $this->outputHeader();
42
43 $opts = new FormOptions;
44 $opts->add( 'type', '' );
45 $opts->add( 'user', '' );
46 $opts->add( 'page', '' );
47 $opts->add( 'pattern', false );
48 $opts->add( 'year', null, FormOptions::INTNULL );
49 $opts->add( 'month', null, FormOptions::INTNULL );
50 $opts->add( 'tagfilter', '' );
51 $opts->add( 'offset', '' );
52 $opts->add( 'dir', '' );
53 $opts->add( 'offender', '' );
54
55 // Set values
56 $opts->fetchValuesFromRequest( $this->getRequest() );
57 if ( $par ) {
58 $this->parseParams( $opts, (string)$par );
59 }
60
61 # Don't let the user get stuck with a certain date
62 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
63 $opts->setValue( 'year', '' );
64 $opts->setValue( 'month', '' );
65 }
66
67 // Reset the log type to default (nothing) if it's invalid or if the
68 // user does not possess the right to view it
69 $type = $opts->getValue( 'type' );
70 if ( !LogPage::isLogType( $type )
71 || ( isset( $wgLogRestrictions[$type] )
72 && !$this->getUser()->isAllowed( $wgLogRestrictions[$type] ) )
73 ) {
74 $opts->setValue( 'type', '' );
75 }
76
77 # Handle type-specific inputs
78 $qc = array();
79 if ( $opts->getValue( 'type' ) == 'suppress' ) {
80 $offender = User::newFromName( $opts->getValue( 'offender' ), false );
81 if ( $offender && $offender->getId() > 0 ) {
82 $qc = array( 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() );
83 } elseif ( $offender && IP::isIPAddress( $offender->getName() ) ) {
84 $qc = array( 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() );
85 }
86 }
87
88 $this->show( $opts, $qc );
89 }
90
91 private function parseParams( FormOptions $opts, $par ) {
92 global $wgLogTypes;
93
94 # Get parameters
95 $parms = explode( '/', ($par = ( $par !== null ) ? $par : '' ) );
96 $symsForAll = array( '*', 'all' );
97 if ( $parms[0] != '' && ( in_array( $par, $wgLogTypes ) || in_array( $par, $symsForAll ) ) ) {
98 $opts->setValue( 'type', $par );
99 } elseif ( count( $parms ) == 2 ) {
100 $opts->setValue( 'type', $parms[0] );
101 $opts->setValue( 'user', $parms[1] );
102 } elseif ( $par != '' ) {
103 $opts->setValue( 'user', $par );
104 }
105 }
106
107 private function show( FormOptions $opts, array $extraConds ) {
108 # Create a LogPager item to get the results and a LogEventsList item to format them...
109 $loglist = new LogEventsList( $this->getSkin(), $this->getOutput(), 0 );
110 $pager = new LogPager( $loglist, $opts->getValue( 'type' ), $opts->getValue( 'user' ),
111 $opts->getValue( 'page' ), $opts->getValue( 'pattern' ), $extraConds, $opts->getValue( 'year' ),
112 $opts->getValue( 'month' ), $opts->getValue( 'tagfilter' ) );
113
114 $this->addHeader( $opts->getValue( 'type' ) );
115
116 # Set relevant user
117 if ( $pager->getPerformer() ) {
118 $this->getSkin()->setRelevantUser( User::newFromName( $pager->getPerformer() ) );
119 }
120
121 # Show form options
122 $loglist->showOptions( $pager->getType(), $pager->getPerformer(), $pager->getPage(), $pager->getPattern(),
123 $pager->getYear(), $pager->getMonth(), $pager->getFilterParams(), $opts->getValue( 'tagfilter' ) );
124
125 # Insert list
126 $logBody = $pager->getBody();
127 if ( $logBody ) {
128 $this->getOutput()->addHTML(
129 $pager->getNavigationBar() .
130 $loglist->beginLogEventsList() .
131 $logBody .
132 $loglist->endLogEventsList() .
133 $pager->getNavigationBar()
134 );
135 } else {
136 $this->getOutput()->addWikiMsg( 'logempty' );
137 }
138 }
139
140 /**
141 * Set page title and show header for this log type
142 * @param $type string
143 * @since 1.19
144 */
145 protected function addHeader( $type ) {
146 $page = new LogPage( $type );
147 $this->getOutput()->setPageTitle( $page->getName()->text() );
148 $this->getOutput()->addHTML( $page->getDescription()->parseAsBlock() );
149 }
150
151 }