| Conditions | 28 |
| Paths | 3138 |
| Total Lines | 173 |
| Code Lines | 106 |
| Lines | 14 |
| Ratio | 8.09 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 49 | public function doMainQuery( $conds, $opts ) { |
||
| 50 | $target = $opts['target']; |
||
| 51 | $showlinkedto = $opts['showlinkedto']; |
||
| 52 | $limit = $opts['limit']; |
||
| 53 | |||
| 54 | if ( $target === '' ) { |
||
| 55 | return false; |
||
| 56 | } |
||
| 57 | $outputPage = $this->getOutput(); |
||
| 58 | $title = Title::newFromText( $target ); |
||
| 59 | if ( !$title || $title->isExternal() ) { |
||
| 60 | $outputPage->addHTML( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' ) |
||
| 61 | ->parse() . '</div>' ); |
||
| 62 | |||
| 63 | return false; |
||
| 64 | } |
||
| 65 | |||
| 66 | $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) ); |
||
| 67 | |||
| 68 | /* |
||
| 69 | * Ordinary links are in the pagelinks table, while transclusions are |
||
| 70 | * in the templatelinks table, categorizations in categorylinks and |
||
| 71 | * image use in imagelinks. We need to somehow combine all these. |
||
| 72 | * Special:Whatlinkshere does this by firing multiple queries and |
||
| 73 | * merging the results, but the code we inherit from our parent class |
||
| 74 | * expects only one result set so we use UNION instead. |
||
| 75 | */ |
||
| 76 | |||
| 77 | $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' ); |
||
| 78 | $id = $title->getArticleID(); |
||
| 79 | $ns = $title->getNamespace(); |
||
| 80 | $dbkey = $title->getDBkey(); |
||
| 81 | |||
| 82 | $tables = [ 'recentchanges' ]; |
||
| 83 | $select = RecentChange::selectFields(); |
||
| 84 | $join_conds = []; |
||
| 85 | $query_options = []; |
||
| 86 | |||
| 87 | // left join with watchlist table to highlight watched rows |
||
| 88 | $uid = $this->getUser()->getId(); |
||
| 89 | View Code Duplication | if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) { |
|
| 90 | $tables[] = 'watchlist'; |
||
| 91 | $select[] = 'wl_user'; |
||
| 92 | $join_conds['watchlist'] = [ 'LEFT JOIN', [ |
||
| 93 | 'wl_user' => $uid, |
||
| 94 | 'wl_title=rc_title', |
||
| 95 | 'wl_namespace=rc_namespace' |
||
| 96 | ] ]; |
||
| 97 | } |
||
| 98 | View Code Duplication | if ( $this->getUser()->isAllowed( 'rollback' ) ) { |
|
| 99 | $tables[] = 'page'; |
||
| 100 | $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ]; |
||
| 101 | $select[] = 'page_latest'; |
||
| 102 | } |
||
| 103 | ChangeTags::modifyDisplayQuery( |
||
| 104 | $tables, |
||
| 105 | $select, |
||
| 106 | $conds, |
||
| 107 | $join_conds, |
||
| 108 | $query_options, |
||
| 109 | $opts['tagfilter'] |
||
| 110 | ); |
||
| 111 | |||
| 112 | if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds, |
||
| 113 | $opts ) |
||
| 114 | ) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( $ns == NS_CATEGORY && !$showlinkedto ) { |
||
| 119 | // special handling for categories |
||
| 120 | // XXX: should try to make this less kludgy |
||
| 121 | $link_tables = [ 'categorylinks' ]; |
||
| 122 | $showlinkedto = true; |
||
| 123 | } else { |
||
| 124 | // for now, always join on these tables; really should be configurable as in whatlinkshere |
||
| 125 | $link_tables = [ 'pagelinks', 'templatelinks' ]; |
||
| 126 | // imagelinks only contains links to pages in NS_FILE |
||
| 127 | if ( $ns == NS_FILE || !$showlinkedto ) { |
||
| 128 | $link_tables[] = 'imagelinks'; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | if ( $id == 0 && !$showlinkedto ) { |
||
| 133 | return false; // nonexistent pages can't link to any pages |
||
| 134 | } |
||
| 135 | |||
| 136 | // field name prefixes for all the various tables we might want to join with |
||
| 137 | $prefix = [ |
||
| 138 | 'pagelinks' => 'pl', |
||
| 139 | 'templatelinks' => 'tl', |
||
| 140 | 'categorylinks' => 'cl', |
||
| 141 | 'imagelinks' => 'il' |
||
| 142 | ]; |
||
| 143 | |||
| 144 | $subsql = []; // SELECT statements to combine with UNION |
||
| 145 | |||
| 146 | foreach ( $link_tables as $link_table ) { |
||
| 147 | $pfx = $prefix[$link_table]; |
||
| 148 | |||
| 149 | // imagelinks and categorylinks tables have no xx_namespace field, |
||
| 150 | // and have xx_to instead of xx_title |
||
| 151 | if ( $link_table == 'imagelinks' ) { |
||
| 152 | $link_ns = NS_FILE; |
||
| 153 | } elseif ( $link_table == 'categorylinks' ) { |
||
| 154 | $link_ns = NS_CATEGORY; |
||
| 155 | } else { |
||
| 156 | $link_ns = 0; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ( $showlinkedto ) { |
||
| 160 | // find changes to pages linking to this page |
||
| 161 | if ( $link_ns ) { |
||
| 162 | if ( $ns != $link_ns ) { |
||
| 163 | continue; |
||
| 164 | } // should never happen, but check anyway |
||
| 165 | $subconds = [ "{$pfx}_to" => $dbkey ]; |
||
| 166 | } else { |
||
| 167 | $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ]; |
||
| 168 | } |
||
| 169 | $subjoin = "rc_cur_id = {$pfx}_from"; |
||
| 170 | } else { |
||
| 171 | // find changes to pages linked from this page |
||
| 172 | $subconds = [ "{$pfx}_from" => $id ]; |
||
| 173 | if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) { |
||
| 174 | $subconds["rc_namespace"] = $link_ns; |
||
| 175 | $subjoin = "rc_title = {$pfx}_to"; |
||
| 176 | } else { |
||
| 177 | $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ]; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | if ( $dbr->unionSupportsOrderAndLimit() ) { |
||
| 182 | $order = [ 'ORDER BY' => 'rc_timestamp DESC' ]; |
||
| 183 | } else { |
||
| 184 | $order = []; |
||
| 185 | } |
||
| 186 | |||
| 187 | $query = $dbr->selectSQLText( |
||
| 188 | array_merge( $tables, [ $link_table ] ), |
||
| 189 | $select, |
||
| 190 | $conds + $subconds, |
||
| 191 | __METHOD__, |
||
| 192 | $order + $query_options, |
||
| 193 | $join_conds + [ $link_table => [ 'INNER JOIN', $subjoin ] ] |
||
| 194 | ); |
||
| 195 | |||
| 196 | if ( $dbr->unionSupportsOrderAndLimit() ) { |
||
| 197 | $query = $dbr->limitResult( $query, $limit ); |
||
| 198 | } |
||
| 199 | |||
| 200 | $subsql[] = $query; |
||
| 201 | } |
||
| 202 | |||
| 203 | if ( count( $subsql ) == 0 ) { |
||
| 204 | return false; // should never happen |
||
| 205 | } |
||
| 206 | if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) { |
||
| 207 | $sql = $subsql[0]; |
||
| 208 | } else { |
||
| 209 | // need to resort and relimit after union |
||
| 210 | $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC'; |
||
| 211 | $sql = $dbr->limitResult( $sql, $limit, false ); |
||
| 212 | } |
||
| 213 | |||
| 214 | $res = $dbr->query( $sql, __METHOD__ ); |
||
| 215 | |||
| 216 | if ( $res->numRows() == 0 ) { |
||
| 217 | $this->mResultEmpty = true; |
||
|
|
|||
| 218 | } |
||
| 219 | |||
| 220 | return $res; |
||
| 221 | } |
||
| 222 | |||
| 279 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: