1<?php
2/**
3 * Class definition for a wanted query page.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24use Wikimedia\Rdbms\IDatabase;
25use Wikimedia\Rdbms\IResultWrapper;
26
27/**
28 * Class definition for a wanted query page like
29 * WantedPages, WantedTemplates, etc
30 * @stable to extend
31 * @ingroup SpecialPage
32 */
33abstract class WantedQueryPage extends QueryPage {
34	public function isExpensive() {
35		return true;
36	}
37
38	public function isSyndicated() {
39		return false;
40	}
41
42	/**
43	 * Cache page existence for performance
44	 * @stable to override
45	 * @param IDatabase $db
46	 * @param IResultWrapper $res
47	 */
48	protected function preprocessResults( $db, $res ) {
49		$this->executeLBFromResultWrapper( $res );
50	}
51
52	/**
53	 * Should formatResult() always check page existence, even if
54	 * the results are fresh?  This is a (hopefully temporary)
55	 * kluge for Special:WantedFiles, which may contain false
56	 * positives for files that exist e.g. in a shared repo (bug
57	 * 6220).
58	 * @stable to override
59	 * @return bool
60	 */
61	protected function forceExistenceCheck() {
62		return false;
63	}
64
65	/**
66	 * Format an individual result
67	 *
68	 * @stable to override
69	 *
70	 * @param Skin $skin Skin to use for UI elements
71	 * @param stdClass $result Result row
72	 * @return string
73	 */
74	public function formatResult( $skin, $result ) {
75		$linkRenderer = $this->getLinkRenderer();
76		$title = Title::makeTitleSafe( $result->namespace, $result->title );
77		if ( $title instanceof Title ) {
78			if ( $this->isCached() || $this->forceExistenceCheck() ) {
79				$pageLink = $this->existenceCheck( $title )
80					? '<del>' . $linkRenderer->makeLink( $title ) . '</del>'
81					: $linkRenderer->makeLink( $title );
82			} else {
83				$pageLink = $linkRenderer->makeBrokenLink( $title );
84			}
85			return $this->getLanguage()->specialList( $pageLink, $this->makeWlhLink( $title, $result ) );
86		} else {
87			return $this->msg( 'wantedpages-badtitle', $result->title )->escaped();
88		}
89	}
90
91	/**
92	 * Does the Title currently exists
93	 *
94	 * This method allows a subclass to override this check
95	 * (For example, wantedfiles, would want to check if the file exists
96	 * not just that a page in the file namespace exists).
97	 *
98	 * This will only control if the link is crossed out. Whether or not the link
99	 * is blue vs red is controlled by if the title exists.
100	 *
101	 * @note This will only be run if the page is cached (ie $wgMiserMode = true)
102	 *   unless forceExistenceCheck() is true.
103	 * @since 1.24
104	 * @stable to override
105	 *
106	 * @param Title $title
107	 * @return bool
108	 */
109	protected function existenceCheck( Title $title ) {
110		return $title->isKnown();
111	}
112
113	/**
114	 * Make a "what links here" link for a given title
115	 *
116	 * @param Title $title Title to make the link for
117	 * @param stdClass $result Result row
118	 * @return string
119	 */
120	protected function makeWlhLink( $title, $result ) {
121		$wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
122		$label = $this->msg( 'nlinks' )->numParams( $result->value )->text();
123		return $this->getLinkRenderer()->makeLink( $wlh, $label );
124	}
125
126	/**
127	 * Order by title for pages with the same number of links to them
128	 *
129	 * @stable to override
130	 * @return array
131	 * @since 1.29
132	 */
133	protected function getOrderFields() {
134		return [ 'value DESC', 'namespace', 'title' ];
135	}
136
137	/**
138	 * Do not order descending for all order fields.  We will use DESC only on one field, see
139	 * getOrderFields above. This overwrites sortDescending from QueryPage::getOrderFields().
140	 * Do NOT change this to true unless you remove the phrase DESC in getOrderFiels above.
141	 * If you do a database error will be thrown due to double adding DESC to query!
142	 *
143	 * @stable to override
144	 * @return bool
145	 * @since 1.29
146	 */
147	protected function sortDescending() {
148		return false;
149	}
150
151	/**
152	 * Also use the order fields returned by getOrderFields when fetching from the cache.
153	 * @stable to override
154	 * @return array
155	 * @since 1.29
156	 */
157	protected function getCacheOrderFields() {
158		return $this->getOrderFields();
159	}
160
161}
162